Class HydrationUtility


  • public final class HydrationUtility
    extends Object
    Utility class to support graceful hydration of entity references.

    For example, imagine a Product is being returned by a certain API, but before it is returned, its Product#optionGroup needs to be fully hydrated (rather than just having its ProductOptionGroup#id set). The API would need to call some version of CrudEntityService.readByContextId(String, ContextInfo) in order to get the full object, but there is a potential that this method throws an EntityMissingException if that ProductOptionGroup does not exist in the data store for some reason.

    Rather than simply throwing that exception and killing the entire flow, this class can be used to gracefully handle the exception.

    Author:
    Samarth Dhruva (samarthd), Nathan Moore (nathanmoore)
    • Method Detail

      • hydrateIfFound

        public static <O> void hydrateIfFound​(@NonNull
                                              Supplier<O> valueProvider,
                                              @NonNull
                                              Consumer<O> hydrationSetter,
                                              @Nullable
                                              String logMessageOnError)
        Will attempt to fetch the requested value from the valueProvider and supply it to the target hydrationSetter. If an EntityMissingException is thrown, no value is set.
        Type Parameters:
        O - the type of the value that will be fetched and set
        Parameters:
        valueProvider - the provider function that is responsible for fetching the value. This is the function that has the potential to throw EntityMissingException.
        hydrationSetter - the setter function that is responsible for accepting the found value and setting it on the ultimate target object.
        logMessageOnError - the message to log if an EntityMissingException is encountered. If null, no message is logged.
      • hydrateIfNotNull

        public static <O> void hydrateIfNotNull​(@Nullable
                                                O value,
                                                @NonNull
                                                Consumer<O> hydrationSetter,
                                                @Nullable
                                                String logMessageOnError)
        Supplies the given value to the target hydrationSetter if the value is not null. Otherwise, does nothing.
        Type Parameters:
        O - the type of the value that will be fetched and set
        Parameters:
        value - the value that should be supplied to the hydrationSetter if not null
        hydrationSetter - the setter function that is responsible for accepting the found value and setting it on the ultimate target object.
        logMessageOnError - the message to log if the value is null. If not specified, no message is logged.
      • getGenericErrorMessage

        public static String getGenericErrorMessage​(String hydrationTarget,
                                                    String domainOfValue,
                                                    String contextIdOfMissingValue)
        Helper function to create the error message string for a hydration failure.
        Parameters:
        hydrationTarget - the name of the ultimate target/field whose value would be set
        domainOfValue - the name of the domain of the value that was being fetched
        contextIdOfMissingValue - the context ID of the value that could not be found
        Returns:
        an error message string that could be used for a log message on hydration failure