MobileAppIndexBootstrap.java 4.64 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.google.common.collect.ImmutableList
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.index.impl.boostrap;

import com.adobe.cq.mobile.index.impl.builder.AggregationRule;
import com.adobe.cq.mobile.index.impl.builder.AggregationRuleEntry;
import com.adobe.cq.mobile.index.impl.builder.IndexRule;
import com.adobe.cq.mobile.index.impl.builder.LuceneIndexDefinition;
import com.adobe.cq.mobile.index.impl.builder.PropertyDefinition;
import com.adobe.cq.mobile.index.impl.builder.SimplePropertyDefinition;
import com.adobe.cq.mobile.index.impl.helper.IndexHelper;
import com.google.common.collect.ImmutableList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class MobileAppIndexBootstrap {
    @Reference(policy=ReferencePolicy.STATIC)
    private ResourceResolverFactory resolverFactory;
    private static final String INDEX_ADMIN = "index-admin";
    private static final Logger log = LoggerFactory.getLogger(MobileAppIndexBootstrap.class);
    private static final String INDEX_ROOT_PATH = "/oak:index";

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void activate(ComponentContext context) {
        ResourceResolver resolver = null;
        try {
            HashMap<String, String> param = new HashMap<String, String>();
            param.put("sling.service.subservice", "index-admin");
            resolver = this.resolverFactory.getServiceResourceResolver(param);
            this.createDefaultIndex(resolver);
        }
        catch (LoginException e) {
            log.error("Error while setting up index", (Throwable)e);
        }
        finally {
            if (resolver != null) {
                resolver.close();
            }
        }
    }

    private void createDefaultIndex(ResourceResolver resolver) {
        Resource indexRootRsc = resolver.getResource("/");
        LuceneIndexDefinition indexDef = new LuceneIndexDefinition("cqMobileAppLucene", MobileAppIndexBootstrap.createAggregationRules(), MobileAppIndexBootstrap.createIndexRules());
        if (IndexHelper.getReIndexCount(indexRootRsc.getChild("oak:index/cqMobileAppLucene")) <= 0) {
            if (indexDef.build(indexRootRsc, true)) {
                log.debug("IndexDefinition created successfully at {}", (Object)indexRootRsc.getPath());
            } else {
                log.debug("IndexDefinition creation failed");
            }
        }
    }

    public static List<AggregationRule> createAggregationRules() {
        return ImmutableList.of((Object)new AggregationRule("cq:MobileApp", (List<AggregationRuleEntry>)ImmutableList.of((Object)new AggregationRuleEntry("path", "jcr:content", true), (Object)new AggregationRuleEntry("path", "shell/jcr:content", true), (Object)new AggregationRuleEntry("path", "shell/jcr:content/widget/*", true))));
    }

    public static List<IndexRule> createIndexRules() {
        return ImmutableList.of((Object)new IndexRule("cq:MobileApp", (List<? extends PropertyDefinition>)ImmutableList.of((Object)new SimplePropertyDefinition("jcr:content/jcr:title", "appTitle", "String", true, true).setUseInSpellcheck(true).setUseInSuggest(true), (Object)new SimplePropertyDefinition("jcr:content/jcr:description", "description", "String"), (Object)new SimplePropertyDefinition("jcr:content/widget/description/text", "widgetDescription", "String"))));
    }

    protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resolverFactory = resourceResolverFactory;
    }

    protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resolverFactory == resourceResolverFactory) {
            this.resolverFactory = null;
        }
    }
}