Search in sources :

Example 41 with ServiceException

use of org.broadleafcommerce.common.exception.ServiceException 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 42 with ServiceException

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

the class CustomPersistenceHandlerAdapter method getResultSet.

/**
 * This is a helper method that can be invoked as the last step in a custom inspect phase. It will assemble the
 * appropriate DynamicResultSet from the given parameters.
 */
protected DynamicResultSet getResultSet(PersistencePackage persistencePackage, InspectHelper helper, Map<String, FieldMetadata> metadata) throws ServiceException {
    String entityName = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        if (helper instanceof PersistenceManager) {
            Class<?>[] entities = ((PersistenceManager) helper).getPolymorphicEntities(entityName);
            Map<MergedPropertyType, Map<String, FieldMetadata>> allMergedProperties = new HashMap<MergedPropertyType, Map<String, FieldMetadata>>();
            allMergedProperties.put(MergedPropertyType.PRIMARY, metadata);
            ClassMetadata mergedMetadata = helper.buildClassMetadata(entities, persistencePackage, allMergedProperties);
            DynamicResultSet results = new DynamicResultSet(mergedMetadata);
            return results;
        }
    } catch (ClassNotFoundException e) {
        throw new ServiceException(e);
    }
    return new DynamicResultSet();
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistenceManager(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager) HashMap(java.util.HashMap) MergedPropertyType(org.broadleafcommerce.openadmin.dto.MergedPropertyType) ServiceException(org.broadleafcommerce.common.exception.ServiceException) HashMap(java.util.HashMap) Map(java.util.Map) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 43 with ServiceException

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

