Firmware.java 2.82 KB
/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2016 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package libs.screens.dcc.components.dashboard;

import com.adobe.cq.sightly.WCMUsePojo;
import com.day.cq.search.PredicateGroup;
import com.day.cq.wcm.api.Page;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;

import javax.jcr.Session;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Firmware extends WCMUsePojo {
    private ResourceResolver resolver;
    private QueryBuilder queryBuilder;

    private Map<String, String> searchProperties;
    private List<Page> firmwares;

    @Override
    public void activate() throws Exception {
        resolver = getResourceResolver();
        queryBuilder = resolver.adaptTo(QueryBuilder.class);

        searchProperties = new HashMap<String, String>();
        firmwares = new ArrayList<Page>();

        searchProperties.put("path", "/libs/screens/player/content");
        searchProperties.put("p.limit", "25");
        searchProperties.put("p.offset", "0");

        searchProperties.put("group.0_property", "sling:resourceType");
        searchProperties.put("group.0_property.value", "screens/player/components/firmware");

        searchProperties.put("group.1_property", "sling:resourceSuperType");
        searchProperties.put("group.1_property.value", "screens/player/components/firmware");

        searchProperties.put("group.p.or", "true");

        Query query = queryBuilder.createQuery(PredicateGroup.create(searchProperties), resolver.adaptTo(Session.class));

        Iterator<Resource> it = query.getResult().getResources();
        while (it.hasNext()) {
            Resource firmwareResource = it.next();
            Page firmwarePage = firmwareResource.getParent().adaptTo(Page.class);

            if (firmwarePage != null) {
                firmwares.add(firmwarePage);
            }
        }
    }

    /**
     * @return List of all available firmwares
     */
    public List<Page> getAll() {
        return firmwares;
    }
}