Search in sources :

Example 1 with FilterMapping

use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping in project BroadleafCommerce by BroadleafCommerce.

the class AdornedTargetListPersistenceModule method add.

@Override
public Entity add(PersistencePackage persistencePackage) throws ServiceException {
    String[] customCriteria = persistencePackage.getCustomCriteria();
    if (customCriteria != null && customCriteria.length > 0) {
        LOG.warn("custom persistence handlers and custom criteria not supported for add types other than BASIC");
    }
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    Entity entity = persistencePackage.getEntity();
    AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
    if (!adornedTargetList.getMutable()) {
        throw new SecurityServiceException("Field is not mutable");
    }
    Entity payload;
    try {
        Class<?>[] entities = persistenceManager.getPolymorphicEntities(ceilingEntityFullyQualifiedClassname);
        Map<String, FieldMetadata> mergedPropertiesTarget = persistenceManager.getDynamicEntityDao().getMergedProperties(ceilingEntityFullyQualifiedClassname, entities, null, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
        Class<?>[] entities2 = persistenceManager.getPolymorphicEntities(adornedTargetList.getAdornedTargetEntityClassname());
        Map<String, FieldMetadata> mergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(adornedTargetList.getAdornedTargetEntityClassname(), entities2, null, new String[] {}, new ForeignKey[] {}, MergedPropertyType.ADORNEDTARGETLIST, false, new String[] {}, new String[] {}, null, "");
        CriteriaTransferObject ctoInserted = new CriteriaTransferObject();
        FilterAndSortCriteria filterCriteriaInsertedLinked = ctoInserted.get(adornedTargetList.getCollectionFieldName());
        String linkedPath;
        String targetPath;
        if (adornedTargetList.getInverse()) {
            linkedPath = adornedTargetList.getTargetObjectPath() + "." + adornedTargetList.getTargetIdProperty();
            targetPath = adornedTargetList.getLinkedObjectPath() + "." + adornedTargetList.getLinkedIdProperty();
        } else {
            targetPath = adornedTargetList.getTargetObjectPath() + "." + adornedTargetList.getTargetIdProperty();
            linkedPath = adornedTargetList.getLinkedObjectPath() + "." + adornedTargetList.getLinkedIdProperty();
        }
        filterCriteriaInsertedLinked.setFilterValue(entity.findProperty(adornedTargetList.getInverse() ? targetPath : linkedPath).getValue());
        FilterAndSortCriteria filterCriteriaInsertedTarget = ctoInserted.get(adornedTargetList.getCollectionFieldName() + "Target");
        filterCriteriaInsertedTarget.setFilterValue(entity.findProperty(adornedTargetList.getInverse() ? linkedPath : targetPath).getValue());
        List<FilterMapping> filterMappingsInserted = getAdornedTargetFilterMappings(persistencePerspective, ctoInserted, mergedProperties, adornedTargetList);
        List<Serializable> recordsInserted = getPersistentRecords(adornedTargetList.getAdornedTargetEntityClassname(), filterMappingsInserted, ctoInserted.getFirstResult(), ctoInserted.getMaxResults());
        if (recordsInserted.size() > 0) {
            payload = getRecords(mergedPropertiesTarget, recordsInserted, mergedProperties, adornedTargetList.getTargetObjectPath(), null)[0];
        } else {
            Serializable instance = createPopulatedAdornedTargetInstance(adornedTargetList, entity);
            instance = createPopulatedInstance(instance, entity, mergedProperties, false, persistencePackage.isValidateUnsubmittedProperties());
            instance = createPopulatedInstance(instance, entity, mergedPropertiesTarget, false, persistencePackage.isValidateUnsubmittedProperties());
            FieldManager fieldManager = getFieldManager();
            if (fieldManager.getField(instance.getClass(), "id") != null) {
                fieldManager.setFieldValue(instance, "id", null);
            }
            if (adornedTargetList.getSortField() != null) {
                // Construct a query that gets the last element in the join list ordered by the sort property. This will
                // ensure that the new record is always the last element in the list
                CriteriaTransferObject cto = new CriteriaTransferObject();
                FilterAndSortCriteria filterCriteria = cto.get(adornedTargetList.getCollectionFieldName());
                filterCriteria.setFilterValue(entity.findProperty(adornedTargetList.getInverse() ? targetPath : linkedPath).getValue());
                FilterAndSortCriteria sortCriteria = cto.get(adornedTargetList.getSortField());
                // criteria for which way to sort should be the opposite of how it is normally sorted so that it is
                // always inserted at the end
                sortCriteria.setSortAscending(!adornedTargetList.getSortAscending());
                List<FilterMapping> filterMappings = getAdornedTargetFilterMappings(persistencePerspective, cto, mergedProperties, adornedTargetList);
                List<Serializable> joinList = getPersistentRecords(adornedTargetList.getAdornedTargetEntityClassname(), filterMappings, 0, 1);
                Object adornedLastOrdering = null;
                if (CollectionUtils.isNotEmpty(joinList)) {
                    adornedLastOrdering = fieldManager.getFieldValue(joinList.get(0), adornedTargetList.getSortField());
                }
                Field sortFieldDef = fieldManager.getField(instance.getClass(), adornedTargetList.getSortField());
                int add = (adornedLastOrdering == null) ? 0 : 1;
                if (sortFieldDef.getType().isAssignableFrom(Long.class)) {
                    adornedLastOrdering = (adornedLastOrdering == null) ? new Long(0) : adornedLastOrdering;
                    fieldManager.setFieldValue(instance, adornedTargetList.getSortField(), new Long(((Long) adornedLastOrdering).longValue() + add));
                } else if (sortFieldDef.getType().isAssignableFrom(Integer.class)) {
                    adornedLastOrdering = (adornedLastOrdering == null) ? new Integer(0) : adornedLastOrdering;
                    fieldManager.setFieldValue(instance, adornedTargetList.getSortField(), new Integer(((Integer) adornedLastOrdering).intValue() + add));
                } else if (sortFieldDef.getType().isAssignableFrom(BigDecimal.class)) {
                    adornedLastOrdering = (adornedLastOrdering == null) ? BigDecimal.ZERO : adornedLastOrdering;
                    fieldManager.setFieldValue(instance, adornedTargetList.getSortField(), ((BigDecimal) adornedLastOrdering).add(new BigDecimal(add)));
                }
            }
            instance = persistenceManager.getDynamicEntityDao().merge(instance);
            persistenceManager.getDynamicEntityDao().clear();
            List<Serializable> recordsInserted2 = getPersistentRecords(adornedTargetList.getAdornedTargetEntityClassname(), filterMappingsInserted, ctoInserted.getFirstResult(), ctoInserted.getMaxResults());
            payload = getRecords(mergedPropertiesTarget, recordsInserted2, mergedProperties, adornedTargetList.getTargetObjectPath(), null)[0];
        }
    } catch (Exception e) {
        throw new ServiceException("Problem adding new entity : " + e.getMessage(), e);
    }
    return payload;
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) Serializable(java.io.Serializable) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) Field(java.lang.reflect.Field) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) BigDecimal(java.math.BigDecimal) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)

