Open for edit for sub nodes of versioned nodes
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 3 Nov 2023 10:08:42 +0000 (11:08 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 3 Nov 2023 10:08:42 +0000 (11:08 +0100)
org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java
org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrSessionAdapter.java

index 32366a4fb810df15f5bb777b7a07ade013d15abc..880f29eeaebf4c30f87fd850ddd8ab84036b48c1 100644 (file)
@@ -290,6 +290,7 @@ public class JcrContent extends AbstractContent {
        @Override
        public Content add(QName name, QName... classes) {
                try {
+                       Node node = openForEdit();
                        Node child;
                        if (classes.length > 0) {
                                classes: for (int i = 0; i < classes.length; i++) {
@@ -302,7 +303,6 @@ public class JcrContent extends AbstractContent {
                                        }
                                }
                                QName primaryType = classes[0];
-                               Node node = openForEdit();
                                child = Jcr.addNode(node, name.toString(), primaryType.toString());
 
                                for (int i = 1; i < classes.length; i++)
@@ -311,18 +311,11 @@ public class JcrContent extends AbstractContent {
                                if (NtType.file.qName().equals(primaryType)) {
                                        // TODO optimise when we have a proper save mechanism
                                        child.addNode(Node.JCR_CONTENT, NodeType.NT_UNSTRUCTURED);
-//                                     Binary binary;
-//                                     try (InputStream in = new ByteArrayInputStream(new byte[0])) {
-//                                             binary = content.getSession().getValueFactory().createBinary(in);
-//                                             content.setProperty(Property.JCR_DATA, binary);
-//                                     } catch (IOException e) {
-//                                             throw new UncheckedIOException(e);
-//                                     }
-                                       child.getSession().save();
                                }
                        } else {
-                               child = Jcr.addNode(getJcrNode(), name.toString(), NodeType.NT_UNSTRUCTURED);
+                               child = Jcr.addNode(node, name.toString(), NodeType.NT_UNSTRUCTURED);
                        }
+                       saveEditedNode(node);
                        return new JcrContent(getSession(), provider, jcrWorkspace, child.getPath());
                } catch (RepositoryException e) {
                        throw new JcrException("Cannot add child to " + jcrPath + " in " + jcrWorkspace, e);
index 0fc46b8c9cc734f344ecc2233922a006734d20a5..58a5b77e58b78ec5811a32cca93eceaeb44ef458 100644 (file)
@@ -107,22 +107,35 @@ class JcrSessionAdapter {
        public synchronized Node openForEdit(String workspace, String jcrPath) throws RepositoryException {
                Session session = getWriteSession(workspace);
                Node node = session.getNode(jcrPath);
-               if (node.isNodeType(NodeType.MIX_SIMPLE_VERSIONABLE)) {
-                       VersionManager versionManager = session.getWorkspace().getVersionManager();
-                       if (versionManager.isCheckedOut(jcrPath)) {
+               VersionManager versionManager = session.getWorkspace().getVersionManager();
+
+               Node versionedAncestor = findVersionedAncestor(node);
+               boolean checkedOut = versionManager.isCheckedOut(jcrPath);
+
+               if (versionedAncestor != null) {
+                       if (checkedOut) {
                                if (!checkedOutModified.containsKey(workspace))
                                        checkedOutModified.put(workspace, new TreeSet<>());
-                               checkedOutModified.get(workspace).add(jcrPath);
+                               checkedOutModified.get(workspace).add(versionedAncestor.getPath());
                        } else {
                                if (!checkedInModified.containsKey(workspace))
                                        checkedInModified.put(workspace, new TreeSet<>());
-                               checkedInModified.get(workspace).add(jcrPath);
-                               versionManager.checkout(jcrPath);
+                               checkedInModified.get(workspace).add(versionedAncestor.getPath());
+                               versionManager.checkout(versionedAncestor.getPath());
                        }
                }
                return node;
        }
 
+       private Node findVersionedAncestor(Node node) throws RepositoryException {
+               if (node.isNodeType(NodeType.MIX_SIMPLE_VERSIONABLE))
+                       return node;
+               Node parent = node.getParent();
+               if (parent == null)
+                       return null;
+               return findVersionedAncestor(parent);
+       }
+
        public synchronized Node freeze(String workspace, String jcrPath) throws RepositoryException {
                Session session = getWriteSession(workspace);
                Node node = session.getNode(jcrPath);