CQPageTranslator.java 15.7 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.utils.GuideUtils
 *  com.adobe.granite.translation.api.TranslationException
 *  com.adobe.granite.translation.api.TranslationException$ErrorCode
 *  com.adobe.granite.translation.api.TranslationService
 *  com.day.cq.tagging.Tag
 *  com.day.cq.tagging.TagManager
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  javax.jcr.Value
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.translation.impl;

import com.adobe.aemds.guide.utils.GuideUtils;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.TranslationUtils;
import com.adobe.granite.translation.api.TranslationException;
import com.adobe.granite.translation.api.TranslationService;
import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import java.io.IOException;
import java.util.ArrayList;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

abstract class CQPageTranslator {
    private static final Logger logger = LoggerFactory.getLogger(CQPageTranslator.class);
    protected Resource rootPage = null;
    protected TranslationService tSvc;
    protected TranslationRuleConfigurationFile ruleFile;
    protected ResourceResolver resourceResolver = null;

    public CQPageTranslator(TranslationRuleConfigurationFile ruleFile, TranslationService tSvc) {
        this.tSvc = tSvc;
        this.ruleFile = ruleFile;
        this.resourceResolver = ruleFile.getResourceResolver();
    }

    protected void processTagTranslationNode(Node node, ArrayList<String> tagList) throws RepositoryException, TranslationException, IOException {
        this.processNodePropertiesForTags(node, tagList);
        this.processChildNodesForTags(node, tagList);
    }

    protected boolean processTagListNow(ArrayList<String> tagList) throws TranslationException, RepositoryException {
        boolean bSave = false;
        TagManager tagManager = this.ruleFile.getTagManager();
        if (tagManager != null) {
            for (String strTag : tagList) {
                Tag tagObj = tagManager.resolve(strTag);
                if (tagObj == null) continue;
                String strTagAttributeName = String.format("jcr:title.%s", this.ruleFile.getDestinationLanguage());
                if (!this.processTagElement((Node)tagObj.adaptTo(Node.class), strTagAttributeName, tagObj.getTitle())) continue;
                bSave = true;
            }
        }
        return bSave;
    }

    protected void processChildNodesForTags(Node node, ArrayList<String> tagList) throws TranslationException, IOException {
        logger.debug("FUNCTION: processChildNodesForTags");
        try {
            NodeIterator inodes = node.getNodes();
            if (inodes != null) {
                while (inodes.hasNext()) {
                    Node childNode = inodes.nextNode();
                    if (!this.isPageNode(childNode)) {
                        this.processTagTranslationNode(childNode, tagList);
                        continue;
                    }
                    logger.debug("Skipping child Node {}", (Object)childNode.getPath());
                }
            }
        }
        catch (RepositoryException e) {
            logger.error("translateChildNodes: Error translating", (Throwable)e);
        }
    }

    protected void processNodePropertiesForTags(Node node, ArrayList<String> tagList) throws TranslationException, IOException {
        logger.debug("FUNCTION: processNodePropertiesForTags");
        try {
            PropertyIterator propIterator = node.getProperties();
            while (propIterator.hasNext()) {
                Property prop = propIterator.nextProperty();
                String strPropertyName = prop.getName();
                if (!"cq:tags".equals(strPropertyName) || !prop.isMultiple()) continue;
                Value[] val_array = prop.getValues();
                if (val_array != null && val_array.length > 0) {
                    for (int index = 0; index < val_array.length; ++index) {
                        Value value = val_array[index];
                        String strTagName = value.getString();
                        if (tagList.contains(strTagName)) continue;
                        tagList.add(strTagName);
                    }
                }
                break;
            }
        }
        catch (RepositoryException e) {
            logger.error("translateNodeProperties: Error translating", (Throwable)e);
        }
    }

    protected boolean processTranslationNode(Node node) throws RepositoryException, TranslationException, IOException {
        boolean bSave = false;
        if (!GuideUtils.isGuideContainerResource((Resource)this.resourceResolver.getResource(node.getPath()))) {
            logger.debug("FUNCTION: processTranslationNode ", (Object)node.getPath());
            bSave = this.processNodeProperties(node);
            if (this.processChildNodes(node)) {
                bSave = true;
            }
        }
        return bSave;
    }