Example 2 with FilterMapping

use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping in project BroadleafCommerce by BroadleafCommerce.

the class AdornedTargetListPersistenceModule method getAdornedTargetFilterMappings.

public List<FilterMapping> getAdornedTargetFilterMappings(PersistencePerspective persistencePerspective, CriteriaTransferObject cto, Map<String, FieldMetadata> mergedProperties, AdornedTargetList adornedTargetList) throws ClassNotFoundException {
    List<FilterMapping> filterMappings = getFilterMappings(persistencePerspective, cto, adornedTargetList.getAdornedTargetEntityClassname(), mergedProperties);
    FilterMapping filterMapping = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty(adornedTargetList.getLinkedObjectPath() + "." + adornedTargetList.getLinkedIdProperty())).withFilterValues(cto.get(adornedTargetList.getCollectionFieldName()).getFilterValues()).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider<Serializable, String>() {

        @Override
        public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path<Serializable> explicitPath, List<String> directValues) {
            if (String.class.isAssignableFrom(explicitPath.getJavaType())) {
                return builder.equal(explicitPath, directValues.get(0));
            } else {
                return builder.equal(explicitPath, Long.parseLong(directValues.get(0)));
            }
        }
    }));
    filterMappings.add(filterMapping);
    FilterMapping filterMapping2 = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty(adornedTargetList.getTargetObjectPath() + "." + adornedTargetList.getTargetIdProperty())).withFilterValues(cto.get(adornedTargetList.getCollectionFieldName() + "Target").getFilterValues()).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider<Serializable, String>() {

        @Override
        public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path<Serializable> explicitPath, List<String> directValues) {
            if (String.class.isAssignableFrom(explicitPath.getJavaType())) {
                return builder.equal(explicitPath, directValues.get(0));
            } else {
                return builder.equal(explicitPath, Long.parseLong(directValues.get(0)));
            }
        }
    }));
    filterMappings.add(filterMapping2);
    return filterMappings;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) Restriction(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction) PredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) ArrayList(java.util.ArrayList) List(java.util.List) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) From(javax.persistence.criteria.From) FieldPathBuilder(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder)

Example 3 with FilterMapping

use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping in project BroadleafCommerce by BroadleafCommerce.

the class AdornedTargetListPersistenceModule method remove.

