Cq560CatalogConverterImpl.java 14.1 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.commerce.common.CommerceHelper
 *  com.day.cq.commons.Filter
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.LiveRelationshipManager
 *  com.day.cq.wcm.msm.api.RolloutConfig
 *  com.day.cq.wcm.msm.api.RolloutManager
 *  com.day.cq.wcm.msm.api.RolloutManager$Trigger
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Value
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.commerce.pim.impl.cq560catalogconverter;

import com.adobe.cq.commerce.common.CommerceHelper;
import com.adobe.cq.commerce.pim.impl.cataloggenerator.CatalogUtils;
import com.adobe.cq.commerce.pim.impl.cq560catalogconverter.Cq560CatalogConverter;
import com.day.cq.commons.Filter;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.api.RolloutManager;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Iterator;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, label="%cq560catalogconverterimpl.name", description="%cq560catalogconverterimpl.description")
@Service
@Property(name="service.description", value={"Updates 5.6.0 catalogs and catalog blueprints to 5.6.1 format"})
public class Cq560CatalogConverterImpl
implements Cq560CatalogConverter {
    private final Logger log = LoggerFactory.getLogger(Cq560CatalogConverterImpl.class);
    @Reference
    private LiveRelationshipManager liveRelationshipManager;
    static final String DEFAULT_TEMPLATES_FOLDER_TITLE = "Template Pages";
    @Property(label="%Template Pages Folder Name", description="%Folder name where template pages will be created", value={"Template Pages"})
    private static final String TEMPLATES_FOLDER_TITLE = "cq.commerce.templatepagesfoldername";
    private static String templatesFolderTitle;
    private static String[] rolloutConfigs;

    @Activate
    protected void activate(ComponentContext ctx) throws Exception {
        templatesFolderTitle = PropertiesUtil.toString(ctx.getProperties().get("cq.commerce.templatepagesfoldername"), (String)"Template Pages");
    }

    @Override
    public void updateInstanceMap(ResourceResolver resolver, Page blueprintRoot) {
        this.log.info("Creating catalog instance map for blueprint {}", (Object)blueprintRoot.getPath());
        PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
        try {
            Collection targets = this.liveRelationshipManager.getLiveRelationships(blueprintRoot, null, null, false);
            for (LiveRelationship target : targets) {
                Page targetCatalog;
                boolean isCatalogTarget = false;
                for (RolloutConfig config : target.getRolloutConfigs()) {
                    if (!config.getPath().equals("/etc/msm/rolloutconfigs/catalog")) continue;
                    isCatalogTarget = true;
                    break;
                }
                if (!isCatalogTarget || (targetCatalog = pageManager.getContainingPage(target.getTargetPath())) == null) continue;
                CatalogUtils.registerCatalogInstance(blueprintRoot, targetCatalog, rolloutConfigs);
            }
        }
        catch (Exception e) {
            this.log.error("Failed to update catalog instance map", (Throwable)e);
        }
    }

    @Override
    public void updateBlueprint(ResourceResolver resolver, Page blueprintRoot) throws WCMException {
        if (blueprintRoot.getContentResource("cq:CatalogSyncConfig") == null) {
            this.updateInstanceMap(resolver, blueprintRoot);
        }
        PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
        String parentPath = Text.getRelativeParent((String)blueprintRoot.getPath(), (int)1);
        String templatesFolderName = JcrUtil.createValidName((String)templatesFolderTitle);
        Page templatesFolder = pageManager.getPage(parentPath + "/" + templatesFolderName);
        if (templatesFolder == null) {
            try {
                this.log.info("Creating page {} to hold template pages", (Object)(parentPath + "/" + templatesFolderName));
                templatesFolder = pageManager.create(parentPath, templatesFolderName, null, templatesFolderTitle);
                Node contentNode = (Node)templatesFolder.getContentResource().adaptTo(Node.class);
                contentNode.setProperty("sling:resourceType", "commerce/components/templatepagefolder");
            }
            catch (Exception e) {
                throw new WCMException("Couldn't create folder for template pages", (Throwable)e);
            }
        }
        try {
            this.log.info("Updating catalog blueprint {}", (Object)blueprintRoot.getPath());
            this.updateBlueprintPage(resolver, templatesFolder, blueprintRoot);
            ((Node)blueprintRoot.getContentResource().adaptTo(Node.class)).setProperty("cq:blueprintVersion", 561);
        }
        catch (Exception e) {
            throw new WCMException("Exception encountered updating catalog blueprint", (Throwable)e);
        }
    }

    private void updateBlueprintPage(ResourceResolver resolver, Page templatesFolder, Page blueprint) throws WCMException, RepositoryException {
        Resource references = blueprint.getContentResource("templates");
        if (references != null) {
            Node referencesNode = (Node)references.adaptTo(Node.class);
            if (referencesNode.hasProperty("catalog")) {
                this.validateTemplateReference(resolver, templatesFolder, referencesNode, "catalog");
            }
            if (referencesNode.hasProperty("section")) {
                this.validateTemplateReference(resolver, templatesFolder, referencesNode, "section");
            }
            if (referencesNode.hasProperty("product")) {
                this.validateTemplateReference(resolver, templatesFolder, referencesNode, "product");
            }
            if (referencesNode.hasProperty("cq_rolloutConfigs")) {
                referencesNode.setProperty("cq_rolloutConfigs", (Value[])null);
            }
        }
        Iterator i = blueprint.listChildren();
        while (i.hasNext()) {
            this.updateBlueprintPage(resolver, templatesFolder, (Page)i.next());
        }
    }

    private void validateTemplateReference(ResourceResolver resolver, Page templatesFolder, Node references, String referenceName) throws WCMException, RepositoryException {
        if (references.hasProperty(referenceName)) {
            String templatePath = references.getProperty(referenceName).getString();
            Resource resource = resolver.getResource(templatePath);
            if (resource == null) {
                throw new WCMException("Template reference not found");
            }
            if (((Node)resource.adaptTo(Node.class)).isNodeType("cq:Template")) {
                Page templatePage = this.getTemplatePage(templatesFolder, templatePath);
                if (templatePage == null) {
                    PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
                    String name = Text.getName((String)templatePath);
                    String title = (String)ResourceUtil.getValueMap((Resource)resource).get("jcr:title", (Object)"");
                    templatePage = pageManager.create(templatesFolder.getPath(), name, templatePath, title);
                }
                references.setProperty(referenceName, templatePage.getPath());
            }
        }
    }

    private Page getTemplatePage(Page templatesFolder, String templatePath) {
        class TemplateFilter
        implements Filter<Page> {
            private String templatePath;

            public TemplateFilter(String templatePath) {
                this.templatePath = templatePath;
            }

            public boolean includes(Page page) {
                return ((String)page.getProperties().get("cq:template", (Object)"")).equals(this.templatePath);
            }
        }
        Iterator i = templatesFolder.listChildren((Filter)new TemplateFilter(templatePath), true);
        if (i.hasNext()) {
            return (Page)i.next();
        }
        return null;
    }

    @Override
    public void updateCatalog(ResourceResolver resolver, Page catalogRoot) throws WCMException {
        try {
            this.log.info("Updating product catalog {}", (Object)catalogRoot.getPath());
            this.firstPass(catalogRoot);
            this.secondPass((PageManager)resolver.adaptTo(PageManager.class), catalogRoot, null, CatalogUtils.getConfigs(resolver, rolloutConfigs));
        }
        catch (Exception e) {
            throw new WCMException("Exception encountered while updating product catalog", (Throwable)e);
        }
    }

    private void firstPass(Page catalogPage) throws WCMException, RepositoryException {
        Resource contentResource = catalogPage.getContentResource();
        ValueMap properties = ResourceUtil.getValueMap((Resource)contentResource);
        Node contentNode = (Node)catalogPage.getContentResource().adaptTo(Node.class);
        if (((String)properties.get("cq:commerceType", (Object)"")).equals("section")) {
            LiveRelationship lr = this.liveRelationshipManager.getLiveRelationship(contentResource, false);
            String blueprintPath = lr.getSourcePath();
            blueprintPath = blueprintPath.substring(0, blueprintPath.length() - "/jcr:content".length());
            contentNode.setProperty("cq:catalogBlueprint", blueprintPath);
        } else {
            Resource productReference = CommerceHelper.findProductResource((Page)catalogPage);
            if (productReference != null) {
                contentNode.setProperty("cq:productMaster", (String)ResourceUtil.getValueMap((Resource)productReference).get("productData", (Object)""));
            }
        }
        Iterator i = catalogPage.listChildren();
        while (i.hasNext()) {
            this.firstPass((Page)i.next());
        }
    }

    private void secondPass(PageManager pageManager, Page catalogPage, Page parentBlueprint, RolloutConfig[] rolloutConfigs) throws WCMException, RepositoryException {
        Resource contentResource = catalogPage.getContentResource();
        ValueMap properties = ResourceUtil.getValueMap((Resource)contentResource);
        if (properties.get("cq:catalogBlueprint", String.class) != null) {
            this.liveRelationshipManager.endRelationship(contentResource, false);
            String blueprintPath = (String)properties.get("cq:catalogBlueprint", String.class);
            parentBlueprint = pageManager.getPage(blueprintPath);
            if (parentBlueprint != null) {
                Page templatePage = CatalogUtils.getTemplatePage(parentBlueprint, null);
                if (templatePage != null) {
                    this.liveRelationshipManager.establishRelationship(templatePage, catalogPage, false, false, rolloutConfigs);
                } else {
                    this.log.warn("Section blueprint {} didn't provide a valid section template page", (Object)parentBlueprint.getPath());
                }
            } else {
                this.log.warn("Catalog blueprint path {} doesn't point to a page", (Object)blueprintPath);
            }
        } else if (properties.get("cq:productMaster", String.class) != null) {
            if (parentBlueprint != null) {
                Page templatePage = CatalogUtils.getTemplatePage(parentBlueprint, "product");
                if (templatePage != null) {
                    this.liveRelationshipManager.establishRelationship(templatePage, catalogPage, false, false, rolloutConfigs);
                } else {
                    this.log.warn("Section blueprint {} didn't provide a valid product template page", (Object)parentBlueprint.getPath());
                }
            } else {
                this.log.warn("Found a product page but the parent section had no blueprint");
            }
        }
        Iterator i = catalogPage.listChildren();
        while (i.hasNext()) {
            this.secondPass(pageManager, (Page)i.next(), parentBlueprint, rolloutConfigs);
        }
    }

    static {
        rolloutConfigs = new String[]{"/etc/msm/rolloutconfigs/catalogpagecontent", "/etc/msm/rolloutconfigs/catalogrollouthooks"};
    }

    protected void bindLiveRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        this.liveRelationshipManager = liveRelationshipManager;
    }

    protected void unbindLiveRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        if (this.liveRelationshipManager == liveRelationshipManager) {
            this.liveRelationshipManager = null;
        }
    }

}