Search in sources :

Example 1 with PersistencePerspective

use of org.broadleafcommerce.openadmin.dto.PersistencePerspective in project BroadleafCommerce by BroadleafCommerce.

the class AdornedTargetCollectionFieldMetadataProvider method buildAdornedTargetCollectionMetadata.

protected void buildAdornedTargetCollectionMetadata(Class<?> parentClass, Class<?> targetClass, Map<String, FieldMetadata> attributes, FieldInfo field, FieldMetadataOverride adornedTargetCollectionMetadata, DynamicEntityDao dynamicEntityDao) {
    AdornedTargetCollectionMetadata serverMetadata = (AdornedTargetCollectionMetadata) attributes.get(field.getName());
    Class<?> resolvedClass = parentClass == null ? targetClass : parentClass;
    AdornedTargetCollectionMetadata metadata;
    if (serverMetadata != null) {
        metadata = serverMetadata;
    } else {
        metadata = new AdornedTargetCollectionMetadata();
    }
    metadata.setTargetClass(targetClass.getName());
    metadata.setFieldName(field.getName());
    if (adornedTargetCollectionMetadata.getReadOnly() != null) {
        metadata.setMutable(!adornedTargetCollectionMetadata.getReadOnly());
    }
    if (adornedTargetCollectionMetadata.getShowIfProperty() != null) {
        metadata.setShowIfProperty(adornedTargetCollectionMetadata.getShowIfProperty());
    }
    if (adornedTargetCollectionMetadata.getShowIfFieldEquals() != null) {
        metadata.setShowIfFieldEquals(adornedTargetCollectionMetadata.getShowIfFieldEquals());
    }
    org.broadleafcommerce.openadmin.dto.OperationTypes dtoOperationTypes = new org.broadleafcommerce.openadmin.dto.OperationTypes(OperationType.ADORNEDTARGETLIST, OperationType.ADORNEDTARGETLIST, OperationType.ADORNEDTARGETLIST, OperationType.ADORNEDTARGETLIST, OperationType.BASIC);
    if (adornedTargetCollectionMetadata.getAddType() != null) {
        dtoOperationTypes.setAddType(adornedTargetCollectionMetadata.getAddType());
    }
    if (adornedTargetCollectionMetadata.getRemoveType() != null) {
        dtoOperationTypes.setRemoveType(adornedTargetCollectionMetadata.getRemoveType());
    }
    if (adornedTargetCollectionMetadata.getFetchType() != null) {
        dtoOperationTypes.setFetchType(adornedTargetCollectionMetadata.getFetchType());
    }
    if (adornedTargetCollectionMetadata.getInspectType() != null) {
        dtoOperationTypes.setInspectType(adornedTargetCollectionMetadata.getInspectType());
    }
    if (adornedTargetCollectionMetadata.getUpdateType() != null) {
        dtoOperationTypes.setUpdateType(adornedTargetCollectionMetadata.getUpdateType());
    }
    // don't allow additional non-persistent properties or additional foreign keys for an advanced collection datasource - they don't make sense in this context
    PersistencePerspective persistencePerspective;
    if (serverMetadata != null) {
        persistencePerspective = metadata.getPersistencePerspective();
        persistencePerspective.setOperationTypes(dtoOperationTypes);
    } else {
        persistencePerspective = new PersistencePerspective(dtoOperationTypes, new String[] {}, new ForeignKey[] {});
        metadata.setPersistencePerspective(persistencePerspective);
    }
    String parentObjectProperty = null;
    if (serverMetadata != null) {
        parentObjectProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST)).getLinkedObjectPath();
    }
    if (!StringUtils.isEmpty(adornedTargetCollectionMetadata.getParentObjectProperty())) {
        parentObjectProperty = adornedTargetCollectionMetadata.getParentObjectProperty();
    }
    if (parentObjectProperty == null && !StringUtils.isEmpty(field.getOneToManyMappedBy())) {
        parentObjectProperty = field.getOneToManyMappedBy();
    }
    if (parentObjectProperty == null && !StringUtils.isEmpty(field.getManyToManyMappedBy())) {
        parentObjectProperty = field.getManyToManyMappedBy();
    }
    if (StringUtils.isEmpty(parentObjectProperty)) {
        throw new IllegalArgumentException("Unable to infer a parentObjectProperty for the @AdminPresentationAdornedTargetCollection annotated field(" + field.getName() + "). If not using the mappedBy property of @OneToMany or @ManyToMany, please make sure to explicitly define the parentObjectProperty property");
    }
    String sortProperty = null;
    if (serverMetadata != null) {
        sortProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST)).getSortField();
    }
    if (!StringUtils.isEmpty(adornedTargetCollectionMetadata.getSortProperty())) {
        sortProperty = adornedTargetCollectionMetadata.getSortProperty();
    }
    metadata.setParentObjectClass(resolvedClass.getName());
    if (adornedTargetCollectionMetadata.getMaintainedAdornedTargetFields() != null) {
        metadata.setMaintainedAdornedTargetFields(adornedTargetCollectionMetadata.getMaintainedAdornedTargetFields());
    }
    if (adornedTargetCollectionMetadata.getGridVisibleFields() != null) {
        metadata.setGridVisibleFields(adornedTargetCollectionMetadata.getGridVisibleFields());
    }
    if (adornedTargetCollectionMetadata.getSelectizeVisibleField() != null) {
        metadata.setSelectizeVisibleField(adornedTargetCollectionMetadata.getSelectizeVisibleField());
    }
    String parentObjectIdProperty = null;
    if (serverMetadata != null) {
        parentObjectIdProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST)).getLinkedIdProperty();
    }
    if (adornedTargetCollectionMetadata.getParentObjectIdProperty() != null) {
        parentObjectIdProperty = adornedTargetCollectionMetadata.getParentObjectIdProperty();
    }
    String targetObjectProperty = null;
    if (serverMetadata != null) {
        targetObjectProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST)).getTargetObjectPath();
    }
    if (adornedTargetCollectionMetadata.getTargetObjectProperty() != null) {
        targetObjectProperty = adornedTargetCollectionMetadata.getTargetObjectProperty();
    }
    if (StringUtils.isEmpty(parentObjectIdProperty)) {
        throw new IllegalArgumentException("targetObjectProperty not defined");
    }
    String joinEntityClass = null;
    if (serverMetadata != null) {
        joinEntityClass = ((AdornedTargetList) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST)).getJoinEntityClass();
    }
    if (adornedTargetCollectionMetadata.getJoinEntityClass() != null) {
        joinEntityClass = adornedTargetCollectionMetadata.getJoinEntityClass();
    }
    Class<?> collectionTarget = null;
    try {
        checkCeiling: {
            try {
                ParameterizedType pt = (ParameterizedType) field.getGenericType();
                java.lang.reflect.Type collectionType = pt.getActualTypeArguments()[0];
                collectionTarget = (Class<?>) collectionType;
                // Hibernate class is from that
                if (collectionTarget.isInterface()) {
                    collectionTarget = entityConfiguration.lookupEntityClass(collectionTarget.getName());
                }
                break checkCeiling;
            } catch (NoSuchBeanDefinitionException e) {
            // We weren't successful at looking at entity configuration to find the type of this collection.
            // We will continue and attempt to find it via the Hibernate annotations
            }
            if (!StringUtils.isEmpty(field.getOneToManyTargetEntity()) && !void.class.getName().equals(field.getOneToManyTargetEntity())) {
                collectionTarget = Class.forName(field.getOneToManyTargetEntity());
                break checkCeiling;
            }
            if (!StringUtils.isEmpty(field.getManyToManyTargetEntity()) && !void.class.getName().equals(field.getManyToManyTargetEntity())) {
                collectionTarget = Class.forName(field.getManyToManyTargetEntity());
                break checkCeiling;
            }
        }
        if (StringUtils.isNotBlank(joinEntityClass)) {
            collectionTarget = Class.forName(joinEntityClass);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    if (collectionTarget == null) {
        throw new IllegalArgumentException("Unable to infer the type of the collection from the targetEntity property of a OneToMany or ManyToMany collection.");
    }
    Field collectionTargetField = dynamicEntityDao.getFieldManager().getField(collectionTarget, targetObjectProperty);
    ManyToOne manyToOne = collectionTargetField.getAnnotation(ManyToOne.class);
    String ceiling = null;
    checkCeiling: {
        if (manyToOne != null && manyToOne.targetEntity() != void.class) {
            ceiling = manyToOne.targetEntity().getName();
            break checkCeiling;
        }
        ceiling = collectionTargetField.getType().getName();
    }
    if (!StringUtils.isEmpty(ceiling)) {
        metadata.setCollectionCeilingEntity(ceiling);
    }
    String targetObjectIdProperty = null;
    if (serverMetadata != null) {
        targetObjectIdProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST)).getTargetIdProperty();
    }
    if (adornedTargetCollectionMetadata.getTargetObjectIdProperty() != null) {
        targetObjectIdProperty = adornedTargetCollectionMetadata.getTargetObjectIdProperty();
    }
    Boolean isAscending = true;
    if (serverMetadata != null) {
        isAscending = ((AdornedTargetList) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST)).getSortAscending();
    }
    if (adornedTargetCollectionMetadata.isSortAscending() != null) {
        isAscending = adornedTargetCollectionMetadata.isSortAscending();
    }
    if (serverMetadata != null) {
        AdornedTargetList adornedTargetList = (AdornedTargetList) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
        adornedTargetList.setCollectionFieldName(field.getName());
        adornedTargetList.setLinkedObjectPath(parentObjectProperty);
        adornedTargetList.setLinkedIdProperty(parentObjectIdProperty);
        adornedTargetList.setTargetObjectPath(targetObjectProperty);
        adornedTargetList.setTargetIdProperty(targetObjectIdProperty);
        adornedTargetList.setJoinEntityClass(joinEntityClass);
        adornedTargetList.setIdProperty((String) dynamicEntityDao.getIdMetadata(collectionTarget).get("name"));
        adornedTargetList.setAdornedTargetEntityClassname(collectionTarget.getName());
        adornedTargetList.setSortField(sortProperty);
        adornedTargetList.setSortAscending(isAscending);
        adornedTargetList.setMutable(metadata.isMutable());
    } else {
        AdornedTargetList adornedTargetList = new AdornedTargetList(field.getName(), parentObjectProperty, parentObjectIdProperty, targetObjectProperty, targetObjectIdProperty, collectionTarget.getName(), sortProperty, isAscending);
        adornedTargetList.setJoinEntityClass(joinEntityClass);
        adornedTargetList.setIdProperty((String) dynamicEntityDao.getIdMetadata(collectionTarget).get("name"));
        adornedTargetList.setMutable(metadata.isMutable());
        persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.ADORNEDTARGETLIST, adornedTargetList);
    }
    if (adornedTargetCollectionMetadata.getExcluded() != null) {
        if (LOG.isDebugEnabled()) {
            if (adornedTargetCollectionMetadata.getExcluded()) {
                LOG.debug("buildAdornedTargetCollectionMetadata:Excluding " + field.getName() + " because it was explicitly declared in config");
            } else {
                LOG.debug("buildAdornedTargetCollectionMetadata:Showing " + field.getName() + " because it was explicitly declared in config");
            }
        }
        metadata.setExcluded(adornedTargetCollectionMetadata.getExcluded());
    }
    if (adornedTargetCollectionMetadata.getLazyFetch() != null) {
        metadata.setLazyFetch(adornedTargetCollectionMetadata.getLazyFetch());
    }
    if (adornedTargetCollectionMetadata.getManualFetch() != null) {
        metadata.setManualFetch(adornedTargetCollectionMetadata.getManualFetch());
    }
    if (adornedTargetCollectionMetadata.getFriendlyName() != null) {
        metadata.setFriendlyName(adornedTargetCollectionMetadata.getFriendlyName());
    }
    if (adornedTargetCollectionMetadata.getSecurityLevel() != null) {
        metadata.setSecurityLevel(adornedTargetCollectionMetadata.getSecurityLevel());
    }
    if (adornedTargetCollectionMetadata.getOrder() != null) {
        metadata.setOrder(adornedTargetCollectionMetadata.getOrder());
    }
    if (adornedTargetCollectionMetadata.getTab() != null) {
        metadata.setTab(adornedTargetCollectionMetadata.getTab());
    }
    if (adornedTargetCollectionMetadata.getTabOrder() != null) {
        metadata.setTabOrder(adornedTargetCollectionMetadata.getTabOrder());
    }
    if (adornedTargetCollectionMetadata.getGroup() != null) {
        metadata.setGroup(adornedTargetCollectionMetadata.getGroup());
    }
    if (adornedTargetCollectionMetadata.getCustomCriteria() != null) {
        metadata.setCustomCriteria(adornedTargetCollectionMetadata.getCustomCriteria());
    }
    if (adornedTargetCollectionMetadata.getUseServerSideInspectionCache() != null) {
        persistencePerspective.setUseServerSideInspectionCache(adornedTargetCollectionMetadata.getUseServerSideInspectionCache());
    }
    if (adornedTargetCollectionMetadata.isIgnoreAdornedProperties() != null) {
        metadata.setIgnoreAdornedProperties(adornedTargetCollectionMetadata.isIgnoreAdornedProperties());
    }
    if (adornedTargetCollectionMetadata.getAdornedTargetAddMethodType() != null) {
        metadata.setAdornedTargetAddMethodType(adornedTargetCollectionMetadata.getAdornedTargetAddMethodType());
    }
    if (adornedTargetCollectionMetadata.getCurrencyCodeField() != null) {
        metadata.setCurrencyCodeField(adornedTargetCollectionMetadata.getCurrencyCodeField());
    }
    attributes.put(field.getName(), metadata);
}
Also used : ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) ManyToOne(javax.persistence.ManyToOne) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) AdminPresentationOperationTypes(org.broadleafcommerce.common.presentation.AdminPresentationOperationTypes) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) AdornedTargetCollectionMetadata(org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)

