X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.jcr%2Fsrc%2Forg%2Fargeo%2Fcms%2Fjcr%2Facr%2FJcrContent.java;h=6a0cccb84fb8e9d0ae976a163e6f983c12e566bb;hb=8f85fc13d0d2cd5ce8c97ba8e4d0838e74928d08;hp=9c8a2f4bdcaeb716d1b09f905c3df1ba14ccce59;hpb=6b149cd77b13dafd166e134b11d5c2d5dc7bc8b1;p=gpl%2Fargeo-jcr.git diff --git a/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java b/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java index 9c8a2f4..6a0cccb 100644 --- a/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java +++ b/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java @@ -46,7 +46,7 @@ import org.argeo.api.acr.NamespaceUtils; 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.cms.acr.CmsContent; import org.argeo.cms.util.AsyncPipedOutputStream; import org.argeo.jcr.Jcr; import org.argeo.jcr.JcrException; @@ -64,8 +64,8 @@ public class JcrContent extends AbstractContent { /* OPTIMISATIONS */ /** - * While we want to support thread-safe access, it is very likely that only - * thread and only one sesssion will be used (typically from a single-threaded + * While we want to support thread-safe access, it is very likely that only one + * thread and only one session will be used (typically from a single-threaded * UI). We therefore cache was long as the same thread is calling. */ private Thread lastRetrievingThread = null; @@ -78,7 +78,7 @@ public class JcrContent extends AbstractContent { this.jcrWorkspace = jcrWorkspace; this.jcrPath = jcrPath; - this.isMountBase = ContentUtils.SLASH_STRING.equals(jcrPath); + this.isMountBase = "/".equals(jcrPath); } /* @@ -90,7 +90,7 @@ public class JcrContent extends AbstractContent { String name = Jcr.getName(getJcrNode()); if (name.equals("")) {// root String mountPath = provider.getMountPath(); - name = ContentUtils.getParentPath(mountPath)[1]; + name = CmsContent.getParentPath(mountPath)[1]; // name = Jcr.getWorkspaceName(getJcrNode()); } return NamespaceUtils.parsePrefixedName(provider, name); @@ -251,7 +251,7 @@ public class JcrContent extends AbstractContent { String mountPath = provider.getMountPath(); if (mountPath == null || mountPath.equals("/")) return null; - String[] parent = ContentUtils.getParentPath(mountPath); + String[] parent = CmsContent.getParentPath(mountPath); return getSession().get(parent[0]); } // if (Jcr.isRoot(getJcrNode())) // root @@ -290,10 +290,19 @@ 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++) { + if (classes[i].equals(DName.collection.qName())) { + List lst = new ArrayList<>(Arrays.asList(classes)); + lst.add(0, NtType.folder.qName()); + lst.remove(DName.collection.qName()); + classes = lst.toArray(new QName[lst.size()]); + break classes; + } + } QName primaryType = classes[0]; - Node node = openForEdit(); child = Jcr.addNode(node, name.toString(), primaryType.toString()); for (int i = 1; i < classes.length; i++) @@ -302,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); @@ -327,11 +329,7 @@ public class JcrContent extends AbstractContent { lst.add(0, NtType.file.qName()); classes = lst.toArray(new QName[lst.size()]); } - if (attrs.containsKey(DName.collection.qName())) { - List lst = Arrays.asList(classes); - lst.add(0, NtType.folder.qName()); - classes = lst.toArray(new QName[lst.size()]); - } + Content child = add(name, classes); child.putAll(attrs); return child; @@ -341,12 +339,13 @@ public class JcrContent extends AbstractContent { public void remove() { Node node = openForEdit(); Jcr.remove(node); - saveJcrSession(); + saveEditedNode(node); } - private void saveJcrSession() { + private void saveEditedNode(Node node) { try { - getJcrSession().save(); + node.getSession().save(); + getJcrSession().refresh(true); } catch (RepositoryException e) { throw new JcrException("Cannot persist " + jcrPath + " in " + jcrWorkspace, e); } @@ -363,7 +362,7 @@ public class JcrContent extends AbstractContent { throw new JcrException("Cannot remove property " + key + " from " + getJcrNode(), e); } } - saveJcrSession(); + saveEditedNode(node); } @Override @@ -413,7 +412,7 @@ public class JcrContent extends AbstractContent { node.setProperty(property, newValue); } // FIXME proper edition - saveJcrSession(); + saveEditedNode(node); return old; } catch (RepositoryException e) { throw new JcrException("Cannot set property " + key + " on " + jcrPath + " in " + jcrWorkspace, e); @@ -436,7 +435,7 @@ public class JcrContent extends AbstractContent { node.addMixin(nodeType.getName()); } // FIXME proper edition - saveJcrSession(); + saveEditedNode(node); } catch (RepositoryException e) { throw new JcrException( "Cannot add content classes " + contentClass + " to " + jcrPath + " in " + jcrWorkspace, e); @@ -456,9 +455,9 @@ public class JcrContent extends AbstractContent { @Override public boolean isParentAccessible() { - String jcrParentPath = ContentUtils.getParentPath(jcrPath)[0]; + String jcrParentPath = CmsContent.getParentPath(jcrPath)[0]; if ("".equals(jcrParentPath)) // JCR root node - jcrParentPath = ContentUtils.SLASH_STRING; + jcrParentPath = "/"; try { return getJcrSession().hasPermission(jcrParentPath, Session.ACTION_READ); } catch (RepositoryException e) { @@ -509,11 +508,13 @@ public class JcrContent extends AbstractContent { try { if (InputStream.class.isAssignableFrom(clss)) { Node node = getJcrNode(); +// System.out.println(node.getSession()); if (Jcr.isNodeType(node, NodeType.NT_FILE)) { return (C) JcrUtils.getFileAsStream(node); } } else if (OutputStream.class.isAssignableFrom(clss)) { - Node node = getJcrNode(); + Node node = openForEdit(); +// System.out.println(node.getSession()); if (Jcr.isNodeType(node, NodeType.NT_FILE)) { Node content = node.getNode(Node.JCR_CONTENT); AsyncPipedOutputStream out = new AsyncPipedOutputStream(); @@ -523,41 +524,12 @@ public class JcrContent extends AbstractContent { try { Binary binary = valueFactory.createBinary(in); content.setProperty(Property.JCR_DATA, binary); - saveJcrSession(); -// System.out.println("Binary written"); + saveEditedNode(node); } catch (RepositoryException e) { throw new JcrException( "Cannot create binary in " + jcrPath + " in workspace " + jcrWorkspace, e); } }); -// -// PipedInputStream in = new PipedInputStream() { -// -// @Override -// public void close() throws IOException { -// System.out.println("Piped IN closing..."); -// super.close(); -// } -// }; -// CompletableFuture done = CompletableFuture.runAsync(() -> { -// try { -// Binary binary = valueFactory.createBinary(in); -// content.setProperty(Property.JCR_DATA, binary); -// saveJcrSession(); -// } catch (RepositoryException e) { -// throw new JcrException( -// "Cannot create binary in " + jcrPath + " in workspace " + jcrWorkspace, e); -// } -// }); -// PipedOutputStream out = new PipedOutputStream(in) { -// @Override -// public void close() throws IOException { -// super.flush(); -// System.out.println("Piped OUT closing..."); -// super.close(); -// done.join(); -// } -// }; return (C) out; } } @@ -710,7 +682,14 @@ public class JcrContent extends AbstractContent { * COMMON UTILITIES */ protected Session getJcrSession() { - return provider.getJcrSession(getSession(), jcrWorkspace); + Session s = provider.getJcrSession(getSession(), jcrWorkspace); +// if (getSession().isEditing()) +// try { +// s.refresh(false); +// } catch (RepositoryException e) { +// throw new JcrException("Cannot refresh session", e); +// } + return s; } protected Node getJcrNode() {