Search in sources :

Example 16 with Sku

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);
    }
}
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 17 with Sku

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;
}
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 18 with Sku

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;
}
Also used : Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 19 with Sku

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";
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) ListGrid(org.broadleafcommerce.openadmin.web.form.component.ListGrid) SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Property(org.broadleafcommerce.openadmin.dto.Property) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 20 with Sku

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;
}
Also used : Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Aggregations

Sku (org.broadleafcommerce.core.catalog.domain.Sku)80 Product (org.broadleafcommerce.core.catalog.domain.Product)34 ArrayList (java.util.ArrayList)27 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)22 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)18 Money (org.broadleafcommerce.common.money.Money)16 ProductImpl (org.broadleafcommerce.core.catalog.domain.ProductImpl)16 Order (org.broadleafcommerce.core.order.domain.Order)15 Category (org.broadleafcommerce.core.catalog.domain.Category)13 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)12 Test (org.testng.annotations.Test)12 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)10 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)10 Transactional (org.springframework.transaction.annotation.Transactional)10 ProductBundle (org.broadleafcommerce.core.catalog.domain.ProductBundle)9 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)9 HashMap (java.util.HashMap)8 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)8 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)8 Entity (org.broadleafcommerce.openadmin.dto.Entity)8