Class RepositoryContribution

java.lang.Object
com.broadleafcommerce.data.tracking.core.RepositoryContribution
All Implemented Interfaces:
org.springframework.core.Ordered

public class RepositoryContribution extends Object implements org.springframework.core.Ordered
Beans of this type allow for contribution of repository fragments to out-of-the-box framework repositories. The system will pick up beans of this type and attempt to match the baseRepositoryInterface with an existing repository base interface type. If found, the concreteFragments and queryFragments will be added to the repositories list of fragments and will be available to call at runtime.

Note, since these fragments are not directly part of the base repository interface, usage requires casting the repository instance to the desired fragment interface in order to exercise its API. For example:
      ((MyFragmentInterface) productRepository).myMethod(..);
 
Note, if overriding a method that already exists in the repository, it is not necessary to cast to your fragment interface. A call to the original method signature without the cast will still result in your fragment implementation being called.

Use withBaseRepositoryInterface(Class) to match for exact repository classes, or match for castable interfaces (e.g. TrackableRepository). Use withBaseRepositoryInterfaceMatchRegex(String) to match using a regex pattern against repository fully qualified classnames. Note, you may set either or both of these match parameters to fine tune how repositories are chosen (e.g. match all TrackableRepository repositories in a particular package).

For cases where multiple repositories intentionally match for the same fragment, it is often useful to determine domain type information at runtime so that the correct type can be queried in your fragment. The RepositoryContext can be retrieved in your concrete method implementation (e.g. RepositoryContext.getContext()) and inspected to determine the correct domain type for the current repository. In turn, this information can be used to influence query construction, usually via JPA criteria. For example:
     ...
     RepositoryContext context = RepositoryContext.getContext();
     Class domainType = (Class) context.getMetadata().getDomainType();
     CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
     CriteriaQuery criteria = criteriaBuilder.createQuery(domainType);
     Root entity = criteria.from(domainType);
     ...
 

RepositoryContribution instances implement Ordered. Use this feature to give precedence to one or more instances. This feature is useful in cases where you have provided a more general override, but in some cases you want to prefer a more specific override of the same method signature.
  • Constructor Details

    • RepositoryContribution

      public RepositoryContribution()
  • Method Details

    • withConcreteFragments

      public RepositoryContribution withConcreteFragments(Map<Class<?>,?> fragments)
      Pass a map of relationships between fragment interface and implementation. The implementation is usually another bean with the appropriate EntityManager (and any other dependencies) already injected. Note, a method signature from a framework repository fragment for this same repository may be used and implemented here in order to provide an override of the framework functionality.
      Type Parameters:
      T - The interface type
      Parameters:
      fragments - Map of relationships between fragment interface and implementation
      Returns:
      The RepositoryContribution instance with the concrete elements set
    • withQueryFragments

      public RepositoryContribution withQueryFragments(List<Class<?>> fragments)
      Pass a list of fragment interfaces whose method signatures inform dynamic query construction. These interface have no implementation counterpart. See https://www.baeldung.com/spring-data-derived-queries for more information on this topic.
      Type Parameters:
      T - The interface type
      Parameters:
      fragments - List of fragment interfaces whose method signatures inform dynamic query construction
      Returns:
      The RepositoryContribution instance with the query method interface elements set
    • withBaseRepositoryInterface

      public RepositoryContribution withBaseRepositoryInterface(Class<?> baseRepositoryInterface)
      Provide an interface that is equal to, or that the existing repository can be cast to. If a repository is detected that matches either of these criteria, it is determined to be a match for this particular check.
      Parameters:
      baseRepositoryInterface - Interface that is equal to, or that the existing repository can be cast to
      Returns:
      The RepositoryContribution instance with the match interface set
    • withBaseRepositoryInterfaceMatchRegex

      public RepositoryContribution withBaseRepositoryInterfaceMatchRegex(String regex)
      Provide a regex pattern that will match one or more fully qualified classnames of existing repositories. If a repository is detected whose fully qualified name matches the pattern, it is determined to be a match for this particular check.
      Parameters:
      regex - Regex pattern that will match one or more fully qualified classnames of existing repositories
      Returns:
      The RepositoryContribution instance with the match regex set
    • getConcreteFragments

      public Map<Class<Object>,Object> getConcreteFragments()
    • withOrder

      public RepositoryContribution withOrder(int order)
      Set a precedence value for this contribution to cause it to be favored (or not) over others when considering the same method signature.
      Parameters:
      order - The order value for sorting RepositoryContribution instances. Lower values win. The default is 0.
      Returns:
      The RepositoryContribution instance with the order set
    • getBaseRepositoryInterface

      public Class<?> getBaseRepositoryInterface()
    • getBaseRepositoryInterfaceMatchRegex

      public Pattern getBaseRepositoryInterfaceMatchRegex()
    • getQueryFragments

      public List<Class<?>> getQueryFragments()
    • getOrder

      public int getOrder()
      Specified by:
      getOrder in interface org.springframework.core.Ordered
    • setBaseRepositoryInterface

      public void setBaseRepositoryInterface(Class<?> baseRepositoryInterface)
    • setBaseRepositoryInterfaceMatchRegex

      public void setBaseRepositoryInterfaceMatchRegex(Pattern baseRepositoryInterfaceMatchRegex)
    • setConcreteFragments

      public void setConcreteFragments(Map<Class<?>,?> concreteFragments)
    • setQueryFragments

      public void setQueryFragments(List<Class<?>> queryFragments)
    • setOrder

      public void setOrder(int order)
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object