Search in sources :

Example 41 with Product

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

the class ProductCustomPersistenceHandler 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(Product.class.getName(), persistencePerspective);
        BasicFieldMetadata defaultCategory = ((BasicFieldMetadata) adminProperties.get("defaultCategory"));
        defaultCategory.setFriendlyName("ProductImpl_Parent_Category");
        if (entity.findProperty("defaultCategory") != null && !StringUtils.isEmpty(entity.findProperty("defaultCategory").getValue())) {
            // Change the inherited type so that this property is disconnected from the entity and validation is temporarily skipped.
            // This is useful when the defaultCategory was previously completely empty for whatever reason. Without this, such
            // a case would fail the validation, even though the property was specified in the submission.
            defaultCategory.setInheritedFromType(String.class.getName());
        }
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        Product adminInstance = (Product) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
        if (adminInstance instanceof ProductBundle) {
            removeBundleFieldRestrictions((ProductBundle) adminInstance, adminProperties, entity);
        }
        CategoryProductXref oldDefault = getCurrentDefaultXref(adminInstance);
        // so override required flag for that field during deployment
        if (BroadleafRequestContext.getBroadleafRequestContext().isProductionSandBox()) {
            ((BasicFieldMetadata) adminProperties.get("defaultCategory")).setRequiredOverride(false);
        }
        adminInstance = (Product) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        adminInstance = dynamicEntityDao.merge(adminInstance);
        boolean handled = false;
        if (extensionManager != null) {
            ExtensionResultStatusType result = extensionManager.getProxy().manageParentCategoryForUpdate(persistencePackage, adminInstance);
            handled = ExtensionResultStatusType.NOT_HANDLED != result;
            extensionManager.getProxy().manageFields(persistencePackage, adminInstance);
        }
        if (!handled) {
            setupXref(adminInstance);
            removeOldDefault(adminInstance, oldDefault, entity);
        }
        return helper.getRecord(adminProperties, adminInstance, null, null);
    } catch (Exception e) {
        throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) CategoryProductXref(org.broadleafcommerce.core.catalog.domain.CategoryProductXref) ProductBundle(org.broadleafcommerce.core.catalog.domain.ProductBundle) Product(org.broadleafcommerce.core.catalog.domain.Product) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ServiceException(org.broadleafcommerce.common.exception.ServiceException) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)

Example 42 with Product

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

the class ProductCustomPersistenceHandler method remove.

