MobileAppJCRUtil.java 4.41 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.nodetype.NodeDefinition
 *  javax.jcr.nodetype.NodeType
 *  org.apache.commons.lang.StringUtils
 *  org.apache.jackrabbit.commons.JcrUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.platform.impl.utils;

import com.day.cq.commons.jcr.JcrUtil;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeDefinition;
import javax.jcr.nodetype.NodeType;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MobileAppJCRUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(MobileAppJCRUtil.class);

    public static Node copy(Node src, Node dstParent, boolean bestEffort, boolean deep) throws RepositoryException {
        String name = src.getName();
        Node dst = null;
        dst = dstParent.hasNode(name) ? dstParent.getNode(name) : dstParent.addNode(name, src.getPrimaryNodeType().getName());
        for (NodeType mix : src.getMixinNodeTypes()) {
            try {
                dst.addMixin(mix.getName());
                continue;
            }
            catch (RepositoryException e) {
                MobileAppJCRUtil.assertBestEffort(bestEffort, e);
            }
        }
        PropertyIterator iter = src.getProperties();
        while (iter.hasNext()) {
            try {
                JcrUtil.copy((Property)iter.nextProperty(), (Node)dst, (String)null);
            }
            catch (RepositoryException e) {
                MobileAppJCRUtil.assertBestEffort(bestEffort, e);
            }
        }
        if (deep) {
            if (dst.hasNode("jcr:content")) {
                dst.getNode("jcr:content").remove();
            }
            iter = src.getNodes();
            while (iter.hasNext()) {
                Node n = iter.nextNode();
                if (n.getDefinition().isProtected()) continue;
                try {
                    MobileAppJCRUtil.copy(n, dst, bestEffort, deep);
                }
                catch (RepositoryException e) {
                    MobileAppJCRUtil.assertBestEffort(bestEffort, e);
                }
            }
        }
        return dst;
    }

    private static void assertBestEffort(boolean bestEffort, RepositoryException ex) throws RepositoryException {
        if (!bestEffort) {
            throw ex;
        }
        LOGGER.warn("Copy error ignored, best effort set to true", (Throwable)ex);
    }

    public static void setMobileAppProperty(Resource resource, String path, String[] value) throws RepositoryException {
        String trimmedParamName = null;
        int pos = path.lastIndexOf(47);
        String childPath = "";
        if (pos > 0) {
            childPath = path.substring(0, pos);
            trimmedParamName = path.substring(pos + 1);
        } else if (pos == -1) {
            childPath = ".";
            trimmedParamName = path;
        }
        Node parentNode = JcrUtils.getOrCreateByPath((String)(resource.getPath() + "/" + childPath), (String)"nt:unstructured", (Session)((Session)resource.getResourceResolver().adaptTo(Session.class)));
        boolean hasProperty = parentNode.hasProperty(trimmedParamName);
        if (value == null || value.length == 1 && StringUtils.isEmpty((String)value[0])) {
            if (hasProperty) {
                Property property = parentNode.getProperty(trimmedParamName);
                property.setValue((String)null);
            }
        } else {
            if (hasProperty) {
                Property property = parentNode.getProperty(trimmedParamName);
                property.setValue((String)null);
            }
            if (value.length > 1) {
                parentNode.setProperty(trimmedParamName, value);
            } else {
                parentNode.setProperty(trimmedParamName, value[0]);
            }
        }
    }
}