Search in sources :

Example 6 with FieldGroup

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

Example 7 with FieldGroup

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

the class PageTemplateCustomPersistenceHandler method getFieldGroups.

protected List<FieldGroup> getFieldGroups(Page page, PageTemplate template) {
    List<PageTemplateFieldGroupXref> fieldGroupXrefs = null;
    List<FieldGroup> fieldGroups = new ArrayList<FieldGroup>();
    if (template != null) {
        fieldGroupXrefs = template.getFieldGroupXrefs();
    }
    if (page.getPageTemplate() != null) {
        fieldGroupXrefs = page.getPageTemplate().getFieldGroupXrefs();
    }
    if (fieldGroupXrefs != null) {
        for (PageTemplateFieldGroupXref xref : fieldGroupXrefs) {
            fieldGroups.add(xref.getFieldGroup());
        }
    }
    return fieldGroups;
}
Also used : PageTemplateFieldGroupXref(org.broadleafcommerce.cms.page.domain.PageTemplateFieldGroupXref) FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) ArrayList(java.util.ArrayList)

Example 8 with FieldGroup

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

the class PageTemplateCustomPersistenceHandler method processFieldGroups.

protected void processFieldGroups(List<String> dirtyFields, Map<String, PageField> pageFieldMap, List<Property> propertiesList, List<FieldGroup> fieldGroups) {
    for (FieldGroup fieldGroup : fieldGroups) {
        for (FieldDefinition def : fieldGroup.getFieldDefinitions()) {
            Property property = new Property();
            propertiesList.add(property);
            property.setName(def.getName());
            String value = null;
            if (!MapUtils.isEmpty(pageFieldMap)) {
                PageField pageField = pageFieldMap.get(def.getName());
                if (pageField == null) {
                    value = "";
                } else {
                    value = pageField.getValue();
                }
            }
            property.setValue(value);
            if (!CollectionUtils.isEmpty(dirtyFields) && dirtyFields.contains(property.getName())) {
                property.setIsDirty(true);
            }
            if (StringUtils.isNotBlank(def.getAdditionalForeignKeyClass())) {
                property.getMetadata().setOwningClass(def.getAdditionalForeignKeyClass());
            }
        }
    }
}
Also used : PageField(org.broadleafcommerce.cms.page.domain.PageField) FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) FieldDefinition(org.broadleafcommerce.cms.field.domain.FieldDefinition) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 9 with FieldGroup

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

the class DynamicFieldPersistenceHandlerHelper method buildDynamicPropertyList.

/**
 * Builds all of the metadata for all of the dynamic properties within a {@link StructuredContentType}, gleaned from
 * the {@link FieldGroup}s and {@link FieldDefinition}s.
 *
 * @param fieldGroups groups that the {@link Property}s are built from
 * @param inheritedType the value that each built {@link FieldMetadata} for each property will use to notate where the
 * dynamic field actually came from (meaning {@link FieldMetadata#setAvailableToTypes(String[])} and {@link FieldMetadata#setInheritedFromType(String)}
 * @return
 */
