Search in sources :

Example 21 with ServiceException

use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.

the class PersistenceManagerImpl method add.

@Override
public PersistenceResponse add(PersistencePackage persistencePackage) throws ServiceException {
    for (PersistenceManagerEventHandler handler : persistenceManagerEventHandlers) {
        PersistenceManagerEventHandlerResponse response = handler.preAdd(this, persistencePackage);
        if (PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.HANDLED_BREAK == response.getStatus()) {
            break;
        }
    }
    // check to see if there is a custom handler registered
    // execute the root PersistencePackage
    Entity response;
    try {
        checkRoot: {
            // entity to execute the subPackage code later
            for (CustomPersistenceHandler handler : getCustomPersistenceHandlers()) {
                if (handler.canHandleAdd(persistencePackage)) {
                    if (!handler.willHandleSecurity(persistencePackage)) {
                        adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.ADD);
                    }
                    response = handler.add(persistencePackage, dynamicEntityDao, (RecordHelper) getCompatibleModule(OperationType.BASIC));
                    break checkRoot;
                }
            }
            adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.ADD);
            PersistenceModule myModule = getCompatibleModule(persistencePackage.getPersistencePerspective().getOperationTypes().getAddType());
            response = myModule.add(persistencePackage);
        }
    } catch (ValidationException e) {
        response = e.getEntity();
    } catch (ServiceException e) {
        if (e.getCause() instanceof ValidationException) {
            response = ((ValidationException) e.getCause()).getEntity();
        } else {
            throw e;
        }
    }
    if (!MapUtils.isEmpty(persistencePackage.getSubPackages())) {
        // Once the entity has been saved, we can utilize its id for the subsequent dynamic forms
        Class<?> entityClass;
        try {
            entityClass = Class.forName(response.getType()[0]);
        } catch (ClassNotFoundException e) {
            throw new ServiceException(e);
        }
        Map<String, Object> idMetadata = getDynamicEntityDao().getIdMetadata(entityClass);
        String idProperty = (String) idMetadata.get("name");
        String idVal = response.findProperty(idProperty).getValue();
        Map<String, List<String>> subPackageValidationErrors = new HashMap<>();
        for (Map.Entry<String, PersistencePackage> subPackage : persistencePackage.getSubPackages().entrySet()) {
            Entity subResponse;
            try {
                subPackage.getValue().getCustomCriteria()[1] = idVal;
                // Run through any subPackages -- add up any validation errors
                checkHandler: {
                    for (CustomPersistenceHandler handler : getCustomPersistenceHandlers()) {
                        if (handler.canHandleAdd(subPackage.getValue())) {
                            subResponse = handler.add(subPackage.getValue(), dynamicEntityDao, (RecordHelper) getCompatibleModule(OperationType.BASIC));
                            subPackage.getValue().setEntity(subResponse);
                            break checkHandler;
                        }
                    }
                    PersistenceModule subModule = getCompatibleModule(subPackage.getValue().getPersistencePerspective().getOperationTypes().getAddType());
                    subResponse = subModule.add(persistencePackage);
                    subPackage.getValue().setEntity(subResponse);
                }
            } catch (ValidationException e) {
                subPackage.getValue().setEntity(e.getEntity());
            } catch (ServiceException e) {
                if (e.getCause() instanceof ValidationException) {
                    response = ((ValidationException) e.getCause()).getEntity();
                } else {
                    throw e;
                }
            }
        }
        // Build up validation errors in all of the subpackages, even those that might not have thrown ValidationExceptions
        for (Map.Entry<String, PersistencePackage> subPackage : persistencePackage.getSubPackages().entrySet()) {
            for (Map.Entry<String, List<String>> error : subPackage.getValue().getEntity().getPropertyValidationErrors().entrySet()) {
                subPackageValidationErrors.put(subPackage.getKey() + DynamicEntityFormInfo.FIELD_SEPARATOR + error.getKey(), error.getValue());
            }
        }
        response.getPropertyValidationErrors().putAll(subPackageValidationErrors);
    }
    if (response.isValidationFailure()) {
        PersistenceResponse validationResponse = executeValidationProcessors(persistencePackage, new PersistenceResponse().withEntity(response));
        Entity entity = validationResponse.getEntity();
        String message = ValidationUtil.buildErrorMessage(entity.getPropertyValidationErrors(), entity.getGlobalValidationErrors());
        throw new ValidationException(entity, message);
    }
    return executePostAddHandlers(persistencePackage, new PersistenceResponse().withEntity(response));
}
Also used : PersistencePackage(org.broadleafcommerce.openadmin.dto.PersistencePackage) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) HashMap(java.util.HashMap) CustomPersistenceHandler(org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandler) PersistenceModule(org.broadleafcommerce.openadmin.server.service.persistence.module.PersistenceModule) ServiceException(org.broadleafcommerce.common.exception.ServiceException) RecordHelper(org.broadleafcommerce.openadmin.server.service.persistence.module.RecordHelper) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 22 with ServiceException

