Search in sources :

Example 1 with ProductOptionValue

use of org.broadleafcommerce.core.catalog.domain.ProductOptionValue in project BroadleafCommerce by BroadleafCommerce.

the class SkuBundleItemCustomPersistenceHandler method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
        // sort it
        if (foreignKey != null && foreignKey.getSortField() != null) {
            FilterAndSortCriteria sortCriteria = cto.get(foreignKey.getSortField());
            sortCriteria.setSortAscending(foreignKey.getSortAscending());
        }
        // get the default properties from Inventory and its subclasses
        Map<String, FieldMetadata> originalProps = helper.getSimpleMergedProperties(SkuBundleItem.class.getName(), persistencePerspective);
        // Pull back the Inventory based on the criteria from the client
        List<FilterMapping> filterMappings = helper.getFilterMappings(persistencePerspective, cto, ceilingEntityFullyQualifiedClassname, originalProps);
        // attach the product option criteria
        skuPersistenceHandler.applyProductOptionValueCriteria(filterMappings, cto, persistencePackage, "sku");
        List<Serializable> records = helper.getPersistentRecords(persistencePackage.getCeilingEntityFullyQualifiedClassname(), filterMappings, cto.getFirstResult(), cto.getMaxResults());
        // Convert Skus into the client-side Entity representation
        Entity[] payload = helper.getRecords(originalProps, records);
        int totalRecords = helper.getTotalRecords(persistencePackage.getCeilingEntityFullyQualifiedClassname(), filterMappings);
        for (int i = 0; i < payload.length; i++) {
            Entity entity = payload[i];
            SkuBundleItem bundleItem = (SkuBundleItem) records.get(i);
            Sku bundleSku = bundleItem.getSku();
            entity.findProperty("sku").setDisplayValue(bundleSku.getName());
            List<ProductOptionValue> productOptionValues = new ArrayList<>();
            for (SkuProductOptionValueXref skuProductOptionValueXref : bundleSku.getProductOptionValueXrefs()) {
                productOptionValues.add(skuProductOptionValueXref.getProductOptionValue());
            }
            Property optionsProperty = skuPersistenceHandler.getConsolidatedOptionProperty(productOptionValues);
            entity.addProperty(optionsProperty);
        }
        return new DynamicResultSet(null, payload, totalRecords);
    } catch (Exception e) {
        throw new ServiceException("There was a problem fetching inventory. See server logs for more details", e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Serializable(java.io.Serializable) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) ArrayList(java.util.ArrayList) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) SkuProductOptionValueXref(org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXref) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SkuBundleItem(org.broadleafcommerce.core.catalog.domain.SkuBundleItem) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Property(org.broadleafcommerce.openadmin.dto.Property) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 2 with ProductOptionValue

use of org.broadleafcommerce.core.catalog.domain.ProductOptionValue in project BroadleafCommerce by BroadleafCommerce.

the class SkuCustomPersistenceHandler method createExplicitEnumerationIndividualOptionField.

protected FieldMetadata createExplicitEnumerationIndividualOptionField(ProductOption option, int order) {
    BasicFieldMetadata metadata = new BasicFieldMetadata();
    List<ProductOptionValue> allowedValues = option.getAllowedValues();
    if (CollectionUtils.isNotEmpty(allowedValues)) {
        metadata.setFieldType(SupportedFieldType.EXPLICIT_ENUMERATION);
        metadata.setMutable(true);
        metadata.setInheritedFromType(SkuImpl.class.getName());
        metadata.setAvailableToTypes(getPolymorphicClasses(SkuImpl.class, em, skuMetadataCacheService.useCache()));
        metadata.setForeignKeyCollection(false);
        metadata.setMergedPropertyType(MergedPropertyType.PRIMARY);
        // Set up the enumeration based on the product option values
        String[][] optionValues = new String[allowedValues.size()][2];
        for (int i = 0; i < allowedValues.size(); i++) {
            ProductOptionValue value = option.getAllowedValues().get(i);
            optionValues[i][0] = value.getId().toString();
            optionValues[i][1] = value.getAttributeValue();
        }
        metadata.setEnumerationValues(optionValues);
        metadata.setName(PRODUCT_OPTION_FIELD_PREFIX + option.getId());
        metadata.setFriendlyName(option.getLabel());
        metadata.setGroup("productOption_group");
        metadata.setGroupOrder(-1);
        metadata.setOrder(order);
        metadata.setExplicitFieldType(SupportedFieldType.UNKNOWN);
        metadata.setProminent(false);
        metadata.setVisibility(VisibilityEnum.FORM_EXPLICITLY_SHOWN);
        metadata.setBroadleafEnumeration("");
        metadata.setReadOnly(false);
        metadata.setRequiredOverride(BooleanUtils.isFalse(option.getRequired()));
        return metadata;
    }
    return null;
}
Also used : ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)

