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 thebaseRepositoryInterface
with an existing repository base interface type. If found, theconcreteFragments
andqueryFragments
will be added to the repositories list of fragments and will be available to call at runtime.((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.withBaseRepositoryInterface(Class)
to match for exact repository classes, or match for castable interfaces (e.g.TrackableRepository
). UsewithBaseRepositoryInterfaceMatchRegex(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 allTrackableRepository
repositories in a particular package).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 implementOrdered
. 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 Summary
Constructors Constructor Description RepositoryContribution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
canEqual(Object other)
boolean
equals(Object o)
Class<?>
getBaseRepositoryInterface()
Pattern
getBaseRepositoryInterfaceMatchRegex()
Map<Class<Object>,Object>
getConcreteFragments()
int
getOrder()
List<Class<?>>
getQueryFragments()
int
hashCode()
void
setBaseRepositoryInterface(Class<?> baseRepositoryInterface)
void
setBaseRepositoryInterfaceMatchRegex(Pattern baseRepositoryInterfaceMatchRegex)
void
setConcreteFragments(Map<Class<?>,?> concreteFragments)
void
setOrder(int order)
void
setQueryFragments(List<Class<?>> queryFragments)
String
toString()
RepositoryContribution
withBaseRepositoryInterface(Class<?> baseRepositoryInterface)
Provide an interface that is equal to, or that the existing repository can be cast to.RepositoryContribution
withBaseRepositoryInterfaceMatchRegex(String regex)
Provide a regex pattern that will match one or more fully qualified classnames of existing repositories.RepositoryContribution
withConcreteFragments(Map<Class<?>,?> fragments)
Pass a map of relationships between fragment interface and implementation.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.RepositoryContribution
withQueryFragments(List<Class<?>> fragments)
Pass a list of fragment interfaces whose method signatures inform dynamic query construction.
-
-
-
Method Detail
-
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
-
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()
-
getOrder
public int getOrder()
- Specified by:
getOrder
in interfaceorg.springframework.core.Ordered
-
setBaseRepositoryInterface
public void setBaseRepositoryInterface(Class<?> baseRepositoryInterface)
-
setBaseRepositoryInterfaceMatchRegex
public void setBaseRepositoryInterfaceMatchRegex(Pattern baseRepositoryInterfaceMatchRegex)
-
setOrder
public void setOrder(int order)
-
canEqual
protected boolean canEqual(Object other)
-
-