use of org.broadleafcommerce.core.catalog.domain.Sku 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.Sku 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.Sku in project BroadleafCommerce by BroadleafCommerce.
the class ForeignSkuFieldPersistenceProvider method extractValue.
@Override
public MetadataProviderResponse extractValue(ExtractValueRequest extractValueRequest, Property property) {
if (!canHandleExtraction(extractValueRequest, property)) {
return MetadataProviderResponse.NOT_HANDLED;
}
try {
String val = extractValueRequest.getFieldManager().getFieldValue(extractValueRequest.getRequestedValue(), extractValueRequest.getMetadata().getForeignKeyProperty()).toString();
String displayVal = null;
if (!StringUtils.isEmpty(extractValueRequest.getMetadata().getForeignKeyDisplayValueProperty())) {
String nameProperty = extractValueRequest.getMetadata().getForeignKeyDisplayValueProperty();
Sku sku = (Sku) extractValueRequest.getRequestedValue();
displayVal = extractValueRequest.getRecordHelper().getStringValueFromGetter(sku, nameProperty);
}
extractValueRequest.setDisplayVal(displayVal);
property.setValue(val);
property.setDisplayValue(displayVal);
} catch (Exception e) {
return MetadataProviderResponse.NOT_HANDLED;
}
return MetadataProviderResponse.HANDLED_BREAK;
}
use of org.broadleafcommerce.core.catalog.domain.Sku in project BroadleafCommerce by BroadleafCommerce.
the class AdminProductController method showUpdateAdditionalSku.
protected String showUpdateAdditionalSku(HttpServletRequest request, Model model, String id, String collectionItemId, Map<String, String> pathVars, EntityForm entityForm) throws Exception {
String collectionField = "additionalSkus";
// Find out metadata for the additionalSkus property
String mainClassName = getClassNameForSection(SECTION_KEY);
List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, SECTION_KEY, id);
ClassMetadata mainMetadata = service.getClassMetadata(getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars)).getDynamicResultSet().getClassMetaData();
Property collectionProperty = mainMetadata.getPMap().get(collectionField);
FieldMetadata md = collectionProperty.getMetadata();
// Find the metadata and the entity for the selected sku
PersistencePackageRequest ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs).withCustomCriteria(new String[] { id });
ClassMetadata collectionMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
if (collectionMetadata.getCeilingType().equals(SkuImpl.class.getName())) {
collectionMetadata.setCeilingType(Sku.class.getName());
}
Entity entity = service.getRecord(ppr, collectionItemId, collectionMetadata, true).getDynamicResultSet().getRecords()[0];
String currentTabName = getCurrentTabName(pathVars, collectionMetadata);
Map<String, DynamicResultSet> subRecordsMap = service.getRecordsForSelectedTab(collectionMetadata, entity, sectionCrumbs, currentTabName);
if (entityForm == null) {
entityForm = formService.createEntityForm(collectionMetadata, entity, subRecordsMap, sectionCrumbs);
} else {
entityForm.clearFieldsMap();
formService.populateEntityForm(collectionMetadata, entity, subRecordsMap, entityForm, sectionCrumbs);
// remove all the actions since we're not trying to redisplay them on the form
entityForm.removeAllActions();
}
entityForm.removeAction(DefaultEntityFormActions.DELETE);
// Ensure that operations on the Sku subcollections go to the proper URL
for (ListGrid lg : entityForm.getAllListGrids()) {
lg.setSectionKey("org.broadleafcommerce.core.catalog.domain.Sku");
lg.setSectionCrumbs(sectionCrumbs);
}
model.addAttribute("entityForm", entityForm);
model.addAttribute("viewType", "modal/simpleEditEntity");
model.addAttribute("currentUrl", request.getRequestURL().toString());
model.addAttribute("modalHeaderType", ModalHeaderType.UPDATE_COLLECTION_ITEM.getType());
model.addAttribute("collectionProperty", collectionProperty);
setModelAttributes(model, SECTION_KEY);
return "modules/modalContainer";
}
use of org.broadleafcommerce.core.catalog.domain.Sku in project BroadleafCommerce by BroadleafCommerce.
the class AdminProductTranslationExtensionHandler method applyTransformation.
/**
* If we are trying to translate a field on Product that starts with "defaultSku.", we really want to associate the
* translation with Sku, its associated id, and the property name without "defaultSku."
*/
@Override
public ExtensionResultStatusType applyTransformation(TranslationForm form) {
String defaultSkuPrefix = "defaultSku.";
String unencodedPropertyName = JSCompatibilityHelper.unencode(form.getPropertyName());
if (form.getCeilingEntity().equals(Product.class.getName()) && unencodedPropertyName.startsWith(defaultSkuPrefix)) {
Product p = catalogService.findProductById(Long.parseLong(form.getEntityId()));
form.setCeilingEntity(Sku.class.getName());
form.setEntityId(String.valueOf(p.getDefaultSku().getId()));
form.setPropertyName(unencodedPropertyName.substring(defaultSkuPrefix.length()));
}
return ExtensionResultStatusType.HANDLED;
}
Aggregations