use of org.broadleafcommerce.common.util.dao.TQRestriction in project BroadleafCommerce by BroadleafCommerce.
the class BasicPersistenceModule method buildStandardRestrictions.
/**
* Generate LIKE or EQUALS restrictions for any filter property specified on the root entity (not the collection field in the @Embeddable object)
*
* @see #getSpecialCaseQueryBuilder(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath, java.util.List, String)
* @param embeddedCollectionPath the path for the collection field in the @Embeddable object - this is what caused the whole thing
* @param filterMappings all the fetch restrictions for this request
* @return the list of restrictions on the root entity
*/
protected List<TQRestriction> buildStandardRestrictions(FieldPath embeddedCollectionPath, List<FilterMapping> filterMappings) {
String expression = embeddedCollectionPath.getTargetProperty().substring(0, embeddedCollectionPath.getTargetProperty().lastIndexOf("."));
List<TQRestriction> restrictions = new ArrayList<TQRestriction>();
for (FilterMapping mapping : filterMappings) {
checkProperty: {
String mappingProperty = mapping.getFieldPath() == null ? null : mapping.getFieldPath().getTargetProperty();
if (StringUtils.isEmpty(mappingProperty)) {
mappingProperty = mapping.getFullPropertyName();
}
if (!embeddedCollectionPath.getTargetProperty().equals(mappingProperty) && !StringUtils.isEmpty(mappingProperty)) {
PredicateProvider predicateProvider = mapping.getRestriction().getPredicateProvider();
if (predicateProvider != null) {
FilterValueConverter converter = mapping.getRestriction().getFilterValueConverter();
if (converter != null && CollectionUtils.isNotEmpty(mapping.getFilterValues())) {
Object val = converter.convert(mapping.getFilterValues().get(0));
if (predicateProvider instanceof LikePredicateProvider) {
restrictions.add(new TQRestriction("specialEntity." + mappingProperty, "LIKE", val + "%"));
break checkProperty;
} else if (predicateProvider instanceof EqPredicateProvider) {
restrictions.add(new TQRestriction("specialEntity." + mappingProperty, "=", val));
break checkProperty;
}
}
}
LOG.warn(String.format("Unable to filter the embedded collection (%s) on an additional property (%s)", StringUtil.sanitize(expression), StringUtil.sanitize(mappingProperty)));
}
}
}
return restrictions;
}
use of org.broadleafcommerce.common.util.dao.TQRestriction in project BroadleafCommerce by BroadleafCommerce.
the class BasicPersistenceModule method buildSpecialRestrictions.
/**
* Generate EQUALS restrictions for any filter property specified on the entity member of the collection field in the @Embeddable object
*
* @see #getSpecialCaseQueryBuilder(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath, java.util.List, String)
* @param specialExpression the String representation of the path for the collection field in the @Embeddable object
* @param filterMappings all the fetch restrictions for this request
* @return the list of restrictions on the collection in the @Embeddable object
*/
protected List<TQRestriction> buildSpecialRestrictions(String specialExpression, List<FilterMapping> filterMappings) {
List<TQRestriction> restrictions = new ArrayList<TQRestriction>();
for (FilterMapping mapping : filterMappings) {
if (mapping.getFieldPath() != null && mapping.getFieldPath().getTargetProperty() != null && mapping.getFieldPath().getTargetProperty().startsWith(specialExpression)) {
FilterValueConverter converter = mapping.getRestriction().getFilterValueConverter();
if (converter != null && CollectionUtils.isNotEmpty(mapping.getFilterValues())) {
Object val = converter.convert(mapping.getFilterValues().get(0));
String property = mapping.getFieldPath().getTargetProperty().substring(mapping.getFieldPath().getTargetProperty().lastIndexOf(".") + 1, mapping.getFieldPath().getTargetProperty().length());
restrictions.add(new TQRestriction("embeddedCollection." + property, "=", val));
}
}
}
return restrictions;
}
use of org.broadleafcommerce.common.util.dao.TQRestriction in project BroadleafCommerce by BroadleafCommerce.
the class BasicPersistenceModule method getSpecialCaseQueryBuilder.
/**
* Use an alternate approach to generating a fetch query for a collection located inside of an @Embeddable object. Related
* to https://hibernate.atlassian.net/browse/HHH-8802. The alternate approach leverages HQL rather than JPA criteria,
* which seems to alleviate the problem.
*
* @param embeddedCollectionPath the path to the collection field itself
* @param filterMappings all the fetch restrictions for this request
* @param collectionClass the type of the collection members
* @return the builder capable of generating an appropriate HQL query
*/
protected TypedQueryBuilder getSpecialCaseQueryBuilder(FieldPath embeddedCollectionPath, List<FilterMapping> filterMappings, String collectionClass) {
String specialPath = embeddedCollectionPath.getTargetProperty();
String[] pieces = specialPath.split("\\.");
if (pieces.length != 3) {
throw new CriteriaConversionException(String.format("Expected to find a target property of format [embedded field].[collection field].[property] for the embedded collection path (%s)", specialPath), embeddedCollectionPath);
}
String expression = specialPath.substring(0, specialPath.lastIndexOf("."));
TypedQueryBuilder builder;
try {
builder = new TypedQueryBuilder(Class.forName(collectionClass), "specialEntity").addJoin(new TQJoin("specialEntity." + expression, "embeddedCollection"));
} catch (Exception e) {
throw ExceptionHelper.refineException(e);
}
for (TQRestriction restriction : buildSpecialRestrictions(expression, filterMappings)) {
builder = builder.addRestriction(restriction);
}
for (TQRestriction restriction : buildStandardRestrictions(embeddedCollectionPath, filterMappings)) {
builder = builder.addRestriction(restriction);
}
for (FilterMapping mapping : filterMappings) {
if (mapping.getSortDirection() != null) {
String mappingProperty = mapping.getFieldPath() == null ? null : mapping.getFieldPath().getTargetProperty();
if (StringUtils.isEmpty(mappingProperty)) {
mappingProperty = mapping.getFullPropertyName();
}
builder = builder.addOrder(new TQOrder("specialEntity." + mappingProperty, SortDirection.ASCENDING == mapping.getSortDirection()));
}
}
return builder;
}
use of org.broadleafcommerce.common.util.dao.TQRestriction in project BroadleafCommerce by BroadleafCommerce.
the class TypedQueryBuilderTest method testOneNested.
public void testOneNested() {
TypedQueryBuilder<String> q = new TypedQueryBuilder<>(String.class, "test");
TQRestriction r = new TQRestriction(TQRestriction.Mode.AND).addChildRestriction(new TQRestriction("test.startDate", "<", "123")).addChildRestriction(new TQRestriction(TQRestriction.Mode.OR).addChildRestriction(new TQRestriction("test.endDate", "is null")).addChildRestriction(new TQRestriction("test.endDate", ">", "456")));
q.addRestriction("test.attr", "=", "sample");
q.addRestriction(r);
StringBuilder expected = new StringBuilder("SELECT test FROM " + String.class.getName() + " test").append(" WHERE (test.attr = :p0)").append(" AND ((test.startDate < :p1_0) AND ((test.endDate is null) OR (test.endDate > :p1_1_1)))");
assertEquals(q.toQueryString(), expected.toString());
assertEquals(q.getParamMap().get("p0"), "sample");
assertEquals(q.getParamMap().get("p1_0"), "123");
assertEquals(q.getParamMap().get("p1_1"), null);
assertEquals(q.getParamMap().get("p1_1_0"), null);
assertEquals(q.getParamMap().get("p1_1_1"), "456");
assertEquals(q.getParamMap().size(), 5);
}
use of org.broadleafcommerce.common.util.dao.TQRestriction in project BroadleafCommerce by BroadleafCommerce.
the class TypedQueryBuilderTest method testTwoNested.
public void testTwoNested() {
TypedQueryBuilder<String> q = new TypedQueryBuilder<>(String.class, "test");
TQRestriction r = new TQRestriction(TQRestriction.Mode.AND).addChildRestriction(new TQRestriction("test.startDate", "<", "123")).addChildRestriction(new TQRestriction(TQRestriction.Mode.OR).addChildRestriction(new TQRestriction("test.endDate", "is null")).addChildRestriction(new TQRestriction("test.endDate", ">", "456")));
TQRestriction r2 = new TQRestriction(TQRestriction.Mode.OR).addChildRestriction(new TQRestriction("test.res1", "=", "333")).addChildRestriction(new TQRestriction(TQRestriction.Mode.AND).addChildRestriction(new TQRestriction("test.res2", "is null")).addChildRestriction(new TQRestriction("test.res3", ">", "456")));
q.addRestriction("test.attr", "=", "sample");
q.addRestriction(r);
q.addRestriction(r2);
System.out.println(q.toQueryString());
StringBuilder expected = new StringBuilder("SELECT test FROM " + String.class.getName() + " test").append(" WHERE (test.attr = :p0)").append(" AND ((test.startDate < :p1_0) AND ((test.endDate is null) OR (test.endDate > :p1_1_1)))").append(" AND ((test.res1 = :p2_0) OR ((test.res2 is null) AND (test.res3 > :p2_1_1)))");
assertEquals(q.toQueryString(), expected.toString());
assertEquals(q.getParamMap().get("p0"), "sample");
assertEquals(q.getParamMap().get("p1_0"), "123");
assertEquals(q.getParamMap().get("p1_1"), null);
assertEquals(q.getParamMap().get("p1_1_0"), null);
assertEquals(q.getParamMap().get("p1_1_1"), "456");
assertEquals(q.getParamMap().get("p2_0"), "333");
assertEquals(q.getParamMap().get("p2_1"), null);
assertEquals(q.getParamMap().get("p2_1_0"), null);
assertEquals(q.getParamMap().get("p2_1_1"), "456");
assertEquals(q.getParamMap().size(), 9);
}
Aggregations