Search in sources :

Example 51 with ExtensionResultHolder

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

the class RuleFieldPersistenceProvider method transformId.

protected Long transformId(Long id, Object rule) {
    if (extensionManager != null) {
        ExtensionResultHolder<Long> resultHolder = new ExtensionResultHolder<Long>();
        ExtensionResultStatusType result = extensionManager.getProxy().transformId(rule, resultHolder);
        if (ExtensionResultStatusType.NOT_HANDLED != result && resultHolder.getResult() != null) {
            id = resultHolder.getResult();
        }
    }
    return id;
}
Also used : ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 52 with ExtensionResultHolder

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

the class BasicFieldPersistenceProvider method extractValue.

@Override
public MetadataProviderResponse extractValue(ExtractValueRequest extractValueRequest, Property property) throws PersistenceException {
    if (!canHandleExtraction(extractValueRequest, property)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    try {
        if (extractValueRequest.getRequestedValue() != null) {
            String val = null;
            if (extractValueRequest.getMetadata().getForeignKeyCollection()) {
                ((BasicFieldMetadata) property.getMetadata()).setFieldType(extractValueRequest.getMetadata().getFieldType());
            } else if (extractValueRequest.getMetadata().getFieldType().equals(SupportedFieldType.BOOLEAN) && extractValueRequest.getRequestedValue() instanceof Character) {
                val = (extractValueRequest.getRequestedValue().equals('Y')) ? "true" : "false";
            } else if (Date.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
                val = extractValueRequest.getDataFormatProvider().getSimpleDateFormatter().format((Date) extractValueRequest.getRequestedValue());
            } else if (Timestamp.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
                val = extractValueRequest.getDataFormatProvider().getSimpleDateFormatter().format(new Date(((Timestamp) extractValueRequest.getRequestedValue()).getTime()));
            } else if (Calendar.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
                val = extractValueRequest.getDataFormatProvider().getSimpleDateFormatter().format(((Calendar) extractValueRequest.getRequestedValue()).getTime());
            } else if (Double.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
                val = extractValueRequest.getDataFormatProvider().getDecimalFormatter().format(extractValueRequest.getRequestedValue());
            } else if (BigDecimal.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
                BigDecimal decimal = (BigDecimal) extractValueRequest.getRequestedValue();
                DecimalFormat format = extractValueRequest.getDataFormatProvider().getDecimalFormatter();
                // track all the decimal places in the scale of the BigDecimal - even if they're all zeros
                StringBuilder sb = new StringBuilder();
                sb.append("0");
                if (decimal.scale() > 0) {
                    sb.append(".");
                    for (int j = 0; j < decimal.scale(); j++) {
                        sb.append("0");
                    }
                }
                format.applyPattern(sb.toString());
                val = format.format(extractValueRequest.getRequestedValue());
            } else if (extractValueRequest.getMetadata().getForeignKeyClass() != null) {
                try {
                    val = extractValueRequest.getFieldManager().getFieldValue(extractValueRequest.getRequestedValue(), extractValueRequest.getMetadata().getForeignKeyProperty()).toString();
                    if (extensionManager != null) {
                        ExtensionResultHolder<Serializable> resultHolder = new ExtensionResultHolder<Serializable>();
                        ExtensionResultStatusType result = extensionManager.getProxy().transformForeignKey(extractValueRequest, property, resultHolder);
                        if (ExtensionResultStatusType.NOT_HANDLED != result && resultHolder.getResult() != null) {
                            val = String.valueOf(resultHolder.getResult());
                        }
                    }
                    // see if there's a name property and use it for the display value
                    String entityName = null;
                    if (extractValueRequest.getRequestedValue() instanceof AdminMainEntity) {
                        entityName = ((AdminMainEntity) extractValueRequest.getRequestedValue()).getMainEntityName();
                    }
                    Object temp = null;
                    if (!StringUtils.isEmpty(extractValueRequest.getMetadata().getForeignKeyDisplayValueProperty())) {
                        String nameProperty = extractValueRequest.getMetadata().getForeignKeyDisplayValueProperty();
                        try {
                            temp = extractValueRequest.getFieldManager().getFieldValue(extractValueRequest.getRequestedValue(), nameProperty);
                        } catch (FieldNotAvailableException e) {
                        // do nothing
                        }
                    }
                    if (temp == null && StringUtils.isEmpty(entityName)) {
                        try {
                            temp = extractValueRequest.getFieldManager().getFieldValue(extractValueRequest.getRequestedValue(), "name");
                        } catch (FieldNotAvailableException e) {
                        // do nothing
                        }
                    }
                    if (temp != null) {
                        extractValueRequest.setDisplayVal(temp.toString());
                    } else if (!StringUtils.isEmpty(entityName)) {
                        extractValueRequest.setDisplayVal(entityName);
                    }
                } catch (FieldNotAvailableException e) {
                    throw new IllegalArgumentException(e);
                }
            } else if (SupportedFieldType.ID == extractValueRequest.getMetadata().getFieldType()) {
                val = extractValueRequest.getRequestedValue().toString();
                if (extensionManager != null) {
                    ExtensionResultHolder<Serializable> resultHolder = new ExtensionResultHolder<Serializable>();
                    ExtensionResultStatusType result = extensionManager.getProxy().transformId(extractValueRequest, property, resultHolder);
                    if (ExtensionResultStatusType.NOT_HANDLED != result && resultHolder.getResult() != null) {
                        val = String.valueOf(resultHolder.getResult());
                    }
                }
            } else {
                val = extractValueRequest.getRequestedValue().toString();
            }
            property.setValue(val);
            property.setDisplayValue(extractValueRequest.getDisplayVal());
        }
    } catch (IllegalAccessException e) {
        throw new PersistenceException(e);
    }
    return MetadataProviderResponse.HANDLED;
}
Also used : Serializable(java.io.Serializable) Calendar(java.util.Calendar) DecimalFormat(java.text.DecimalFormat) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) Timestamp(java.sql.Timestamp) Date(java.util.Date) BigDecimal(java.math.BigDecimal) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity)

