CollectionHelperImpl.java 5.94 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.dam.cfm.CollectionHelper
 *  com.day.cq.dam.commons.util.DateParser
 *  com.day.cq.search.PredicateGroup
 *  com.day.cq.search.Query
 *  com.day.cq.search.QueryBuilder
 *  com.day.cq.search.result.SearchResult
 *  javax.jcr.Session
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.resource.collection.ResourceCollection
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.dam.cfm.impl;

import com.adobe.cq.dam.cfm.CollectionHelper;
import com.day.cq.dam.commons.util.DateParser;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.SearchResult;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.resource.collection.ResourceCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@Component(metatype=0)
public class CollectionHelperImpl
implements CollectionHelper {
    private final Logger log;
    @Reference
    private QueryBuilder queryBuilder;

    public CollectionHelperImpl() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    private boolean isValidCollection(Resource res) {
        return res.adaptTo(ResourceCollection.class) != null;
    }

    public Iterator<Resource> getCollections(ResourceResolver resourceResolver, boolean includeSmartCollections) {
        Session session = (Session)resourceResolver.adaptTo(Session.class);
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("path", "/content/dam");
        map.put("group.0_property", "sling:resourceType");
        map.put("group.0_property.0_value", "dam/collection");
        if (includeSmartCollections) {
            map.put("group.0_property.1_value", "dam/smartcollection");
        }
        map.put("group.1_property", "sling:resourceSuperType");
        map.put("group.1_property.value", "dam/collection");
        map.put("group.p.or", "true");
        map.put("type", "nt:unstructured");
        map.put("p.limit", "0");
        map.put("p.offset", "0");
        Query query = this.queryBuilder.createQuery(PredicateGroup.create(map), session);
        ArrayList<Resource> collections = new ArrayList<Resource>();
        SearchResult result = query.getResult();
        Iterator it = result.getResources();
        while (it.hasNext()) {
            Resource p = (Resource)it.next();
            if (!this.isValidCollection(p)) continue;
            collections.add(p);
        }
        Collections.sort(collections, new Comparator<Resource>(){

            @Override
            public int compare(Resource r1, Resource r2) {
                long date2;
                ValueMap v1 = r1.getValueMap();
                ValueMap v2 = r2.getValueMap();
                String d1 = (String)v1.get("jcr:lastModified", v1.get("jcr:created", (Object)""));
                String d2 = (String)v2.get("jcr:lastModified", v2.get("jcr:created", (Object)""));
                if (StringUtils.isEmpty((String)d1)) {
                    return -1;
                }
                if (StringUtils.isEmpty((String)d2)) {
                    return 1;
                }
                long date1 = DateParser.parseDate((String)d1).getTime();
                return date1 < (date2 = DateParser.parseDate((String)d2).getTime()) ? 1 : -1;
            }
        });
        Authorizable user = (Authorizable)resourceResolver.adaptTo(Authorizable.class);
        if (null != user) {
            try {
                String lightboxCollectionName = "lightbox";
                ArrayList<Resource> collectionsWithoutLightbox = new ArrayList<Resource>();
                for (Resource currentCollection : collections) {
                    String currentCollectionName = currentCollection.getName();
                    if (lightboxCollectionName.equals(currentCollectionName)) {
                        String currentCollectionPath = currentCollection.getPath();
                        String tenantUserHome = "/home/users";
                        String userHome = user.getPath();
                        userHome = userHome.indexOf(tenantUserHome) == 0 ? StringUtils.substringAfter((String)userHome, (String)tenantUserHome) : userHome;
                        String userCollsHome = "/content/dam/collections" + userHome;
                        String lightboxCollectionPath = userCollsHome + "/" + lightboxCollectionName;
                        if (!currentCollectionPath.endsWith(lightboxCollectionPath)) continue;
                    }
                    collectionsWithoutLightbox.add(currentCollection);
                }
                collections = collectionsWithoutLightbox;
            }
            catch (Exception e) {
                // empty catch block
            }
        }
        return collections.iterator();
    }

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

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

}