    protected boolean isPageNode(Node node) throws PathNotFoundException, RepositoryException {
        boolean bRetVal = false;
        String strResult = TranslationUtils.getStringAttribute(logger, node, "jcr:primaryType", null);
        if (strResult != null) {
            bRetVal = "cq:Page".equals(strResult);
        }
        return bRetVal;
    }

    protected boolean processChildNodes(Node node) throws TranslationException, IOException {
        logger.debug("FUNCTION: processChildNodes");
        boolean bSave = false;
        try {
            NodeIterator inodes = node.getNodes();
            if (inodes != null) {
                while (inodes.hasNext()) {
                    Node childNode = inodes.nextNode();
                    if (!this.isPageNode(childNode)) {
                        if (!this.processTranslationNode(childNode)) continue;
                        bSave = true;
                        continue;
                    }
                    logger.debug("Skipping child Node {}", (Object)childNode.getPath());
                }
            }
        }
        catch (RepositoryException e) {
            logger.error("translateChildNodes: Error translating", (Throwable)e);
        }
        return bSave;
    }

    protected boolean processTagElement(Node tagNode, String strTagAttributeName, String strSourceTagTitle) throws TranslationException, RepositoryException {
        throw new TranslationException("Please override this function", TranslationException.ErrorCode.GENERAL_EXCEPTION);
    }

    protected boolean processNodeLanguagePatchProperty(Node node, Property prop, String strPropertyName) throws RepositoryException {
        boolean bSave = false;
        if (prop.isMultiple()) {
            Value[] val_array = prop.getValues();
            if (val_array != null && val_array.length > 0) {
                ArrayList<String> strOutputArray = new ArrayList<String>();
                for (int index = 0; index < val_array.length; ++index) {
                    String strTranslationText = this.ruleFile.getDestinationLanguage();
                    if (StringUtils.isEmpty((CharSequence)strTranslationText)) continue;
                    strOutputArray.add(strTranslationText);
                    bSave = true;
                }
                String[] newVals = TranslationUtils.getStringArray(strOutputArray);
                prop.setValue(newVals);
            }
        } else {
            String strTranslationText = this.ruleFile.getDestinationLanguage();
            if (!StringUtils.isEmpty((CharSequence)strTranslationText)) {
                prop.setValue(strTranslationText);
                bSave = true;
            }
        }
        return bSave;
    }

    protected boolean processNodeProperty(Node node, Property prop, String strPropertyName) throws TranslationException {
        throw new TranslationException("Please override this function", TranslationException.ErrorCode.GENERAL_EXCEPTION);
    }

    protected boolean processNodeProperties(Node node) throws TranslationException, IOException {
        logger.debug("FUNCTION: processNodeProperties");
        boolean bSave = false;
        try {
            PropertyIterator propIterator = node.getProperties();
            ArrayList<String> translationList = this.getTranslatablePropertyListForNode(node);
            ArrayList<String> languagePatchList = this.getLanguagePatchPropertyListForNode(node);
            while (propIterator.hasNext()) {
                Property prop = propIterator.nextProperty();
                String strPropertyName = prop.getName();
                if (translationList != null && translationList.contains(strPropertyName)) {
                    if (!this.processNodeProperty(node, prop, strPropertyName)) continue;
                    bSave = true;
                    continue;
                }
                if (languagePatchList == null || !languagePatchList.contains(strPropertyName) || !this.processNodeLanguagePatchProperty(node, prop, strPropertyName)) continue;
                bSave = true;
            }
        }
        catch (RepositoryException e) {
            logger.error("translateNodeProperties: Error translating", (Throwable)e);
        }
        return bSave;
    }

    protected ArrayList<String> getTranslatablePropertyListForNode(Node node) throws RepositoryException, IOException {
        return this.ruleFile.getTranslatablePropertyListForNode(node);
    }

    protected ArrayList<String> getLanguagePatchPropertyListForNode(Node node) throws RepositoryException, IOException {
        return this.ruleFile.getLanguagePatchPropertyListForNode(node);
    }

    protected boolean processUpdateTranslationNode(Node node, String prefixTemporaryPath) throws RepositoryException, TranslationException, IOException {
        boolean bSave = false;
        if (!GuideUtils.isGuideContainerResource((Resource)this.resourceResolver.getResource(node.getPath()))) {
            logger.debug("FUNCTION: processUpdateTranslationNode ", (Object)node.getPath());
            bSave = this.processUpdateNodeProperties(node, prefixTemporaryPath);
            if (this.processUpdateChildNodes(node, prefixTemporaryPath)) {
                bSave = true;
            }
        }
        return bSave;
    }

