AssetUtils.java 3.43 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Item
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  javax.jcr.Value
 *  javax.jcr.nodetype.NodeDefinition
 *  javax.jcr.nodetype.NodeType
 *  javax.jcr.nodetype.PropertyDefinition
 *  org.apache.sling.api.resource.Resource
 */
package com.adobe.granite.asset.core.impl;

import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeDefinition;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.PropertyDefinition;
import org.apache.sling.api.resource.Resource;

public final class AssetUtils {
    public static Item copy(Item src, Node dstParent, String name) throws RepositoryException {
        if (src.isNode()) {
            return AssetUtils.copy((Node)src, dstParent, name);
        }
        return AssetUtils.copy((Property)src, dstParent, name);
    }

    public static Node copy(Node src, Node dstParent, String name) throws RepositoryException {
        return AssetUtils.copy(src, dstParent, name, false);
    }

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

    public static Property copy(Property src, Node dstParent, String name) throws RepositoryException {
        if (!src.getDefinition().isProtected()) {
            if (name == null) {
                name = src.getName();
            }
            if (dstParent.hasProperty(name)) {
                dstParent.getProperty(name).remove();
            }
            if (src.getDefinition().isMultiple()) {
                return dstParent.setProperty(name, src.getValues());
            }
            return dstParent.setProperty(name, src.getValue());
        }
        return null;
    }

    public static boolean isAsset(Resource resource) throws RepositoryException {
        Node node = (Node)resource.adaptTo(Node.class);
        return node.isNodeType("dam:Asset");
    }
}