use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.

the class SystemPropertyCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        // Get an instance of SystemProperty with the updated values from the form
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        SystemProperty adminInstance = (SystemProperty) Class.forName(entity.getType()[0]).newInstance();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(SystemProperty.class.getName(), persistencePerspective);
        adminInstance = (SystemProperty) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        // Verify that the value entered matches up with the type of this property
        Entity errorEntity = validateTypeAndValueCombo(adminInstance);
        if (errorEntity != null) {
            entity.setPropertyValidationErrors(errorEntity.getPropertyValidationErrors());
            return entity;
        }
        adminInstance = (SystemProperty) dynamicEntityDao.merge(adminInstance);
        // Fill out the DTO and add in the product option value properties to it
        return helper.getRecord(adminProperties, adminInstance, null, null);
    } catch (Exception e) {
        throw new ServiceException("Unable to perform fetch for entity: " + SystemProperty.class.getName(), e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SystemProperty(org.broadleafcommerce.common.config.domain.SystemProperty) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 23 with ServiceException

use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.

the class SystemPropertyCustomPersistenceHandler method update.

@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        // Get an instance of SystemProperty with the updated values from the form
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(SystemProperty.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        SystemProperty adminInstance = (SystemProperty) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
        adminInstance = (SystemProperty) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        // Verify that the value entered matches up with the type of this property
        Entity errorEntity = validateTypeAndValueCombo(adminInstance);
        if (errorEntity != null) {
            entity.setPropertyValidationErrors(errorEntity.getPropertyValidationErrors());
            return entity;
        }
        adminInstance = (SystemProperty) dynamicEntityDao.merge(adminInstance);
        // Fill out the DTO and add in the product option value properties to it
        return helper.getRecord(adminProperties, adminInstance, null, null);
    } catch (Exception e) {
        throw new ServiceException("Unable to perform fetch for entity: " + SystemProperty.class.getName(), e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SystemProperty(org.broadleafcommerce.common.config.domain.SystemProperty) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 24 with ServiceException

use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.

the class CategoryCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        entity = validateParentCategory(entity, true);
        if (entity.isValidationFailure()) {
            return entity;
        } else {
            PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
            Category adminInstance = (Category) Class.forName(entity.getType()[0]).newInstance();
            Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Category.class.getName(), persistencePerspective);
            adminInstance = (Category) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
            adminInstance = dynamicEntityDao.merge(adminInstance);
            boolean handled = false;
            if (extensionManager != null) {
                ExtensionResultStatusType result = extensionManager.getProxy().manageParentCategoryForAdd(persistencePackage, adminInstance);
                handled = ExtensionResultStatusType.NOT_HANDLED != result;
            }
            if (!handled) {
                setupXref(adminInstance);
            }
            adminInstance = dynamicEntityDao.merge(adminInstance);
            return helper.getRecord(adminProperties, adminInstance, null, null);
        }
    } catch (Exception e) {
        throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Category(org.broadleafcommerce.core.catalog.domain.Category) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 25 with ServiceException

use of org.broadleafcommerce.common.exception.ServiceException in project BroadleafCommerce by BroadleafCommerce.

the class CustomerCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Customer adminInstance = (Customer) Class.forName(entity.getType()[0]).newInstance();
        adminInstance.setId(customerService.findNextCustomerId());
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Customer.class.getName(), persistencePerspective);
        adminInstance = (Customer) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        if (useEmailForLogin) {
            adminInstance.setUsername(adminInstance.getEmailAddress());
        }
        if (!entity.isPreAdd()) {
            Entity errorEntity = validateUniqueUsername(entity, adminInstance);
            if (errorEntity != null) {
                return errorEntity;
            }
        }
        adminInstance = dynamicEntityDao.merge(adminInstance);
        customerService.createRegisteredCustomerRoles(adminInstance);
        Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
        return adminEntity;
    } catch (Exception e) {
        LOG.error("Unable to execute persistence activity", e);
        throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Customer(org.broadleafcommerce.profile.core.domain.Customer) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Aggregations

ServiceException (org.broadleafcommerce.common.exception.ServiceException)77 Entity (org.broadleafcommerce.openadmin.dto.Entity)46 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)44 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)39 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)25 InvocationTargetException (java.lang.reflect.InvocationTargetException)19 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)17 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)16 Serializable (java.io.Serializable)15 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)14 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)13 Property (org.broadleafcommerce.openadmin.dto.Property)12 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)11 Map (java.util.Map)11 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)9 ForeignKey (org.broadleafcommerce.openadmin.dto.ForeignKey)9 StreamCapableTransactionalOperationAdapter (org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter)6 Sku (org.broadleafcommerce.core.catalog.domain.Sku)6 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)6