Search in sources :

Example 16 with Product

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

the class ProductDataProvider method createProducts.

@DataProvider(name = "setupProducts")
public static Object[][] createProducts() {
    Product p1 = getProduct(null);
    Product p2 = getProduct(null);
    Product p3 = getProduct(null);
    Product p4 = getProduct(null);
    Product p5 = getProduct(null);
    Product p6 = getProduct(null);
    Product p7 = getProduct(null);
    Object[][] objs = new Object[7][1];
    objs[0] = new Object[] { p1 };
    objs[1] = new Object[] { p2 };
    objs[2] = new Object[] { p3 };
    objs[3] = new Object[] { p4 };
    objs[4] = new Object[] { p5 };
    objs[5] = new Object[] { p6 };
    objs[6] = new Object[] { p7 };
    return objs;
}
Also used : Product(org.broadleafcommerce.core.catalog.domain.Product) DataProvider(org.testng.annotations.DataProvider)

Example 17 with Product

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

the class ProductCustomPersistenceHandler method getAdminInstance.

protected Product getAdminInstance(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper, Entity entity) throws ClassNotFoundException {
    PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
    Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Product.class.getName(), persistencePerspective);
    Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
    String type = entity.getType()[0];
    Product adminInstance = (Product) dynamicEntityDao.retrieve(Class.forName(type), primaryKey);
    return adminInstance;
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) Product(org.broadleafcommerce.core.catalog.domain.Product) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)

Example 18 with Product

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

the class ProductCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Product adminInstance = (Product) Class.forName(entity.getType()[0]).newInstance();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Product.class.getName(), persistencePerspective);
        if (adminInstance instanceof ProductBundle) {
            removeBundleFieldRestrictions((ProductBundle) adminInstance, adminProperties, entity);
        }
        adminInstance = (Product) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        adminInstance = dynamicEntityDao.merge(adminInstance);
        // any Sku fields, and thus a Sku would not be created. Product still needs a default Sku so instantiate one
        if (adminInstance.getDefaultSku() == null) {
            Sku newSku = catalogService.createSku();
            dynamicEntityDao.persist(newSku);
            adminInstance.setDefaultSku(newSku);
            adminInstance = dynamicEntityDao.merge(adminInstance);
        }
        // also set the default product for the Sku
        adminInstance.getDefaultSku().setDefaultProduct(adminInstance);
        dynamicEntityDao.merge(adminInstance.getDefaultSku());
        // if this is a Pre-Add, skip the rest of the method
        if (entity.isPreAdd()) {
            return helper.getRecord(adminProperties, adminInstance, null, null);
        }
        boolean handled = false;
        if (extensionManager != null) {
            ExtensionResultStatusType result = extensionManager.getProxy().manageParentCategoryForAdd(persistencePackage, adminInstance);
            handled = ExtensionResultStatusType.NOT_HANDLED != result;
        }
        if (!handled) {
            setupXref(adminInstance);
        }
        return helper.getRecord(adminProperties, adminInstance, null, null);
    } catch (Exception e) {
        throw new ServiceException("Unable to add 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) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ProductBundle(org.broadleafcommerce.core.catalog.domain.ProductBundle) Product(org.broadleafcommerce.core.catalog.domain.Product) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) Sku(org.broadleafcommerce.core.catalog.domain.Sku) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 19 with Product

use of org.broadleafcommerce.core.catalog.domain.Product 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;
    }
}
Also used : ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) HashMap(java.util.HashMap) Product(org.broadleafcommerce.core.catalog.domain.Product) MergedPropertyType(org.broadleafcommerce.openadmin.dto.MergedPropertyType) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Map(java.util.Map) HashMap(java.util.HashMap) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 20 with Product

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

the class ProductParentCategoryFieldPersistenceProvider method populateValue.

@Override
public MetadataProviderResponse populateValue(PopulateValueRequest populateValueRequest, Serializable instance) {
    if (!canHandlePersistence(populateValueRequest, instance)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    boolean handled = false;
    if (extensionManager != null) {
        ExtensionResultStatusType result = extensionManager.getProxy().manageParentCategory(populateValueRequest.getProperty(), (Product) instance);
        handled = ExtensionResultStatusType.NOT_HANDLED != result;
    }
    if (!handled || BroadleafRequestContext.getBroadleafRequestContext().isProductionSandBox()) {
        Long requestedValue = null;
        if (StringUtils.isNotEmpty(populateValueRequest.getRequestedValue())) {
            requestedValue = Long.parseLong(populateValueRequest.getRequestedValue());
        }
        boolean dirty = checkDirtyState((Product) instance, requestedValue);
        if (dirty) {
            populateValueRequest.getProperty().setIsDirty(true);
            if (requestedValue != null) {
                ((Product) instance).setCategory((Category) populateValueRequest.getPersistenceManager().getDynamicEntityDao().find(CategoryImpl.class, requestedValue));
            } else {
                ((Product) instance).setCategory(null);
            }
        }
    }
    return MetadataProviderResponse.HANDLED_BREAK;
}
Also used : Product(org.broadleafcommerce.core.catalog.domain.Product) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType)

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