@Override
public void remove(PersistencePackage persistencePackage) throws ServiceException {
    String[] customCriteria = persistencePackage.getCustomCriteria();
    if (customCriteria != null && customCriteria.length > 0) {
        LOG.warn("custom persistence handlers and custom criteria not supported for remove types other than BASIC");
    }
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    Entity entity = persistencePackage.getEntity();
    try {
        AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
        if (!adornedTargetList.getMutable()) {
            throw new SecurityServiceException("Field is not mutable");
        }
        Class<?>[] entities = persistenceManager.getPolymorphicEntities(adornedTargetList.getAdornedTargetEntityClassname());
        Map<String, FieldMetadata> mergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(adornedTargetList.getAdornedTargetEntityClassname(), entities, null, new String[] {}, new ForeignKey[] {}, MergedPropertyType.ADORNEDTARGETLIST, false, new String[] {}, new String[] {}, null, "");
        CriteriaTransferObject ctoInserted = new CriteriaTransferObject();
        FilterAndSortCriteria filterCriteriaInsertedLinked = ctoInserted.get(adornedTargetList.getCollectionFieldName());
        filterCriteriaInsertedLinked.setFilterValue(entity.findProperty(adornedTargetList.getLinkedObjectPath() + "." + adornedTargetList.getLinkedIdProperty()).getValue());
        FilterAndSortCriteria filterCriteriaInsertedTarget = ctoInserted.get(adornedTargetList.getCollectionFieldName() + "Target");
        filterCriteriaInsertedTarget.setFilterValue(entity.findProperty(adornedTargetList.getTargetObjectPath() + "." + adornedTargetList.getTargetIdProperty()).getValue());
        List<FilterMapping> filterMappings = getAdornedTargetFilterMappings(persistencePerspective, ctoInserted, mergedProperties, adornedTargetList);
        List<Serializable> recordsInserted = getPersistentRecords(adornedTargetList.getAdornedTargetEntityClassname(), filterMappings, ctoInserted.getFirstResult(), ctoInserted.getMaxResults());
        Assert.isTrue(!CollectionUtils.isEmpty(recordsInserted), "Entity not found");
        persistenceManager.getDynamicEntityDao().remove(recordsInserted.get(0));
    } catch (Exception e) {
        throw new ServiceException("Problem removing entity : " + e.getMessage(), e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) Serializable(java.io.Serializable) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)

Example 4 with FilterMapping

use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping in project BroadleafCommerce by BroadleafCommerce.

the class BasicPersistenceModule method getFilterMappings.

@Override
public List<FilterMapping> getFilterMappings(PersistencePerspective persistencePerspective, CriteriaTransferObject cto, String ceilingEntityFullyQualifiedClassname, Map<String, FieldMetadata> mergedUnfilteredProperties, RestrictionFactory customRestrictionFactory) {
    Map<String, FieldMetadata> mergedProperties = filterOutCollectionMetadata(mergedUnfilteredProperties);
    List<FilterMapping> filterMappings = new ArrayList<FilterMapping>();
    for (String propertyId : cto.getCriteriaMap().keySet()) {
        if (mergedProperties.containsKey(propertyId)) {
            boolean handled = false;
            for (FieldPersistenceProvider fieldPersistenceProvider : fieldPersistenceProviders) {
                MetadataProviderResponse response = fieldPersistenceProvider.addSearchMapping(new AddSearchMappingRequest(persistencePerspective, cto, ceilingEntityFullyQualifiedClassname, mergedProperties, propertyId, getFieldManager(), this, this, customRestrictionFactory == null ? restrictionFactory : customRestrictionFactory), filterMappings);
                if (MetadataProviderResponse.NOT_HANDLED != response) {
                    handled = true;
                }
                if (MetadataProviderResponse.HANDLED_BREAK == response) {
                    break;
                }
            }
            if (!handled) {
                defaultFieldPersistenceProvider.addSearchMapping(new AddSearchMappingRequest(persistencePerspective, cto, ceilingEntityFullyQualifiedClassname, mergedProperties, propertyId, getFieldManager(), this, this, customRestrictionFactory == null ? restrictionFactory : customRestrictionFactory), filterMappings);
            }
        }
    }
    return filterMappings;
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) MetadataProviderResponse(org.broadleafcommerce.openadmin.server.service.type.MetadataProviderResponse) ArrayList(java.util.ArrayList) FieldPersistenceProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.provider.FieldPersistenceProvider) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) AddSearchMappingRequest(org.broadleafcommerce.openadmin.server.service.persistence.module.provider.request.AddSearchMappingRequest)

Example 5 with FilterMapping

use of org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping 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;
}
Also used : EqPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.EqPredicateProvider) LikePredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.LikePredicateProvider) EqPredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.EqPredicateProvider) PredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider) ArrayList(java.util.ArrayList) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) LikePredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.LikePredicateProvider) FilterValueConverter(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.converter.FilterValueConverter) TQRestriction(org.broadleafcommerce.common.util.dao.TQRestriction)

Aggregations

FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)22 Restriction (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction)11 PredicateProvider (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider)11 ArrayList (java.util.ArrayList)10 List (java.util.List)10 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)10 From (javax.persistence.criteria.From)10 Path (javax.persistence.criteria.Path)10 FilterAndSortCriteria (org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria)10 FieldPath (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath)10 FieldPathBuilder (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder)10 Serializable (java.io.Serializable)8 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)8 ServiceException (org.broadleafcommerce.common.exception.ServiceException)7 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)7 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)7 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)6 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)6 Entity (org.broadleafcommerce.openadmin.dto.Entity)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5