public Property[] buildDynamicPropertyList(List<FieldGroup> fieldGroups, Class<?> inheritedType) {
    List<Property> propertiesList = new ArrayList<Property>();
    int groupCount = 1;
    int fieldCount = 0;
    for (FieldGroup group : fieldGroups) {
        List<FieldDefinition> definitions = group.getFieldDefinitions();
        for (FieldDefinition def : definitions) {
            Property property = buildDynamicProperty(def, inheritedType);
            BasicFieldMetadata fieldMetadata = (BasicFieldMetadata) property.getMetadata();
            fieldMetadata.setOrder(fieldCount++);
            fieldMetadata.setGroup(group.getName());
            fieldMetadata.setGroupOrder(groupCount);
            fieldMetadata.setGroupCollapsed(group.getInitCollapsedFlag());
            propertiesList.add(property);
        }
        groupCount++;
        fieldCount = 0;
    }
    Property property = new Property();
    property.setName("id");
    BasicFieldMetadata fieldMetadata = new BasicFieldMetadata();
    property.setMetadata(fieldMetadata);
    fieldMetadata.setFieldType(SupportedFieldType.ID);
    fieldMetadata.setSecondaryType(SupportedFieldType.INTEGER);
    fieldMetadata.setMutable(true);
    fieldMetadata.setInheritedFromType(inheritedType.getName());
    fieldMetadata.setAvailableToTypes(new String[] { inheritedType.getName() });
    fieldMetadata.setForeignKeyCollection(false);
    fieldMetadata.setMergedPropertyType(MergedPropertyType.PRIMARY);
    fieldMetadata.setName("id");
    fieldMetadata.setFriendlyName("ID");
    fieldMetadata.setSecurityLevel("");
    fieldMetadata.setVisibility(VisibilityEnum.HIDDEN_ALL);
    fieldMetadata.setExplicitFieldType(SupportedFieldType.UNKNOWN);
    fieldMetadata.setLargeEntry(false);
    fieldMetadata.setProminent(false);
    fieldMetadata.setColumnWidth("*");
    fieldMetadata.setBroadleafEnumeration("");
    fieldMetadata.setReadOnly(true);
    propertiesList.add(property);
    Property[] properties = new Property[propertiesList.size()];
    properties = propertiesList.toArray(properties);
    Arrays.sort(properties, new Comparator<Property>() {

        @Override
        public int compare(Property o1, Property o2) {
            /*
                     * First, compare properties based on order fields
                     */
            if (o1.getMetadata().getOrder() != null && o2.getMetadata().getOrder() != null) {
                return o1.getMetadata().getOrder().compareTo(o2.getMetadata().getOrder());
            } else if (o1.getMetadata().getOrder() != null && o2.getMetadata().getOrder() == null) {
                /*
                          * Always favor fields that have an order identified
                          */
                return -1;
            } else if (o1.getMetadata().getOrder() == null && o2.getMetadata().getOrder() != null) {
                /*
                          * Always favor fields that have an order identified
                          */
                return 1;
            } else if (o1.getMetadata().getFriendlyName() != null && o2.getMetadata().getFriendlyName() != null) {
                return o1.getMetadata().getFriendlyName().compareTo(o2.getMetadata().getFriendlyName());
            } else {
                return o1.getName().compareTo(o2.getName());
            }
        }
    });
    return properties;
}
Also used : FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FieldDefinition(org.broadleafcommerce.cms.field.domain.FieldDefinition) ArrayList(java.util.ArrayList) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 10 with FieldGroup

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

the class PageTemplateImpl method setFieldGroups.

@Override
@Deprecated
public void setFieldGroups(List<FieldGroup> fieldGroups) {
    this.legacyFieldGroups.clear();
    this.fieldGroups.clear();
    for (FieldGroup group : fieldGroups) {
        this.fieldGroups.add(new PageTemplateFieldGroupXrefImpl(this, group));
    }
}
Also used : FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) ClonePolicyCollectionOverride(org.broadleafcommerce.common.extensibility.jpa.clone.ClonePolicyCollectionOverride)

Aggregations

FieldGroup (org.broadleafcommerce.cms.field.domain.FieldGroup)10 Property (org.broadleafcommerce.openadmin.dto.Property)7 ArrayList (java.util.ArrayList)6 FieldDefinition (org.broadleafcommerce.cms.field.domain.FieldDefinition)6 Entity (org.broadleafcommerce.openadmin.dto.Entity)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 ServiceException (org.broadleafcommerce.common.exception.ServiceException)3 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)3 HashMap (java.util.HashMap)2 Page (org.broadleafcommerce.cms.page.domain.Page)2 PageTemplateImpl (org.broadleafcommerce.cms.page.domain.PageTemplateImpl)2 StructuredContent (org.broadleafcommerce.cms.structure.domain.StructuredContent)2 StructuredContentType (org.broadleafcommerce.cms.structure.domain.StructuredContentType)2 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)2 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)2 BigDecimal (java.math.BigDecimal)1 Map (java.util.Map)1 PageFieldImpl (org.broadleafcommerce.cms.page.domain.PageFieldImpl)1