Search in sources :

Example 46 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class PageServiceUtility method getFieldDefinition.

protected FieldDefinition getFieldDefinition(Page page, String fieldKey) {
    ExtensionResultHolder<FieldDefinition> erh = new ExtensionResultHolder<FieldDefinition>();
    ExtensionResultStatusType result = extensionManager.getProxy().getFieldDefinition(erh, page, fieldKey);
    if (result == ExtensionResultStatusType.HANDLED) {
        return erh.getResult();
    }
    if (page.getPageTemplate() != null) {
        for (PageTemplateFieldGroupXref fgXrefs : page.getPageTemplate().getFieldGroupXrefs()) {
            for (FieldDefinition fd : fgXrefs.getFieldGroup().getFieldDefinitions()) {
                if (fd.getName().equals(fieldKey)) {
                    return fd;
                }
            }
        }
    }
    return null;
}
Also used : PageTemplateFieldGroupXref(org.broadleafcommerce.cms.page.domain.PageTemplateFieldGroupXref) FieldDefinition(org.broadleafcommerce.cms.field.domain.FieldDefinition) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 47 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class StaticAssetStorageServiceImpl method getFileFromLocalRepository.

protected File getFileFromLocalRepository(String cachedFileName) {
    // Look for a shared file (this represents a file that was based on a file originally in the classpath.
    File cacheFile = null;
    if (extensionManager != null) {
        ExtensionResultHolder holder = new ExtensionResultHolder();
        ExtensionResultStatusType result = extensionManager.getProxy().fileExists(cachedFileName, holder);
        if (ExtensionResultStatusType.HANDLED.equals(result)) {
            cacheFile = (File) holder.getResult();
        }
    }
    if (cacheFile == null) {
        cacheFile = broadleafFileService.getSharedLocalResource(cachedFileName);
    }
    if (cacheFile.exists()) {
        return cacheFile;
    } else {
        return broadleafFileService.getLocalResource(cachedFileName);
    }
}
Also used : ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 48 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class MediaFieldPersistenceProvider method extractValue.

@Override
public MetadataProviderResponse extractValue(ExtractValueRequest extractValueRequest, Property property) throws PersistenceException {
    if (!canHandleExtraction(extractValueRequest, property)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    if (extractValueRequest.getRequestedValue() != null) {
        Object requestedValue = extractValueRequest.getRequestedValue();
        if (!StringUtils.isEmpty(extractValueRequest.getMetadata().getToOneTargetProperty())) {
            try {
                requestedValue = extractValueRequest.getFieldManager().getFieldValue(requestedValue, extractValueRequest.getMetadata().getToOneTargetProperty());
            } catch (IllegalAccessException e) {
                throw ExceptionHelper.refineException(e);
            } catch (FieldNotAvailableException e) {
                throw ExceptionHelper.refineException(e);
            }
        }
        if (requestedValue instanceof Media) {
            Media media = (Media) requestedValue;
            String jsonString = convertMediaToJson(media);
            if (extensionManager != null) {
                ExtensionResultHolder<Long> resultHolder = new ExtensionResultHolder<Long>();
                ExtensionResultStatusType result = extensionManager.getProxy().transformId(media, resultHolder);
                if (ExtensionResultStatusType.NOT_HANDLED != result && resultHolder.getResult() != null) {
                    Class<?> type;
                    if (media.isUnwrappableAs(Media.class)) {
                        type = media.unwrap(Media.class).getClass();
                    } else {
                        type = media.getClass();
                    }
                    Media converted = mediaBuilderService.convertJsonToMedia(jsonString, type);
                    converted.setId(resultHolder.getResult());
                    jsonString = convertMediaToJson(converted);
                }
            }
            property.setValue(jsonString);
            property.setUnHtmlEncodedValue(jsonString);
            property.setDisplayValue(extractValueRequest.getDisplayVal());
            return MetadataProviderResponse.HANDLED_BREAK;
        } else {
            throw new UnsupportedOperationException("MEDIA type is currently only supported on fields of type Media");
        }
    }
    return MetadataProviderResponse.HANDLED;
}
Also used : FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) Media(org.broadleafcommerce.common.media.domain.Media) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 49 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class RuleFieldPersistenceProvider method updateQuantityRule.