Example 2 with PersistencePerspective

use of org.broadleafcommerce.openadmin.dto.PersistencePerspective in project BroadleafCommerce by BroadleafCommerce.

the class PersistencePackageFactoryImpl method create.

@Override
public PersistencePackage create(PersistencePackageRequest request) {
    PersistencePerspective persistencePerspective = new PersistencePerspective();
    persistencePerspective.setAdditionalForeignKeys(request.getAdditionalForeignKeys());
    persistencePerspective.setAdditionalNonPersistentProperties(new String[] {});
    if (request.getForeignKey() != null) {
        persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.FOREIGNKEY, request.getForeignKey());
    }
    switch(request.getType()) {
        case STANDARD:
            persistencePerspective.setOperationTypes(getDefaultOperationTypes());
            break;
        case ADORNED:
            if (request.getAdornedList() == null) {
                throw new IllegalArgumentException("ADORNED type requires the adornedList to be set");
            }
            persistencePerspective.setOperationTypes(getOperationTypes(OperationType.ADORNEDTARGETLIST));
            persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.ADORNEDTARGETLIST, request.getAdornedList());
            break;
        case MAP:
            if (request.getMapStructure() == null) {
                throw new IllegalArgumentException("MAP type requires the mapStructure to be set");
            }
            persistencePerspective.setOperationTypes(getOperationTypes(OperationType.MAP));
            persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.MAPSTRUCTURE, request.getMapStructure());
            break;
    }
    if (request.getOperationTypesOverride() != null) {
        persistencePerspective.setOperationTypes(request.getOperationTypesOverride());
    }
    PersistencePackage pp = new PersistencePackage();
    pp.setCeilingEntityFullyQualifiedClassname(request.getCeilingEntityClassname());
    if (!StringUtils.isEmpty(request.getSecurityCeilingEntityClassname())) {
        pp.setSecurityCeilingEntityFullyQualifiedClassname(request.getSecurityCeilingEntityClassname());
    }
    if (!ArrayUtils.isEmpty(request.getSectionCrumbs())) {
        SectionCrumb[] converted = new SectionCrumb[request.getSectionCrumbs().length];
        int index = 0;
        for (SectionCrumb crumb : request.getSectionCrumbs()) {
            SectionCrumb temp = new SectionCrumb();
            String originalSectionIdentifier = crumb.getSectionIdentifier();
            String sectionAsClassName;
            try {
                sectionAsClassName = getClassNameForSection(crumb.getSectionIdentifier());
            } catch (Exception e) {
                sectionAsClassName = request.getCeilingEntityClassname();
            }
            if (sectionAsClassName != null && !sectionAsClassName.equals(originalSectionIdentifier)) {
                temp.setOriginalSectionIdentifier(originalSectionIdentifier);
            }
            temp.setSectionIdentifier(sectionAsClassName);
            temp.setSectionId(crumb.getSectionId());
            converted[index] = temp;
            index++;
        }
        pp.setSectionCrumbs(converted);
    } else {
        pp.setSectionCrumbs(new SectionCrumb[0]);
    }
    pp.setSectionEntityField(request.getSectionEntityField());
    pp.setFetchTypeFullyQualifiedClassname(null);
    pp.setPersistencePerspective(persistencePerspective);
    pp.setCustomCriteria(request.getCustomCriteria());
    pp.setCsrfToken(null);
    pp.setRequestingEntityName(request.getRequestingEntityName());
    pp.setValidateUnsubmittedProperties(request.isValidateUnsubmittedProperties());
    pp.setIsTreeCollection(request.isTreeCollection());
    pp.setAddOperationInspect(request.isAddOperationInspect());
    if (request.getEntity() != null) {
        pp.setEntity(request.getEntity());
    }
    for (Map.Entry<String, PersistencePackageRequest> subRequest : request.getSubRequests().entrySet()) {
        pp.getSubPackages().put(subRequest.getKey(), create(subRequest.getValue()));
    }
    return pp;
}
Also used : PersistencePackage(org.broadleafcommerce.openadmin.dto.PersistencePackage) SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) Map(java.util.Map)

