Search in sources :

Example 6 with StructuredContent

use of org.broadleafcommerce.cms.structure.domain.StructuredContent 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 7 with StructuredContent

use of org.broadleafcommerce.cms.structure.domain.StructuredContent in project BroadleafCommerce by BroadleafCommerce.

the class StructuredContentServiceImpl method buildStructuredContentDTOList.

/**
 * Converts a list of structured content items to a list of structured content DTOs.<br>
 * Internally calls buildStructuredContentDTO(...).
 *
 * @param structuredContentList
 * @param secure
 * @return
 * @see {@link #buildStructuredContentDTO(StructuredContent, boolean)}
 */
@Override
public List<StructuredContentDTO> buildStructuredContentDTOList(List<StructuredContent> structuredContentList, boolean secure) {
    List<StructuredContentDTO> dtoList = new ArrayList<>();
    structuredContentList = ListUtils.emptyIfNull(structuredContentList);
    for (StructuredContent sc : structuredContentList) {
        dtoList.add(buildStructuredContentDTO(sc, secure));
    }
    return dtoList;
}
Also used : StructuredContentDTO(org.broadleafcommerce.common.structure.dto.StructuredContentDTO) ArrayList(java.util.ArrayList) StructuredContent(org.broadleafcommerce.cms.structure.domain.StructuredContent)

Example 8 with StructuredContent

use of org.broadleafcommerce.cms.structure.domain.StructuredContent in project BroadleafCommerce by BroadleafCommerce.

the class StructuredContentServiceImpl method getStructuredContentItemsByContentName.

@Override
public List<StructuredContentDTO> getStructuredContentItemsByContentName(String contentName, Locale locale, boolean secure) {
    List<StructuredContentDTO> contentDTOList = null;
    Locale languageOnlyLocale = findLanguageOnlyLocale(locale);
    BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
    Long site = (context.getNonPersistentSite() != null) ? context.getNonPersistentSite().getId() : null;
    String cacheKey = buildNameKey(context.getSandBox(), site, languageOnlyLocale, "any", contentName, secure);
    cacheKey = cacheKey + "-" + secure;
    if (context.isProductionSandBox()) {
        contentDTOList = getStructuredContentListFromCache(cacheKey);
    }
    if (contentDTOList == null) {
        List<StructuredContent> productionContentList = structuredContentDao.findActiveStructuredContentByName(contentName, locale, languageOnlyLocale);
        contentDTOList = buildStructuredContentDTOList(productionContentList, secure);
        if (context.isProductionSandBox()) {
            addStructuredContentListToCache(cacheKey, contentDTOList);
        }
    }
    return contentDTOList;
}
Also used : Locale(org.broadleafcommerce.common.locale.domain.Locale) StructuredContentDTO(org.broadleafcommerce.common.structure.dto.StructuredContentDTO) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) StructuredContent(org.broadleafcommerce.cms.structure.domain.StructuredContent)

Example 9 with StructuredContent

use of org.broadleafcommerce.cms.structure.domain.StructuredContent in project BroadleafCommerce by BroadleafCommerce.

the class StructuredContentServiceImpl method convertToDtos.

@Override
public List<StructuredContentDTO> convertToDtos(List<StructuredContent> scs, boolean isSecure) {
    List<StructuredContentDTO> contentDTOList = new ArrayList<>();
    BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
    SandBox sandbox = context.getSandBox();
    boolean isProductionSandbox = context.isProductionSandBox();
    StructuredContentDTO dto;
    for (StructuredContent sc : scs) {
        String cacheKey = buildNameKey(sandbox, sc, isSecure);
        dto = null;
        if (isProductionSandbox) {
            dto = getSingleStructuredContentFromCache(cacheKey);
        }
        if (dto == null) {
            dto = buildStructuredContentDTO(sc, isSecure);
            if (isProductionSandbox) {
                addSingleStructuredContentToCache(cacheKey, dto);
            }
        }
        contentDTOList.add(dto);
    }
    return contentDTOList;
}
Also used : SandBox(org.broadleafcommerce.common.sandbox.domain.SandBox) StructuredContentDTO(org.broadleafcommerce.common.structure.dto.StructuredContentDTO) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) ArrayList(java.util.ArrayList) StructuredContent(org.broadleafcommerce.cms.structure.domain.StructuredContent)

Aggregations

StructuredContent (org.broadleafcommerce.cms.structure.domain.StructuredContent)9 StructuredContentDTO (org.broadleafcommerce.common.structure.dto.StructuredContentDTO)5 ArrayList (java.util.ArrayList)4 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)4 Locale (org.broadleafcommerce.common.locale.domain.Locale)3 FieldDefinition (org.broadleafcommerce.cms.field.domain.FieldDefinition)2 FieldGroup (org.broadleafcommerce.cms.field.domain.FieldGroup)2 StructuredContentField (org.broadleafcommerce.cms.structure.domain.StructuredContentField)2 StructuredContentFieldXref (org.broadleafcommerce.cms.structure.domain.StructuredContentFieldXref)2 Entity (org.broadleafcommerce.openadmin.dto.Entity)2 Property (org.broadleafcommerce.openadmin.dto.Property)2 HashMap (java.util.HashMap)1 NoResultException (javax.persistence.NoResultException)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 StructuredContentFieldImpl (org.broadleafcommerce.cms.structure.domain.StructuredContentFieldImpl)1 StructuredContentFieldXrefImpl (org.broadleafcommerce.cms.structure.domain.StructuredContentFieldXrefImpl)1 StructuredContentImpl (org.broadleafcommerce.cms.structure.domain.StructuredContentImpl)1 StructuredContentType (org.broadleafcommerce.cms.structure.domain.StructuredContentType)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 SandBox (org.broadleafcommerce.common.sandbox.domain.SandBox)1