Make JCR content independent of a given node.
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 16 May 2022 08:11:11 +0000 (10:11 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 16 May 2022 08:11:11 +0000 (10:11 +0200)
jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java
jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContentProvider.java
jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrSessionAdapter.java

index 94fe7cf58ef9f8b2ffd6c3a53f70af265f4a7107..2008fabe48f5ab137656646dcd66c86508a3a9b5 100644 (file)
@@ -27,40 +27,45 @@ import org.argeo.jcr.JcrException;
 
 /** A JCR {@link Node} accessed as {@link Content}. */
 public class JcrContent extends AbstractContent {
-       private Node jcrNode;
+//     private Node jcrNode;
 
        private JcrContentProvider provider;
        private ProvidedSession session;
 
-       protected JcrContent(ProvidedSession session, JcrContentProvider provider, Node node) {
+       private String jcrWorkspace;
+       private String jcrPath;
+
+       protected JcrContent(ProvidedSession session, JcrContentProvider provider, String jcrWorkspace, String jcrPath) {
                this.session = session;
                this.provider = provider;
-               this.jcrNode = node;
+               this.jcrWorkspace = jcrWorkspace;
+               this.jcrPath = jcrPath;
        }
 
        @Override
        public QName getName() {
-               String name = Jcr.getName(jcrNode);
+               String name = Jcr.getName(getJcrNode());
                if (name.equals("")) {// root
-                       name = Jcr.getWorkspaceName(jcrNode);
+                       name = Jcr.getWorkspaceName(getJcrNode());
                }
                return NamespaceUtils.parsePrefixedName(provider, name);
        }
 
+       @SuppressWarnings("unchecked")
        @Override
        public <A> Optional<A> get(QName key, Class<A> clss) {
                if (isDefaultAttrTypeRequested(clss)) {
-                       return Optional.of((A) get(jcrNode, key.toString()));
+                       return Optional.of((A) get(getJcrNode(), key.toString()));
                }
-               return Optional.of((A) Jcr.get(jcrNode, key.toString()));
+               return Optional.of((A) Jcr.get(getJcrNode(), key.toString()));
        }
 
        @Override
        public Iterator<Content> iterator() {
                try {
-                       return new JcrContentIterator(jcrNode.getNodes());
+                       return new JcrContentIterator(getJcrNode().getNodes());
                } catch (RepositoryException e) {
-                       throw new JcrException("Cannot list children of " + jcrNode, e);
+                       throw new JcrException("Cannot list children of " + getJcrNode(), e);
                }
        }
 
@@ -68,7 +73,7 @@ public class JcrContent extends AbstractContent {
        protected Iterable<QName> keys() {
                try {
                        Set<QName> keys = new HashSet<>();
-                       properties: for (PropertyIterator propertyIterator = jcrNode.getProperties(); propertyIterator.hasNext();) {
+                       for (PropertyIterator propertyIterator = getJcrNode().getProperties(); propertyIterator.hasNext();) {
                                Property property = propertyIterator.nextProperty();
                                // TODO convert standard names
                                // TODO skip technical properties
@@ -77,25 +82,17 @@ public class JcrContent extends AbstractContent {
                        }
                        return keys;
                } catch (RepositoryException e) {
-                       throw new JcrException("Cannot list properties of " + jcrNode, e);
+                       throw new JcrException("Cannot list properties of " + getJcrNode(), e);
                }
-
-//             return new Iterable<QName>() {
-//
-//                     @Override
-//                     public Iterator<QName> iterator() {
-//                             try {
-//                                     PropertyIterator propertyIterator = jcrNode.getProperties();
-//                                     return new JcrKeyIterator(provider, propertyIterator);
-//                             } catch (RepositoryException e) {
-//                                     throw new JcrException("Cannot retrive properties from " + jcrNode, e);
-//                             }
-//                     }
-//             };
        }
 
        public Node getJcrNode() {
-               return jcrNode;
+               try {
+                       // TODO caching?
+                       return provider.getJcrSession(session, jcrWorkspace).getNode(jcrPath);
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot retrieve " + jcrPath + " from workspace " + jcrWorkspace, e);
+               }
        }
 
        /** Cast to a standard Java object. */
@@ -154,7 +151,7 @@ public class JcrContent extends AbstractContent {
 
                @Override
                public Content next() {
-                       current = new JcrContent(session, provider, nodeIterator.nextNode());
+                       current = new JcrContent(session, provider, jcrWorkspace, Jcr.getPath(nodeIterator.nextNode()));
                        return current;
                }
 
@@ -169,7 +166,7 @@ public class JcrContent extends AbstractContent {
 
        @Override
        public Content getParent() {
-               return new JcrContent(session, provider, Jcr.getParent(getJcrNode()));
+               return new JcrContent(session, provider, jcrWorkspace, Jcr.getParentPath(getJcrNode()));
        }
 
        @Override
@@ -209,31 +206,37 @@ public class JcrContent extends AbstractContent {
 
        }
 
-       class JcrKeyIterator implements Iterator<QName> {
-               private final JcrContentProvider contentSession;
-               private final PropertyIterator propertyIterator;
-
-               protected JcrKeyIterator(JcrContentProvider contentSession, PropertyIterator propertyIterator) {
-                       this.contentSession = contentSession;
-                       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);
-                       }
-               }
+       /*
+        * ADAPTERS
+        */
+       public <A> A adapt(Class<A> clss) {
 
+               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);
+//                     }
+//             }
+//
+//     }
 }
index fc4a61bf9e61a72c316f5b5f5f8bf50a291932fb..a7005ce23c826b464c7716a5277a6edf1e2e2e95 100644 (file)
@@ -6,7 +6,6 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 
-import javax.jcr.Node;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
@@ -41,7 +40,12 @@ public class JcrContentProvider implements ContentProvider, NamespaceContext {
 
        @Override
        public Content get(ProvidedSession contentSession, String mountPath, String relativePath) {
-               String workspace = ContentUtils.getParentPath(mountPath)[1];
+               String jcrWorkspace = ContentUtils.getParentPath(mountPath)[1];
+               String jcrPath = "/" + relativePath;
+               return new JcrContent(contentSession, this, jcrWorkspace, jcrPath);
+       }
+
+       public Session getJcrSession(ProvidedSession contentSession, String jcrWorkspace) {
                JcrSessionAdapter sessionAdapter = sessionAdapters.get(contentSession);
                if (sessionAdapter == null) {
                        final JcrSessionAdapter newSessionAdapter = new JcrSessionAdapter(jcrRepository,
@@ -51,15 +55,8 @@ public class JcrContentProvider implements ContentProvider, NamespaceContext {
                        sessionAdapter = newSessionAdapter;
                }
 
-               Session jcrSession = sessionAdapter.getSession(workspace);
-               String jcrPath = "/" + relativePath;
-               try {
-                       Node node = jcrSession.getNode(jcrPath);
-                       return new JcrContent(contentSession, this, node);
-               } catch (RepositoryException e) {
-                       throw new JcrException("Cannot get JCR content '" + jcrPath + ", mounted from '" + mountPath
-                                       + "' with JCR session " + jcrSession, e);
-               }
+               Session jcrSession = sessionAdapter.getSession(jcrWorkspace);
+               return jcrSession;
        }
 
        /*
index 10b243bcbe24c8c4b90e2bec8de81172234d26c7..896f30a9e0248c1c8896ee7f1eac712e489af64d 100644 (file)
@@ -3,9 +3,7 @@ package org.argeo.cms.jcr.acr;
 import java.security.PrivilegedAction;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Map;
-import java.util.Set;
 
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;