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