Search in sources :

Example 1 with FieldDefinition

use of org.broadleafcommerce.cms.field.domain.FieldDefinition in project BroadleafCommerce by BroadleafCommerce.

the class StructuredContentServiceImpl method buildFieldValues.

/**
 * Parses the given {@link StructuredContent} into its {@link StructuredContentDTO} representation. This will also
 * format the values from {@link StructuredContentDTO#getValues()} into their actual data types. For instance, if the
 * given {@link StructuredContent} has a DATE field, then this method will ensure that the resulting object in the values
 * map of the DTO is a {@link Date} rather than just a String representing a date.
 * <p/>
 * Current support of parsing field types is:
 * DATE - {@link Date}
 * BOOLEAN - {@link Boolean}
 * DECIMAL - {@link BigDecimal}
 * INTEGER - {@link Integer}
 * MONEY - {@link Money}
 * <p/>
 * All other fields are treated as strings. This will also fix URL strings that have the CMS prefix (like images) by
 * prepending the standard CMS prefix with the particular environment prefix
 *
 * @param sc
 * @param scDTO
 * @param secure
 * @see {@link StaticAssetService#getStaticAssetEnvironmentUrlPrefix()}
 */
protected void buildFieldValues(StructuredContent sc, StructuredContentDTO scDTO, boolean secure) {
    String cmsPrefix = staticAssetPathService.getStaticAssetUrlPrefix();
    Map<String, StructuredContentFieldXref> scFieldXrefs = MapUtils.emptyIfNull(sc.getStructuredContentFieldXrefs());
    scDTO.getValues().put("id", sc.getId());
    for (String fieldKey : scFieldXrefs.keySet()) {
        StructuredContentField scf = scFieldXrefs.get(fieldKey).getStructuredContentField();
        String originalValue = scf.getValue();
        if (hasCmsPrefix(originalValue, cmsPrefix)) {
            buildFieldValueWithCmsPrefix(originalValue, scDTO, secure, fieldKey);
        } else {
            FieldDefinition definition = null;
            StructuredContentType scType = sc.getStructuredContentType();
            StructuredContentFieldTemplate scFieldTemplate = scType.getStructuredContentFieldTemplate();
            List<FieldGroup> scFieldGroups = ListUtils.emptyIfNull(scFieldTemplate.getFieldGroups());
            Iterator<FieldGroup> groupIterator = scFieldGroups.iterator();
            while (groupIterator.hasNext() && definition == null) {
                FieldGroup group = groupIterator.next();
                for (FieldDefinition def : group.getFieldDefinitions()) {
                    if (def.getName().equals(fieldKey)) {
                        definition = def;
                        break;
                    }
                }
            }
            if (definition != null) {
                Object value = null;
                if (originalValue != null) {
                    switch(definition.getFieldType()) {
                        case DATE:
                            try {
                                value = FormatUtil.getDateFormat().parse(originalValue);
                            } catch (Exception e) {
                                value = originalValue;
                            }
                            break;
                        case BOOLEAN:
                            value = new Boolean(originalValue);
                            break;
                        case DECIMAL:
                            value = new BigDecimal(originalValue);
                            break;
                        case INTEGER:
                            value = Integer.parseInt(originalValue);
                            break;
                        case MONEY:
                            value = new Money(originalValue);
                            break;
                        case ADDITIONAL_FOREIGN_KEY:
                            value = FOREIGN_LOOKUP + '|' + definition.getAdditionalForeignKeyClass() + '|' + originalValue;
                            break;
                        default:
                            value = originalValue;
                            break;
                    }
                }
                scDTO.getValues().put(fieldKey, value);
            } else {
                scDTO.getValues().put(fieldKey, scFieldXrefs.get(fieldKey).getStructuredContentField().getValue());
            }
        }
    }
    // allow modules to contribute to the fields of the DTO
    extensionManager.getProxy().populateAdditionalStructuredContentFields(sc, scDTO, secure);
}
Also used : FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) FieldDefinition(org.broadleafcommerce.cms.field.domain.FieldDefinition) BigDecimal(java.math.BigDecimal) StructuredContentFieldTemplate(org.broadleafcommerce.cms.structure.domain.StructuredContentFieldTemplate) Money(org.broadleafcommerce.common.money.Money) StructuredContentField(org.broadleafcommerce.cms.structure.domain.StructuredContentField) StructuredContentType(org.broadleafcommerce.cms.structure.domain.StructuredContentType) StructuredContentFieldXref(org.broadleafcommerce.cms.structure.domain.StructuredContentFieldXref)

