Class HydrationUtility
java.lang.Object
com.broadleafcommerce.data.tracking.core.mapping.support.HydrationUtility
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 Summary
Modifier and TypeMethodDescriptionstatic String
getGenericErrorMessage
(String hydrationTarget, String domainOfValue, String contextIdOfMissingValue) Helper function to create the error message string for a hydration failure.static <O> Optional<O>
getOptionalValue
(Supplier<O> valueProvider, String logMessageOnError) Calls the value provider and places the value in anOptional
, returningOptional.empty()
if anEntityMissingException
was thrown or the result was null.static <O> void
hydrateIfFound
(Supplier<O> valueProvider, Consumer<O> hydrationSetter, String logMessageOnError) Will attempt to fetch the requested value from thevalueProvider
and supply it to the targethydrationSetter
.static <O> void
hydrateIfNotNull
(O value, Consumer<O> hydrationSetter, String logMessageOnError) Supplies the givenvalue
to the targethydrationSetter
if the value is not null.
-
Method Details
-
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 thevalueProvider
and supply it to the targethydrationSetter
. If anEntityMissingException
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 throwEntityMissingException
.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 anEntityMissingException
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 givenvalue
to the targethydrationSetter
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 thehydrationSetter
if not nullhydrationSetter
- 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 setdomainOfValue
- the name of the domain of the value that was being fetchedcontextIdOfMissingValue
- 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
-
getOptionalValue
public static <O> Optional<O> getOptionalValue(Supplier<O> valueProvider, @Nullable String logMessageOnError) Calls the value provider and places the value in anOptional
, returningOptional.empty()
if anEntityMissingException
was thrown or the result was null.- Type Parameters:
O
- the type of the value that should be fetched- Parameters:
valueProvider
- the provider function that is responsible for fetching the value. This is the function that has the potential to throwEntityMissingException
.logMessageOnError
- the message to log if anEntityMissingException
is encountered. If null, no message is logged.- Returns:
- an
Optional
containing the fetched value, orOptional.empty()
if anEntityMissingException
was thrown (or the value was null)
-