    protected boolean processUpdateChildNodes(Node node, String prefixTemporaryPath) throws TranslationException, IOException {
        logger.debug("FUNCTION: processChildNodes");
        boolean bSave = false;
        try {
            NodeIterator inodes = node.getNodes();
            if (inodes != null) {
                while (inodes.hasNext()) {
                    Node childNode = inodes.nextNode();
                    if (!this.isPageNode(childNode)) {
                        if (!this.processUpdateTranslationNode(childNode, prefixTemporaryPath)) continue;
                        bSave = true;
                        continue;
                    }
                    logger.debug("Skipping child Node {}", (Object)childNode.getPath());
                }
            }
        }
        catch (RepositoryException e) {
            logger.error("translateChildNodes: Error translating", (Throwable)e);
        }
        return bSave;
    }

    protected boolean processUpdateNodeProperties(Node node, String prefixTemporaryPath) throws TranslationException, IOException {
        logger.debug("FUNCTION: processUpdateNodeProperties");
        boolean bSave = false;
        try {
            ArrayList<String> languagePatchList;
            ArrayList<String> translationList;
            PropertyIterator propIterator = node.getProperties();
            String currentNodePath = node.getPath();
            Resource destinationResource = null;
            if (currentNodePath.startsWith(prefixTemporaryPath)) {
                String destinationResourcePath = currentNodePath.replaceFirst(prefixTemporaryPath, "");
                destinationResource = this.resourceResolver.getResource(destinationResourcePath);
                String damMountPoint = "/content/dam/";
                while (null == destinationResource && destinationResourcePath.startsWith(damMountPoint)) {
                    destinationResourcePath = destinationResourcePath.substring(0, destinationResourcePath.lastIndexOf(47));
                    destinationResource = this.resourceResolver.getResource(destinationResourcePath);
                }
            }
            if (null != destinationResource) {
                Node destinationNode = (Node)destinationResource.adaptTo(Node.class);
                translationList = this.getTranslatablePropertyListForNode(destinationNode);
                languagePatchList = this.getLanguagePatchPropertyListForNode(destinationNode);
            } else {
                translationList = this.getTranslatablePropertyListForNode(node);
                languagePatchList = this.getLanguagePatchPropertyListForNode(node);
            }
            while (propIterator.hasNext()) {
                Property prop = propIterator.nextProperty();
                String strPropertyName = prop.getName();
                if (translationList != null && translationList.contains(strPropertyName)) {
                    if (!this.processNodeProperty(node, prop, strPropertyName)) continue;
                    bSave = true;
                    continue;
                }
                if (languagePatchList == null || !languagePatchList.contains(strPropertyName) || !this.processNodeLanguagePatchProperty(node, prop, strPropertyName)) continue;
                bSave = true;
            }
        }
        catch (RepositoryException e) {
            logger.error("translateNodeProperties: Error translating", (Throwable)e);
        }
        return bSave;
    }

    protected boolean processTranslationOrTemporaryUpdateNode(Node currentRootNode) throws RepositoryException, IOException, TranslationException {
        boolean saverequired = false;
        String updateAssetDestinationNodePath = TranslationUtils.getUpdateAssetDestinationNodePath(currentRootNode);
        if (null != updateAssetDestinationNodePath) {
            int rootFolderLength;
            String updateAssetDestinationFolderPath;
            int destinationFolderLength;
            String currentRootNodePath = currentRootNode.getPath();
            String currentRootFolderPath = currentRootNodePath.substring(0, rootFolderLength = currentRootNodePath.lastIndexOf(47));
            if (currentRootFolderPath.endsWith(updateAssetDestinationFolderPath = updateAssetDestinationNodePath.substring(0, destinationFolderLength = updateAssetDestinationNodePath.lastIndexOf(47)))) {
                int preFixPathLength = currentRootFolderPath.length() - updateAssetDestinationFolderPath.length();
                String prefixPath = currentRootNodePath.substring(0, preFixPathLength);
                saverequired = this.processUpdateTranslationNode(currentRootNode, prefixPath);
            } else {
                logger.warn("Invalid value: {} for Property - dam:destinationLanguageCopy at Path {}", (Object)updateAssetDestinationNodePath, (Object)currentRootNodePath);
            }
        } else {
            saverequired = this.processTranslationNode(currentRootNode);
        }
        return saverequired;
    }
}