Search in sources :

Example 1 with EntityResult

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

the class BasicPersistenceModule method add.

@Override
public EntityResult add(PersistencePackage persistencePackage, boolean includeRealEntityObject) throws ServiceException {
    EntityResult entityResult = new EntityResult();
    Entity entity = persistencePackage.getEntity();
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
    if (foreignKey != null && !foreignKey.getMutable()) {
        throw new SecurityServiceException("Entity not mutable");
    }
    try {
        Class<?>[] entities = persistenceManager.getPolymorphicEntities(persistencePackage.getCeilingEntityFullyQualifiedClassname());
        Map<String, FieldMetadata> mergedUnfilteredProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(persistencePackage.getCeilingEntityFullyQualifiedClassname(), entities, foreignKey, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
        Map<String, FieldMetadata> mergedProperties = filterOutCollectionMetadata(mergedUnfilteredProperties);
        String idProperty = null;
        for (String property : mergedProperties.keySet()) {
            if (((BasicFieldMetadata) mergedProperties.get(property)).getFieldType() == SupportedFieldType.ID) {
                idProperty = property;
                break;
            }
        }
        if (idProperty == null) {
            throw new RuntimeException("Could not find a primary key property in the passed entity with type: " + entity.getType()[0]);
        }
        Object primaryKey = null;
        try {
            primaryKey = getPrimaryKey(entity, mergedProperties);
        } catch (Exception e) {
        // don't do anything - this is a valid case
        }
        if (primaryKey == null) {
            Serializable instance = (Serializable) Class.forName(entity.getType()[0]).newInstance();
            instance = createPopulatedInstance(instance, entity, mergedProperties, false);
            if (foreignKey != null && foreignKey.getSortField() != null) {
                ExtensionResultHolder<Serializable> result = new ExtensionResultHolder<Serializable>();
                extensionManager.getProxy().rebalanceForAdd(this, persistencePackage, instance, mergedProperties, result);
                instance = result.getResult();
            }
            instance = persistenceManager.getDynamicEntityDao().merge(instance);
            if (includeRealEntityObject) {
                entityResult.setEntityBackingObject(instance);
            }
            List<Serializable> entityList = new ArrayList<Serializable>(1);
            entityList.add(instance);
            entity = getRecords(mergedProperties, entityList, null, null, null)[0];
            entityResult.setEntity(entity);
            return entityResult;
        } else {
            return update(persistencePackage, primaryKey, includeRealEntityObject);
        }
    } catch (Exception e) {
        throw new ServiceException("Problem adding new entity : " + e.getMessage(), e);
    }
}
Also used : AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) 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) ArrayList(java.util.ArrayList) EntityResult(org.broadleafcommerce.openadmin.dto.EntityResult) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) CriteriaConversionException(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException) BeansException(org.springframework.beans.BeansException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 2 with EntityResult

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

the class BasicPersistenceModule method update.

protected EntityResult update(PersistencePackage persistencePackage, Object primaryKey, boolean includeRealEntity) throws ServiceException {
    EntityResult entityResult = new EntityResult();
    Entity entity = persistencePackage.getEntity();
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
    if (foreignKey != null && !foreignKey.getMutable()) {
        throw new SecurityServiceException("Entity not mutable");
    }
    try {
        Class<?>[] entities = persistenceManager.getPolymorphicEntities(persistencePackage.getCeilingEntityFullyQualifiedClassname());
        Map<String, FieldMetadata> mergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(persistencePackage.getCeilingEntityFullyQualifiedClassname(), entities, foreignKey, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
        if (primaryKey == null) {
            primaryKey = getPrimaryKey(entity, mergedProperties);
        }
        Serializable instance = persistenceManager.getDynamicEntityDao().retrieve(Class.forName(entity.getType()[0]), primaryKey);
        Assert.isTrue(instance != null, "Entity not found");
        if (!entity.isValidationFailure()) {
            // Re-Balance the list if it is a Foreign Key toMany collection with a sort field property
            if (foreignKey != null && foreignKey.getSortField() != null && entity.findProperty(foreignKey.getSortField()) != null && entity.findProperty(foreignKey.getSortField()).getValue() != null) {
                ExtensionResultHolder<Serializable> result = new ExtensionResultHolder<Serializable>();
                extensionManager.getProxy().rebalanceForUpdate(this, persistencePackage, instance, mergedProperties, primaryKey, result);
                instance = result.getResult();
            } else {
                instance = createPopulatedInstance(instance, entity, mergedProperties, false, persistencePackage.isValidateUnsubmittedProperties());
            }
            instance = persistenceManager.getDynamicEntityDao().merge(instance);
            if (includeRealEntity) {
                entityResult.setEntityBackingObject(instance);
            }
            List<Serializable> entityList = new ArrayList<Serializable>(1);
            entityList.add(instance);
            entity = getRecords(mergedProperties, entityList, null, null, null)[0];
            entityResult.setEntity(entity);
            return entityResult;
        } else {
            entityResult.setEntity(entity);
            return entityResult;
        }
    } catch (Exception e) {
        throw new ServiceException("Problem updating entity : " + e.getMessage(), e);
    }
}
Also used : AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) 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) ArrayList(java.util.ArrayList) EntityResult(org.broadleafcommerce.openadmin.dto.EntityResult) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) CriteriaConversionException(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException) BeansException(org.springframework.beans.BeansException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Aggregations

Serializable (java.io.Serializable)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)2 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)2 ServiceException (org.broadleafcommerce.common.exception.ServiceException)2 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)2 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)2 Entity (org.broadleafcommerce.openadmin.dto.Entity)2 EntityResult (org.broadleafcommerce.openadmin.dto.EntityResult)2 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)2 ForeignKey (org.broadleafcommerce.openadmin.dto.ForeignKey)2 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)2 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)2 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)2 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)2 CriteriaConversionException (org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException)2 BeansException (org.springframework.beans.BeansException)2 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)1