the class TranslationCustomPersistenceHandler 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();
        Translation adminInstance = (Translation) Class.forName(entity.getType()[0]).newInstance();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Translation.class.getName(), persistencePerspective);
        adminInstance = (Translation) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        Translation res = translationService.getTranslation(adminInstance.getEntityType(), adminInstance.getEntityId(), adminInstance.getFieldName(), adminInstance.getLocaleCode());
        if (res == null) {
            adminInstance = dynamicEntityDao.merge(adminInstance);
            Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
            return adminEntity;
        } else {
            Entity errorEntity = new Entity();
            errorEntity.addValidationError("localeCode", "translation.record.exists.for.locale");
            return errorEntity;
        }
    } catch (Exception e) {
        throw new ServiceException("Unable to perform add for entity: " + Translation.class.getName(), e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Translation(org.broadleafcommerce.common.i18n.domain.Translation) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 44 with ServiceException

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

the class FormProcessor method getInjectedModelAndTagAttributes.

@Override
public BroadleafTemplateModelModifierDTO getInjectedModelAndTagAttributes(String rootTagName, Map<String, String> rootTagAttributes, BroadleafTemplateContext context) {
    Map<String, String> formAttributes = new HashMap<>();
    formAttributes.putAll(rootTagAttributes);
    BroadleafTemplateModel model = context.createModel();
    BroadleafTemplateModelModifierDTO dto = new BroadleafTemplateModelModifierDTO();
    // We do this instead of checking for a POST because post is default if nothing is specified
    if (!"GET".equalsIgnoreCase(formAttributes.get("method"))) {
        try {
            String csrfToken = eps.getCSRFToken();
            String stateVersionToken = null;
            if (spps.isEnabled()) {
                stateVersionToken = spps.getStateVersionToken();
            }
            // detect multipart form
            if ("multipart/form-data".equalsIgnoreCase(formAttributes.get("enctype"))) {
                String csrfQueryParameter = "?" + eps.getCsrfTokenParameter() + "=" + csrfToken;
                if (stateVersionToken != null) {
                    csrfQueryParameter += "&" + spps.getStateVersionTokenParameter() + "=" + stateVersionToken;
                }
                // Add this into the attribute map to be used for the new <form> tag. The expression has already
                // been executed, don't need to treat the value as an expression
                String actionValue = formAttributes.get("action");
                actionValue += csrfQueryParameter;
                formAttributes.put("action", actionValue);
            } else {
                Map<String, String> csrfAttributes = new HashMap<>();
                csrfAttributes.put("type", "hidden");
                csrfAttributes.put("name", eps.getCsrfTokenParameter());
                csrfAttributes.put("value", csrfToken);
                BroadleafTemplateElement csrfTag = context.createStandaloneElement("input", csrfAttributes, true);
                model.addElement(csrfTag);
                if (stateVersionToken != null) {
                    Map<String, String> stateVersionAttributes = new HashMap<>();
                    stateVersionAttributes.put("type", "hidden");
                    stateVersionAttributes.put("name", spps.getStateVersionTokenParameter());
                    stateVersionAttributes.put("value", stateVersionToken);
                    BroadleafTemplateElement stateVersionTag = context.createStandaloneElement("input", stateVersionAttributes, true);
                    model.addElement(stateVersionTag);
                }
                dto.setModel(model);
            }
        } catch (ServiceException e) {
            throw new RuntimeException("Could not get a CSRF token for this session", e);
        }
    }
    dto.setFormParameters(formAttributes);
    dto.setReplacementTagName("form");
    return dto;
}
Also used : BroadleafTemplateElement(org.broadleafcommerce.presentation.model.BroadleafTemplateElement) ServiceException(org.broadleafcommerce.common.exception.ServiceException) HashMap(java.util.HashMap) BroadleafTemplateModel(org.broadleafcommerce.presentation.model.BroadleafTemplateModel) BroadleafTemplateModelModifierDTO(org.broadleafcommerce.presentation.model.BroadleafTemplateModelModifierDTO)

Example 45 with ServiceException

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

the class ProductCustomPersistenceHandler method update.

@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Product.class.getName(), persistencePerspective);
        BasicFieldMetadata defaultCategory = ((BasicFieldMetadata) adminProperties.get("defaultCategory"));
        defaultCategory.setFriendlyName("ProductImpl_Parent_Category");
        if (entity.findProperty("defaultCategory") != null && !StringUtils.isEmpty(entity.findProperty("defaultCategory").getValue())) {
            // Change the inherited type so that this property is disconnected from the entity and validation is temporarily skipped.
            // This is useful when the defaultCategory was previously completely empty for whatever reason. Without this, such
            // a case would fail the validation, even though the property was specified in the submission.
            defaultCategory.setInheritedFromType(String.class.getName());
        }
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        Product adminInstance = (Product) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
        if (adminInstance instanceof ProductBundle) {
            removeBundleFieldRestrictions((ProductBundle) adminInstance, adminProperties, entity);
        }
        CategoryProductXref oldDefault = getCurrentDefaultXref(adminInstance);
        // so override required flag for that field during deployment
        if (BroadleafRequestContext.getBroadleafRequestContext().isProductionSandBox()) {
            ((BasicFieldMetadata) adminProperties.get("defaultCategory")).setRequiredOverride(false);
        }
        adminInstance = (Product) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        adminInstance = dynamicEntityDao.merge(adminInstance);
        boolean handled = false;
        if (extensionManager != null) {
            ExtensionResultStatusType result = extensionManager.getProxy().manageParentCategoryForUpdate(persistencePackage, adminInstance);
            handled = ExtensionResultStatusType.NOT_HANDLED != result;
            extensionManager.getProxy().manageFields(persistencePackage, adminInstance);
        }
        if (!handled) {
            setupXref(adminInstance);
            removeOldDefault(adminInstance, oldDefault, entity);
        }
        return helper.getRecord(adminProperties, adminInstance, null, null);
    } catch (Exception e) {
        throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) CategoryProductXref(org.broadleafcommerce.core.catalog.domain.CategoryProductXref) ProductBundle(org.broadleafcommerce.core.catalog.domain.ProductBundle) Product(org.broadleafcommerce.core.catalog.domain.Product) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ServiceException(org.broadleafcommerce.common.exception.ServiceException) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)

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