Search in sources :

Example 1 with ProductOption

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

the class ProductOptionsCustomPersistenceHandler 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(ProductOption.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        ProductOption adminInstance = (ProductOption) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
        adminInstance = (ProductOption) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        // validate "Use in Sku generation"
        if (needsAllowedValue(adminInstance)) {
            String errorMessage = "Must add at least 1 Allowed Value when Product Option is used in Sku generation";
            entity.addValidationError("useInSkuGeneration", errorMessage);
            return entity;
        }
        adminInstance = (ProductOption) dynamicEntityDao.merge(adminInstance);
        return helper.getRecord(adminProperties, adminInstance, null, null);
    } catch (Exception e) {
        throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
    }
}
Also used : ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 2 with ProductOption

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

the class SkuCustomPersistenceHandler method inspect.

/**
 * Build out the extra fields for the product options
 */
@Override
public DynamicResultSet inspect(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, InspectHelper helper) throws ServiceException {
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Map<MergedPropertyType, Map<String, FieldMetadata>> allMergedProperties = new HashMap<>();
        String productIdStr = null;
        if (persistencePackage.getCustomCriteria() != null && persistencePackage.getCustomCriteria().length > 0) {
            productIdStr = persistencePackage.getCustomCriteria()[0];
        }
        String cacheKey = skuMetadataCacheService.buildCacheKey(productIdStr);
        Map<String, FieldMetadata> properties = null;
        boolean useCache = skuMetadataCacheService.useCache();
        if (useCache) {
            properties = skuMetadataCacheService.getFromCache(cacheKey);
        }
        if (properties == null) {
            // Grab the default properties for the Sku
            properties = helper.getSimpleMergedProperties(SkuImpl.class.getName(), persistencePerspective);
            boolean isFirstCriteriaNAN = persistencePackage.getCustomCriteria() == null || persistencePackage.getCustomCriteria().length == 0;
            if (!isFirstCriteriaNAN && useToOneLookupSkuProductOptionValue) {
                isFirstCriteriaNAN = !DIGIT.matchesAllOf(persistencePackage.getCustomCriteria()[0]);
            }
            if (isFirstCriteriaNAN) {
                // look up all the ProductOptions and then create new fields for each of them
                List<ProductOption> options = catalogService.readAllProductOptions();
                int order = 0;
                for (ProductOption option : options) {
                    // add this to the built Sku properties
                    FieldMetadata md = createIndividualOptionField(option, order);
                    if (md != null) {
                        properties.put("productOption" + option.getId(), md);
                    }
                }
            } else {
                // If we have a product to filter the list of available product options, then use it
                try {
                    Long productId = Long.parseLong(persistencePackage.getCustomCriteria()[0]);
                    Product product = catalogService.findProductById(productId);
                    for (ProductOption option : product.getProductOptions()) {
                        FieldMetadata md = createIndividualOptionField(option, 0);
                        if (md != null) {
                            properties.put("productOption" + option.getId(), md);
                        }
                    }
                } catch (NumberFormatException e) {
                // the criteria wasn't a product id, just don't do anything
                }
            }
            // also build the consolidated field; if using the SkuBasicClientEntityModule then this field will be
            // permanently hidden
            properties.put(CONSOLIDATED_PRODUCT_OPTIONS_FIELD_NAME, createConsolidatedOptionField(SkuImpl.class));
            if (useCache) {
                skuMetadataCacheService.addToCache(cacheKey, properties);
            }
        }
        allMergedProperties.put(MergedPropertyType.PRIMARY, properties);
        // allow the adorned list to contribute properties as well in the case of Sku bundle items
        adornedPersistenceModule.setPersistenceManager((PersistenceManager) helper);
        adornedPersistenceModule.updateMergedProperties(persistencePackage, allMergedProperties);
        Class<?>[] entityClasses = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(SkuImpl.class);
        for (Map.Entry<MergedPropertyType, Map<String, FieldMetadata>> entry : allMergedProperties.entrySet()) {
            filterOutProductMetadata(entry.getValue());
        }
        ClassMetadata mergedMetadata = helper.buildClassMetadata(entityClasses, persistencePackage, allMergedProperties);
        DynamicResultSet results = new DynamicResultSet(mergedMetadata, null, null);
        return results;
    } catch (Exception e) {
        ServiceException ex = new ServiceException("Unable to retrieve inspection results for " + persistencePackage.getCeilingEntityFullyQualifiedClassname(), e);
        throw ex;
    }
}
Also used : ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) HashMap(java.util.HashMap) Product(org.broadleafcommerce.core.catalog.domain.Product) MergedPropertyType(org.broadleafcommerce.openadmin.dto.MergedPropertyType) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Map(java.util.Map) HashMap(java.util.HashMap) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 3 with ProductOption

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

