Search in sources :

Example 1 with CustomPersistenceHandler

use of org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandler 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 CustomPersistenceHandler

use of org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandler in project BroadleafCommerce by BroadleafCommerce.

the class PersistenceManagerImpl method remove.

@Override
public PersistenceResponse remove(PersistencePackage persistencePackage) throws ServiceException {
    for (PersistenceManagerEventHandler handler : persistenceManagerEventHandlers) {
        PersistenceManagerEventHandlerResponse response = handler.preRemove(this, persistencePackage);
        if (PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.HANDLED_BREAK == response.getStatus()) {
            break;
        }
    }
    // check to see if there is a custom handler registered
    for (CustomPersistenceHandler handler : getCustomPersistenceHandlers()) {
        if (handler.canHandleRemove(persistencePackage)) {
            if (!handler.willHandleSecurity(persistencePackage)) {
                adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.REMOVE);
            }
            handler.remove(persistencePackage, dynamicEntityDao, (RecordHelper) getCompatibleModule(OperationType.BASIC));
            return executePostRemoveHandlers(persistencePackage, new PersistenceResponse());
        }
    }
    adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.REMOVE);
    PersistenceModule myModule = getCompatibleModule(persistencePackage.getPersistencePerspective().getOperationTypes().getRemoveType());
    myModule.remove(persistencePackage);
    return executePostRemoveHandlers(persistencePackage, new PersistenceResponse());
}
Also used : CustomPersistenceHandler(org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandler) PersistenceModule(org.broadleafcommerce.openadmin.server.service.persistence.module.PersistenceModule)

Example 3 with CustomPersistenceHandler

use of org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandler 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 4 with CustomPersistenceHandler

use of org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandler in project BroadleafCommerce by BroadleafCommerce.

the class PersistenceManagerImpl method fetch.

@Override
public PersistenceResponse fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto) throws ServiceException {
    for (PersistenceManagerEventHandler handler : persistenceManagerEventHandlers) {
        PersistenceManagerEventHandlerResponse response = handler.preFetch(this, persistencePackage, cto);
        if (PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.HANDLED_BREAK == response.getStatus()) {
            break;
        }
    }
    // check to see if there is a custom handler registered
    for (CustomPersistenceHandler handler : getCustomPersistenceHandlers()) {
        if (handler.canHandleFetch(persistencePackage)) {
            if (!handler.willHandleSecurity(persistencePackage)) {
                adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.FETCH);
            }
            DynamicResultSet results = handler.fetch(persistencePackage, cto, dynamicEntityDao, (RecordHelper) getCompatibleModule(OperationType.BASIC));
            return executePostFetchHandlers(persistencePackage, cto, new PersistenceResponse().withDynamicResultSet(results));
        }
    }
    adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.FETCH);
    PersistenceModule myModule = getCompatibleModule(persistencePackage.getPersistencePerspective().getOperationTypes().getFetchType());
    try {
        DynamicResultSet results = myModule.fetch(persistencePackage, cto);
        return executePostFetchHandlers(persistencePackage, cto, new PersistenceResponse().withDynamicResultSet(results));
    } catch (ServiceException e) {
        if (e.getCause() instanceof NoPossibleResultsException) {
            DynamicResultSet drs = new DynamicResultSet(null, new Entity[] {}, 0);
            return executePostFetchHandlers(persistencePackage, cto, new PersistenceResponse().withDynamicResultSet(drs));
        }
        throw e;
    }
}
Also used : NoPossibleResultsException(org.broadleafcommerce.common.exception.NoPossibleResultsException) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) ServiceException(org.broadleafcommerce.common.exception.ServiceException) CustomPersistenceHandler(org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandler) PersistenceModule(org.broadleafcommerce.openadmin.server.service.persistence.module.PersistenceModule) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 5 with CustomPersistenceHandler

use of org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandler in project BroadleafCommerce by BroadleafCommerce.

the class PersistenceManagerImpl method honorExplicitPersistenceHandlerSorting.

/**
 * Honor ordering for those CPH instances that have explicitly declared a order other than the default order
 * This achieves several goals:
 * 1. We leave the general CPH population in its established order, which is the best for backwards compatibility
 * 2. Those items that are intended to get explicit ordering still achieve their goal
 */
protected void honorExplicitPersistenceHandlerSorting() {
    List<CustomPersistenceHandler> exceptions = new ArrayList<>(customPersistenceHandlers.size());
    List<CustomPersistenceHandler> sorted = new ArrayList<>(customPersistenceHandlers.size());
    for (CustomPersistenceHandler handler : customPersistenceHandlers) {
        if (CustomPersistenceHandler.DEFAULT_ORDER == handler.getOrder()) {
            sorted.add(handler);
        } else {
            exceptions.add(handler);
        }
    }
    if (!CollectionUtils.isEmpty(exceptions)) {
        Integer position = 0;
        Map<Integer, List<CustomPersistenceHandler>> positions = new HashMap<>();
        for (CustomPersistenceHandler handler : sorted) {
            if (CollectionUtils.isEmpty(exceptions)) {
                break;
            }
            Iterator<CustomPersistenceHandler> itr = exceptions.iterator();
            while (itr.hasNext()) {
                CustomPersistenceHandler exception = itr.next();
                if (exception.getOrder() <= handler.getOrder()) {
                    List<CustomPersistenceHandler> items = positions.get(position);
                    if (items == null) {
                        items = new ArrayList<>();
                        positions.put(position, items);
                    }
                    items.add(exception);
                    itr.remove();
                }
            }
        }
        for (Map.Entry<Integer, List<CustomPersistenceHandler>> entry : positions.entrySet()) {
            for (CustomPersistenceHandler handler : entry.getValue()) {
                sorted.add(entry.getKey(), handler);
            }
        }
        customPersistenceHandlers = sorted;
    }
}
Also used : HashMap(java.util.HashMap) CustomPersistenceHandler(org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandler) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

CustomPersistenceHandler (org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandler)7 PersistenceModule (org.broadleafcommerce.openadmin.server.service.persistence.module.PersistenceModule)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 List (java.util.List)3 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)3 ServiceException (org.broadleafcommerce.common.exception.ServiceException)3 Entity (org.broadleafcommerce.openadmin.dto.Entity)3 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)2 PersistencePackage (org.broadleafcommerce.openadmin.dto.PersistencePackage)2 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)2 RecordHelper (org.broadleafcommerce.openadmin.server.service.persistence.module.RecordHelper)2 NoPossibleResultsException (org.broadleafcommerce.common.exception.NoPossibleResultsException)1 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)1 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)1 MergedPropertyType (org.broadleafcommerce.openadmin.dto.MergedPropertyType)1 CustomPersistenceHandlerFilter (org.broadleafcommerce.openadmin.server.service.handler.CustomPersistenceHandlerFilter)1