-> Finalisation of file download for RAP
authorBruno Sinou <bsinou@argeo.org>
Wed, 9 Mar 2011 22:43:17 +0000 (22:43 +0000)
committerBruno Sinou <bsinou@argeo.org>
Wed, 9 Mar 2011 22:43:17 +0000 (22:43 +0000)
-> some little UI Bugs

git-svn-id: https://svn.argeo.org/commons/trunk@4274 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/JcrFileProvider.java [new file with mode: 0644]
eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/NodeViewerComparer.java [new file with mode: 0644]

diff --git a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/JcrFileProvider.java b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/JcrFileProvider.java
new file mode 100644 (file)
index 0000000..f8de5f3
--- /dev/null
@@ -0,0 +1,85 @@
+package org.argeo.eclipse.ui.jcr.utils;
+
+import java.io.InputStream;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+
+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;
+
+/**
+ * Implements a FileProvider for UI purposes. Note that it might not be very
+ * 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
+ * 
+ * @author bsinou
+ * 
+ */
+
+public class JcrFileProvider implements FileProvider {
+
+       private RepositoryNode repositoryNode;
+
+       /**
+        * Must be set in order for the provider to be able to search the repository
+        * 
+        * @param repositoryNode
+        */
+       public void setRepositoryNode(RepositoryNode repositoryNode) {
+               this.repositoryNode = repositoryNode;
+       }
+
+       public byte[] getByteArrayFileFromId(String fileId) {
+               try {
+                       Object[] nodes = repositoryNode.getChildren();
+
+                       repos: for (int i = 0; i < nodes.length; i++) {
+                               WorkspaceNode wNode = (WorkspaceNode) nodes[i];
+                               Node node = null;
+                               node = wNode.getSession().getNodeByIdentifier(fileId);
+
+                               if (node == null)
+                                       continue repos;
+
+                               if (!node.isNodeType("nt:file"))
+                                       throw new ArgeoException(
+                                                       "Cannot open file children Node that are not of 'nt:resource' type.");
+
+                               Node child = node.getNodes().nextNode();
+                               if (!child.isNodeType("nt:resource"))
+                                       throw new ArgeoException(
+                                                       "Cannot open file children Node that are not of 'nt:resource' type.");
+
+                               InputStream fis = null;
+                               byte[] ba = null;
+                               try {
+                                       fis = (InputStream) child.getProperty("jcr:data")
+                                                       .getBinary().getStream();
+                                       ba = IOUtils.toByteArray(fis);
+
+                               } catch (Exception e) {
+                                       throw new ArgeoException("Stream error while opening file",
+                                                       e);
+                               } finally {
+                                       IOUtils.closeQuietly(fis);
+                               }
+                               if (ba != null)
+                                       return ba;
+                       }
+
+               } catch (RepositoryException re) {
+                       throw new ArgeoException("RepositoryException while reading file ",
+                                       re);
+               }
+
+               throw new ArgeoException("File not found for ID " + fileId);
+       }
+}
diff --git a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/NodeViewerComparer.java b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/NodeViewerComparer.java
new file mode 100644 (file)
index 0000000..63463d2
--- /dev/null
@@ -0,0 +1,35 @@
+package org.argeo.eclipse.ui.jcr.utils;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+
+import org.argeo.ArgeoException;
+import org.eclipse.jface.viewers.IElementComparer;
+
+public class NodeViewerComparer implements IElementComparer {
+
+       // force comparison on Node IDs only.
+       public boolean equals(Object elementA, Object elementB) {
+               if (!(elementA instanceof Node) || !(elementB instanceof Node)) {
+                       return elementA == null ? elementB == null : elementA
+                                       .equals(elementB);
+               } else {
+
+                       boolean result = false;
+                       try {
+                               String idA = ((Node) elementA).getIdentifier();
+                               String idB = ((Node) elementB).getIdentifier();
+                               result = idA == null ? idB == null : idA.equals(idB);
+                       } catch (RepositoryException re) {
+                               throw new ArgeoException("cannot compare nodes", re);
+                       }
+
+                       return result;
+               }
+       }
+
+       public int hashCode(Object element) {
+               // TODO enhanced this method.
+               return element.getClass().toString().hashCode();
+       }
+}
\ No newline at end of file