svn commit: r1186093 - in /ofbiz/branches/jackrabbit20100709/framework: example/src/org/ofbiz/example/ jcr/src/org/ofbiz/jcr/api/ jcr/src/org/ofbiz/jcr/orm/jackrabbit/ jcr/src/org/ofbiz/jcr/test/ jcr/src/org/ofbiz/jcr/util/ jcr/src/org/ofbiz/jcr/util/j...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1186093 - in /ofbiz/branches/jackrabbit20100709/framework: example/src/org/ofbiz/example/ jcr/src/org/ofbiz/jcr/api/ jcr/src/org/ofbiz/jcr/orm/jackrabbit/ jcr/src/org/ofbiz/jcr/test/ jcr/src/org/ofbiz/jcr/util/ jcr/src/org/ofbiz/jcr/util/j...

sascharodekamp
Author: sascharodekamp
Date: Wed Oct 19 11:08:40 2011
New Revision: 1186093

URL: http://svn.apache.org/viewvc?rev=1186093&view=rev
Log:
UPDATE: Improve the file handling and extend the file uploading unit tests

Modified:
    ofbiz/branches/jackrabbit20100709/framework/example/src/org/ofbiz/example/JackrabbitEvents.java
    ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/JcrFileHelper.java
    ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitFile.java
    ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitFolder.java
    ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitHierarchyNode.java
    ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JcrTests.java
    ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/JcrUtil.java
    ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/jackrabbit/JcrUtilJackrabbit.java

Modified: ofbiz/branches/jackrabbit20100709/framework/example/src/org/ofbiz/example/JackrabbitEvents.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/example/src/org/ofbiz/example/JackrabbitEvents.java?rev=1186093&r1=1186092&r2=1186093&view=diff
==============================================================================
--- ofbiz/branches/jackrabbit20100709/framework/example/src/org/ofbiz/example/JackrabbitEvents.java (original)
+++ ofbiz/branches/jackrabbit20100709/framework/example/src/org/ofbiz/example/JackrabbitEvents.java Wed Oct 19 11:08:40 2011
@@ -243,6 +243,9 @@ public class JackrabbitEvents {
         } catch (ItemExistsException e) {
             Debug.logError(e, module);
             request.setAttribute("_ERROR_MESSAGE_", e.toString());
+        } catch (RepositoryException e) {
+            Debug.logError(e, module);
+            request.setAttribute("_ERROR_MESSAGE_", e.toString());
         } finally {
             fileHelper.closeContentSession();
         }

Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/JcrFileHelper.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/JcrFileHelper.java?rev=1186093&r1=1186092&r2=1186093&view=diff
==============================================================================
--- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/JcrFileHelper.java (original)
+++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/JcrFileHelper.java Wed Oct 19 11:08:40 2011
@@ -5,7 +5,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.GregorianCalendar;
 
-import javax.jcr.ItemExistsException;
+import javax.jcr.RepositoryException;
 
 import org.apache.jackrabbit.ocm.exception.ObjectContentManagerException;
 import org.apache.tika.Tika;
@@ -18,6 +18,7 @@ import org.ofbiz.jcr.orm.jackrabbit.Ofbi
 import org.ofbiz.jcr.orm.jackrabbit.OfbizRepositoryMappingJackrabbitFolder;
 import org.ofbiz.jcr.orm.jackrabbit.OfbizRepositoryMappingJackrabbitHierarchyNode;
 import org.ofbiz.jcr.orm.jackrabbit.OfbizRepositoryMappingJackrabbitResource;
+import org.ofbiz.jcr.util.jackrabbit.JcrUtilJackrabbit;
 
 /**
  * This Helper class encapsulate the jcr file content bean. it provide all
@@ -98,9 +99,9 @@ public class JcrFileHelper extends Abstr
      * @param folderPath
      * @param mimeType
      * @throws ObjectContentManagerException
-     * @throws ItemExistsException
+     * @throws RepositoryException
      */
