CQ62SearchFacetsConfigUpdate.java 6.29 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.nodetype.NodeDefinition
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.compat.codeupgrade.impl.cq62;

import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import java.util.Dictionary;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeDefinition;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, immediate=1, label="Adobe CQ 6.2 Search Facets Config upgrader", description="moves customized search facets from apps to conf")
public class CQ62SearchFacetsConfigUpdate
implements ProgressInfoProvider {
    private static final Logger log = LoggerFactory.getLogger((String)CQ62SearchFacetsConfigUpdate.class.getName());
    private static final String TENANTS_ROOT = "/etc/tenants";
    private static final String RESOURCE_TYPE_CONTSYS = "granite/ui/components/foundation/contsys";
    private String progressInfo;
    @Property(value={"cq/gui/content/facets:cq/search/facets", "dam/gui/content/facets:dam/search/facets", "commerce/gui/content/facets:commerce/search/facets", "screens/dcc/content/facets:screens/dcc/search/facets", "cq/core/content/projects/facets:projects/search/facets"})
    public static final String PROP_FACET_PATH_LIST = "facet.path.list";
    private static String[][] facetPathList;

    @Activate
    public void activate(ComponentContext ctx) throws Exception {
        Dictionary properties = ctx.getProperties();
        String[] facetMap = (String[])properties.get("facet.path.list");
        facetPathList = new String[facetMap.length][2];
        for (int i = 0; i < facetMap.length; ++i) {
            CQ62SearchFacetsConfigUpdate.facetPathList[i] = facetMap[i].split(":");
        }
    }

    @Override
    public String getProgressInfo() {
        return this.progressInfo;
    }

    void setProgressInfo(String info) {
        this.progressInfo = info;
        log.info(this.progressInfo);
    }

    void doUpgrade(Session session) {
        try {
            this.setProgressInfo("Migrating search facets from /apps to /conf");
            for (String[] relativePath : facetPathList) {
                Node tenantsNode;
                if (session.nodeExists("/conf/global/settings")) {
                    this.move("/apps", "/conf/global/settings", relativePath, session);
                }
                if ((tenantsNode = session.getNode("/etc/tenants")) == null) continue;
                NodeIterator it = tenantsNode.getNodes();
                while (it.hasNext()) {
                    Node node = it.nextNode();
                    if (node.getDefinition().isProtected()) continue;
                    String tenantId = node.getName();
                    if (!session.nodeExists("/conf/tenants/" + tenantId + "/settings")) continue;
                    this.move("/apps/" + tenantId, "/conf/tenants/" + tenantId + "/settings", relativePath, session);
                }
            }
            if (session.hasPendingChanges()) {
                session.save();
            }
            this.setProgressInfo("migration of custom search facets configurations complete");
        }
        catch (RepositoryException e) {
            this.setProgressInfo("migration of custom search facets configurations failed");
            log.error("migration failed", (Throwable)e);
        }
    }

    private void move(String fromRP, String toRP, String[] mapping, Session session) {
        String from = fromRP + "/" + mapping[0];
        String to = toRP + "/" + mapping[1];
        try {
            Node fromNode = session.getNode(from);
            if (!session.nodeExists(from)) {
                return;
            }
            Node toNode = !session.nodeExists(to) ? JcrUtil.createPath((String)to, (String)"cq:Page", (String)"cq:Page", (Session)session, (boolean)false) : session.getNode(to);
            toNode.addNode("jcr:content", "nt:unstructured").setProperty("mergeList", true);
            NodeIterator fromItr = fromNode.getNodes();
            while (fromItr.hasNext()) {
                Node fromFacet = fromItr.nextNode();
                String fromFacetPath = fromFacet.getPath();
                String facetName = fromFacet.getName();
                if (toNode.hasNode(facetName) || !fromFacet.hasProperty("sling:resourceType") || !fromFacet.getProperty("sling:resourceType").getString().equals("granite/ui/components/foundation/contsys")) continue;
                Node toFacetNode = toNode.addNode(facetName, "cq:Page");
                session.move(fromFacet.getPath(), toFacetNode.getPath() + "/" + "jcr:content");
                Node toJcrContent = toFacetNode.getNode("jcr:content");
                String predicatesConfig = toJcrContent.getProperty("predicatesConfig").getString();
                int index = predicatesConfig.indexOf(mapping[0]);
                if (index > -1) {
                    toJcrContent.setProperty("predicatesConfig", "/mnt/overlay/settings/" + predicatesConfig.substring(index));
                }
                String libsPath = "/libs" + toJcrContent.getPath().substring(toJcrContent.getPath().indexOf("/settings"));
                Node libsNode = null;
                libsNode = session.nodeExists(libsPath) ? session.getNode(libsPath) : session.getNode("/libs" + fromFacetPath.substring(fromRP.length()));
                if (libsNode == null || !libsNode.hasProperty("jcr:title")) continue;
                toJcrContent.setProperty("jcr:title", libsNode.getProperty("jcr:title").getString());
            }
        }
        catch (RepositoryException e) {
            log.error("error in moving facets from " + from + " to " + to, (Throwable)e);
        }
    }
}