CMAssetViewHandler.java 10.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.icc.dbforms.obj.Letter
 *  com.adobe.icc.helper.util.CMAssetsQuery
 *  com.adobe.icc.services.api.LetterService
 *  com.day.cq.search.QueryBuilder
 *  com.day.cq.wcm.core.contentfinder.ViewHandler
 *  com.day.cq.wcm.core.contentfinder.ViewQuery
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.commons.query.GQL
 *  org.apache.jackrabbit.commons.query.GQL$ParserCallback
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.fd.adaddon.internal.servlet;

import com.adobe.fd.adaddon.internal.servlet.DocumentFragmentCFViewQuery;
import com.adobe.icc.dbforms.obj.Letter;
import com.adobe.icc.helper.util.CMAssetsQuery;
import com.adobe.icc.services.api.LetterService;
import com.day.cq.search.QueryBuilder;
import com.day.cq.wcm.core.contentfinder.ViewHandler;
import com.day.cq.wcm.core.contentfinder.ViewQuery;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.commons.query.GQL;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(label="CMAssetViewHandler", description="CMAssetViewHandler for listing CM Asset on Content finder search")
@Service
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"fd/ad/contentfinder/cmassets/view"})})
public class CMAssetViewHandler
extends ViewHandler {
    private static Logger logger = LoggerFactory.getLogger(CMAssetViewHandler.class);
    @Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
    private LetterService letterService;
    @Reference
    private QueryBuilder queryBuilder;
    private static final int DEFAULT_LIMIT = 50;
    private static final String ASSET_TYPE = "assetType";
    private static final String ASSET_TYPE_ALL = "ALL";
    private static final String ASSET_TYPE_TEXT = "TEXT_MODULE";
    private static final String ASSET_TYPE_LIST = "LIST_MODULE";
    private static final String ASSET_TYPE_CONDITION = "CONDITIONAL_MODULE";
    private static final String ASSET_TYPE_IMAGE = "DAM_IMAGE";
    private static final Map<String, String[]> assetTypeMap = new HashMap<String, String[]>();
    private static final String GUIDE_PATH_PARAM = "guidePath";
    private static final String GUIDE_CONTAINER_PATH = "/jcr:content/guideContainer";
    private static final String DD_REF = "ddRef";
    private static final String START = "start";

    private String[] getAssetTypes(SlingHttpServletRequest request) {
        String[] assetTypes;
        String assetType = request.getParameter("assetType");
        if (StringUtils.isBlank((CharSequence)assetType)) {
            assetType = "ALL";
        }
        if (assetType.indexOf(",") > -1) {
            String[] parts = assetType.split(",");
            ArrayList<Object> assetTypeList = new ArrayList<Object>();
            for (String part : parts) {
                if (assetTypeMap.get(part) == null) continue;
                assetTypeList.addAll(Arrays.asList((Object[])assetTypeMap.get(part)));
            }
            assetTypes = assetTypeList.toArray(new String[assetTypeList.size()]);
        } else {
            assetTypes = assetTypeMap.get(assetType);
        }
        if (assetTypes == null) {
            logger.error("Unknown asset type: " + assetType);
        }
        return assetTypes;
    }

    private Map<String, Object> getCMAssetsQueryParams(SlingHttpServletRequest request, QueryParser parsedQuery) {
        String ddRef = this.getDDRef(request);
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("PARAM_ASSET_TYPES", this.getAssetTypes(request));
        if (StringUtils.isNotBlank((CharSequence)parsedQuery.getName())) {
            params.put("PARAM_NAME", parsedQuery.getName());
        }
        if (StringUtils.isNotBlank((CharSequence)parsedQuery.getFulltext())) {
            params.put("PARAM_FULLTEXT", parsedQuery.getFulltext());
        }
        if (StringUtils.isNotBlank((CharSequence)ddRef)) {
            params.put("PARAM_DD_NAME", ddRef);
        }
        return params;
    }

    protected ViewQuery createQuery(SlingHttpServletRequest request, Session session, String queryString) throws Exception {
        QueryParser queryParser = new QueryParser(session);
        queryParser.parse(queryString);
        Map<String, Object> params = this.getCMAssetsQueryParams(request, queryParser);
        CMAssetsQuery queryObj = new CMAssetsQuery(this.queryBuilder, session, params);
        queryObj.setOffset(this.getStart(request));
        queryObj.setHitsPerPage(this.getLimit(request));
        return new DocumentFragmentCFViewQuery(request, session, queryObj);
    }

    private int getLimit(SlingHttpServletRequest request) {
        String limitParamValue = request.getParameter("limit");
        int limit = 50;
        try {
            if (StringUtils.isNotBlank((CharSequence)limitParamValue)) {
                limit = Integer.parseInt(limitParamValue);
            }
        }
        catch (NumberFormatException e) {
            logger.warn("Exception while parsing limit param", (Throwable)e);
        }
        return limit;
    }

    private int getStart(SlingHttpServletRequest request) {
        String startParamValue = request.getParameter("start");
        int start = 0;
        try {
            if (StringUtils.isNotBlank((CharSequence)startParamValue)) {
                start = Integer.parseInt(startParamValue);
            }
        }
        catch (NumberFormatException e) {
            logger.warn("Exception while parsing start param", (Throwable)e);
        }
        return start;
    }

    private String getDDRef(SlingHttpServletRequest request) {
        Resource guideContainer;
        ResourceResolver rr;
        String guidePath = request.getParameter("guidePath");
        String ddRef = null;
        if (StringUtils.isNotBlank((CharSequence)guidePath) && (rr = request.getResourceResolver()) != null && (guideContainer = rr.getResource(guidePath + "/jcr:content/guideContainer")) != null) {
            ValueMap map = (ValueMap)guideContainer.adaptTo(ValueMap.class);
            ddRef = map.get((Object)"letterRef") != null ? this.getDDRefFromLetter((String)map.get("letterRef", (Object)"")) : (String)map.get("ddRef", (Object)null);
        }
        return ddRef;
    }

    private String getDDRefFromLetter(String letterRef) {
        String ddRef = null;
        if (StringUtils.isNotBlank((CharSequence)letterRef)) {
            if (this.letterService != null) {
                Letter letter = this.letterService.getLetter(letterRef, false);
                if (letter != null) {
                    ddRef = letter.getDataDictionaryRef();
                } else {
                    logger.error("getDDRefFromLetter: Could not read letter:  " + letterRef);
                }
            } else {
                logger.error("getDDRefFromLetter: Letter service is null ");
            }
        } else {
            logger.error("getDDRefFromLetter: LetterRef is blank " + letterRef);
        }
        return ddRef;
    }

    static {
        assetTypeMap.put("ALL", new String[]{"com.adobe.icc.dbforms.obj.TextModule", "com.adobe.icc.dbforms.obj.ListDataModule", "com.adobe.icc.dbforms.obj.ConditionalDataModule", "IMAGE"});
        assetTypeMap.put("TEXT_MODULE", new String[]{"com.adobe.icc.dbforms.obj.TextModule"});
        assetTypeMap.put("LIST_MODULE", new String[]{"com.adobe.icc.dbforms.obj.ListDataModule"});
        assetTypeMap.put("CONDITIONAL_MODULE", new String[]{"com.adobe.icc.dbforms.obj.ConditionalDataModule"});
        assetTypeMap.put("DAM_IMAGE", new String[]{"IMAGE"});
    }

    protected void bindLetterService(LetterService letterService) {
        this.letterService = letterService;
    }

    protected void unbindLetterService(LetterService letterService) {
        if (this.letterService == letterService) {
            this.letterService = null;
        }
    }

    protected void bindQueryBuilder(QueryBuilder queryBuilder) {
        this.queryBuilder = queryBuilder;
    }

    protected void unbindQueryBuilder(QueryBuilder queryBuilder) {
        if (this.queryBuilder == queryBuilder) {
            this.queryBuilder = null;
        }
    }

    static class QueryParser
    implements GQL.ParserCallback {
        Session session;
        String fulltext;
        String name;

        public QueryParser(Session session) {
            this.session = session;
        }

        public void parse(String query) throws RepositoryException {
            GQL.parse((String)query, (Session)this.session, (GQL.ParserCallback)this);
        }

        public void term(String property, String value, boolean b) throws RepositoryException {
            if ("name".equals(property)) {
                this.name = value;
            } else if (StringUtils.isEmpty((CharSequence)property)) {
                this.fulltext = value;
            }
        }

        public String getFulltext() {
            return this.fulltext;
        }

        public String getName() {
            return this.name;
        }
    }

}