the class ProductOptionDisplayProcessor method populateModelVariables.

@Override
public Map<String, Object> populateModelVariables(String tagName, Map<String, String> tagAttributes, BroadleafTemplateContext context) {
    HashMap<String, String> productOptionDisplayValues = new HashMap<>();
    Object item = context.parseExpression(tagAttributes.get("orderItem"));
    if (item instanceof DiscreteOrderItem) {
        DiscreteOrderItem orderItem = (DiscreteOrderItem) item;
        for (String i : orderItem.getOrderItemAttributes().keySet()) {
            for (ProductOption option : orderItem.getProduct().getProductOptions()) {
                if (option.getAttributeName().equals(i) && !StringUtils.isEmpty(orderItem.getOrderItemAttributes().get(i).toString())) {
                    productOptionDisplayValues.put(option.getLabel(), orderItem.getOrderItemAttributes().get(i).toString());
                }
            }
        }
    }
    return ImmutableMap.of("productOptionDisplayValues", (Object) productOptionDisplayValues);
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) HashMap(java.util.HashMap)

Example 4 with ProductOption

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

the class ProductOptionDaoImpl method getProductIdsUsingProductOptionByIdQuery.

private TypedQuery<Long> getProductIdsUsingProductOptionByIdQuery(Long productOptionId, boolean count) {
    // Set up the criteria query that specifies we want to return Products
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
    // The root of our search is ProductOptionXref
    Root<ProductOptionXrefImpl> productOptionXref = criteria.from(ProductOptionXrefImpl.class);
    Join<ProductOptionXref, Product> product = productOptionXref.join("product");
    Join<ProductOptionXref, ProductOption> productOption = productOptionXref.join("productOption");
    if (count) {
        criteria.select(builder.count(product));
    } else {
        // Product IDs are what we want back
        criteria.select(product.get("id").as(Long.class));
    }
    criteria.distinct(true);
    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(productOption.get("id").in(sandBoxHelper.mergeCloneIds(ProductOptionImpl.class, productOptionId)));
    // Execute the query with the restrictions
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    return em.createQuery(criteria);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) ProductOptionXref(org.broadleafcommerce.core.catalog.domain.ProductOptionXref) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) Predicate(javax.persistence.criteria.Predicate) ProductOptionXrefImpl(org.broadleafcommerce.core.catalog.domain.ProductOptionXrefImpl)

Example 5 with ProductOption

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

the class ProductOptionsProcessor method addAllProductOptionsToModel.

protected void addAllProductOptionsToModel(Map<String, Object> newModelVars, Product product) {
    List<ProductOption> productOptions = product.getProductOptions();
    List<ProductOptionDTO> dtos = new ArrayList<>();
    for (ProductOption option : productOptions) {
        ProductOptionDTO dto = new ProductOptionDTO();
        dto.setId(option.getId());
        dto.setType(option.getType().getType());
        Map<Long, String> values = new HashMap<>();
        for (ProductOptionValue value : option.getAllowedValues()) {
            values.put(value.getId(), value.getAttributeValue());
        }
        dto.setValues(values);
        dtos.add(dto);
    }
    writeJSONToModel(newModelVars, "allProductOptions", dtos);
}
Also used : ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Aggregations

ProductOption (org.broadleafcommerce.core.catalog.domain.ProductOption)9 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Product (org.broadleafcommerce.core.catalog.domain.Product)3 ProductOptionXref (org.broadleafcommerce.core.catalog.domain.ProductOptionXref)3 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)3 ServiceException (org.broadleafcommerce.common.exception.ServiceException)2 ProductOptionValue (org.broadleafcommerce.core.catalog.domain.ProductOptionValue)2 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)2 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)2 List (java.util.List)1 Map (java.util.Map)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 ProductOptionXrefImpl (org.broadleafcommerce.core.catalog.domain.ProductOptionXrefImpl)1 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)1 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)1 Order (org.broadleafcommerce.core.order.domain.Order)1 OrderItemAttribute (org.broadleafcommerce.core.order.domain.OrderItemAttribute)1 RequiredAttributeNotProvidedException (org.broadleafcommerce.core.order.service.exception.RequiredAttributeNotProvidedException)1