@Override
public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        Product adminInstance = getAdminInstance(persistencePackage, dynamicEntityDao, helper, entity);
        removeProduct(persistencePackage, adminInstance, helper);
    } catch (ClassNotFoundException e) {
        throw new ServiceException("Unable to remove entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Product(org.broadleafcommerce.core.catalog.domain.Product)

Example 43 with Product

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

the class ProductUrlFieldPersistenceProvider method populateValue.

@Override
public MetadataProviderResponse populateValue(PopulateValueRequest request, Serializable instance) {
    String propName = request.getProperty().getName();
    String val = request.getRequestedValue();
    if ("url".equals(propName) && ProductImpl.class.isAssignableFrom(instance.getClass())) {
        Product product = (Product) instance;
        ExtensionResultHolder<String> holder = new ExtensionResultHolder<>();
        ExtensionResultStatusType result = extensionManager.getProxy().modifyUrl(val, product, holder);
        if (ExtensionResultStatusType.HANDLED == result) {
            product.setUrl(holder.getResult());
            return MetadataProviderResponse.HANDLED;
        }
    }
    return super.populateValue(request, instance);
}
Also used : ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) Product(org.broadleafcommerce.core.catalog.domain.Product) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 44 with Product

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

the class SkuPricingPersistenceProvider method getCurrency.

@Override
protected Currency getCurrency(ExtractValueRequest extractValueRequest, Property property) {
    BroadleafCurrency bc = null;
    if (extractValueRequest.getEntity() instanceof Product && isDefaultSkuProperty(extractValueRequest, property)) {
        Product p = (Product) extractValueRequest.getEntity();
        bc = p.getDefaultSku().getCurrency();
    } else if (extractValueRequest.getEntity() instanceof Sku) {
        Sku s = (Sku) extractValueRequest.getEntity();
        bc = s.getCurrency();
    }
    if (bc == null) {
        BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
        return brc.getJavaCurrency();
    } else {
        return Currency.getInstance(bc.getCurrencyCode());
    }
}
Also used : BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 45 with Product

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

the class AdminCatalogServiceImpl method generateSkusFromProduct.

@Override
public Integer generateSkusFromProduct(Long productId) {
    Product product = catalogService.findProductById(productId);
    if (CollectionUtils.isEmpty(product.getProductOptions())) {
        return -1;
    }
    List<List<ProductOptionValue>> allPermutations = generatePermutations(0, new ArrayList<ProductOptionValue>(), product.getProductOptions());
    // return -2 to indicate that one of the Product Options used in Sku generation has no Allowed Values
    if (allPermutations == null) {
        return -2;
    }
    LOG.info("Total number of permutations: " + allPermutations.size());
    LOG.info(allPermutations);
    // determine the permutations that I already have Skus for
    List<List<ProductOptionValue>> previouslyGeneratedPermutations = new ArrayList<List<ProductOptionValue>>();
    if (CollectionUtils.isNotEmpty(product.getAdditionalSkus())) {
        for (Sku additionalSku : product.getAdditionalSkus()) {
            if (CollectionUtils.isNotEmpty(additionalSku.getProductOptionValues())) {
                previouslyGeneratedPermutations.add(additionalSku.getProductOptionValues());
            }
        }
    }
    List<List<ProductOptionValue>> permutationsToGenerate = new ArrayList<List<ProductOptionValue>>();
    for (List<ProductOptionValue> permutation : allPermutations) {
        boolean previouslyGenerated = false;
        for (List<ProductOptionValue> generatedPermutation : previouslyGeneratedPermutations) {
            if (isSamePermutation(permutation, generatedPermutation)) {
                previouslyGenerated = true;
                break;
            }
        }
        if (!previouslyGenerated) {
            permutationsToGenerate.add(permutation);
        }
    }
    int numPermutationsCreated = 0;
    if (extensionManager != null) {
        ExtensionResultHolder<Integer> result = new ExtensionResultHolder<Integer>();
        ExtensionResultStatusType resultStatusType = extensionManager.getProxy().persistSkuPermutation(product, permutationsToGenerate, result);
        if (ExtensionResultStatusType.HANDLED == resultStatusType) {
            numPermutationsCreated = result.getResult();
        }
    }
    return numPermutationsCreated;
}
Also used : ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) ArrayList(java.util.ArrayList) List(java.util.List) Sku(org.broadleafcommerce.core.catalog.domain.Sku) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Aggregations

Product (org.broadleafcommerce.core.catalog.domain.Product)77 Sku (org.broadleafcommerce.core.catalog.domain.Sku)34 ArrayList (java.util.ArrayList)23 Category (org.broadleafcommerce.core.catalog.domain.Category)18 ProductImpl (org.broadleafcommerce.core.catalog.domain.ProductImpl)17 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)12 Order (org.broadleafcommerce.core.order.domain.Order)12 Transactional (org.springframework.transaction.annotation.Transactional)11 Test (org.testng.annotations.Test)11 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)10 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)9 Money (org.broadleafcommerce.common.money.Money)9 ProductBundle (org.broadleafcommerce.core.catalog.domain.ProductBundle)9 CategoryProductXref (org.broadleafcommerce.core.catalog.domain.CategoryProductXref)8 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)7 CategoryImpl (org.broadleafcommerce.core.catalog.domain.CategoryImpl)7 CategoryProductXrefImpl (org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 RelatedProduct (org.broadleafcommerce.core.catalog.domain.RelatedProduct)6 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)6