Example 53 with ExtensionResultHolder

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

the class AdornedTargetMaintainedFieldPropertyValidator method validate.

@Override
public PropertyValidationResult validate(Entity entity, Serializable instance, Map<String, FieldMetadata> entityFieldMetadata, BasicFieldMetadata propertyMetadata, String propertyName, String value) {
    ExtensionResultHolder<Boolean> validationResult = new ExtensionResultHolder<Boolean>();
    ExtensionResultStatusType status = adornedTargetAutoPopulateExtensionManager.getProxy().validateSubmittedAdornedTargetManagedFields(entity, instance, entityFieldMetadata, propertyMetadata, propertyName, value, validationResult);
    Boolean valid = true;
    if (ExtensionResultStatusType.NOT_HANDLED != status && validationResult.getResult() != null) {
        valid = validationResult.getResult();
    }
    return new PropertyValidationResult(valid, ERROR_MESSAGE);
}
Also used : ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 54 with ExtensionResultHolder

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

the class CategoriesProcessor method populateModelVariables.

@Override
public Map<String, Object> populateModelVariables(String tagName, Map<String, String> tagAttributes, BroadleafTemplateContext context) {
    String resultVar = tagAttributes.get("resultVar");
    String parentCategory = tagAttributes.get("parentCategory");
    String unparsedMaxResults = tagAttributes.get("maxResults");
    Map<String, Object> newModelVars = new HashMap<>();
    if (extensionManager != null) {
        ExtensionResultHolder holder = new ExtensionResultHolder();
        ExtensionResultStatusType result = extensionManager.getProxy().findAllPossibleChildCategories(parentCategory, unparsedMaxResults, holder);
        if (ExtensionResultStatusType.HANDLED.equals(result)) {
            newModelVars.put(resultVar, holder.getResult());
            return newModelVars;
        }
    }
    // TODO: Potentially write an algorithm that will pick the minimum depth category
    // instead of the first category in the list
    List<Category> categories = catalogService.findCategoriesByName(parentCategory);
    if (categories != null && categories.size() > 0) {
        // gets child categories in order ONLY if they are in the xref table and active
        List<CategoryXref> subcategories = categories.get(0).getChildCategoryXrefs();
        List<Category> results = Collections.emptyList();
        if (subcategories != null && !subcategories.isEmpty()) {
            results = new ArrayList<>(subcategories.size());
            if (StringUtils.isNotEmpty(unparsedMaxResults)) {
                int maxResults = Integer.parseInt(unparsedMaxResults);
                if (subcategories.size() > maxResults) {
                    subcategories = subcategories.subList(0, maxResults);
                }
            }
            for (CategoryXref xref : subcategories) {
                results.add(xref.getSubCategory());
            }
        }
        newModelVars.put(resultVar, results);
    }
    return newModelVars;
}
Also used : Category(org.broadleafcommerce.core.catalog.domain.Category) HashMap(java.util.HashMap) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) CategoryXref(org.broadleafcommerce.core.catalog.domain.CategoryXref) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 55 with ExtensionResultHolder

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

the class ProductOptionsProcessor method createPricingDto.

protected ProductOptionPricingDTO createPricingDto(Sku sku, List<Long> productOptionValueIds, Map<String, String> tagAttributes, BroadleafTemplateContext context) {
    Long[] values = new Long[productOptionValueIds.size()];
    productOptionValueIds.toArray(values);
    ProductOptionPricingDTO dto = new ProductOptionPricingDTO();
    Money currentPrice = sku.getPrice();
    // Check for Price Overrides
    ExtensionResultHolder<Money> priceHolder = new ExtensionResultHolder<>();
    priceHolder.setResult(currentPrice);
    if (extensionManager != null) {
        extensionManager.getProxy().modifyPriceForOverrides(sku, priceHolder, context, tagAttributes);
    }
    dto.setPrice(BLCMoneyFormatUtils.formatPrice(priceHolder.getResult()));
    if (sku.getRetailPrice() != null) {
        dto.setRetailPrice(BLCMoneyFormatUtils.formatPrice(sku.getRetailPrice()));
    }
    if (sku.getSalePrice() != null) {
        dto.setSalePrice(BLCMoneyFormatUtils.formatPrice(sku.getSalePrice()));
    }
    dto.setOnSale(sku.isOnSale());
    dto.setSelectedOptions(values);
    return dto;
}
Also used : Money(org.broadleafcommerce.common.money.Money) 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