Search in sources :

Example 31 with FieldMetadata

use of org.broadleafcommerce.openadmin.dto.FieldMetadata 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 32 with FieldMetadata

use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.

the class ProductOptionsCustomPersistenceHandler 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(ProductOption.class.getName(), persistencePerspective);
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        ProductOption adminInstance = (ProductOption) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
        adminInstance = (ProductOption) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        // validate "Use in Sku generation"
        if (needsAllowedValue(adminInstance)) {
            String errorMessage = "Must add at least 1 Allowed Value when Product Option is used in Sku generation";
            entity.addValidationError("useInSkuGeneration", errorMessage);
            return entity;
        }
        adminInstance = (ProductOption) dynamicEntityDao.merge(adminInstance);
        return helper.getRecord(adminProperties, adminInstance, null, null);
    } catch (Exception e) {
        throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
    }
}
Also used : ProductOption(org.broadleafcommerce.core.catalog.domain.ProductOption) Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 33 with FieldMetadata

use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.

the class SkuBundleItemCustomPersistenceHandler method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
        // sort it
        if (foreignKey != null && foreignKey.getSortField() != null) {
            FilterAndSortCriteria sortCriteria = cto.get(foreignKey.getSortField());
            sortCriteria.setSortAscending(foreignKey.getSortAscending());
        }
        // get the default properties from Inventory and its subclasses
        Map<String, FieldMetadata> originalProps = helper.getSimpleMergedProperties(SkuBundleItem.class.getName(), persistencePerspective);
        // Pull back the Inventory based on the criteria from the client
        List<FilterMapping> filterMappings = helper.getFilterMappings(persistencePerspective, cto, ceilingEntityFullyQualifiedClassname, originalProps);
        // attach the product option criteria
        skuPersistenceHandler.applyProductOptionValueCriteria(filterMappings, cto, persistencePackage, "sku");
        List<Serializable> records = helper.getPersistentRecords(persistencePackage.getCeilingEntityFullyQualifiedClassname(), filterMappings, cto.getFirstResult(), cto.getMaxResults());
        // Convert Skus into the client-side Entity representation
        Entity[] payload = helper.getRecords(originalProps, records);
        int totalRecords = helper.getTotalRecords(persistencePackage.getCeilingEntityFullyQualifiedClassname(), filterMappings);
        for (int i = 0; i < payload.length; i++) {
            Entity entity = payload[i];
            SkuBundleItem bundleItem = (SkuBundleItem) records.get(i);
            Sku bundleSku = bundleItem.getSku();
            entity.findProperty("sku").setDisplayValue(bundleSku.getName());
            List<ProductOptionValue> productOptionValues = new ArrayList<>();
            for (SkuProductOptionValueXref skuProductOptionValueXref : bundleSku.getProductOptionValueXrefs()) {
                productOptionValues.add(skuProductOptionValueXref.getProductOptionValue());
            }
            Property optionsProperty = skuPersistenceHandler.getConsolidatedOptionProperty(productOptionValues);
            entity.addProperty(optionsProperty);
        }
        return new DynamicResultSet(null, payload, totalRecords);
    } catch (Exception e) {
        throw new ServiceException("There was a problem fetching inventory. See server logs for more details", e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Serializable(java.io.Serializable) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) ArrayList(java.util.ArrayList) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) SkuProductOptionValueXref(org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXref) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SkuBundleItem(org.broadleafcommerce.core.catalog.domain.SkuBundleItem) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Property(org.broadleafcommerce.openadmin.dto.Property) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 34 with FieldMetadata

use of org.broadleafcommerce.openadmin.dto.FieldMetadata 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 35 with FieldMetadata

use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.

the class StructuredContentTypeCustomPersistenceHandler method addOrUpdate.