Example 3 with ProductOptionValue

use of org.broadleafcommerce.core.catalog.domain.ProductOptionValue in project BroadleafCommerce by BroadleafCommerce.

the class SkuCustomPersistenceHandler method validateUniqueProductOptionValueCombination.

/**
 * Ensures that the given list of {@link ProductOptionValue} IDs is unique for the given {@link Product}.
 *
 * If sku browsing is enabled, then it is assumed that a single combination of {@link ProductOptionValue} IDs
 * is not unique and more than one {@link Sku} could have the exact same combination of {@link ProductOptionValue} IDs.
 * In this case, the following validation is skipped.
 *
 * @param product
 * @param productOptionProperties
 * @param currentSku - for update operations, this is the current Sku that is being updated; should be excluded from
 * attempting validation
 * @return <b>null</b> if successfully validation, the error entity otherwise
 */
protected Entity validateUniqueProductOptionValueCombination(Product product, List<Property> productOptionProperties, Sku currentSku) {
    if (useSku) {
        return null;
    }
    // do not attempt POV validation if no PO properties were passed in
    if (CollectionUtils.isNotEmpty(productOptionProperties)) {
        List<Long> productOptionValueIds = new ArrayList<>();
        for (Property property : productOptionProperties) {
            productOptionValueIds.add(Long.parseLong(property.getValue()));
        }
        boolean validated = true;
        for (Sku sku : product.getAdditionalSkus()) {
            if (currentSku == null || !sku.getId().equals(currentSku.getId())) {
                List<Long> testList = new ArrayList<>();
                for (ProductOptionValue optionValue : sku.getProductOptionValues()) {
                    testList.add(optionValue.getId());
                }
                if (CollectionUtils.isNotEmpty(testList) && productOptionValueIds.containsAll(testList) && productOptionValueIds.size() == testList.size()) {
                    validated = false;
                    break;
                }
            }
        }
        if (!validated) {
            Entity errorEntity = new Entity();
            for (Property productOptionProperty : productOptionProperties) {
                errorEntity.addValidationError(productOptionProperty.getName(), "uniqueSkuError");
            }
            return errorEntity;
        }
    }
    return null;
}
Also used : ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) Entity(org.broadleafcommerce.openadmin.dto.Entity) ArrayList(java.util.ArrayList) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 4 with ProductOptionValue

use of org.broadleafcommerce.core.catalog.domain.ProductOptionValue in project BroadleafCommerce by BroadleafCommerce.

the class SkuCustomPersistenceHandler method associateProductOptionValuesToSku.

/**
 * This initially removes all of the product option values that are currently related to the Sku and then re-associates
 * the {@link ProductOptionValue}s
 * @param entity
 * @param adminInstance
 */
