JcrResourceUtil.java 2.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  javax.jcr.ValueFactory
 */
package com.adobe.cq.dam.s7imaging.impl.jcr;

import java.io.InputStream;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.Calendar;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;

public class JcrResourceUtil {
    public static Value createValue(Object value, Session session) throws RepositoryException {
        ValueFactory fac = session.getValueFactory();
        Value val = value instanceof Calendar ? fac.createValue((Calendar)value) : (value instanceof InputStream ? fac.createValue(fac.createBinary((InputStream)value)) : (value instanceof Node ? fac.createValue((Node)value) : (value instanceof BigDecimal ? fac.createValue((BigDecimal)value) : (value instanceof Long ? fac.createValue(((Long)value).longValue()) : (value instanceof Short ? fac.createValue((long)((Short)value).shortValue()) : (value instanceof Integer ? fac.createValue((long)((Integer)value).intValue()) : (value instanceof Number ? fac.createValue(((Number)value).doubleValue()) : (value instanceof Boolean ? fac.createValue(((Boolean)value).booleanValue()) : (value instanceof String ? fac.createValue((String)value) : null)))))))));
        return val;
    }

    public static void setProperty(Node node, String propertyName, Object propertyValue) throws RepositoryException {
        if (propertyValue == null) {
            node.setProperty(propertyName, (String)null);
        } else if (propertyValue.getClass().isArray()) {
            int length = Array.getLength(propertyValue);
            Value[] setValues = new Value[length];
            for (int i = 0; i < length; ++i) {
                Object value = Array.get(propertyValue, i);
                setValues[i] = JcrResourceUtil.createValue(value, node.getSession());
            }
            node.setProperty(propertyName, setValues);
        } else {
            node.setProperty(propertyName, JcrResourceUtil.createValue(propertyValue, node.getSession()));
        }
    }
}