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;
}
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);
}
}
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);
}
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);
}
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;
}
Aggregations