]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java
Improve ACR.
[lgpl/argeo-commons.git] / jcr / org.argeo.cms.jcr / src / org / argeo / cms / jcr / acr / JcrContent.java
index 6d274c60c039a0a00b8939035cef5d257b02fae2..116e45c7bf09fc0d546aeadc216a7239bcab4b9b 100644 (file)
@@ -1,5 +1,8 @@
 package org.argeo.cms.jcr.acr;
 
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
 import java.io.PipedInputStream;
 import java.io.PipedOutputStream;
 import java.util.ArrayList;
@@ -9,6 +12,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
+import java.util.concurrent.ForkJoinPool;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
@@ -20,13 +24,18 @@ import javax.jcr.Value;
 import javax.jcr.nodetype.NodeType;
 import javax.xml.namespace.QName;
 import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
 
 import org.argeo.api.acr.Content;
 import org.argeo.api.acr.NamespaceUtils;
-import org.argeo.api.acr.spi.AbstractContent;
+import org.argeo.api.acr.spi.ContentProvider;
 import org.argeo.api.acr.spi.ProvidedSession;
+import org.argeo.api.cms.CmsConstants;
+import org.argeo.cms.acr.AbstractContent;
+import org.argeo.cms.acr.ContentUtils;
 import org.argeo.jcr.Jcr;
 import org.argeo.jcr.JcrException;
+import org.argeo.jcr.JcrUtils;
 
 /** A JCR {@link Node} accessed as {@link Content}. */
 public class JcrContent extends AbstractContent {
@@ -49,7 +58,9 @@ public class JcrContent extends AbstractContent {
        public QName getName() {
                String name = Jcr.getName(getJcrNode());
                if (name.equals("")) {// root
-                       name = Jcr.getWorkspaceName(getJcrNode());
+                       String mountPath = provider.getMountPath();
+                       name = ContentUtils.getParentPath(mountPath)[1];
+                       // name = Jcr.getWorkspaceName(getJcrNode());
                }
                return NamespaceUtils.parsePrefixedName(provider, name);
        }
@@ -169,6 +180,8 @@ public class JcrContent extends AbstractContent {
 
        @Override
        public Content getParent() {
+               if (Jcr.isRoot(getJcrNode())) // root
+                       return null;
                return new JcrContent(session, provider, jcrWorkspace, Jcr.getParentPath(getJcrNode()));
        }
 
@@ -209,42 +222,92 @@ public class JcrContent extends AbstractContent {
 
        }
 
+       boolean exists() {
+               try {
+                       return provider.getJcrSession(session, jcrWorkspace).itemExists(jcrPath);
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot check whether " + jcrPath + " exists", e);
+               }
+       }
+
        /*
         * ADAPTERS
         */
+       @SuppressWarnings("unchecked")
        public <A> A adapt(Class<A> clss) {
                if (Source.class.isAssignableFrom(clss)) {
-                       PipedInputStream in = new PipedInputStream();
-                       PipedOutputStream out = new PipedOutputStream();
-               }
-//             provider.getJcrSession(session, jcrWorkspace).exportDocumentView(jcrPath, out, true, false);
-
-               return super.adapt(clss);
-       }
-
-//     class JcrKeyIterator implements Iterator<QName> {
-//             private final PropertyIterator propertyIterator;
-//
-//             protected JcrKeyIterator(PropertyIterator propertyIterator) {
-//                     this.propertyIterator = propertyIterator;
-//             }
-//
-//             @Override
-//             public boolean hasNext() {
-//                     return propertyIterator.hasNext();
-//             }
-//
-//             @Override
-//             public QName next() {
-//                     Property property = null;
 //                     try {
-//                             property = propertyIterator.nextProperty();
-//                             // TODO map standard property names
-//                             return NamespaceUtils.parsePrefixedName(provider, property.getName());
-//                     } catch (RepositoryException e) {
-//                             throw new JcrException("Cannot retrieve property " + property, null);
+                       PipedInputStream in = new PipedInputStream();
+
+                       ForkJoinPool.commonPool().execute(() -> {
+                               try (PipedOutputStream out = new PipedOutputStream(in)) {
+                                       provider.getJcrSession(session, jcrWorkspace).exportDocumentView(jcrPath, out, true, false);
+                                       out.flush();
+                               } catch (IOException | RepositoryException e) {
+                                       throw new RuntimeException("Cannot export " + jcrPath + " in workspace " + jcrWorkspace, e);
+                               }
+
+                       });
+                       return (A) new StreamSource(in);
+//                     } catch (IOException e) {
+//                             throw new RuntimeException("Cannot adapt " + JcrContent.this + " to " + clss, e);
 //                     }
-//             }
-//
-//     }
+               } else
+
+                       return super.adapt(clss);
+       }
+
+       @SuppressWarnings("unchecked")
+       @Override
+       public <C extends Closeable> C open(Class<C> clss) throws IOException, IllegalArgumentException {
+               if (InputStream.class.isAssignableFrom(clss)) {
+                       Node node = getJcrNode();
+                       if (Jcr.isNodeType(node, NodeType.NT_FILE)) {
+                               try {
+                                       return (C) JcrUtils.getFileAsStream(node);
+                               } catch (RepositoryException e) {
+                                       throw new JcrException("Cannot open " + jcrPath + " in workspace " + jcrWorkspace, e);
+                               }
+                       }
+               }
+               return super.open(clss);
+       }
+
+       @Override
+       public ProvidedSession getSession() {
+               return session;
+       }
+
+       @Override
+       public ContentProvider getProvider() {
+               return provider;
+       }
+
+       @Override
+       public String getSessionLocalId() {
+               try {
+                       return getJcrNode().getIdentifier();
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot get identifier for " + getJcrNode(), e);
+               }
+       }
+
+       /*
+        * STATIC UTLITIES
+        */
+       public static Content nodeToContent(Node node) {
+               if (node == null)
+                       return null;
+               try {
+                       ProvidedSession contentSession = (ProvidedSession) node.getSession()
+                                       .getAttribute(ProvidedSession.class.getName());
+                       if (contentSession == null)
+                               throw new IllegalArgumentException(
+                                               "Cannot adapt " + node + " to content, because it was not loaded from a content session");
+                       return contentSession.get(CmsConstants.SYS_WORKSPACE + node.getPath());
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot adapt " + node + " to a content", e);
+               }
+       }
+
 }