All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.fd.fp.util.PropertyUtils Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2013 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.adobe.fd.fp.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyType;
import javax.jcr.Value;
import javax.jcr.ValueFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author sharoon
 *
 */
public class PropertyUtils {
	
	/**
	 * Default Logger
	 */
	private static final Logger log = LoggerFactory.getLogger(PropertyUtils.class);

	public static void setBinaryValue(Node currentNode, String propertyName, Object propObj, ValueFactory factory) throws Exception {
		if (propObj instanceof InputStream) {
			InputStream inStream = (InputStream) propObj;
			currentNode.setProperty(propertyName, factory
					.createBinary(inStream));
		} else if (propObj instanceof Byte[]) {
			Byte[] bytes = (Byte[]) propObj;
			byte[] smallByte = new byte[bytes.length];
			for (int i = 0; i < bytes.length; ++i) {
				smallByte[i] = bytes[i];
			}
			ByteArrayInputStream byteStream = new ByteArrayInputStream(
					smallByte);
			currentNode.setProperty(propertyName, factory
					.createBinary(byteStream));

		} else if (propObj instanceof byte[]) {
			byte[] bytes = (byte[]) propObj;
			ByteArrayInputStream byteStream = new ByteArrayInputStream(
					bytes);
			currentNode.setProperty(propertyName, factory
					.createBinary(byteStream));
		}
        else if (propObj instanceof String) {
            byte[] bytes = ((String) propObj).getBytes("UTF-8");
            ByteArrayInputStream byteStream = new ByteArrayInputStream(
                    bytes);
            currentNode.setProperty(propertyName, factory
                    .createBinary(byteStream));
        }
	}
	
	public static Object getPropertyValue(Property property)
			throws Exception {
		Object propValue = null;
		switch (property.getType()) {
		case PropertyType.BINARY: 
			propValue = getBinaryValue(property);
			break;
		case PropertyType.BOOLEAN: 
			propValue = getBooleanValue(property);
			break;
		case PropertyType.DATE: 
			propValue = getDateValue(property);
			break;
		case PropertyType.DECIMAL: 
			propValue = getDecimalValue(property);
			break;
		case PropertyType.DOUBLE: 
			propValue = getDoubleValue(property);
			break;
		case PropertyType.LONG: 
			propValue = getLongValue(property);
			break;
		case PropertyType.STRING: 
			propValue = getStringValue(property);
			break;
		case PropertyType.PATH: 
		    propValue = getPathValue(property);
		    break;
		case PropertyType.REFERENCE: 
			break;
		case PropertyType.WEAKREFERENCE:
			// do nothing as this will be handled separately
			break;
		
		default: {
			log.warn("Unable to find the property type for "
					+ property.getName());
			propValue = getStringValue(property);
		}
		break;
		}
		return propValue;
	}
	
	private static Object getBooleanValue(
			Property property) throws Exception {
		Object propValue = null;
		if (property.isMultiple()) {
			List valueList = new ArrayList();
			propValue = valueList;
			Value[] values = property.getValues();
			for (Value value : values) {
				if (value != null) {
					valueList.add(value.getBoolean());
				}
			}
		} else {
			propValue = property.getBoolean();
		}
		return propValue;
	}
	private static Object getStringValue(
			Property property) throws Exception {
		Object propValue = null;
		if (property.isMultiple()) {
			List valueList = new ArrayList();
			propValue = valueList;
			Value[] values = property.getValues();
			for (Value value : values) {
				if (value != null) {
					valueList.add(value.getString());
				}
			}
		} else {
			propValue = property.getString();
		}
		return propValue;
	}
	
	private static Object getPathValue(
            Property property) throws Exception {
        Object propValue = null;
        if (property.isMultiple()) {
            List valueList = new ArrayList();
            propValue = valueList;
            Value[] values = property.getValues();
            for (Value value : values) {
                if (value != null) {
                    valueList.add(value.getString());
                }
            }
        } else {
            propValue = property.getString();
        }
        return propValue;
    }

	private static Object getDateValue(
			Property property) throws Exception {
		Object propValue = null;
		if (property.isMultiple()) {
			ArrayList valueList = new ArrayList();
			propValue = valueList;
			Value[] values = property.getValues();
			for (Value value : values) {
				if (value != null && value.getDate() != null) {
					valueList.add(value.getDate().getTime());
				}
			}
		} else {
			if (property.getDate() != null) {
				propValue = property.getDate().getTimeInMillis();
			}
		}
		return propValue;
	}
	
	private static Object getDoubleValue(
			Property property) throws Exception {
		Object propValue = null;
		if (property.isMultiple()) {
			List valueList = new ArrayList();
			propValue = valueList;
			Value[] values = property.getValues();
			for (Value value : values) {
				valueList.add(value.getDouble());
			}
		} else {
			propValue = property.getDouble();
		}
		return propValue;
	}
	
	private static Object getDecimalValue(
			Property property) throws Exception {
		Object propValue = null;
		if (property.isMultiple()) {
			List valueList = new ArrayList();
			propValue = valueList;
			Value[] values = property.getValues();
			for (Value value : values) {
				valueList.add(value.getDecimal());
			}
		} else {
			propValue = property.getDecimal();
		}
		return propValue;
	}
	
	private static Object getLongValue(
			Property property) throws Exception {
		Object propValue = null;
		if (property.isMultiple()) {
			List valueList = new ArrayList();
			propValue = valueList;
			Value[] values = property.getValues();
			for (Value value : values) {
				valueList.add(value.getLong());
			}
		} else {
			propValue = property.getLong();
		}
		return propValue;
	}
	
	private static Object getBinaryValue(
			Property property) throws Exception {
		Object propValue = null;
		if (property.isMultiple()) {
			List valueList = new ArrayList();
			propValue = valueList;
			Value[] values = property.getValues();
			for (Value value : values) {
				Binary binary = value.getBinary();
				if (binary != null) {
					InputStream inStream = binary.getStream();
					if (inStream != null) {
						byte[] bytes = new byte[inStream.available()];
						inStream.read(bytes);
						inStream.close();
						valueList.add(bytes);
					}
				}
			}
		} else {
			Binary binary = property.getBinary();
			if (binary != null) {
				InputStream inStream = binary.getStream();
				if (inStream != null) {
					byte[] bytes = new byte[inStream.available()];
					inStream.read(bytes);
					inStream.close();
					propValue = bytes;
				}
			}
		}
		return propValue;
	}
	
	private static Object getBinaryValueAsStream(
			Property property) throws Exception {
		Object propValue = null;
		if (property.isMultiple()) {
			List valueList = new ArrayList();
			propValue = valueList;
			Value[] values = property.getValues();
			for (Value value : values) {
				Binary binary = value.getBinary();
				if (binary != null) {
					InputStream inStream = binary.getStream();
					if (inStream != null) {
						valueList.add(inStream);
					}
				}
			}
		} else {
			Binary binary = property.getBinary();
			if (binary != null) {
				InputStream inStream = binary.getStream();
				if (inStream != null) {
					propValue = inStream;
				}
			}
		}
		return propValue;
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy