ContentFragmentTranslator.java 11.7 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.utils.GuideUtils
 *  com.adobe.granite.translation.api.TranslationConstants
 *  com.adobe.granite.translation.api.TranslationConstants$ContentType
 *  com.adobe.granite.translation.api.TranslationException
 *  com.adobe.granite.translation.api.TranslationException$ErrorCode
 *  com.adobe.granite.translation.api.TranslationService
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  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.TranslationConstants;
import com.adobe.granite.translation.api.TranslationException;
import com.adobe.granite.translation.api.TranslationService;
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 org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

abstract class ContentFragmentTranslator {
    private static final Logger logger = LoggerFactory.getLogger(ContentFragmentTranslator.class);
    protected Resource contentFragmentRoot = null;
    protected TranslationService tSvc;
    protected TranslationRuleConfigurationFile ruleFile;
    protected ResourceResolver resourceResolver = null;
    protected static String MIME_TYPE_HTML = "text/html";
    protected static String MIME_TYPE_MARKDOWN = "text/x-markdown";
    protected static String MIME_TYPE_PLAIN = "text/plain";

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

    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 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);
            while (propIterator.hasNext()) {
                Property prop = propIterator.nextProperty();
                String strPropertyName = prop.getName();
                if (translationList == null || !translationList.contains(strPropertyName) || !this.processNodeProperty(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 Logger getLogger() {
        return logger;
    }

    protected boolean processVariations(Node contentFragmentNode) throws TranslationException, IOException, RepositoryException {
        logger.debug("FUNCTION: processVariations");
        boolean bSave = false;
        NodeIterator ntFileNodes = TranslationUtils.getAllNtFilesAtPath(this.resourceResolver, contentFragmentNode.getPath(), this.getLogger());
        if (ntFileNodes != null) {
            while (ntFileNodes.hasNext()) {
                Node variationNode = ntFileNodes.nextNode();
                if (!this.processVariation(variationNode)) continue;
                bSave = true;
            }
        }
        return bSave;
    }

    protected boolean processVariation(Node variationNode) throws TranslationException, RepositoryException {
        throw new TranslationException("Please override this function", TranslationException.ErrorCode.GENERAL_EXCEPTION);
    }

    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> 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);
            } else {
                translationList = this.getTranslatablePropertyListForNode(node);
            }
            while (propIterator.hasNext()) {
                Property prop = propIterator.nextProperty();
                String strPropertyName = prop.getName();
                if (translationList == null || !translationList.contains(strPropertyName) || !this.processNodeProperty(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) {
            String currentRootNodePath = currentRootNode.getPath();
            if (currentRootNodePath.endsWith(updateAssetDestinationNodePath)) {
                int preFixPathLength = currentRootNodePath.length() - updateAssetDestinationNodePath.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;
    }

    protected TranslationConstants.ContentType getContentType(Node variation) throws RepositoryException {
        TranslationConstants.ContentType retVal = TranslationConstants.ContentType.PLAIN;
        if (this.getMimeType(variation).equals(MIME_TYPE_HTML)) {
            retVal = TranslationConstants.ContentType.HTML;
        }
        return retVal;
    }

    protected String getMimeType(Node variation) throws RepositoryException {
        Node contentNode;
        String retVal = "";
        if (variation.hasNode("jcr:content") && (contentNode = variation.getNode("jcr:content")).hasProperty("jcr:mimeType")) {
            retVal = contentNode.getProperty("jcr:mimeType").getString();
        }
        return retVal;
    }
}