protected void associateProductOptionValuesToSku(Entity entity, Sku adminInstance, DynamicEntityDao dynamicEntityDao) {
    // Get the list of product option value ids that were selected from the form
    List<Long> productOptionValueIds = new ArrayList<>();
    for (Property property : getProductOptionProperties(entity)) {
        Long propId = Long.parseLong(property.getValue());
        productOptionValueIds.add(propId);
        property.setIsDirty(true);
    }
    // Only process associations if product option value changes came in via the form
    if (CollectionUtils.isNotEmpty(productOptionValueIds)) {
        // remove the current list of product option values from the Sku
        if (adminInstance.getProductOptionValueXrefs().size() > 0) {
            Iterator<SkuProductOptionValueXref> iterator = adminInstance.getProductOptionValueXrefs().iterator();
            while (iterator.hasNext()) {
                dynamicEntityDao.remove(iterator.next());
            }
            dynamicEntityDao.merge(adminInstance);
        }
        // Associate the product option values from the form with the Sku
        for (Long id : productOptionValueIds) {
            // Simply find the changed ProductOptionValues directly - seems to work better with sandboxing code
            ProductOptionValue pov = (ProductOptionValue) dynamicEntityDao.find(ProductOptionValueImpl.class, id);
            SkuProductOptionValueXref xref = new SkuProductOptionValueXrefImpl(adminInstance, pov);
            xref = dynamicEntityDao.merge(xref);
            adminInstance.getProductOptionValueXrefs().add(xref);
        }
    }
}
Also used : SkuProductOptionValueXref(org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXref) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) ArrayList(java.util.ArrayList) SkuProductOptionValueXrefImpl(org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXrefImpl) Property(org.broadleafcommerce.openadmin.dto.Property) ProductOptionValueImpl(org.broadleafcommerce.core.catalog.domain.ProductOptionValueImpl)

Example 5 with ProductOptionValue

use of org.broadleafcommerce.core.catalog.domain.ProductOptionValue in project BroadleafCommerce by BroadleafCommerce.

the class ProductOptionValueProcessor method getModifiedAttributes.

@Override
public BroadleafAttributeModifier getModifiedAttributes(String tagName, Map<String, String> tagAttributes, String attributeName, String attributeValue, BroadleafTemplateContext context) {
    Map<String, String> newAttributes = new HashMap<>();
    ProductOptionValue productOptionValue = (ProductOptionValue) context.parseExpression(attributeValue);
    ProductOptionValueDTO dto = new ProductOptionValueDTO();
    dto.setOptionId(productOptionValue.getProductOption().getId());
    dto.setValueId(productOptionValue.getId());
    dto.setValueName(productOptionValue.getAttributeValue());
    dto.setRawValue(productOptionValue.getRawAttributeValue());
    if (productOptionValue.getPriceAdjustment() != null) {
        dto.setPriceAdjustment(productOptionValue.getPriceAdjustment().getAmount());
    }
    try {
        ObjectMapper mapper = new ObjectMapper();
        Writer strWriter = new StringWriter();
        mapper.writeValue(strWriter, dto);
        newAttributes.put("data-product-option-value", strWriter.toString());
    } catch (Exception ex) {
        LOG.error("There was a problem writing the product option value to JSON", ex);
    }
    return new BroadleafAttributeModifier(newAttributes);
}
Also used : ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) BroadleafAttributeModifier(org.broadleafcommerce.presentation.model.BroadleafAttributeModifier) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

ProductOptionValue (org.broadleafcommerce.core.catalog.domain.ProductOptionValue)12 ArrayList (java.util.ArrayList)7 Sku (org.broadleafcommerce.core.catalog.domain.Sku)6 Property (org.broadleafcommerce.openadmin.dto.Property)4 Entity (org.broadleafcommerce.openadmin.dto.Entity)3 HashMap (java.util.HashMap)2 List (java.util.List)2 ProductOption (org.broadleafcommerce.core.catalog.domain.ProductOption)2 SkuProductOptionValueXref (org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXref)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Serializable (java.io.Serializable)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1 Product (org.broadleafcommerce.core.catalog.domain.Product)1 ProductOptionValueImpl (org.broadleafcommerce.core.catalog.domain.ProductOptionValueImpl)1 SkuBundleItem (org.broadleafcommerce.core.catalog.domain.SkuBundleItem)1 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)1