This customization example page is for you if:
-
You have custom fields inside the extension object
-
At least one of these fields is a code that must be decoded to an id in order to proceed with communication with the privateAPIs
-
Your intent is to create a new entity object using an id associated with a code that must be decoded in the custom validation step, before being saved in the after processing step
Let's see in detail with the following code fragments that explain, step by step, how to proceed with the decoding/validation of a code passed as input inside the extension object and how to use the code just decoded to save a new custom object.
-
First of all use customValidation step to decode an extension’s field:
@Component // Always add this annotation to your custom class
@RequiredArgsConstructor // Add this annotation if you have something to inject (like AssetMeasuringElementsStatusDataManager)
public class AssetGetValidateExtensionField implements AssetGetValidation {
// Inject the *DataManager (existing or custom) to correctly call the decoding method
// The following DataManager is used just to provide an example
private final AssetMeasuringElementsStatusDataManager assetMeasuringElementsStatusDataManager;
@Override
public Mono<Void> validate(final AssetGetCompleteFilterDTO assetGetCompleteFilterDTO) {
// Let's assume we have two fields in the extension object: one must be decoded
// in the pre-execution validation phase and, in conjunction with the other one, they will be used
// to create a new obj in the after processing step
// extension object
final Map<String, Object> extension = assetGetCompleteFilterDTO.getExtension();
// code of the AssetMeasuringElement obj that has to be created - no decoding needed in this phase
// we will use this code in the AfterProcessing step execution
// (variable not used, extracted only to make you understand clearly the intent of this step and
// how we are going to proceed)
final Object myCustomElementCode = extension.get("assetMeasuringElementsCode");
// statusCode that have been validated and decoded in the custom validation step
final Object myCustomElementStatusCodeToBeDecoded = extension.get("assetMeasuringElementsStatusCode");
return
assetMeasuringElementsStatusDataManager.findUniqueIdByCodeReactive(String.valueOf(myCustomElementStatusCodeToBeDecoded))
.flatMap(myCustomFieldDecodedToId -> {
extension.put("assetMeasuringElementsStatusId", myCustomFieldDecodedToId);
return Mono.empty();
});
}
}
-
Then, use afterProcessing step to save the extension’s field:
@Component // Always add this annotation to your custom class
@RequiredArgsConstructor
// Add this annotation if you have something to inject (like AssetMeasuringElementsDataManager)
public class AssetGetExecuteCustomCodeAfterProcessing implements AssetGetAfterProcessing {
// Inject the *DataManager (existing or custom) to correctly call the after processing execution method
// The following DataManager is used just to provide an example
private final AssetMeasuringElementsDataManager assetMeasuringElementsDataManager;
@Override
public Mono<Void> execute(final AssetGetExtensionContext assetGetExtensionContext) {
//Let's assume we have two fields in the extension object that we decoded in the
// default pre-execution validation phase and that now we use them to create an object that must be saved
//requestDTO extracted from ExtensionContext
final AssetGetCompleteFilterDTO filterDTO = assetGetExtensionContext.getRequest();
//responseDTO extracted from ExtensionContext
final AssetGetCompleteResponseDTO responseDTO = assetGetExtensionContext.getResponse();
//extension object
final Map<String, Object> extension = filterDTO.getExtension();
// code of the AssetMeasuringElement obj that need to be created
final Object myCustomElementCode = extension.get("assetMeasuringElementsCode");
// statusId that have been validated and decoded in the custom validation step
final Object myCustomElementStatusCodeDecodedToId =
extension.get("assetMeasuringElementsStatusId");
// First of all, check if the element you want to create already exist
return assetMeasuringElementsDataManager.findUniqueIfExistsByAssetIdAndCodeReactive(
responseDTO.getId(), valueOf(myCustomElementCode))
.flatMap(assetMeasuringElementOptional -> {
if (assetMeasuringElementOptional.isEmpty()) {
// If the element does not exist then create
return assetMeasuringElementsDataManager.createReactive(
// assetId returned by the original response
responseDTO.getId(),
// AssetMeasuringElement object built with the two fields inserted in extension map
AssetMeasuringElement.builder()
.code(valueOf(myCustomElementCode))
.description("customDescription")
.statusId((Long) myCustomElementStatusCodeDecodedToId) // decoded id
.build())
.flatMap(assetMeasuringElementCreatedId -> {
// then flatMap catch the output of the previous Mono (in this case the assetMeasuringElementCreatedId)
// and, after insertion into the responseDTO's extension map, Mono.empty is returned
responseDTO.getExtension()
.put("assetMeasuringElementsId", assetMeasuringElementCreatedId);
return Mono.empty();
});
} else {
final AssetMeasuringElement assetMeasuringElement = assetMeasuringElementOptional.get();
// If the element exists and myCustomElementStatusCodeDecodedToId is different from what
// found in the existing record then update
if (!Objects.equals(assetMeasuringElement.getStatusId(), myCustomElementStatusCodeDecodedToId)) {
final AssetMeasuringElementPatch assetMeasuringElementPatch =
AssetMeasuringElementPatch.builder()
.statusId(SerializableOptional.of((Long) myCustomElementStatusCodeDecodedToId))
.build();
// add the existing assetMeasuringElementsId into the responseDTO's extension map
responseDTO.getExtension()
.put("assetMeasuringElementsId", assetMeasuringElement.getId());
// updateReactive returns a Mono<Void> so you don't need any adjustment
return assetMeasuringElementsDataManager.updateReactive(
responseDTO.getId(),
assetMeasuringElement.getId(),
assetMeasuringElementPatch
);
}
return Mono.empty();
}
}
);
}
}