Skip to content

CustomSettingService

smehta-veeva edited this page Jun 9, 2021 · 2 revisions

Key Concepts

The CustomSettingService is an interface the uses UserDefinedService. CustomSettingService implementations implement methods to get and set a vSDK Setting record from the local and/or remote vault. These methods are explained in more detail below.

getLocalSettings Method

This method returns the local settings as a User-defined model based on the interface name. This method is used in the ExampleUDMTrigger with the ExampleSettings User-defined model.

This method uses a helper method, getLocalSettingResponse which uses the QueryService to get the vSDK Settings record from the local vault.

...
 QueryResponse queryResponse = getLocalSettingResponse(settingsClass, true);
...

Then it uses the JsonService to convert the QueryResult to the provided User-Defined Model class(ExampleSettings in this case).

...
 U settingModel = jsonService.convertToUserDefinedModel(json, settingsClass);
...

This method demonstrates how to use a User-defined model with the JsonService to convert a string Json object to a User-defined model.

getRemoteSettings Method

This method retrieves remote settings based on the interface name and connection. It uses the getRemoteSettingsResponse helper method along with the JsonService to convert a vSDK Settings record from a remote vault to a User-defined model.

...
 SettingRecordModel remoteSettingModel = getRemoteSettingResponse(settingsClass, connectionName);
        if (remoteSettingModel != null) {
            return jsonService.convertToUserDefinedModel(remoteSettingModel.getJson(), settingsClass);
        }
...

This method demonstrates how to use a User-defined model with the JsonService to convert a JsonObject to a User-defined model.

getRemoteSettingsResponse Method

This helper method queries the remote vault using the HTTPService and the connection provided to get the vSDK Settings record as a SettingRecordModel User-defined model.

The following snippet demonstrates how to send a HTTP request and convert the response body to the given UserDefinedModel type.

...
HttpRequest queryRequest = httpService.newHttpRequest(connectionName);
...

 httpService.send(queryRequest, SettingQueryResponseModel.class)
                .onError(response -> {
                    logService.error(response.getMessage());
                })
                .onSuccess(response -> {
                    SettingQueryResponseModel responseModel = response.getResponseBody();
                    if (responseModel != null) {
                        if (responseModel.getData() != null && !responseModel.getData().isEmpty()) {
                            SettingRecordModel remoteSettingModel = responseModel.getData().get(0);
                            results.add(remoteSettingModel);
                        }
                    }
                })
                .execute();

...

saveLocalSettings Method

This method creates or updates a vSDK Setting record in the local vault. First, it converts the provided User-defined model to a string using the JsonService.

...
  String json = jsonService.convertToString(settingsModel);
...

Then it uses the RecordService to add the string to a vSDK Settings Record field and save it to a local vault.

...
record.setValue(OBJECT_FIELD_NAME, getSettingName(settingsClass));
record.setValue(OBJECT_FIELD_JSON, json);
settingRecords.add(record);

...
recordService.batchSaveRecords(settingRecords)
                .rollbackOnErrors()
                .execute();
...

saveRemoteSettings Method

This method creates a vSDK Setting record in a remote vault. It uses the JsonService and UserDefinedModelService along with the HTTPService to convert a local ExampleSettings UDM to a SettingsRecordModel UDM and send it as the HTTP request body to the remote vault.

...
 String json = jsonService.convertToString(settingsModel);
        SettingRecordModel remoteSettingModel = modelService.newUserDefinedModel(SettingRecordModel.class);
        remoteSettingModel.setName(settingName);
        remoteSettingModel.setJson(json);
...

HttpRequest updateRequest = httpService.newHttpRequest(connectionName);
...
updateRequest.setMethod(HttpMethod.POST);
...
updateRequest.setBody(VaultCollections.asList(remoteSettingModel));
...