protected Entity addOrUpdate(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        String structuredContentId = persistencePackage.getCustomCriteria()[1];
        StructuredContent structuredContent = structuredContentService.findStructuredContentById(Long.valueOf(structuredContentId));
        Property[] properties = dynamicFieldUtil.buildDynamicPropertyList(structuredContent.getStructuredContentType().getStructuredContentFieldTemplate().getFieldGroups(), StructuredContentType.class);
        Map<String, FieldMetadata> md = new HashMap<String, FieldMetadata>();
        for (Property property : properties) {
            md.put(property.getName(), property.getMetadata());
        }
        boolean validated = helper.validate(persistencePackage.getEntity(), null, md);
        if (!validated) {
            throw new ValidationException(persistencePackage.getEntity(), "Structured Content dynamic fields failed validation");
        }
        List<String> templateFieldNames = new ArrayList<String>(20);
        for (FieldGroup group : structuredContent.getStructuredContentType().getStructuredContentFieldTemplate().getFieldGroups()) {
            for (FieldDefinition def : group.getFieldDefinitions()) {
                templateFieldNames.add(def.getName());
            }
        }
        Map<String, String> dirtyFieldsOrigVals = new HashMap<String, String>();
        List<String> dirtyFields = new ArrayList<String>();
        Map<String, StructuredContentFieldXref> structuredContentFieldMap = structuredContent.getStructuredContentFieldXrefs();
        for (Property property : persistencePackage.getEntity().getProperties()) {
            if (property.getEnabled() && templateFieldNames.contains(property.getName())) {
                StructuredContentFieldXref scXref = structuredContentFieldMap.get(property.getName());
                if (scXref != null && scXref.getStructuredContentField() != null) {
                    StructuredContentField structuredContentField = scXref.getStructuredContentField();
                    boolean isDirty = (structuredContentField.getValue() == null && property.getValue() != null) || (structuredContentField.getValue() != null && property.getValue() == null);
                    if (isDirty || (structuredContentField.getValue() != null && property.getValue() != null && !structuredContentField.getValue().trim().equals(property.getValue().trim()))) {
                        dirtyFields.add(property.getName());
                        dirtyFieldsOrigVals.put(property.getName(), structuredContentField.getValue());
                        structuredContentField.setValue(property.getValue());
                        scXref = dynamicEntityDao.merge(scXref);
                    }
                } else {
                    StructuredContentField structuredContentField = new StructuredContentFieldImpl();
                    structuredContentField.setFieldKey(property.getName());
                    structuredContentField.setValue(property.getValue());
                    StructuredContentFieldXref scfx = new StructuredContentFieldXrefImpl();
                    scfx.setStructuredContent(structuredContent);
                    scfx.setKey(property.getName());
                    scfx.setStrucuturedContentField(structuredContentField);
                    scfx = dynamicEntityDao.persist(scfx);
                    dirtyFields.add(property.getName());
                }
            }
        }
        List<String> removeItems = new ArrayList<String>();
        for (String key : structuredContentFieldMap.keySet()) {
            if (persistencePackage.getEntity().findProperty(key) == null) {
                removeItems.add(key);
            }
        }
        if (removeItems.size() > 0) {
            for (String removeKey : removeItems) {
                structuredContentFieldMap.remove(removeKey);
            }
        }
        Collections.sort(dirtyFields);
        Entity entity = fetchEntityBasedOnId(structuredContentId, dirtyFields);
        for (Entry<String, String> entry : dirtyFieldsOrigVals.entrySet()) {
            entity.getPMap().get(entry.getKey()).setOriginalValue(entry.getValue());
            entity.getPMap().get(entry.getKey()).setOriginalDisplayValue(entry.getValue());
        }
        return entity;
    } catch (ValidationException e) {
        throw e;
    } catch (Exception e) {
        throw new ServiceException("Unable to perform fetch for entity: " + ceilingEntityFullyQualifiedClassname, e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) StructuredContentFieldXrefImpl(org.broadleafcommerce.cms.structure.domain.StructuredContentFieldXrefImpl) HashMap(java.util.HashMap) FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) FieldDefinition(org.broadleafcommerce.cms.field.domain.FieldDefinition) ArrayList(java.util.ArrayList) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) StructuredContent(org.broadleafcommerce.cms.structure.domain.StructuredContent) ServiceException(org.broadleafcommerce.common.exception.ServiceException) StructuredContentField(org.broadleafcommerce.cms.structure.domain.StructuredContentField) StructuredContentFieldXref(org.broadleafcommerce.cms.structure.domain.StructuredContentFieldXref) Property(org.broadleafcommerce.openadmin.dto.Property) StructuredContentFieldImpl(org.broadleafcommerce.cms.structure.domain.StructuredContentFieldImpl)

Aggregations

FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)114 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)80 Entity (org.broadleafcommerce.openadmin.dto.Entity)51 ServiceException (org.broadleafcommerce.common.exception.ServiceException)46 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)44 HashMap (java.util.HashMap)39 Property (org.broadleafcommerce.openadmin.dto.Property)38 Map (java.util.Map)28 ArrayList (java.util.ArrayList)23 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)22 Field (java.lang.reflect.Field)19 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)19 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)19 Serializable (java.io.Serializable)18 InvocationTargetException (java.lang.reflect.InvocationTargetException)18 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)18 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)17 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)15 FieldMetadataOverride (org.broadleafcommerce.openadmin.dto.override.FieldMetadataOverride)15 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)13