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);
}
}
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;
}
}
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);
}
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);
}
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);
}
Aggregations