]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/JcrFileProvider.java
Update license headers
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui.jcr / src / main / java / org / argeo / eclipse / ui / jcr / utils / JcrFileProvider.java
index 6cf4d29c58ba058d0b5a6181de67eeab2d8cda33..2906197f2d3a78c78b843f22a812da1d404fbdcb 100644 (file)
@@ -1,14 +1,29 @@
+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.argeo.eclipse.ui.jcr.utils;
 
 import java.io.InputStream;
 
 import javax.jcr.Node;
+import javax.jcr.Property;
 import javax.jcr.RepositoryException;
+import javax.jcr.nodetype.NodeType;
 
 import org.apache.commons.io.IOUtils;
 import org.argeo.ArgeoException;
-import org.argeo.eclipse.ui.jcr.browser.RepositoryNode;
-import org.argeo.eclipse.ui.jcr.browser.WorkspaceNode;
 import org.argeo.eclipse.ui.specific.FileProvider;
 
 /**
@@ -16,34 +31,64 @@ import org.argeo.eclipse.ui.specific.FileProvider;
  * reliable as long as we have not fixed login & multi repository issues that
  * will be addressed in the next version.
  * 
- * We are also very dependant of the repository architecture for file nodes. We
- * assume the content of the file is stored in a nt:resource child node of the
- * nt:file in the jcr:data property
+ * NOTE: id used here is the real id of the JCR Node, not the JCR Path
  * 
- * @author bsinou
+ * Relies on common approach for JCR file handling implementation.
  * 
  */
 
 public class JcrFileProvider implements FileProvider {
 
-       private RepositoryNode repositoryNode;
+       // private Object[] rootNodes;
+       private Node refNode;
 
        /**
-        * Must be set in order for the provider to be able to search the repository
+        * Must be set in order for the provider to be able to get current session
+        * and thus have the ability to get the file node corresponding to a given
+        * file ID
+        * 
+        * FIXME : this introduces some concurrences ISSUES.
         * 
         * @param repositoryNode
         */
-       public void setRepositoryNode(RepositoryNode repositoryNode) {
-               this.repositoryNode = repositoryNode;
+       public void setReferenceNode(Node refNode) {
+               this.refNode = refNode;
        }
 
+       /**
+        * Must be set in order for the provider to be able to search the repository
+        * Provided object might be either JCR Nodes or UI RepositoryNode for the
+        * time being.
+        * 
+        * @param repositoryNode
+        */
+       // public void setRootNodes(Object[] rootNodes) {
+       // List<Object> tmpNodes = new ArrayList<Object>();
+       // for (int i = 0; i < rootNodes.length; i++) {
+       // Object obj = rootNodes[i];
+       // if (obj instanceof Node) {
+       // tmpNodes.add(obj);
+       // } else if (obj instanceof RepositoryRegister) {
+       // RepositoryRegister repositoryRegister = (RepositoryRegister) obj;
+       // Map<String, Repository> repositories = repositoryRegister
+       // .getRepositories();
+       // for (String name : repositories.keySet()) {
+       // // tmpNodes.add(new RepositoryNode(name, repositories
+       // // .get(name)));
+       // }
+       //
+       // }
+       // }
+       // this.rootNodes = tmpNodes.toArray();
+       // }
+
        public byte[] getByteArrayFileFromId(String fileId) {
                InputStream fis = null;
                byte[] ba = null;
                Node child = getFileNodeFromId(fileId);
                try {
-                       fis = (InputStream) child.getProperty("jcr:data").getBinary()
-                                       .getStream();
+                       fis = (InputStream) child.getProperty(Property.JCR_DATA)
+                                       .getBinary().getStream();
                        ba = IOUtils.toByteArray(fis);
 
                } catch (Exception e) {
@@ -59,8 +104,8 @@ public class JcrFileProvider implements FileProvider {
                        InputStream fis = null;
 
                        Node child = getFileNodeFromId(fileId);
-                       fis = (InputStream) child.getProperty("jcr:data").getBinary()
-                                       .getStream();
+                       fis = (InputStream) child.getProperty(Property.JCR_DATA)
+                                       .getBinary().getStream();
                        return fis;
                } catch (RepositoryException re) {
                        throw new ArgeoException("Cannot get stream from file node for Id "
@@ -78,34 +123,50 @@ public class JcrFileProvider implements FileProvider {
         *         never null
         */
        private Node getFileNodeFromId(String fileId) {
-               Object[] nodes = repositoryNode.getChildren();
                try {
-                       Node result = null;
-
-                       repos: for (int i = 0; i < nodes.length; i++) {
-                               WorkspaceNode wNode = (WorkspaceNode) nodes[i];
-                               result = wNode.getSession().getNodeByIdentifier(fileId);
-
-                               if (result == null)
-                                       continue repos;
+                       Node result = refNode.getSession().getNodeByIdentifier(fileId);
+
+                       // rootNodes: for (int j = 0; j < rootNodes.length; j++) {
+                       // // in case we have a classic JCR Node
+                       // if (rootNodes[j] instanceof Node) {
+                       // Node curNode = (Node) rootNodes[j];
+                       // if (result != null)
+                       // break rootNodes;
+                       // } // Case of a repository Node
+                       // else if (rootNodes[j] instanceof RepositoryNode) {
+                       // Object[] nodes = ((RepositoryNode) rootNodes[j])
+                       // .getChildren();
+                       // for (int i = 0; i < nodes.length; i++) {
+                       // Node node = (Node) nodes[i];
+                       // result = node.getSession().getNodeByIdentifier(fileId);
+                       // if (result != null)
+                       // break rootNodes;
+                       // }
+                       // }
+                       // }
+
+                       // Sanity checks
+                       if (result == null)
+                               throw new ArgeoException("File node not found for ID" + fileId);
+
+                       // Ensure that the node have the correct type.
+                       if (!result.isNodeType(NodeType.NT_FILE))
+                               throw new ArgeoException(
+                                               "Cannot open file children Node that are not of '"
+                                                               + NodeType.NT_RESOURCE + "' type.");
+
+                       // Get the usefull part of the Node
+                       Node child = result.getNodes().nextNode();
+                       if (child == null || !child.isNodeType(NodeType.NT_RESOURCE))
+                               throw new ArgeoException(
+                                               "ERROR: IN the current implemented model, '"
+                                                               + NodeType.NT_FILE
+                                                               + "' file node must have one and only one child of the nt:ressource, where actual data is stored");
+                       return child;
 
-                               // Ensure that the node have the correct type.
-                               if (!result.isNodeType("nt:file"))
-                                       throw new ArgeoException(
-                                                       "Cannot open file children Node that are not of 'nt:resource' type.");
-
-                               Node child = result.getNodes().nextNode();
-                               if (child == null || !child.isNodeType("nt:resource"))
-                                       throw new ArgeoException(
-                                                       "ERROR: IN the current implemented model, nt:file file node must have one and only one child of the nt:ressource, where actual data is stored");
-
-                               return child;
-                       }
                } catch (RepositoryException re) {
                        throw new ArgeoException("Erreur while getting file node of ID "
                                        + fileId, re);
                }
-
-               throw new ArgeoException("File node not found for ID" + fileId);
        }
 }