Author: cziegeler
Date: Mon Jun 23 05:37:55 2008
New Revision: 670545

URL: http://svn.apache.org/viewvc?rev=670545&view=rev
Log:
SLING-548: Add methods for setting properties from objects.

Modified:
    
incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java
    
incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java

Modified: 
incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java?rev=670545&r1=670544&r2=670545&view=diff
==============================================================================
--- 
incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java
 (original)
+++ 
incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentCreator.java
 Mon Jun 23 05:37:55 2008
@@ -20,23 +20,79 @@
 
 import javax.jcr.RepositoryException;
 
+/**
+ * The <code>ContentCreator</code>
+ * is used by the [EMAIL PROTECTED] ContentReader} to create the actual 
content.
+ */
 interface ContentCreator {
 
+    /**
+     * Create a new node.
+     * To add properties to this node, one of the createProperty() methods
+     * should be called.
+     * To add child nodes, this method should be called to create a new child 
node.
+     * If all properties and child nodes have been added [EMAIL PROTECTED] 
#finishNode()} must be called.
+     *
+     * @param name The name of the node.
+     * @param primaryNodeType The primary node type or null.
+     * @param mixinNodeTypes The mixin node types or null.
+     * @throws RepositoryException If anything goes wrong.
+     */
     void createNode(String name,
                     String primaryNodeType,
                     String[] mixinNodeTypes)
     throws RepositoryException;
 
+    /**
+     * Indicates that a node is finished.
+     * The parent node of the current node becomes the current node.
+     * @throws RepositoryException
+     */
     void finishNode()
     throws RepositoryException;
 
+    /**
+     * Create a new property to the current node.
+     * @param name The property name.
+     * @param propertyType The type of the property.
+     * @param value The string value.
+     * @throws RepositoryException
+     */
     void createProperty(String name,
                         int    propertyType,
                         String value)
     throws RepositoryException;
 
+    /**
+     * Create a new multi value property to the current node.
+     * @param name The property name.
+     * @param propertyType The type of the property.
+     * @param values The string values.
+     * @throws RepositoryException
+     */
     void createProperty(String name,
                         int    propertyType,
                         String[] values)
     throws RepositoryException;
+
+    /**
+     * Add a new property to the current node.
+     * @param name The property name.
+     * @param value The value.
+     * @throws RepositoryException
+     */
+    void createProperty(String name,
+                        Object value)
+    throws RepositoryException;
+
+    /**
+     * Add a new multi value property to the current node.
+     * @param name The property name.
+     * @param propertyType The type of the property.
+     * @param values The values.
+     * @throws RepositoryException
+     */
+    void createProperty(String name,
+                        Object[] values)
+    throws RepositoryException;
 }

Modified: 
incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java?rev=670545&r1=670544&r2=670545&view=diff
==============================================================================
--- 
incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
 (original)
+++ 
incubator/sling/trunk/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/ContentLoader.java
 Mon Jun 23 05:37:55 2008
@@ -18,7 +18,10 @@
  */
 package org.apache.sling.jcr.contentloader.internal;
 
+import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -29,6 +32,8 @@
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
 
 /**
  * The <code>ContentLoader</code> creates the nodes and properties.
@@ -194,6 +199,74 @@
         node.setProperty(name, values, propertyType);
     }
 
+    protected Value createValue(final ValueFactory factory, Object value) {
+        if ( value == null ) {
+            return null;
+        }
+        if ( value instanceof Long ) {
+            return factory.createValue((Long)value);
+        } else if ( value instanceof Date ) {
+            final Calendar c = Calendar.getInstance();
+            c.setTime((Date)value);
+            return factory.createValue(c);
+        } else if ( value instanceof Calendar ) {
+            return factory.createValue((Calendar)value);
+        } else if ( value instanceof Double ) {
+            return factory.createValue((Double)value);
+        } else if ( value instanceof Boolean ) {
+            return factory.createValue((Boolean)value);
+        } else if ( value instanceof InputStream ) {
+            return factory.createValue((InputStream)value);
+        } else {
+            return factory.createValue(value.toString());
+        }
+
+    }
+    /**
+     * @see 
org.apache.sling.jcr.contentloader.internal.ContentCreator#createProperty(java.lang.String,
 java.lang.Object)
+     */
+    public void createProperty(String name, Object value)
+    throws RepositoryException {
+        final Node node = this.parentNodeStack.peek();
+        // check if the property already exists, don't overwrite it in this 
case
+        if (node.hasProperty(name)
+            && !node.getProperty(name).isNew()) {
+            return;
+        }
+        if ( value == null ) {
+            if ( node.hasProperty(name) ) {
+                node.getProperty(name).remove();
+            }
+        } else {
+            final Value jcrValue = 
this.createValue(node.getSession().getValueFactory(), value);
+            node.setProperty(name, jcrValue);
+        }
+    }
+
+    /**
+     * @see 
org.apache.sling.jcr.contentloader.internal.ContentCreator#createProperty(java.lang.String,
 java.lang.Object[])
+     */
+    public void createProperty(String name, Object[] values)
+    throws RepositoryException {
+        final Node node = this.parentNodeStack.peek();
+        // check if the property already exists, don't overwrite it in this 
case
+        if (node.hasProperty(name)
+            && !node.getProperty(name).isNew()) {
+            return;
+        }
+        if ( values == null || values.length == 0 ) {
+            if ( node.hasProperty(name) ) {
+                node.getProperty(name).remove();
+            }
+        } else {
+            final Value[] jcrValues = new Value[values.length];
+            for(int i = 0; i < values.length; i++) {
+                jcrValues[i] = 
this.createValue(node.getSession().getValueFactory(), values[i]);
+            }
+            node.setProperty(name, jcrValues);
+        }
+    }
+
     /**
      * @see 
org.apache.sling.jcr.contentloader.internal.ContentCreator#finishNode()
      */


Reply via email to