Example 2 with FieldDefinition

use of org.broadleafcommerce.cms.field.domain.FieldDefinition in project BroadleafCommerce by BroadleafCommerce.

the class PageServiceUtility method addPageFieldToDTO.

public void addPageFieldToDTO(Page page, boolean secure, PageDTO pageDTO, String fieldKey, String originalValue) {
    String cmsPrefix = staticAssetPathService.getStaticAssetUrlPrefix();
    PageField pf = page.getPageFields().get(fieldKey);
    if (originalValue == null) {
        originalValue = pf.getValue();
    }
    FieldDefinition fd = getFieldDefinition(page, fieldKey);
    if (fd != null && fd.getFieldType() == SupportedFieldType.ADDITIONAL_FOREIGN_KEY) {
        pageDTO.getPageFields().put(fieldKey, FOREIGN_LOOKUP + '|' + fd.getAdditionalForeignKeyClass() + '|' + originalValue);
    } else if (StringUtils.isNotBlank(originalValue) && StringUtils.isNotBlank(cmsPrefix) && originalValue.contains(cmsPrefix)) {
        // This may either be an ASSET_LOOKUP image path or an HTML block (with multiple <img>) or a plain STRING that contains the cmsPrefix.
        // If there is an environment prefix configured (e.g. a CDN), then we must replace the cmsPrefix with this one.
        String fldValue = staticAssetPathService.convertAllAssetPathsInContent(originalValue, secure);
        pageDTO.getPageFields().put(fieldKey, fldValue);
    } else {
        pageDTO.getPageFields().put(fieldKey, originalValue);
    }
}
Also used : PageField(org.broadleafcommerce.cms.page.domain.PageField) FieldDefinition(org.broadleafcommerce.cms.field.domain.FieldDefinition)

Example 3 with FieldDefinition

use of org.broadleafcommerce.cms.field.domain.FieldDefinition in project BroadleafCommerce by BroadleafCommerce.

the class StructuredContentTypeCustomPersistenceHandler method fetchDynamicEntity.

@Override
public Entity fetchDynamicEntity(Serializable root, List<String> dirtyFields, boolean includeId) throws Exception {
    StructuredContent structuredContent = (StructuredContent) root;
    Map<String, StructuredContentFieldXref> structuredContentFieldMap = structuredContent.getStructuredContentFieldXrefs();
    Entity entity = new Entity();
    entity.setType(new String[] { StructuredContentType.class.getName() });
    List<Property> propertiesList = new ArrayList<Property>();
    for (FieldGroup fieldGroup : structuredContent.getStructuredContentType().getStructuredContentFieldTemplate().getFieldGroups()) {
        for (FieldDefinition def : fieldGroup.getFieldDefinitions()) {
            Property property = new Property();
            property.setName(def.getName());
            String value = null;
            if (!MapUtils.isEmpty(structuredContentFieldMap)) {
                StructuredContentFieldXref structuredContentFieldXref = structuredContentFieldMap.get(def.getName());
                if (structuredContentFieldXref != null) {
                    StructuredContentField structuredContentField = structuredContentFieldXref.getStructuredContentField();
                    if (structuredContentField != null) {
                        value = structuredContentField.getValue();
                    }
                }
            }
            property.setValue(value);
            if (!CollectionUtils.isEmpty(dirtyFields) && dirtyFields.contains(property.getName())) {
                property.setIsDirty(true);
            }
            propertiesList.add(property);
        }
    }
    if (includeId) {
        Property property = new Property();
        propertiesList.add(property);
        property.setName("id");
        property.setValue(String.valueOf(structuredContent.getId()));
    }
    entity.setProperties(propertiesList.toArray(new Property[] {}));
    return entity;
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) StructuredContentField(org.broadleafcommerce.cms.structure.domain.StructuredContentField) FieldDefinition(org.broadleafcommerce.cms.field.domain.FieldDefinition) ArrayList(java.util.ArrayList) StructuredContentType(org.broadleafcommerce.cms.structure.domain.StructuredContentType) StructuredContentFieldXref(org.broadleafcommerce.cms.structure.domain.StructuredContentFieldXref) Property(org.broadleafcommerce.openadmin.dto.Property) StructuredContent(org.broadleafcommerce.cms.structure.domain.StructuredContent)

Example 4 with FieldDefinition

use of org.broadleafcommerce.cms.field.domain.FieldDefinition 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)

Example 5 with FieldDefinition

use of org.broadleafcommerce.cms.field.domain.FieldDefinition in project BroadleafCommerce by BroadleafCommerce.

the class PageTemplateCustomPersistenceHandler method addOrUpdate.

protected Entity addOrUpdate(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        String pageId = persistencePackage.getCustomCriteria()[1];
        if (StringUtils.isBlank(pageId)) {
            return persistencePackage.getEntity();
        }
        Page page = pageService.findPageById(Long.valueOf(pageId));
        Property[] properties = dynamicFieldUtil.buildDynamicPropertyList(getFieldGroups(page, null), PageTemplate.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(), "Page dynamic fields failed validation");
        }
        List<String> templateFieldNames = new ArrayList<String>(20);
        for (FieldGroup group : getFieldGroups(page, null)) {
            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, PageField> pageFieldMap = page.getPageFields();
        for (Property property : persistencePackage.getEntity().getProperties()) {
            if (property.getEnabled() && templateFieldNames.contains(property.getName())) {
                PageField pageField = pageFieldMap.get(property.getName());
                if (pageField != null) {
                    boolean isDirty = (pageField.getValue() == null && property.getValue() != null) || (pageField.getValue() != null && property.getValue() == null);
                    if (isDirty || (pageField.getValue() != null && property.getValue() != null && !pageField.getValue().trim().equals(property.getValue().trim()))) {
                        dirtyFields.add(property.getName());
                        dirtyFieldsOrigVals.put(property.getName(), pageField.getValue());
                        pageField.setValue(property.getValue());
                        pageField = dynamicEntityDao.merge(pageField);
                    }
                } else {
                    pageField = new PageFieldImpl();
                    pageField.setFieldKey(property.getName());
                    pageField.setValue(property.getValue());
                    pageField.setPage(page);
                    dynamicEntityDao.persist(pageField);
                    dirtyFields.add(property.getName());
                }
            }
        }
        List<String> removeItems = new ArrayList<String>();
        for (String key : pageFieldMap.keySet()) {
            if (persistencePackage.getEntity().findProperty(key) == null) {
                removeItems.add(key);
            }
        }
        if (removeItems.size() > 0) {
            for (String removeKey : removeItems) {
                pageFieldMap.remove(removeKey);
            }
        }
        Collections.sort(dirtyFields);
        Entity entity = fetchEntityBasedOnId(pageId, dirtyFields);
        for (Map.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 update for entity: " + ceilingEntityFullyQualifiedClassname, e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) HashMap(java.util.HashMap) FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) FieldDefinition(org.broadleafcommerce.cms.field.domain.FieldDefinition) ArrayList(java.util.ArrayList) Page(org.broadleafcommerce.cms.page.domain.Page) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) PageField(org.broadleafcommerce.cms.page.domain.PageField) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Property(org.broadleafcommerce.openadmin.dto.Property) HashMap(java.util.HashMap) Map(java.util.Map) PageFieldImpl(org.broadleafcommerce.cms.page.domain.PageFieldImpl)

Aggregations

FieldDefinition (org.broadleafcommerce.cms.field.domain.FieldDefinition)8 FieldGroup (org.broadleafcommerce.cms.field.domain.FieldGroup)6 Property (org.broadleafcommerce.openadmin.dto.Property)5 ArrayList (java.util.ArrayList)4 PageField (org.broadleafcommerce.cms.page.domain.PageField)3 StructuredContentField (org.broadleafcommerce.cms.structure.domain.StructuredContentField)3 StructuredContentFieldXref (org.broadleafcommerce.cms.structure.domain.StructuredContentFieldXref)3 Entity (org.broadleafcommerce.openadmin.dto.Entity)3 HashMap (java.util.HashMap)2 StructuredContent (org.broadleafcommerce.cms.structure.domain.StructuredContent)2 StructuredContentType (org.broadleafcommerce.cms.structure.domain.StructuredContentType)2 ServiceException (org.broadleafcommerce.common.exception.ServiceException)2 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)2 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)2 BigDecimal (java.math.BigDecimal)1 Map (java.util.Map)1 Page (org.broadleafcommerce.cms.page.domain.Page)1 PageFieldImpl (org.broadleafcommerce.cms.page.domain.PageFieldImpl)1 PageTemplateFieldGroupXref (org.broadleafcommerce.cms.page.domain.PageTemplateFieldGroupXref)1 StructuredContentFieldImpl (org.broadleafcommerce.cms.structure.domain.StructuredContentFieldImpl)1