Search in sources :

Example 26 with Category

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

the class ProductCustomPersistenceHandler method getExistingDefaultCategory.

protected Category getExistingDefaultCategory(Product product) {
    // Make sure we get the actual field value - not something manipulated in the getter
    Category parentCategory;
    try {
        Field defaultCategory = ProductImpl.class.getDeclaredField("defaultCategory");
        defaultCategory.setAccessible(true);
        parentCategory = (Category) defaultCategory.get(product);
    } catch (NoSuchFieldException e) {
        throw ExceptionHelper.refineException(e);
    } catch (IllegalAccessException e) {
        throw ExceptionHelper.refineException(e);
    }
    return parentCategory;
}
Also used : Field(java.lang.reflect.Field) Category(org.broadleafcommerce.core.catalog.domain.Category)

Example 27 with Category

use of org.broadleafcommerce.core.catalog.domain.Category 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 28 with Category

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

the class ChildCategoriesCustomPersistenceHandler method getChildAndParentCategories.

protected Tuple<Category, Category> getChildAndParentCategories(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao) {
    AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePackage.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
    final String targetPath = adornedTargetList.getTargetObjectPath() + "." + adornedTargetList.getTargetIdProperty();
    final String linkedPath = adornedTargetList.getLinkedObjectPath() + "." + adornedTargetList.getLinkedIdProperty();
    Long parentId = Long.parseLong(persistencePackage.getEntity().findProperty(linkedPath).getValue());
    Long childId = Long.parseLong(persistencePackage.getEntity().findProperty(targetPath).getValue());
    Category parent = (Category) dynamicEntityDao.retrieve(CategoryImpl.class, parentId);
    Category child = (Category) dynamicEntityDao.retrieve(CategoryImpl.class, childId);
    return new Tuple<>(parent, child);
}
Also used : CategoryImpl(org.broadleafcommerce.core.catalog.domain.CategoryImpl) Category(org.broadleafcommerce.core.catalog.domain.Category) AdornedTargetList(org.broadleafcommerce.openadmin.dto.AdornedTargetList) Tuple(org.broadleafcommerce.common.util.Tuple)

Example 29 with Category

use of org.broadleafcommerce.core.catalog.domain.Category 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 30 with Category

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

the class CategoryParentCategoryFieldPersistenceProvider method extractValue.

@Override
public MetadataProviderResponse extractValue(ExtractValueRequest extractValueRequest, Property property) {
    if (!canHandleExtraction(extractValueRequest, property)) {
        return MetadataProviderResponse.NOT_HANDLED;
    }
    Category category = getDefaultCategory((Category) extractValueRequest.getEntity());
    if (category != null) {
        property.setValue(String.valueOf(category.getId()));
        property.setDisplayValue(category.getName());
    }
    return MetadataProviderResponse.HANDLED_BREAK;
}
Also used : Category(org.broadleafcommerce.core.catalog.domain.Category)

Aggregations

Category (org.broadleafcommerce.core.catalog.domain.Category)49 Product (org.broadleafcommerce.core.catalog.domain.Product)18 Sku (org.broadleafcommerce.core.catalog.domain.Sku)13 CategoryImpl (org.broadleafcommerce.core.catalog.domain.CategoryImpl)10 Order (org.broadleafcommerce.core.order.domain.Order)8 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)7 CategoryProductXref (org.broadleafcommerce.core.catalog.domain.CategoryProductXref)7 ProductImpl (org.broadleafcommerce.core.catalog.domain.ProductImpl)7 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)7 ArrayList (java.util.ArrayList)6 CategoryProductXrefImpl (org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl)6 CategoryXref (org.broadleafcommerce.core.catalog.domain.CategoryXref)6 Money (org.broadleafcommerce.common.money.Money)5 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)5 Customer (org.broadleafcommerce.profile.core.domain.Customer)5 ProductBundle (org.broadleafcommerce.core.catalog.domain.ProductBundle)4 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)4 Calendar (java.util.Calendar)3 ServiceException (org.broadleafcommerce.common.exception.ServiceException)3 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)3