Search in sources :

Example 1 with RecordHelper

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

the class PersistenceManagerImpl method update.

@Override
public PersistenceResponse update(PersistencePackage persistencePackage) throws ServiceException {
    for (PersistenceManagerEventHandler handler : persistenceManagerEventHandlers) {
        PersistenceManagerEventHandlerResponse response = handler.preUpdate(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: {
            for (CustomPersistenceHandler handler : getCustomPersistenceHandlers()) {
                if (handler.canHandleUpdate(persistencePackage)) {
                    if (!handler.willHandleSecurity(persistencePackage)) {
                        adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.UPDATE);
                    }
                    response = handler.update(persistencePackage, dynamicEntityDao, (RecordHelper) getCompatibleModule(OperationType.BASIC));
                    break checkRoot;
                }
            }
            adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.UPDATE);
            PersistenceModule myModule = getCompatibleModule(persistencePackage.getPersistencePerspective().getOperationTypes().getUpdateType());
            response = myModule.update(persistencePackage);
        }
    } catch (ValidationException e) {
        response = e.getEntity();
    } catch (ServiceException e) {
        if (e.getCause() instanceof ValidationException) {
            response = ((ValidationException) e.getCause()).getEntity();
        } else {
            throw e;
        }
    }
    Map<String, List<String>> subPackageValidationErrors = new HashMap<>();
    for (Map.Entry<String, PersistencePackage> subPackage : persistencePackage.getSubPackages().entrySet()) {
        try {
            // Run through any subPackages -- add up any validation errors
            checkHandler: {
                for (CustomPersistenceHandler handler : getCustomPersistenceHandlers()) {
                    if (handler.canHandleUpdate(subPackage.getValue())) {
                        Entity subResponse = handler.update(subPackage.getValue(), dynamicEntityDao, (RecordHelper) getCompatibleModule(OperationType.BASIC));
                        subPackage.getValue().setEntity(subResponse);
                        break checkHandler;
                    }
                }
                PersistenceModule subModule = getCompatibleModule(subPackage.getValue().getPersistencePerspective().getOperationTypes().getUpdateType());
                Entity subResponse = subModule.update(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 executePostUpdateHandlers(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) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with RecordHelper

use of org.broadleafcommerce.openadmin.server.service.persistence.module.RecordHelper 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)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)2 ServiceException (org.broadleafcommerce.common.exception.ServiceException)2 Entity (org.broadleafcommerce.openadmin.dto.Entity)2 PersistencePackage (org.broadleafcommerce.openadmin.dto.PersistencePackage)2 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)2 CustomPersistenceHandler (org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandler)2 PersistenceModule (org.broadleafcommerce.openadmin.server.service.persistence.module.PersistenceModule)2 RecordHelper (org.broadleafcommerce.openadmin.server.service.persistence.module.RecordHelper)2 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)1