TranslationBaseObject.java 7.96 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceWrapper
 *  org.slf4j.Logger
 */
package com.adobe.cq.wcm.translation.impl;

import com.adobe.cq.wcm.translation.impl.TranslationUtils;
import com.adobe.cq.wcm.translation.impl.scheduler.SyncTranslationState;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceWrapper;
import org.slf4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class TranslationBaseObject
extends ResourceWrapper {
    private static int MILLISEC_IN_MINUTE = 60000;
    protected javax.jcr.Node node;
    Session userSession;

    public TranslationBaseObject(javax.jcr.Node node, Resource resource, Session userSession) {
        super(resource);
        this.userSession = userSession;
        this.node = node;
    }

    public javax.jcr.Node getNode() {
        return this.node;
    }

    protected Logger getLogger() {
        return null;
    }

    protected int getIntAttribute(String strAttributeName) {
        return (int)this.getLongAttribute(strAttributeName);
    }

    protected long getLongAttribute(String strAttributeName) {
        try {
            Property property;
            if (this.node.hasProperty(strAttributeName) && (property = this.node.getProperty(strAttributeName)) != null && property.getType() == 3) {
                return property.getLong();
            }
        }
        catch (RepositoryException ex) {
            this.getLogger().error("Error while fetching int attribute " + strAttributeName, (Throwable)ex);
        }
        return 0;
    }

    protected boolean getBooleanAttribute(String strAttributeName, boolean bDefault) {
        try {
            Property property;
            if (this.node.hasProperty(strAttributeName) && (property = this.node.getProperty(strAttributeName)) != null && property.getType() == 6) {
                bDefault = property.getBoolean();
            }
        }
        catch (RepositoryException ex) {
            this.getLogger().error("Error while fetching boolean attribute " + strAttributeName, (Throwable)ex);
        }
        return bDefault;
    }

    protected Calendar getCalendarAttribute(String strAttributeName) {
        return TranslationUtils.getCalendarAttribute(this.getLogger(), (Resource)this, strAttributeName);
    }

    protected String getStringAttribute(String strAttributeName) {
        return TranslationUtils.getStringAttribute(this.getLogger(), this.node, strAttributeName, null);
    }

    protected ArrayList<String> getStringListAttribute(String strAttributeName) {
        ArrayList<String> retVal = new ArrayList<String>();
        try {
            Value[] values;
            Property property;
            if (this.node.hasProperty(strAttributeName) && (property = this.node.getProperty(strAttributeName)) != null && property.isMultiple() && (values = property.getValues()) != null) {
                for (Value val : values) {
                    retVal.add(val.getString());
                }
            }
        }
        catch (RepositoryException ex) {
            this.getLogger().error("Error while fetching string attribute " + strAttributeName, (Throwable)ex);
        }
        return retVal;
    }

    protected void setAttribute(String strAttributeName, Calendar attributeValue) {
        try {
            this.node.setProperty(strAttributeName, attributeValue);
        }
        catch (RepositoryException ex) {
            this.getLogger().error("Error while setting string attribute " + strAttributeName + " with value " + attributeValue.toString(), (Throwable)ex);
        }
    }

    protected void setAttribute(String strAttributeName, ArrayList<String> attributeValueList) {
        try {
            Property prop = null;
            if (this.node.hasProperty(strAttributeName)) {
                prop = this.node.getProperty(strAttributeName);
            }
            if (prop == null) {
                prop = this.node.setProperty(strAttributeName, new String[0]);
            }
            if (attributeValueList == null) {
                prop.setValue((String[])null);
            } else {
                String[] newValArray = TranslationUtils.getStringArray(attributeValueList);
                prop.setValue(newValArray);
            }
        }
        catch (RepositoryException ex) {
            this.getLogger().error("Error while setting StringList attribute " + strAttributeName + " with value " + attributeValueList.toString(), (Throwable)ex);
        }
    }

    private void setAttribute(String strAttributeName, long longVal) {
        try {
            this.node.setProperty(strAttributeName, longVal);
        }
        catch (RepositoryException ex) {
            this.getLogger().error("Error while setting long attribute " + strAttributeName + " with value " + longVal, (Throwable)ex);
        }
    }

    protected void setAttribute(String strAttributeName, int iValue) {
        try {
            this.node.setProperty(strAttributeName, (long)iValue);
        }
        catch (RepositoryException ex) {
            this.getLogger().error("Error while setting int attribute " + strAttributeName + " with value " + iValue, (Throwable)ex);
        }
    }

    protected void setAttribute(String strAttributeName, String strAttributeValue) {
        try {
            this.node.setProperty(strAttributeName, strAttributeValue);
        }
        catch (RepositoryException ex) {
            this.getLogger().error("Error while setting string attribute " + strAttributeName + " with value " + strAttributeValue, (Throwable)ex);
        }
    }

    protected void setAttribute(String strAttributeName, boolean bValue) {
        try {
            this.node.setProperty(strAttributeName, bValue);
        }
        catch (RepositoryException ex) {
            this.getLogger().error("Error while setting string attribute " + strAttributeName + " with value " + bValue, (Throwable)ex);
        }
    }

    protected String getXMLNodeProperty(Node node, String attributeName) {
        Element element = (Element)node;
        if (element != null) {
            return element.getAttribute(attributeName);
        }
        return null;
    }

    protected void setXMLNodePropertyValue(Node node, String attributeName, String strValue) {
        ((Element)node).setAttribute(attributeName, strValue);
    }

    public void save() throws RepositoryException {
        if (this.userSession != null) {
            this.userSession.save();
        }
    }

    public void setTranslationObjectID(String strID) {
        this.setAttribute("translationObjectID", strID);
    }

    public String getTranslationObjectID() {
        return this.getStringAttribute("translationObjectID");
    }

    protected boolean isSyncTranslationStateInProgress() {
        long iStartSince;
        boolean bRetVal = false;
        if (this.getBooleanAttribute("syncTranslationStateInProgress", false) && (iStartSince = this.getLongAttribute("syncTranslationStartSince")) > 0) {
            long currentTime = new Date().getTime();
            long waitTime = (long)MILLISEC_IN_MINUTE * SyncTranslationState.getSyncLockTimeout();
            if (iStartSince + waitTime > currentTime) {
                bRetVal = true;
            }
        }
        return bRetVal;
    }

    protected void setSyncTranslationStateInProgress(boolean bState, Logger logger) {
        this.setAttribute("syncTranslationStateInProgress", bState);
        this.setAttribute("syncTranslationStartSince", new Date().getTime());
        try {
            this.save();
        }
        catch (RepositoryException e) {
            logger.error("Error while saving ", (Throwable)e);
        }
    }
}