Search in sources :

Example 6 with CategoryXref

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

the class CategoryCustomPersistenceHandler method update.

@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        entity = validateParentCategory(entity, false);
        if (entity.isValidationFailure()) {
            return entity;
        } else {
            PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
            Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Category.class.getName(), persistencePerspective);
            Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
            Category adminInstance = (Category) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
            CategoryXref oldDefault = getCurrentDefaultXref(adminInstance);
            adminInstance = (Category) 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;
            }
            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) Category(org.broadleafcommerce.core.catalog.domain.Category) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) CategoryXref(org.broadleafcommerce.core.catalog.domain.CategoryXref) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 7 with CategoryXref

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

the class CategoryCustomPersistenceHandler method getCurrentDefaultXref.

protected CategoryXref getCurrentDefaultXref(Category category) {
    CategoryXref currentDefault = null;
    List<CategoryXref> xrefs = category.getAllParentCategoryXrefs();
    if (!CollectionUtils.isEmpty(xrefs)) {
        for (CategoryXref xref : xrefs) {
            if (xref.getCategory().isActive() && xref.getDefaultReference() != null && xref.getDefaultReference()) {
                currentDefault = xref;
                break;
            }
        }
    }
    return currentDefault;
}
Also used : CategoryXref(org.broadleafcommerce.core.catalog.domain.CategoryXref)

Example 8 with CategoryXref

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

the class ChildCategoriesCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    final Tuple<Category, Category> parentAndChild = getChildAndParentCategories(persistencePackage, dynamicEntityDao);
    final Category parent = parentAndChild.getFirst();
    final Category child = parentAndChild.getSecond();
    final CategoryXref categoryXref = createXref(parentAndChild);
    if (parent.getAllChildCategoryXrefs().contains(categoryXref)) {
        throw new ServiceException(BLCMessageUtils.getMessage(ADMIN_CANT_ADD_DUPLICATE_CHILD));
    } else if (Objects.equals(child.getId(), parent.getId())) {
        throw new ServiceException(BLCMessageUtils.getMessage(ADMIN_CANT_ADD_CATEGORY_AS_OWN_PARENT));
    } else if (isChildAlreadyAnAncestor(child, parent)) {
        throw new ServiceException(BLCMessageUtils.getMessage(ADMIN_CANT_ADD_ANCESTOR_AS_CHILD));
    }
    return helper.getCompatibleModule(OperationType.ADORNEDTARGETLIST).add(persistencePackage);
}
Also used : Category(org.broadleafcommerce.core.catalog.domain.Category) ServiceException(org.broadleafcommerce.common.exception.ServiceException) CategoryXref(org.broadleafcommerce.core.catalog.domain.CategoryXref)

Example 9 with CategoryXref

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

the class CatalogTest method testCatalog.

