Search in sources :

Example 1 with PageField

use of org.broadleafcommerce.cms.page.domain.PageField 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 2 with PageField

use of org.broadleafcommerce.cms.page.domain.PageField in project BroadleafCommerce by BroadleafCommerce.

the class PageDaoImpl method readPageFieldsByPageId.

@Override
public List<PageField> readPageFieldsByPageId(Long pageId) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<PageField> criteria = builder.createQuery(PageField.class);
    Root<PageFieldImpl> pageField = criteria.from(PageFieldImpl.class);
    criteria.select(pageField);
    Path<Object> path = pageField.get("page").get("id");
    criteria.where(builder.equal(pageField.get("page").get("id"), pageId));
    TypedQuery<PageField> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    return query.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) PageField(org.broadleafcommerce.cms.page.domain.PageField) PageFieldImpl(org.broadleafcommerce.cms.page.domain.PageFieldImpl)

Example 3 with PageField

use of org.broadleafcommerce.cms.page.domain.PageField in project BroadleafCommerce by BroadleafCommerce.

the class PageTemplateCustomPersistenceHandler method fetchDynamicEntity.

@Override
public Entity fetchDynamicEntity(Serializable root, List<String> dirtyFields, boolean includeId) throws Exception {
    Page page = (Page) root;
    Map<String, PageField> pageFieldMap = page.getPageFields();
    Entity entity = new Entity();
    entity.setType(new String[] { PageTemplateImpl.class.getName() });
    List<Property> propertiesList = new ArrayList<Property>();
    List<FieldGroup> fieldGroups = getFieldGroups(page, null);
    processFieldGroups(dirtyFields, pageFieldMap, propertiesList, fieldGroups);
    processIncludeId(includeId, page, propertiesList);
    entity.setProperties(propertiesList.toArray(new Property[] {}));
    return entity;
}
Also used : PageField(org.broadleafcommerce.cms.page.domain.PageField) Entity(org.broadleafcommerce.openadmin.dto.Entity) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) ArrayList(java.util.ArrayList) PageTemplateImpl(org.broadleafcommerce.cms.page.domain.PageTemplateImpl) Page(org.broadleafcommerce.cms.page.domain.Page) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 4 with PageField

use of org.broadleafcommerce.cms.page.domain.PageField 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 5 with PageField

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

Aggregations

PageField (org.broadleafcommerce.cms.page.domain.PageField)6 FieldDefinition (org.broadleafcommerce.cms.field.domain.FieldDefinition)3 FieldGroup (org.broadleafcommerce.cms.field.domain.FieldGroup)3 Property (org.broadleafcommerce.openadmin.dto.Property)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Page (org.broadleafcommerce.cms.page.domain.Page)2 PageFieldImpl (org.broadleafcommerce.cms.page.domain.PageFieldImpl)2 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)2 Entity (org.broadleafcommerce.openadmin.dto.Entity)2 Map (java.util.Map)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 PageTemplateImpl (org.broadleafcommerce.cms.page.domain.PageTemplateImpl)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)1 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)1