Search in sources :

Example 6 with StructuredContentDTO

use of org.broadleafcommerce.common.structure.dto.StructuredContentDTO in project BroadleafCommerce by BroadleafCommerce.

the class ContentProcessor method populateModelVariables.

@Override
public Map<String, Object> populateModelVariables(String tagName, Map<String, String> tagAttributes, BroadleafTemplateContext context) {
    String contentType = tagAttributes.get("contentType");
    String contentName = tagAttributes.get("contentName");
    String maxResultsStr = tagAttributes.get("maxResults");
    if (StringUtils.isEmpty(contentType) && StringUtils.isEmpty(contentName)) {
        throw new IllegalArgumentException("The content processor must have a non-empty attribute value for 'contentType' or 'contentName'");
    }
    Integer maxResults = null;
    if (maxResultsStr != null) {
        maxResults = Ints.tryParse(maxResultsStr);
    }
    if (maxResults == null) {
        maxResults = Integer.MAX_VALUE;
    }
    String contentListVar = getAttributeValue(tagAttributes, "contentListVar", "contentList");
    String contentItemVar = getAttributeValue(tagAttributes, "contentItemVar", "contentItem");
    String numResultsVar = getAttributeValue(tagAttributes, "numResultsVar", "numResults");
    String fieldFilters = tagAttributes.get("fieldFilters");
    final String sorts = tagAttributes.get("sorts");
    BroadleafRequestContext blcContext = BroadleafRequestContext.getBroadleafRequestContext();
    HttpServletRequest request = blcContext.getRequest();
    Map<String, Object> mvelParameters = buildMvelParameters(blcContext.getRequest(), tagAttributes, context);
    SandBox currentSandbox = blcContext.getSandBox();
    List<StructuredContentDTO> contentItems;
    StructuredContentType structuredContentType = null;
    if (contentType != null) {
        structuredContentType = structuredContentService.findStructuredContentTypeByName(contentType);
    }
    Locale locale = blcContext.getLocale();
    Map<String, Object> newModelVars = new HashMap<>();
    contentItems = getContentItems(contentName, maxResults, request, mvelParameters, currentSandbox, structuredContentType, locale, tagName, tagAttributes, newModelVars, context);
    if (contentItems.size() > 0) {
        // sort the resulting list by the configured property sorts on the tag
        if (StringUtils.isNotEmpty(sorts)) {
            final BroadleafTemplateContext finalContext = context;
            // In order to use the context in a comparator it needs to be final
            Collections.sort(contentItems, new Comparator<StructuredContentDTO>() {

                @Override
                public int compare(StructuredContentDTO o1, StructuredContentDTO o2) {
                    List<BroadleafAssignation> sortAssignments = finalContext.getAssignationSequence(sorts, false);
                    CompareToBuilder compareBuilder = new CompareToBuilder();
                    for (BroadleafAssignation sortAssignment : sortAssignments) {
                        String property = sortAssignment.getLeftStringRepresentation(finalContext);
                        Object val1 = o1.getPropertyValue(property);
                        Object val2 = o2.getPropertyValue(property);
                        if (sortAssignment.parseRight(finalContext).equals("ASCENDING")) {
                            compareBuilder.append(val1, val2);
                        } else {
                            compareBuilder.append(val2, val1);
                        }
                    }
                    return compareBuilder.toComparison();
                }
            });
        }
        List<Map<String, Object>> contentItemFields = new ArrayList<>();
        for (StructuredContentDTO item : contentItems) {
            if (StringUtils.isNotEmpty(fieldFilters)) {
                List<BroadleafAssignation> assignments = context.getAssignationSequence(fieldFilters, false);
                boolean valid = true;
                for (BroadleafAssignation assignment : assignments) {
                    if (ObjectUtils.notEqual(assignment.parseRight(context), item.getValues().get(assignment.getLeftStringRepresentation(context)))) {
                        LOG.info("Excluding content " + item.getId() + " based on the property value of " + assignment.getLeftStringRepresentation(context));
                        valid = false;
                        break;
                    }
                }
                if (valid) {
                    contentItemFields.add(item.getValues());
                }
            } else {
                contentItemFields.add(item.getValues());
            }
        }
        Map<String, Object> contentItem = null;
        if (contentItemFields.size() > 0) {
            contentItem = contentItemFields.get(0);
        }
        newModelVars.put(contentItemVar, contentItem);
        newModelVars.put(contentListVar, contentItemFields);
        newModelVars.put(numResultsVar, contentItems.size());
    } else {
        if (LOG.isInfoEnabled()) {
            LOG.info("**************************The contentItems is null*************************");
        }
        newModelVars.put(contentItemVar, null);
        newModelVars.put(contentListVar, null);
        newModelVars.put(numResultsVar, 0);
    }
    String deepLinksVar = tagAttributes.get("deepLinks");
    if (StringUtils.isNotBlank(deepLinksVar) && contentItems.size() > 0) {
        List<DeepLink> links = contentDeepLinkService.getLinks(contentItems.get(0));
        extensionManager.getProxy().addExtensionFieldDeepLink(links, tagName, tagAttributes, context);
        extensionManager.getProxy().postProcessDeepLinks(links);
        newModelVars.put(deepLinksVar, links);
    }
    return newModelVars;
}
Also used : Locale(org.broadleafcommerce.common.locale.domain.Locale) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ArrayList(java.util.ArrayList) List(java.util.List) CompareToBuilder(org.apache.commons.lang3.builder.CompareToBuilder) BroadleafTemplateContext(org.broadleafcommerce.presentation.model.BroadleafTemplateContext) SandBox(org.broadleafcommerce.common.sandbox.domain.SandBox) StructuredContentDTO(org.broadleafcommerce.common.structure.dto.StructuredContentDTO) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) DeepLink(org.broadleafcommerce.common.web.deeplink.DeepLink) StructuredContentType(org.broadleafcommerce.cms.structure.domain.StructuredContentType) BroadleafAssignation(org.broadleafcommerce.presentation.model.BroadleafAssignation) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with StructuredContentDTO

use of org.broadleafcommerce.common.structure.dto.StructuredContentDTO 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 StructuredContentDTO

use of org.broadleafcommerce.common.structure.dto.StructuredContentDTO 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 StructuredContentDTO

use of org.broadleafcommerce.common.structure.dto.StructuredContentDTO in project BroadleafCommerce by BroadleafCommerce.

the class StructuredContentServiceImpl method evaluateAndPriortizeContent.

@Override
public List<StructuredContentDTO> evaluateAndPriortizeContent(List<StructuredContentDTO> structuredContentList, int count, Map<String, Object> ruleDTOs) {
    // some optimization for single item lists which don't require prioritization
    if (structuredContentList.size() == 1) {
        return processUnprioritizedContent(structuredContentList, ruleDTOs);
    }
    structuredContentList = modifyStructuredContentDtoList(structuredContentList);
    Iterator<StructuredContentDTO> structuredContentIterator = structuredContentList.iterator();
    List<StructuredContentDTO> returnList = new ArrayList<>();
    List<StructuredContentDTO> tmpList = new ArrayList<>();
    Integer lastPriority = Integer.MIN_VALUE;
    while (structuredContentIterator.hasNext()) {
        StructuredContentDTO sc = structuredContentIterator.next();
        if (!lastPriority.equals(sc.getPriority())) {
            // with the previous priority and add them to the return list.
            if (tmpList.size() > 1) {
                Collections.shuffle(tmpList);
            }
            returnList.addAll(tmpList);
            tmpList.clear();
            // list.
            if (returnList.size() == count) {
                return returnList;
            } else if (returnList.size() > count) {
                return returnList.subList(0, count);
            } else {
                if (processContentRules(sc, ruleDTOs)) {
                    tmpList.add(sc);
                }
            }
        } else {
            if (processContentRules(sc, ruleDTOs)) {
                tmpList.add(sc);
            }
        }
        lastPriority = sc.getPriority();
    }
    if (tmpList.size() > 1) {
        Collections.shuffle(tmpList);
    }
    returnList.addAll(tmpList);
    if (returnList.size() > count) {
        return returnList.subList(0, count);
    }
    return returnList;
}
Also used : StructuredContentDTO(org.broadleafcommerce.common.structure.dto.StructuredContentDTO) ArrayList(java.util.ArrayList)

Example 10 with StructuredContentDTO

use of org.broadleafcommerce.common.structure.dto.StructuredContentDTO 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

StructuredContentDTO (org.broadleafcommerce.common.structure.dto.StructuredContentDTO)10 StructuredContent (org.broadleafcommerce.cms.structure.domain.StructuredContent)5 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)5 ArrayList (java.util.ArrayList)4 Locale (org.broadleafcommerce.common.locale.domain.Locale)4 SandBox (org.broadleafcommerce.common.sandbox.domain.SandBox)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Element (net.sf.ehcache.Element)1 CompareToBuilder (org.apache.commons.lang3.builder.CompareToBuilder)1 StructuredContentItemCriteria (org.broadleafcommerce.cms.structure.domain.StructuredContentItemCriteria)1 StructuredContentType (org.broadleafcommerce.cms.structure.domain.StructuredContentType)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 DeepLink (org.broadleafcommerce.common.web.deeplink.DeepLink)1 BroadleafAssignation (org.broadleafcommerce.presentation.model.BroadleafAssignation)1 BroadleafTemplateContext (org.broadleafcommerce.presentation.model.BroadleafTemplateContext)1