@Test(groups = { "testCatalog" })
@Transactional
public void testCatalog() throws Exception {
    Category category = new CategoryImpl();
    category.setName("Soaps");
    category = catalogService.saveCategory(category);
    Category category2 = new CategoryImpl();
    category2.setName("Towels");
    category2 = catalogService.saveCategory(category2);
    Category category3 = new CategoryImpl();
    category3.setName("SuperCategory");
    category3 = catalogService.saveCategory(category3);
    CategoryXref temp = new CategoryXrefImpl();
    temp.setCategory(category);
    temp.setSubCategory(category3);
    category3.getAllParentCategoryXrefs().add(temp);
    category3 = catalogService.saveCategory(category3);
    // Test category hierarchy
    Long cat3Id = category3.getId();
    category3 = null;
    category3 = catalogService.findCategoryById(cat3Id);
    category3.getAllParentCategoryXrefs().clear();
    CategoryXref temp2 = new CategoryXrefImpl();
    temp2.setCategory(category);
    temp2.setSubCategory(category3);
    category3.getAllParentCategoryXrefs().add(temp2);
    CategoryXref temp3 = new CategoryXrefImpl();
    temp3.setCategory(category2);
    temp3.setSubCategory(category3);
    category3.getAllParentCategoryXrefs().add(temp3);
    category3 = catalogService.saveCategory(category3);
    assert category3.getAllParentCategoryXrefs().size() == 2;
    Product newProduct = new ProductImpl();
    Sku newDefaultSku = new SkuImpl();
    newDefaultSku = catalogService.saveSku(newDefaultSku);
    newProduct.setDefaultSku(newDefaultSku);
    newProduct.setName("Lavender Soap");
    Calendar activeStartCal = Calendar.getInstance();
    activeStartCal.add(Calendar.DAY_OF_YEAR, -2);
    newProduct.setActiveStartDate(activeStartCal.getTime());
    // newProduct.setAllParentCategories(allParentCategories);
    newProduct.setDefaultCategory(category);
    newProduct.getAllParentCategoryXrefs().clear();
    newProduct = catalogService.saveProduct(newProduct);
    CategoryProductXref categoryXref = new CategoryProductXrefImpl();
    categoryXref.setProduct(newProduct);
    categoryXref.setCategory(category);
    newProduct.getAllParentCategoryXrefs().add(categoryXref);
    CategoryProductXref categoryXref2 = new CategoryProductXrefImpl();
    categoryXref2.setProduct(newProduct);
    categoryXref2.setCategory(category2);
    newProduct.getAllParentCategoryXrefs().add(categoryXref2);
    newProduct = catalogService.saveProduct(newProduct);
    Long newProductId = newProduct.getId();
    Product testProduct = catalogService.findProductById(newProductId);
    assert testProduct.getId().equals(testProduct.getId());
    Category testCategory = catalogService.findCategoryByName("Soaps");
    assert testCategory.getId().equals(category.getId());
    testCategory = catalogService.findCategoryById(category.getId());
    assert testCategory.getId().equals(category.getId());
    Media media = new MediaImpl();
    media.setAltText("test");
    media.setTitle("large");
    media.setUrl("http://myUrl");
    category.getCategoryMediaXref().put("large", new CategoryMediaXrefImpl(category, media, "large"));
    catalogService.saveCategory(testCategory);
    testCategory = catalogService.findCategoryById(category.getId());
    assert (testCategory.getCategoryMediaXref().get("large") != null);
    List<Category> categories = catalogService.findAllCategories();
    assert categories != null && categories.size() == 3;
    List<Product> products = catalogService.findAllProducts();
    boolean foundProduct = false;
    for (Product product : products) {
        if (product.getId().equals(newProductId)) {
            foundProduct = true;
        }
    }
    assert foundProduct == true;
    products = catalogService.findProductsByName(newProduct.getName());
    foundProduct = false;
    for (Product product : products) {
        if (product.getId().equals(newProductId)) {
            foundProduct = true;
        }
    }
    assert foundProduct == true;
    Sku newSku = new SkuImpl();
    newSku.setName("Under Armor T-Shirt -- Red");
    newSku.setRetailPrice(new Money(14.99));
    newSku.setActiveStartDate(activeStartCal.getTime());
    newSku = catalogService.saveSku(newSku);
    List<Sku> allSkus = new ArrayList<>();
    allSkus.add(newSku);
    newProduct.setAdditionalSkus(allSkus);
    newProduct = catalogService.saveProduct(newProduct);
    Long skuId = newProduct.getSkus().get(0).getId();
    Sku testSku = catalogService.findSkuById(skuId);
    assert testSku.getId().equals(skuId);
    List<Sku> testSkus = catalogService.findAllSkus();
    boolean foundSku = false;
    for (Sku sku : testSkus) {
        if (sku.getId().equals(skuId)) {
            foundSku = true;
        }
    }
    assert foundSku == true;
    List<Long> skuIds = new ArrayList<>();
    skuIds.add(skuId);
    testSkus = catalogService.findSkusByIds(skuIds);
    foundSku = false;
    for (Sku sku : testSkus) {
        if (sku.getId().equals(skuId)) {
            foundSku = true;
        }
    }
    assert foundSku == true;
}
Also used : CategoryXrefImpl(org.broadleafcommerce.core.catalog.domain.CategoryXrefImpl) Category(org.broadleafcommerce.core.catalog.domain.Category) CategoryProductXref(org.broadleafcommerce.core.catalog.domain.CategoryProductXref) MediaImpl(org.broadleafcommerce.common.media.domain.MediaImpl) Calendar(java.util.Calendar) Media(org.broadleafcommerce.common.media.domain.Media) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) CategoryMediaXrefImpl(org.broadleafcommerce.core.catalog.domain.CategoryMediaXrefImpl) CategoryXref(org.broadleafcommerce.core.catalog.domain.CategoryXref) CategoryImpl(org.broadleafcommerce.core.catalog.domain.CategoryImpl) Money(org.broadleafcommerce.common.money.Money) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) CategoryProductXrefImpl(org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with CategoryXref

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

the class CategoriesProcessor method populateModelVariables.

@Override
public Map<String, Object> populateModelVariables(String tagName, Map<String, String> tagAttributes, BroadleafTemplateContext context) {
    String resultVar = tagAttributes.get("resultVar");
    String parentCategory = tagAttributes.get("parentCategory");
    String unparsedMaxResults = tagAttributes.get("maxResults");
    Map<String, Object> newModelVars = new HashMap<>();
    if (extensionManager != null) {
        ExtensionResultHolder holder = new ExtensionResultHolder();
        ExtensionResultStatusType result = extensionManager.getProxy().findAllPossibleChildCategories(parentCategory, unparsedMaxResults, holder);
        if (ExtensionResultStatusType.HANDLED.equals(result)) {
            newModelVars.put(resultVar, holder.getResult());
            return newModelVars;
        }
    }
    // TODO: Potentially write an algorithm that will pick the minimum depth category
    // instead of the first category in the list
    List<Category> categories = catalogService.findCategoriesByName(parentCategory);
    if (categories != null && categories.size() > 0) {
        // gets child categories in order ONLY if they are in the xref table and active
        List<CategoryXref> subcategories = categories.get(0).getChildCategoryXrefs();
        List<Category> results = Collections.emptyList();
        if (subcategories != null && !subcategories.isEmpty()) {
            results = new ArrayList<>(subcategories.size());
            if (StringUtils.isNotEmpty(unparsedMaxResults)) {
                int maxResults = Integer.parseInt(unparsedMaxResults);
                if (subcategories.size() > maxResults) {
                    subcategories = subcategories.subList(0, maxResults);
                }
            }
            for (CategoryXref xref : subcategories) {
                results.add(xref.getSubCategory());
            }
        }
        newModelVars.put(resultVar, results);
    }
    return newModelVars;
}
Also used : Category(org.broadleafcommerce.core.catalog.domain.Category) HashMap(java.util.HashMap) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) CategoryXref(org.broadleafcommerce.core.catalog.domain.CategoryXref) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Aggregations

CategoryXref (org.broadleafcommerce.core.catalog.domain.CategoryXref)10 Category (org.broadleafcommerce.core.catalog.domain.Category)6 CategoryXrefImpl (org.broadleafcommerce.core.catalog.domain.CategoryXrefImpl)3 ServiceException (org.broadleafcommerce.common.exception.ServiceException)2 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)2 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 Query (javax.persistence.Query)1 TypedQuery (javax.persistence.TypedQuery)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 Media (org.broadleafcommerce.common.media.domain.Media)1 MediaImpl (org.broadleafcommerce.common.media.domain.MediaImpl)1 Money (org.broadleafcommerce.common.money.Money)1 CategoryImpl (org.broadleafcommerce.core.catalog.domain.CategoryImpl)1 CategoryMediaXrefImpl (org.broadleafcommerce.core.catalog.domain.CategoryMediaXrefImpl)1 CategoryProductXref (org.broadleafcommerce.core.catalog.domain.CategoryProductXref)1 CategoryProductXrefImpl (org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl)1 Product (org.broadleafcommerce.core.catalog.domain.Product)1 ProductImpl (org.broadleafcommerce.core.catalog.domain.ProductImpl)1