-    public void storeContentInRepository(byte[] fileData, String fileName, String folderPath) throws ObjectContentManagerException, ItemExistsException {
+    public void storeContentInRepository(byte[] fileData, String fileName, String folderPath) throws ObjectContentManagerException, RepositoryException {
 
         // create an ORM Resource Object
         OfbizRepositoryMappingJackrabbitResource ormResource = new OfbizRepositoryMappingJackrabbitResource();
@@ -114,12 +115,25 @@ public class JcrFileHelper extends Abstr
         ormFile.setResource(ormResource);
         ormFile.setPath(fileName);
 
-        // create the ORM folder Object
-        OfbizRepositoryMappingJackrabbitFolder ormFolder = new OfbizRepositoryMappingJackrabbitFolder();
-        ormFolder.addChild(ormFile);
-        ormFolder.setPath(folderPath);
+        // Create the folder if necessary, otherwise we just update the folder content
+        folderPath = JcrUtilJackrabbit.createAbsoluteNodePath(folderPath);
+        if (access.getSession().itemExists(folderPath)) {
+            OfbizRepositoryMapping orm = access.getContentObject(folderPath);
+            if (orm instanceof OfbizRepositoryMappingJackrabbitFolder) {
+                OfbizRepositoryMappingJackrabbitFolder ormFolder = (OfbizRepositoryMappingJackrabbitFolder) orm;
+                ormFolder.addChild(ormFile);
+                access.updateContentObject(ormFolder);
+            }
+        } else {
+            // create the ORM folder Object
+            OfbizRepositoryMappingJackrabbitFolder ormFolder = new OfbizRepositoryMappingJackrabbitFolder();
+            ormFolder.addChild(ormFile);
+            ormFolder.setPath(folderPath);
+
+            access.storeContentObject(ormFolder);
+        }
+
 
-        access.storeContentObject(ormFolder);
     }
 
     /**

Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitFile.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitFile.java?rev=1186093&r1=1186092&r2=1186093&view=diff
==============================================================================
--- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitFile.java (original)
+++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitFile.java Wed Oct 19 11:08:40 2011
@@ -26,7 +26,7 @@ public class OfbizRepositoryMappingJackr
 
         // check that it is a relative path
         if (nodePath.indexOf("/") != -1) {
-            nodePath = nodePath.substring(nodePath.indexOf("/") + 1);
+            nodePath = nodePath.substring(nodePath.lastIndexOf("/") + 1);
         }
 
         super.path = nodePath;

Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitFolder.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitFolder.java?rev=1186093&r1=1186092&r2=1186093&view=diff
==============================================================================
--- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitFolder.java (original)
+++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitFolder.java Wed Oct 19 11:08:40 2011
@@ -9,7 +9,7 @@ import org.apache.jackrabbit.ocm.mapper.
 
 @Node(jcrType = "nt:folder", extend = OfbizRepositoryMappingJackrabbitHierarchyNode.class)
 public class OfbizRepositoryMappingJackrabbitFolder extends OfbizRepositoryMappingJackrabbitHierarchyNode {
-    @Collection(autoUpdate = false, elementClassName = OfbizRepositoryMappingJackrabbitHierarchyNode.class, collectionConverter = NTCollectionConverterImpl.class)
+    @Collection(autoUpdate = true, jcrSameNameSiblings = false, elementClassName = OfbizRepositoryMappingJackrabbitHierarchyNode.class, collectionConverter = NTCollectionConverterImpl.class)
     private List<OfbizRepositoryMappingJackrabbitHierarchyNode> children;
 
     public List<OfbizRepositoryMappingJackrabbitHierarchyNode> getChildren() {

Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitHierarchyNode.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitHierarchyNode.java?rev=1186093&r1=1186092&r2=1186093&view=diff
==============================================================================
--- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitHierarchyNode.java (original)
+++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/orm/jackrabbit/OfbizRepositoryMappingJackrabbitHierarchyNode.java Wed Oct 19 11:08:40 2011
@@ -4,8 +4,8 @@ import java.util.Calendar;
 
 import org.apache.jackrabbit.ocm.mapper.impl.annotation.Field;
 import org.apache.jackrabbit.ocm.mapper.impl.annotation.Node;
-import org.ofbiz.jcr.access.jackrabbit.ConstantsJackrabbit;
 import org.ofbiz.jcr.orm.OfbizRepositoryMapping;
+import org.ofbiz.jcr.util.jackrabbit.JcrUtilJackrabbit;
 
 @Node(jcrType = "nt:hierarchyNode", jcrMixinTypes="mix:versionable")
 public class OfbizRepositoryMappingJackrabbitHierarchyNode implements OfbizRepositoryMapping {
@@ -21,11 +21,7 @@ public class OfbizRepositoryMappingJackr
 
     public void setPath(String nodePath) {
         // check if the node path is an absolute path
-        if (!nodePath.startsWith(ConstantsJackrabbit.ROOTPATH)) {
-            nodePath = ConstantsJackrabbit.ROOTPATH + nodePath;
-        }
-
-        this.path = nodePath;
+        this.path = JcrUtilJackrabbit.createAbsoluteNodePath(nodePath);
     }
 
     public Calendar getCreationDate() {

Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JcrTests.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JcrTests.java?rev=1186093&r1=1186092&r2=1186093&view=diff
==============================================================================
--- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JcrTests.java (original)
+++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JcrTests.java Wed Oct 19 11:08:40 2011
@@ -98,7 +98,7 @@ public class JcrTests extends OFBizTestC
         assertEquals("1.2", repositoryAccess.getBaseVersion("/news/today/en"));
     }
 
-    public void testLanguageDetermination() throws Exception{
+    public void testLanguageDetermination() throws Exception {
         JcrArticleHelper helper = new JcrArticleHelper(userLogin);
 
         helper.storeContentInRepository("news/tomorrow", "en", "The news for tomorrow.", "Content.", new GregorianCalendar());
@@ -125,7 +125,8 @@ public class JcrTests extends OFBizTestC
      */
     public void testCreateRepositoryFileNode() throws Exception {
         File f = new File("stopofbiz.sh");
-        assertTrue(f.exists());
+        File f2 = new File("README");
+        assertTrue(f.exists() && f2.exists());
 
         InputStream file = new FileInputStream(f);
 
@@ -144,6 +145,31 @@ public class JcrTests extends OFBizTestC
         repositoryAccess.storeContentObject(ormFolder);
     }
 
+    /*
+     * Test the File upload - Add a second file to the same folder
+     */
+    public void testCreateRepositoryFileNode_2() throws Exception {
+        File f = new File("README");
+        assertTrue(f.exists());
+
+        InputStream file = new FileInputStream(f);
+
+        OfbizRepositoryMappingJackrabbitResource ormResource = new OfbizRepositoryMappingJackrabbitResource();
+        ormResource.setData(file);
+
+        OfbizRepositoryMappingJackrabbitFile ormFile = new OfbizRepositoryMappingJackrabbitFile();
+        ormFile.setResource(ormResource);
+        // have to be relative
+        ormFile.setPath(f.getName());
+
+        OfbizRepositoryMappingJackrabbitFolder ormFolder = (OfbizRepositoryMappingJackrabbitFolder) repositoryAccess.getContentObject("/fileHome");
+        ormFolder.addChild(ormFile);
+
+        // When we add a file to an existing folder we have to use the update
+        // method - this is something the FileHelper Api is doing for you.
+        repositoryAccess.updateContentObject(ormFolder);
+    }
+
     public void testRemoveRepositoryFileNode() throws Exception {
         repositoryAccess.removeContentObject("/fileHome");
     }

Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/JcrUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/JcrUtil.java?rev=1186093&r1=1186092&r2=1186093&view=diff
==============================================================================
--- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/JcrUtil.java (original)
+++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/JcrUtil.java Wed Oct 19 11:08:40 2011
@@ -1,11 +1,5 @@
 package org.ofbiz.jcr.util;
 
-import java.util.List;
-import java.util.Map;
-
-import javax.jcr.RepositoryException;
-
-import org.ofbiz.entity.GenericValue;
 
 public interface JcrUtil {
 

Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/jackrabbit/JcrUtilJackrabbit.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/jackrabbit/JcrUtilJackrabbit.java?rev=1186093&r1=1186092&r2=1186093&view=diff
==============================================================================
--- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/jackrabbit/JcrUtilJackrabbit.java (original)
+++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/jackrabbit/JcrUtilJackrabbit.java Wed Oct 19 11:08:40 2011
@@ -13,78 +13,104 @@ import javolution.util.FastMap;
 
 import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.entity.GenericValue;
+import org.ofbiz.jcr.access.jackrabbit.ConstantsJackrabbit;
 import org.ofbiz.jcr.loader.JCRFactoryUtil;
 import org.ofbiz.jcr.util.JcrUtil;
 
 public class JcrUtilJackrabbit implements JcrUtil {
 
- public static final String module = JcrUtilJackrabbit.class.getName();
+    public static final String module = JcrUtilJackrabbit.class.getName();
 
- /**
- * A method to list all nodes in the repository. The result List contains
- * the node path and the node type.
- *
- * @param startNodePath
- * @return
- * @throws RepositoryException
- */
- public static List<Map<String, String>> getRepositoryNodes(GenericValue userLogin, String startNodePath) throws RepositoryException {
- List<Map<String, String>> returnList = null;
- Session session = JCRFactoryUtil.getSession();
-
- try {
- returnList = getRepositoryNodes(session, startNodePath);
- } catch (RepositoryException e) {
- throw new RepositoryException(e);
- } finally {
- session.logout();
- }
-
- return returnList;
- }
-
- /**
- * Just a dummy method to list all nodes in the repository.
- *
- * @param startNodePath
- * @return
- * @throws RepositoryException
- */
- private static List<Map<String, String>> getRepositoryNodes(Session session, String startNodePath) throws RepositoryException {
- Node node = null;
-
- List<Map<String, String>> nodeList = FastList.newInstance();
- if (UtilValidate.isEmpty(startNodePath)) {
- node = session.getRootNode();
- } else {
- node = session.getNode(startNodePath);
- }
-
- NodeIterator nodeIterator = node.getNodes();
- Map<String, String> nodeEntry = null;
- while (nodeIterator.hasNext()) {
- Node n = nodeIterator.nextNode();
-
- // recursion - get all subnodes and add the results to our nodeList
- if (n.getNodes().hasNext()) {
- nodeList.addAll(getRepositoryNodes(session, n.getPath()));
- }
-
- nodeEntry = FastMap.newInstance();
-
- // if the node path is a jcr:system node than ignore this
- // entry
- if (n.getPath().startsWith("/jcr:system")) {
- continue;
- }
-
- nodeEntry.put("path", n.getPath());
-
- nodeEntry.put("primaryNodeType", n.getPrimaryNodeType().getName());
-
- nodeList.add(nodeEntry);
- }
-
- return nodeList;
- }
+    /**
+     * A method to list all nodes in the repository. The result List contains
+     * the node path and the node type.
+     *
+     * @param startNodePath
+     * @return
+     * @throws RepositoryException
+     */
+    public static List<Map<String, String>> getRepositoryNodes(GenericValue userLogin, String startNodePath) throws RepositoryException {
+        List<Map<String, String>> returnList = null;
+        Session session = JCRFactoryUtil.getSession();
+
+        try {
+            returnList = getRepositoryNodes(session, startNodePath);
+        } catch (RepositoryException e) {
+            throw new RepositoryException(e);
+        } finally {
+            session.logout();
+        }
+
+        return returnList;
+    }
+
+    /**
+     * Just a dummy method to list all nodes in the repository.
+     *
+     * @param startNodePath
+     * @return
+     * @throws RepositoryException
+     */
+    private static List<Map<String, String>> getRepositoryNodes(Session session, String startNodePath) throws RepositoryException {
+        Node node = null;
+
+        List<Map<String, String>> nodeList = FastList.newInstance();
+        if (UtilValidate.isEmpty(startNodePath)) {
+            node = session.getRootNode();
+        } else {
+            node = session.getNode(startNodePath);
+        }
+
+        NodeIterator nodeIterator = node.getNodes();
+        Map<String, String> nodeEntry = null;
+        while (nodeIterator.hasNext()) {
+            Node n = nodeIterator.nextNode();
+
+            // recursion - get all subnodes and add the results to our nodeList
+            if (n.getNodes().hasNext()) {
+                nodeList.addAll(getRepositoryNodes(session, n.getPath()));
+            }
+
+            nodeEntry = FastMap.newInstance();
+
+            // if the node path is a jcr:system node than ignore this
+            // entry
+            if (n.getPath().startsWith("/jcr:system")) {
+                continue;
+            }
+
+            nodeEntry.put("path", n.getPath());
+
+            nodeEntry.put("primaryNodeType", n.getPrimaryNodeType().getName());
+
+            nodeList.add(nodeEntry);
+        }
+
+        return nodeList;
+    }
+
+    /**
+     * If the past node path is no absolute path, an absolute path will be
+     * created.
+     *
+     * @param nodePath
+     * @return
+     */
+    public static String createAbsoluteNodePath(String nodePath) {
+        if (!checkIfNodePathIsAbsolute(nodePath)) {
+            nodePath = ConstantsJackrabbit.ROOTPATH + nodePath;
+        }
+
+        return nodePath;
+    }
+
+    /**
+     * Returns true if the passed node path is an absolute node path.
+     *
+     * @param nodePath
+     * @return
+     */
+    public static boolean checkIfNodePathIsAbsolute(String nodePath) {
+        return nodePath.startsWith(ConstantsJackrabbit.ROOTPATH);
+    }
 }