CQPageMachineTranslator.java 10.2 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.translation.api.TranslationConstants
 *  com.adobe.granite.translation.api.TranslationConstants$ContentType
 *  com.adobe.granite.translation.api.TranslationException
 *  com.adobe.granite.translation.api.TranslationResult
 *  com.adobe.granite.translation.api.TranslationService
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  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.cq.wcm.translation.impl.CQPageTranslator;
import com.adobe.cq.wcm.translation.impl.TranslationObjectImpl;
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.TranslationResult;
import com.adobe.granite.translation.api.TranslationService;
import java.io.IOException;
import java.util.ArrayList;
import javax.jcr.Node;
import javax.jcr.Property;
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;

public class CQPageMachineTranslator
extends CQPageTranslator {
    private ArrayList<PropertyNodeCache> cacheList = new ArrayList();
    private static final Logger log = LoggerFactory.getLogger(CQPageMachineTranslator.class);

    public CQPageMachineTranslator(TranslationRuleConfigurationFile ruleFile, TranslationService tSvc) {
        super(ruleFile, tSvc);
    }

    @Override
    protected boolean processTagElement(Node tagNode, String strTagAttributeName, String strSourceTagTitle) {
        boolean bRetVal = false;
        try {
            if (tagNode.hasProperty(strTagAttributeName)) {
                if (tagNode.getProperty(strTagAttributeName) == null) {
                    tagNode.setProperty(strTagAttributeName, strSourceTagTitle);
                }
            } else {
                tagNode.setProperty(strTagAttributeName, strSourceTagTitle);
            }
            this.addToCacheQueue(tagNode, strTagAttributeName);
            bRetVal = true;
        }
        catch (RepositoryException e) {
            log.error("Error while processing Tag Element {}", (Throwable)e);
        }
        return bRetVal;
    }

    @Override
    protected boolean processNodeProperty(Node node, Property prop, String strPropertyName) {
        log.debug("FUNCTION: translateNodeProperty ", (Object)strPropertyName);
        this.addToCacheQueue(node, strPropertyName);
        return true;
    }

    private void addToCacheQueue(Node node, String strPropertyName) {
        try {
            PropertyNodeCache propertyNode = new PropertyNodeCache();
            propertyNode.strNodePath = node.getPath();
            propertyNode.strPropertyName = strPropertyName;
            this.cacheList.add(propertyNode);
        }
        catch (RepositoryException e) {
            log.error("Error while adding node to cache list " + strPropertyName);
        }
    }

    public boolean translateResourceList(ResourceResolver resourceResolver, ArrayList<Resource> resourceList, TranslationObjectImpl.TranslationObjectType objectType) throws RepositoryException, TranslationException, IOException {
        boolean bSaveRequired = false;
        ArrayList<String> tagList = new ArrayList<String>();
        for (Resource childResource : resourceList) {
            if (objectType == TranslationObjectImpl.TranslationObjectType.TAGMETADATA) {
                this.processTagTranslationNode((Node)childResource.adaptTo(Node.class), tagList);
                continue;
            }
            if (!this.processTranslationOrTemporaryUpdateNode((Node)childResource.adaptTo(Node.class))) continue;
            bSaveRequired = true;
        }
        if (objectType == TranslationObjectImpl.TranslationObjectType.TAGMETADATA) {
            bSaveRequired = this.processTagListNow(tagList);
        }
        if (this.saveCacheQueue(resourceResolver)) {
            bSaveRequired = true;
        }
        return bSaveRequired;
    }

    private boolean saveCacheQueue(ResourceResolver resourceResolver) {
        if (this.cacheList != null && this.cacheList.size() > 0) {
            ArrayList<Property> propList = new ArrayList<Property>();
            for (int index = 0; index < this.cacheList.size(); ++index) {
                try {
                    Node node;
                    PropertyNodeCache cacheObj = this.cacheList.get(index);
                    Resource resource = resourceResolver.resolve(cacheObj.strNodePath);
                    if (resource == null || (node = (Node)resource.adaptTo(Node.class)) == null) continue;
                    Property prop = node.getProperty(cacheObj.strPropertyName);
                    if (prop != null) {
                        propList.add(prop);
                        continue;
                    }
                    log.trace("Property is null for " + cacheObj.strPropertyName);
                    continue;
                }
                catch (Exception ex) {
                    log.error("Error while getting property ", (Throwable)ex);
                }
            }
            try {
                this.translatePropertyList(propList);
            }
            catch (Exception ex) {
                log.error("Error while translating property ", (Throwable)ex);
            }
        }
        this.cacheList.clear();
        return true;
    }

    private void translatePropertyList(ArrayList<Property> propList) throws TranslationException, RepositoryException {
        String contentCategory = this.tSvc.getDefaultCategory();
        String strSrcLang = this.ruleFile.getSourceLanguage();
        String strDstLang = this.ruleFile.getDestinationLanguage();
        TranslationConstants.ContentType contentType = TranslationConstants.ContentType.HTML;
        String[] strStringArrayToTranslate = this.getTranslatableStringArray(propList);
        TranslationResult[] tResultArray = this.tSvc.translateArray(strStringArrayToTranslate, strSrcLang, strDstLang, contentType, contentCategory);
        this.saveTranslatedProperties(propList, tResultArray);
    }

    private void saveTranslatedProperties(ArrayList<Property> propList, TranslationResult[] tResultArray) throws RepositoryException {
        int currentCount = 0;
        for (Property prop : propList) {
            if (prop.isMultiple()) {
                Value[] val_array = prop.getValues();
                if (val_array == null || val_array.length <= 0) continue;
                ArrayList<String> strOutputArray = new ArrayList<String>();
                for (int index = 0; index < val_array.length; ++index) {
                    Value value = val_array[index];
                    String strTranslationText = this.getTranslatableString(value);
                    if (StringUtils.isEmpty((CharSequence)strTranslationText)) continue;
                    String strNewTranslationText = this.getTranslatedText(value, strTranslationText, tResultArray[currentCount]);
                    if (!StringUtils.isEmpty((CharSequence)strNewTranslationText)) {
                        strOutputArray.add(strNewTranslationText);
                    }
                    ++currentCount;
                }
                String[] newVals = TranslationUtils.getStringArray(strOutputArray);
                prop.setValue(newVals);
                continue;
            }
            Value value = prop.getValue();
            String strTranslationText = this.getTranslatableString(value);
            if (StringUtils.isEmpty((CharSequence)strTranslationText)) continue;
            String strNewTranslationText = this.getTranslatedText(value, strTranslationText, tResultArray[currentCount]);
            if (!StringUtils.isEmpty((CharSequence)strNewTranslationText)) {
                prop.setValue(strNewTranslationText);
            }
            ++currentCount;
        }
    }

    private String getTranslatedText(Value value, String strTranslationText, TranslationResult translationResult) {
        String strRetVal = "";
        String strOriginalTranslationText = translationResult.getSourceString();
        if (strTranslationText.equals(strOriginalTranslationText)) {
            strRetVal = translationResult.getTranslation();
        } else {
            log.warn("Translated Text is not same as what we sent, please check");
        }
        return strRetVal;
    }

    private String[] getTranslatableStringArray(ArrayList<Property> propList) throws RepositoryException {
        ArrayList<String> strOutput = new ArrayList<String>();
        for (Property prop : propList) {
            if (prop.isMultiple()) {
                Value[] val_array = prop.getValues();
                if (val_array == null || val_array.length <= 0) continue;
                for (int index = 0; index < val_array.length; ++index) {
                    Value value = val_array[index];
                    String strTranslationText = this.getTranslatableString(value);
                    if (StringUtils.isEmpty((CharSequence)strTranslationText)) continue;
                    strOutput.add(strTranslationText);
                }
                continue;
            }
            Value value = prop.getValue();
            String strTranslationText = this.getTranslatableString(value);
            if (StringUtils.isEmpty((CharSequence)strTranslationText)) continue;
            strOutput.add(strTranslationText);
        }
        return TranslationUtils.getStringArray(strOutput);
    }

    private String getTranslatableString(Value value) throws IllegalStateException, RepositoryException {
        if (value != null && value.getType() == 1) {
            return value.getString();
        }
        return null;
    }

    class PropertyNodeCache {
        String strNodePath;
        String strPropertyName;

        PropertyNodeCache() {
        }
    }

}