java.lang.Object
com.broadleafcommerce.data.tracking.core.mapping.MappingUtils

public final class MappingUtils extends Object
Utility methods related to mapping activities in data tracking and ModelMapper
  • Field Details

    • CONVERTER_OPTIMIZED_PROPERTIES_BY_MODEL_TYPE

      public static final Map<Class<?>,Set<String>> CONVERTER_OPTIMIZED_PROPERTIES_BY_MODEL_TYPE
      Synthetic properties in the TypeMap when a Converter is primarily responsible for mapping. In this case, these properties only serve to inform the construction of ChangeDetail instances.
    • KEPT_PROPERTIES_BY_MODEL_TYPE

      public static final Map<Class<?>,Set<String>> KEPT_PROPERTIES_BY_MODEL_TYPE
  • Method Details

    • isResidentToMe

      public static boolean isResidentToMe(Class<? extends ModelMapperMappable> entityClass)
      Whether the ModelMapperMappable.toMe() method declaration is resident in the entityClass param. If provided in a superclass, then false.
      Parameters:
      entityClass - The class to investigate
      Returns:
      Whether the ModelMapperMappable.toMe() method declaration is resident in the entityClass param.
    • isResidentFromMe

      public static boolean isResidentFromMe(Class<? extends ModelMapperMappable> entityClass)
      Whether the ModelMapperMappable.fromMe() method declaration is resident in the entityClass param. If provided in a superclass, then false.
      Parameters:
      entityClass - The class to investigate
      Returns:
      Whether the ModelMapperMappable.fromMe() method declaration is resident in the entityClass param.
    • fromMeOptimizedTypeMapWithConverter

      public static void fromMeOptimizedTypeMapWithConverter(@NonNull org.modelmapper.ModelMapper mapper, @NonNull Class<?> sourceType, @NonNull Class<?> destinationType, @NonNull Function<org.modelmapper.spi.MappingContext<?,?>,Object> converter, NearbyNestedMapping... nestedMappings)
      Create a TypeMap instance that uses an explicit converter function (as a ModelMapper preConverter) for performing the mapping conversion. Implicit mappings are still compiled, but are all marked as skipped, except for nestedMappings. The implicit mappings remain to serve their function as reference for ChangeDetail compilation, but are not used during actual mapping. Converting in this way should provide a performance benefit over standard implicit mapping conversion. This represents the mapping from the persistence state to the projection state.
      Parameters:
      mapper - A pre-instantiated ModelMapper instance. Should use strict property matching.
      sourceType - The type of the source domain
      destinationType - The type of the destination domain
      converter - The converter function to use to convert from the source property type to the destination property type
      nestedMappings - The paths for property mappings to keep that require additional mapping. These include field references to complex types or collections/maps containing complex types.
      See Also:
    • toMeOptimizedTypeMapWithConverter

      public static void toMeOptimizedTypeMapWithConverter(@NonNull org.modelmapper.ModelMapper mapper, @NonNull Class<?> sourceType, @NonNull Class<?> destinationType, @NonNull Function<org.modelmapper.spi.MappingContext<?,?>,?> converter, NearbyNestedMapping... nestedMappings)
      Create a TypeMap instance that uses an explicit converter function (as a ModelMapper preConverter) for performing the mapping conversion. Implicit mappings are still compiled, but are all marked as skipped, except for nestedMappings. The implicit mappings remain to serve their function as reference for ChangeDetail compilation, but are not used during actual mapping. Converting in this way should provide a performance benefit over standard implicit mapping conversion. This represents the mapping from the projection state to the persistence state.
      Parameters:
      mapper - A pre-instantiated ModelMapper instance. Should use strict property matching.
      sourceType - The type of the source domain
      destinationType - The type of the destination domain
      converter - The converter function to use to convert from the source property type to the destination property type
      nestedMappings - The paths for property mappings to keep that require additional mapping. These include field references to complex types or collections/maps containing complex types.
      See Also:
    • map

      public static <T, G> void map(@NonNull Supplier<T> from, @NonNull Consumer<G> to, @NonNull Function<T,G> converter, Supplier... conditions)
      Perform mapping for a particular field using getter/setter from a POJO to another POJO. Generally used inside a Converter supplied via fromMeOptimizedTypeMapWithConverter(ModelMapper, Class, Class, Function, NearbyNestedMapping...) and toMeOptimizedTypeMapWithConverter(ModelMapper, Class, Class, Function, NearbyNestedMapping...).
      Type Parameters:
      T - The value supplied
      G - The value applied to the destination
      Parameters:
      from - The supplier for the field value from a POJO
      to - The consumer for applying the field value to the target POJO.
      converter - A function that can perform type conversion on the transfer value
      conditions - Optional array of boolean suppliers. If one or more return false, the mapping will be skipped.
    • map

      public static <T> void map(@NonNull Supplier<T> from, @NonNull Consumer<T> to, Supplier... conditions)
      Perform mapping for a particular field using getter/setter from a POJO to another POJO. Generally used inside a Converter supplied via fromMeOptimizedTypeMapWithConverter(ModelMapper, Class, Class, Function, NearbyNestedMapping...) and toMeOptimizedTypeMapWithConverter(ModelMapper, Class, Class, Function, NearbyNestedMapping...)
      Type Parameters:
      T - The value supplied
      Parameters:
      from - The supplier for the field value from a POJO
      to - The consumer for applying the field value to the target POJO.
      conditions - Optional array of boolean suppliers. If one or more return false, the mapping will be skipped.
    • getTrackingType

      public static <D> Class<? extends Tracking> getTrackingType(Class<D> domainType)
      Get the data tracking type for the repository domain type
      Type Parameters:
      D - The repository domain type
      Parameters:
      domainType - The repository domain type
      Returns:
      The data tracking type
    • getTrackingType

      public static Class<? extends Tracking> getTrackingType(Object domainInstance)
      Get the data tracking type for the repository domain instance
      Parameters:
      domainInstance - The repository domain instance
      Returns:
      The data tracking type
    • includeBaseConverters

      public static void includeBaseConverters(org.modelmapper.ModelMapper mapper, org.modelmapper.TypeMap<?,?> typeMap, Class<?> parentSource, Class<?> parentDestination, @Nullable org.modelmapper.Converter<?,?> preOverride, @Nullable org.modelmapper.Converter<?,?> postOverride, boolean skipParentConverters)
      TypeMap.includeBase(Class, Class) does not automatically include pre and post converters. They must be handled separately. This utility method makes sure those converters are registered on the passed TypeMap instance. Optionally, pre and post converter overrides may be passed, in which case, any parent converters will be called first, before calling the override. Note, if the parent converters should be skipped, then the skipParentConverters can be set to true.
      Parameters:
      mapper - The ModelMapper mapper instance
      typeMap - The inheriting (child) type map instance
      parentSource - The source type of the parent for the mapping
      parentDestination - The destination type of the parent for the mapping
      preOverride - A converter to run prior to mapping. Will also call any parent pre converters, unless skipParentConverters is true.
      postOverride - A converter to run after mapping. Will also call any parent post converters, unless skipParentConverters is true.
      skipParentConverters - Whether or not to skip parent converters
    • setupExtensions

      public static void setupExtensions(org.modelmapper.ModelMapper mapper, Class<?> extendedSource, Class<?> extendedDestination, Class<?> parentSource, Class<?> parentDestination, @Nullable org.modelmapper.Converter<?,?> preOverride, @Nullable org.modelmapper.Converter<?,?> postOverride, boolean skipParentConverters)
      Creates a TypeMap for the extended source and destination. Also, includes mappings from the super classes. Finally, calls includeBaseConverters(ModelMapper, TypeMap, Class, Class, Converter, Converter, boolean) to make sure any converters from the superclass are also included, while also registering the optional pre and post converters passed in.
      Parameters:
      mapper - The ModelMapper mapper instance
      extendedSource - The source type of the extension for the mapping
      extendedDestination - The destination type of the extension for the mapping
      parentSource - The source type of the parent for the mapping
      parentDestination - The destination type of the parent for the mapping
      preOverride - A converter to run prior to mapping. Will also call any parent pre converters, unless skipParentConverters is true.
      postOverride - A converter to run after mapping. Will also call any parent post converters, unless skipParentConverters is true.
      skipParentConverters - Whether or not to skip parent converters
    • setupExtensions

      public static void setupExtensions(org.modelmapper.ModelMapper mapper, Class<?> extendedSource, Class<?> extendedDestination, Class<?> parentSource, Class<?> parentDestination)
      Creates a TypeMap for the extended source and destination. Also, includes mappings from the super classes. Finally, calls includeBaseConverters(ModelMapper, TypeMap, Class, Class) to make sure any converters from the superclass are also included.
      Parameters:
      mapper - The ModelMapper mapper instance
      extendedSource - The source type of the extension for the mapping
      extendedDestination - The destination type of the extension for the mapping
      parentSource - The source type of the parent for the mapping
      parentDestination - The destination type of the parent for the mapping
    • includeBaseConverters

      public static void includeBaseConverters(org.modelmapper.ModelMapper mapper, org.modelmapper.TypeMap<?,?> typeMap, Class<?> parentSource, Class<?> parentDestination)
      TypeMap.includeBase(Class, Class) does not automatically include pre and post converters. They must be handled separately. This utility method makes sure those converters are registered on the passed TypeMap instance.
      Parameters:
      mapper - The ModelMapper mapper instance
      typeMap - The inheriting (child) type map instance
      parentSource - The source type of the parent for the mapping
      parentDestination - The destination type of the parent for the mapping
      See Also: