DocumentFragmentCFViewQuery.java 3.32 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.icc.helper.util.CMAssetsQuery
 *  com.day.cq.wcm.core.contentfinder.Hit
 *  com.day.cq.wcm.core.contentfinder.ViewQuery
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.fd.adaddon.internal.servlet;

import com.adobe.icc.helper.util.CMAssetsQuery;
import com.day.cq.wcm.core.contentfinder.Hit;
import com.day.cq.wcm.core.contentfinder.ViewQuery;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DocumentFragmentCFViewQuery
implements ViewQuery {
    private static Logger logger = LoggerFactory.getLogger(DocumentFragmentCFViewQuery.class);
    private final SlingHttpServletRequest request;
    private final CMAssetsQuery query;
    private final Session session;
    private static final String DAM_ASSET = "dam:Asset";
    private static final String NAME_PROP = "name";
    private static final String PATH_PROP = "path";
    private static final String DESC_PROP = "desc";
    private static final String THUMBNAIL_PATH_PROP = "thumbnail_path";
    private static final String CM_ASSET_THUMBNAIL_SUFFIX = "/renditions/thumbnail.319.319.png";
    private static final String DAM_ASSET_THUMBNAIL_SUFFIX = ".thumb.319.319.png";

    public DocumentFragmentCFViewQuery(SlingHttpServletRequest request, Session session, CMAssetsQuery query) {
        this.request = request;
        this.query = query;
        this.session = session;
    }

    public Collection<Hit> execute() {
        try {
            return this.executeQuery(this.request.getResourceResolver(), this.session, this.query);
        }
        catch (RepositoryException e) {
            logger.error("Error executing query " + (Object)this.query, (Throwable)e);
            return new ArrayList<Hit>();
        }
    }

    private List<Hit> executeQuery(ResourceResolver resolver, Session session, CMAssetsQuery query) throws RepositoryException {
        ArrayList<Hit> hits = new ArrayList<Hit>();
        List qr = query.execute();
        for (Properties props : qr) {
            Hit hit;
            if (props == null || (hit = this.createHit(props)) == null) continue;
            hits.add(hit);
        }
        return hits;
    }

    private Hit createHit(Properties props) {
        if (props != null) {
            Hit hit = new Hit();
            hit.set("name", props.get("name"));
            hit.set("desc", props.get("desc"));
            String path = (String)props.get("id");
            hit.set("path", (Object)path);
            if (this.isImage(path)) {
                hit.set("thumbnail_path", (Object)(path + ".thumb.319.319.png"));
            } else {
                hit.set("thumbnail_path", (Object)(path + "/renditions/thumbnail.319.319.png"));
            }
            return hit;
        }
        return null;
    }

    private boolean isImage(String path) {
        return path.startsWith("/content/dam");
    }
}