use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping 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.openadmin.server.service.persistence.module.criteria.FilterMapping in project BroadleafCommerce by BroadleafCommerce.
the class OfferCustomPersistenceHandler method addIsActiveFiltering.
protected void addIsActiveFiltering(CriteriaTransferObject cto) {
if (isActiveFilter && cto.getCriteriaMap().containsKey(IS_ACTIVE)) {
FilterAndSortCriteria filter = cto.get(IS_ACTIVE);
final Boolean isActive = Boolean.parseBoolean(filter.getFilterValues().get(0));
FilterMapping filterMapping = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty("id")).withDirectFilterValues(new EmptyFilterValues()).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {
@Override
public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) {
Date currentTime = SystemTime.asDate(true);
if (isActive) {
return builder.and(builder.isNotNull(root.get("startDate")), builder.lessThan(root.get("startDate"), currentTime), builder.or(builder.isNull(root.get("endDate")), builder.greaterThan(root.get("endDate"), currentTime)));
} else {
return builder.or(builder.isNull(root.get("startDate")), builder.greaterThan(root.get("startDate"), currentTime), builder.and(builder.isNotNull(root.get("endDate")), builder.lessThan(root.get("endDate"), currentTime)));
}
}
}));
cto.getAdditionalFilterMappings().add(filterMapping);
}
}
use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping in project BroadleafCommerce by BroadleafCommerce.
the class SkuCustomPersistenceHandler method applyProductOptionValueCriteria.
public void applyProductOptionValueCriteria(List<FilterMapping> filterMappings, CriteriaTransferObject cto, PersistencePackage persistencePackage, String skuPropertyPrefix) {
// if the front
final List<Long> productOptionValueFilterIDs = new ArrayList<>();
for (String filterProperty : cto.getCriteriaMap().keySet()) {
if (filterProperty.startsWith(PRODUCT_OPTION_FIELD_PREFIX)) {
FilterAndSortCriteria criteria = cto.get(filterProperty);
productOptionValueFilterIDs.add(Long.parseLong(criteria.getFilterValues().get(0)));
}
}
// also determine if there is a consolidated POV query
final List<String> productOptionValueFilterValues = new ArrayList<>();
FilterAndSortCriteria consolidatedCriteria = cto.get(CONSOLIDATED_PRODUCT_OPTIONS_FIELD_NAME);
if (!consolidatedCriteria.getFilterValues().isEmpty()) {
// the criteria in this case would be a semi-colon delimeter value list
productOptionValueFilterValues.addAll(Arrays.asList(StringUtils.split(consolidatedCriteria.getFilterValues().get(0), CONSOLIDATED_PRODUCT_OPTIONS_DELIMETER)));
}
if (productOptionValueFilterIDs.size() > 0) {
FilterMapping filterMapping = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty(StringUtils.isEmpty(skuPropertyPrefix) ? "" : skuPropertyPrefix + ".productOptionValueXrefs.productOptionValue.id")).withDirectFilterValues(productOptionValueFilterIDs).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {
@Override
public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) {
return explicitPath.as(Long.class).in(directValues);
}
}));
filterMappings.add(filterMapping);
}
if (productOptionValueFilterValues.size() > 0) {
FilterMapping filterMapping = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty(StringUtils.isEmpty(skuPropertyPrefix) ? "" : skuPropertyPrefix + ".productOptionValueXrefs.productOptionValue.attributeValue")).withDirectFilterValues(productOptionValueFilterValues).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {
@Override
public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) {
return explicitPath.as(String.class).in(directValues);
}
}));
filterMappings.add(filterMapping);
}
}
use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping in project BroadleafCommerce by BroadleafCommerce.
the class SkuCustomPersistenceHandler method createToOneIndividualOptionField.
/**
* Using a ToOne lookup performs much better for large product option value lists, speeds up initial page load,
* and is generally more accurate in relation to option value updates and how they impact available selections and cache.
*
* @param option
* @param order
* @return
*/
protected FieldMetadata createToOneIndividualOptionField(ProductOption option, int order) {
PersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
FilterMapping filterMapping = new FilterMapping().withDirectFilterValues(sandBoxHelper.mergeCloneIds(ProductOptionImpl.class, option.getId())).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {
@Override
public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) {
return root.get("productOption").get("id").in(directValues);
}
}));
List<FilterMapping> mappings = new ArrayList<>();
mappings.add(filterMapping);
TypedQuery<Serializable> countQuery = criteriaTranslator.translateCountQuery(persistenceManager.getDynamicEntityDao(), ProductOptionValueImpl.class.getName(), mappings);
Long count = (Long) countQuery.getSingleResult();
BasicFieldMetadata metadata = null;
if (count > 0) {
metadata = new BasicFieldMetadata();
metadata.setFieldType(SupportedFieldType.ADDITIONAL_FOREIGN_KEY);
metadata.setSecondaryType(SupportedFieldType.INTEGER);
metadata.setForeignKeyProperty("id");
metadata.setForeignKeyClass(ProductOptionValueImpl.class.getName());
metadata.setForeignKeyDisplayValueProperty("attributeValue");
metadata.setLookupDisplayProperty("attributeValue");
metadata.setForeignKeyCollection(false);
metadata.setCustomCriteria(new String[] { "option=" + String.valueOf(option.getId()) });
metadata.setName(PRODUCT_OPTION_FIELD_PREFIX + option.getId());
metadata.setFriendlyName(option.getLabel());
metadata.setGroup("productOption_group");
metadata.setGroupOrder(-1);
metadata.setOrder(order);
metadata.setExplicitFieldType(SupportedFieldType.ADDITIONAL_FOREIGN_KEY);
metadata.setProminent(false);
metadata.setVisibility(VisibilityEnum.FORM_EXPLICITLY_SHOWN);
metadata.setReadOnly(false);
// these may not be actually required, but the CPH has this as a requirement for parsing the data, so we'll stick with it here
metadata.setRequiredOverride(true);
metadata.setLookupType(LookupType.STANDARD);
metadata.setMutable(true);
metadata.setInheritedFromType(SkuImpl.class.getName());
metadata.setAvailableToTypes(getPolymorphicClasses(SkuImpl.class, em, skuMetadataCacheService.useCache()));
metadata.setMergedPropertyType(MergedPropertyType.PRIMARY);
metadata.setTargetClass(SkuImpl.class.getName());
metadata.setFieldName(PRODUCT_OPTION_FIELD_PREFIX + option.getId());
}
return metadata;
}
use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping in project BroadleafCommerce by BroadleafCommerce.
the class SkuCustomPersistenceHandler method applySkuBundleItemValueCriteria.
/**
* Add filter restriction such that a ProductBundle cannot add its own default sku as a Sku Bundle Item
*/
private void applySkuBundleItemValueCriteria(List<FilterMapping> filterMappings, CriteriaTransferObject cto, PersistencePackage persistencePackage) {
SectionCrumb[] sectionCrumbs = persistencePackage.getSectionCrumbs();
if (isSkuBundleItemLookup(persistencePackage, sectionCrumbs)) {
final Long defaultSkuId = getOwningProductBundlesDefaultSkuId(sectionCrumbs[0]);
filterMappings.add(new FilterMapping().withDirectFilterValues(Collections.singletonList(defaultSkuId)).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {
@Override
public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) {
return builder.notEqual(root, directValues.get(0));
}
})));
}
}
Aggregations