protected boolean updateQuantityRule(EntityManager em, DataDTOToMVELTranslator translator, String entityKey, String fieldService, String jsonPropertyValue, Collection<QuantityBasedRule> criteriaList, Class<?> memberType, Object parent, String mappedBy, Property property) {
    boolean dirty = false;
    if (!StringUtils.isEmpty(jsonPropertyValue)) {
        // avoid lazy init exception on the criteria list for criteria created during an add
        criteriaList.size();
        DataWrapper dw = ruleFieldExtractionUtility.convertJsonToDataWrapper(jsonPropertyValue);
        if (dw != null && StringUtils.isEmpty(dw.getError())) {
            List<QuantityBasedRule> updatedRules = new ArrayList<QuantityBasedRule>();
            for (DataDTO dto : dw.getData()) {
                if (dto.getPk() != null && !CollectionUtils.isEmpty(criteriaList)) {
                    checkId: {
                        // Update Existing Criteria
                        for (QuantityBasedRule quantityBasedRule : criteriaList) {
                            // make compatible with enterprise module
                            boolean isParentRelated = sandBoxHelper.isRelatedToParentCatalogIds(quantityBasedRule, dto.getPk());
                            boolean isMatch = isParentRelated || dto.getPk().equals(quantityBasedRule.getId());
                            if (isMatch) {
                                String mvel;
                                // don't update if the data has not changed
                                if (!quantityBasedRule.getQuantity().equals(dto.getQuantity())) {
                                    dirty = true;
                                }
                                try {
                                    mvel = ruleFieldExtractionUtility.convertDTOToMvelString(translator, entityKey, dto, fieldService);
                                    if (!quantityBasedRule.getMatchRule().equals(mvel)) {
                                        dirty = true;
                                    }
                                } catch (MVELTranslationException e) {
                                    throw new RuntimeException(e);
                                }
                                if (!dirty && extensionManager != null) {
                                    ExtensionResultHolder<Boolean> resultHolder = new ExtensionResultHolder<Boolean>();
                                    ExtensionResultStatusType result = extensionManager.getProxy().establishDirtyState(quantityBasedRule, resultHolder);
                                    if (ExtensionResultStatusType.NOT_HANDLED != result && resultHolder.getResult() != null) {
                                        dirty = resultHolder.getResult();
                                    }
                                }
                                if (dirty) {
                                    // pre-merge (can result in a clone for enterprise)
                                    quantityBasedRule = em.merge(quantityBasedRule);
                                    // update the quantity based rule
                                    quantityBasedRule.setQuantity(dto.getQuantity());
                                    quantityBasedRule.setMatchRule(mvel);
                                    quantityBasedRule = em.merge(quantityBasedRule);
                                }
                                updatedRules.add(quantityBasedRule);
                                break checkId;
                            }
                        }
                        throw new IllegalArgumentException("Unable to update the rule of type (" + memberType.getName() + ") because an update was requested for id (" + dto.getPk() + "), which does not exist.");
                    }
                } else {
                    // Create a new Criteria
                    QuantityBasedRule quantityBasedRule;
                    try {
                        quantityBasedRule = (QuantityBasedRule) memberType.newInstance();
                        quantityBasedRule.setQuantity(dto.getQuantity());
                        quantityBasedRule.setMatchRule(ruleFieldExtractionUtility.convertDTOToMvelString(translator, entityKey, dto, fieldService));
                        if (StringUtils.isEmpty(quantityBasedRule.getMatchRule()) && !StringUtils.isEmpty(dw.getRawMvel())) {
                            quantityBasedRule.setMatchRule(dw.getRawMvel());
                        }
                        PropertyUtils.setNestedProperty(quantityBasedRule, mappedBy, parent);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    em.persist(quantityBasedRule);
                    dto.setPk(quantityBasedRule.getId());
                    Object contained = findContainedRuleIfApplicable(quantityBasedRule);
                    if (contained != null) {
                        dto.setContainedPk((Long) em.unwrap(Session.class).getIdentifier(contained));
                    }
                    if (extensionManager != null) {
                        ExtensionResultHolder resultHolder = new ExtensionResultHolder();
                        extensionManager.getProxy().postAdd(quantityBasedRule, resultHolder);
                        if (resultHolder.getResult() != null) {
                            quantityBasedRule = (QuantityBasedRule) resultHolder.getResult();
                        }
                    }
                    if (cascadeExtensionManager != null) {
                        ExtensionResultHolder resultHolder = new ExtensionResultHolder();
                        cascadeExtensionManager.getProxy().postCascadeAdd(quantityBasedRule, dto, resultHolder);
                        if (resultHolder.getResult() != null) {
                            quantityBasedRule = (QuantityBasedRule) resultHolder.getResult();
                        }
                    }
                    updatedRules.add(quantityBasedRule);
                    dirty = true;
                }
            }
            // if an item was not included in the comprehensive submit from the client, we can assume that the
            // listing was deleted, so we remove it here.
            Iterator<QuantityBasedRule> itr = criteriaList.iterator();
            // Since this class explicitly removes the quantity based rule - we must also preserve the id of the element
            // as the CacheInvalidationProducer will need this in order to remove each collection member cache instance as well.
            BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
            context.getAdditionalProperties().put("deletedQuantityBasedRules", new HashSet<QuantityBasedRule>());
            while (itr.hasNext()) {
                checkForRemove: {
                    QuantityBasedRule original = itr.next();
                    for (QuantityBasedRule quantityBasedRule : updatedRules) {
                        Long id = sandBoxHelper.getOriginalId(quantityBasedRule);
                        boolean isMatch = original.getId().equals(id) || original.getId().equals(quantityBasedRule.getId());
                        if (isMatch) {
                            break checkForRemove;
                        }
                    }
                    ((Set<QuantityBasedRule>) context.getAdditionalProperties().get("deletedQuantityBasedRules")).add(original);
                    em.remove(original);
                    itr.remove();
                    dirty = true;
                }
            }
            ObjectMapper mapper = new ObjectMapper();
            String json;
            try {
                json = mapper.writeValueAsString(dw);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            property.setValue(json);
        }
    }
    return dirty;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) MVELTranslationException(org.broadleafcommerce.openadmin.web.rulebuilder.MVELTranslationException) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) ArrayList(java.util.ArrayList) DataDTO(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataDTO) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) MVELTranslationException(org.broadleafcommerce.openadmin.web.rulebuilder.MVELTranslationException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) DataWrapper(org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper) QuantityBasedRule(org.broadleafcommerce.common.rule.QuantityBasedRule) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Session(org.hibernate.Session)