Example 3 with PersistencePerspective

use of org.broadleafcommerce.openadmin.dto.PersistencePerspective 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 4 with PersistencePerspective

use of org.broadleafcommerce.openadmin.dto.PersistencePerspective in project BroadleafCommerce by BroadleafCommerce.

the class AdornedTargetListPersistenceModule method updateMergedProperties.

@Override
public void updateMergedProperties(PersistencePackage persistencePackage, Map<MergedPropertyType, Map<String, FieldMetadata>> allMergedProperties) throws ServiceException {
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
        if (adornedTargetList != null) {
            Class<?>[] entities = persistenceManager.getPolymorphicEntities(adornedTargetList.getAdornedTargetEntityClassname());
            Map<String, FieldMetadata> joinMergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(adornedTargetList.getAdornedTargetEntityClassname(), entities, null, new String[] {}, new ForeignKey[] {}, MergedPropertyType.ADORNEDTARGETLIST, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
            String idProp = null;
            for (String key : joinMergedProperties.keySet()) {
                if (joinMergedProperties.get(key) instanceof BasicFieldMetadata && ((BasicFieldMetadata) joinMergedProperties.get(key)).getFieldType() == SupportedFieldType.ID) {
                    idProp = key;
                    break;
                }
            }
            if (idProp != null) {
                joinMergedProperties.remove(idProp);
            }
            allMergedProperties.put(MergedPropertyType.ADORNEDTARGETLIST, joinMergedProperties);
        }
    } catch (Exception e) {
        throw new ServiceException("Unable to fetch results for " + ceilingEntityFullyQualifiedClassname, e);
    }
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException)

Example 5 with PersistencePerspective

use of org.broadleafcommerce.openadmin.dto.PersistencePerspective 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)

Aggregations

PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)51 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)44 ServiceException (org.broadleafcommerce.common.exception.ServiceException)39 Entity (org.broadleafcommerce.openadmin.dto.Entity)34 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)27 Serializable (java.io.Serializable)16 InvocationTargetException (java.lang.reflect.InvocationTargetException)16 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)16 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)16 ForeignKey (org.broadleafcommerce.openadmin.dto.ForeignKey)14 Map (java.util.Map)10 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)9 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)9 FilterMapping (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping)7 HashMap (java.util.HashMap)6 AdornedTargetList (org.broadleafcommerce.openadmin.dto.AdornedTargetList)6 MapStructure (org.broadleafcommerce.openadmin.dto.MapStructure)6 SimpleValueMapStructure (org.broadleafcommerce.openadmin.dto.SimpleValueMapStructure)6 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)6 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)6