Example 50 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class RuleFieldPersistenceProvider method updateSimpleRule.

protected void updateSimpleRule(PopulateValueRequest populateValueRequest, String mvel, boolean persist, SimpleRule rule) throws IllegalAccessException, FieldNotAvailableException {
    if (!persist) {
        // pre-merge (can result in a clone for enterprise)
        rule = populateValueRequest.getPersistenceManager().getDynamicEntityDao().merge(rule);
    }
    rule.setMatchRule(mvel);
    if (persist) {
        populateValueRequest.getPersistenceManager().getDynamicEntityDao().persist(rule);
        if (extensionManager != null) {
            ExtensionResultHolder resultHolder = new ExtensionResultHolder();
            extensionManager.getProxy().postAdd(rule, resultHolder);
        }
    }
}
Also used : ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Aggregations

ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)56 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)35 ArrayList (java.util.ArrayList)11 Product (org.broadleafcommerce.core.catalog.domain.Product)7 Sku (org.broadleafcommerce.core.catalog.domain.Sku)6 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)6 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)5 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)5 List (java.util.List)4 Set (java.util.Set)4 File (java.io.File)3 Serializable (java.io.Serializable)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashMap (java.util.HashMap)3 Query (javax.persistence.Query)3 TypedQuery (javax.persistence.TypedQuery)3 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)3 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)3 Site (org.broadleafcommerce.common.site.domain.Site)3 Category (org.broadleafcommerce.core.catalog.domain.Category)3