X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.jcr%2Fsrc%2Forg%2Fargeo%2Fcms%2Fjcr%2Facr%2FJcrContentProvider.java;h=3850b0c0c4f33e4310f86d3f44218e420edd5361;hb=8f85fc13d0d2cd5ce8c97ba8e4d0838e74928d08;hp=0e641b19a7c0f8bc06522dae41f68a997530a6c3;hpb=8ce0f22554533046eb8018bfd9e3e06b432f53be;p=gpl%2Fargeo-jcr.git diff --git a/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContentProvider.java b/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContentProvider.java index 0e641b1..3850b0c 100644 --- a/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContentProvider.java +++ b/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContentProvider.java @@ -25,7 +25,7 @@ import org.argeo.api.acr.spi.ContentProvider; import org.argeo.api.acr.spi.ProvidedContent; import org.argeo.api.acr.spi.ProvidedSession; import org.argeo.api.cms.CmsConstants; -import org.argeo.cms.acr.ContentUtils; +import org.argeo.cms.acr.CmsContent; import org.argeo.cms.jcr.CmsJcrUtils; import org.argeo.jcr.JcrException; import org.argeo.jcr.JcrUtils; @@ -48,7 +48,7 @@ public class JcrContentProvider implements ContentProvider, NamespaceContext { if ("/".equals(mountPath)) throw new IllegalArgumentException("JCR content provider cannot be root /"); Objects.requireNonNull(mountPath); - jcrWorkspace = ContentUtils.getParentPath(mountPath)[1]; + jcrWorkspace = CmsContent.getParentPath(mountPath)[1]; adminSession = CmsJcrUtils.openDataAdminSession(jcrRepository, jcrWorkspace); } @@ -61,19 +61,27 @@ public class JcrContentProvider implements ContentProvider, NamespaceContext { this.jcrRepository = jcrRepository; } + @Override + public String getMountPath() { + return mountPath; + } + + /* + * READ + */ + @Override public ProvidedContent get(ProvidedSession contentSession, String relativePath) { - String jcrPath = "/" + relativePath; - return new JcrContent(contentSession, this, jcrWorkspace, jcrPath); + return new JcrContent(contentSession, this, jcrWorkspace, toJcrPath(relativePath)); } @Override public boolean exists(ProvidedSession contentSession, String relativePath) { - String jcrPath = ContentUtils.SLASH + relativePath; + String jcrPath = '/' + relativePath; return new JcrContent(contentSession, this, jcrWorkspace, jcrPath).exists(); } - public Session getJcrSession(ProvidedSession contentSession, String jcrWorkspace) { + protected JcrSessionAdapter getJcrSessionAdapter(ProvidedSession contentSession) { JcrSessionAdapter sessionAdapter = sessionAdapters.get(contentSession); if (sessionAdapter == null) { final JcrSessionAdapter newSessionAdapter = new JcrSessionAdapter(jcrRepository, contentSession, @@ -82,7 +90,11 @@ public class JcrContentProvider implements ContentProvider, NamespaceContext { contentSession.onClose().thenAccept((s) -> newSessionAdapter.close()); sessionAdapter = newSessionAdapter; } + return sessionAdapter; + } + public Session getJcrSession(ProvidedSession contentSession, String jcrWorkspace) { + JcrSessionAdapter sessionAdapter = getJcrSessionAdapter(contentSession); Session jcrSession = sessionAdapter.getSession(jcrWorkspace); return jcrSession; } @@ -91,21 +103,63 @@ public class JcrContentProvider implements ContentProvider, NamespaceContext { return getJcrSession(((ProvidedContent) content).getSession(), jcrWorkspace); } + /* + * WRITE + */ + Node openForEdit(ProvidedSession contentSession, String jcrWorkspace, String jcrPath) { + try { + if (contentSession.isEditing()) { + JcrSessionAdapter sessionAdapter = getJcrSessionAdapter(contentSession); + return sessionAdapter.openForEdit(jcrWorkspace, jcrPath); + } else { + return getJcrSession(contentSession, jcrWorkspace).getNode(jcrPath); + } + } catch (RepositoryException e) { + throw new JcrException("Cannot open for edit " + jcrPath + " in workspace " + jcrWorkspace, e); + } + } + @Override - public String getMountPath() { - return mountPath; + public void persist(ProvidedSession contentSession) { + try { + JcrSessionAdapter sessionAdapter = getJcrSessionAdapter(contentSession); + sessionAdapter.persist(); + } catch (RepositoryException e) { + throw new JcrException("Cannot persist " + contentSession, e); + } } - public synchronized T doInAdminSession(Function toDo) { + /* + * EDITING + */ + + @Override + public void openForEdit(ProvidedSession session, String relativePath) { + openForEdit(session, relativePath, toJcrPath(relativePath)); + } + + @Override + public void freeze(ProvidedSession session, String relativePath) { try { - return toDo.apply(adminSession); - } finally { - try { - if (adminSession.hasPendingChanges()) - adminSession.save(); - } catch (RepositoryException e) { - throw new JcrException("Cannot save admin session", e); + String jcrPath = toJcrPath(relativePath); + if (session.isEditing()) { + JcrSessionAdapter sessionAdapter = getJcrSessionAdapter(session); + sessionAdapter.freeze(jcrWorkspace, jcrPath); } + } catch (RepositoryException e) { + throw new JcrException("Cannot freeze " + relativePath + " in workspace " + jcrWorkspace, e); + } + } + + @Override + public boolean isOpenForEdit(ProvidedSession session, String relativePath) { + try { + String jcrPath = toJcrPath(relativePath); + JcrSessionAdapter sessionAdapter = getJcrSessionAdapter(session); + return sessionAdapter.isOpenForEdit(jcrWorkspace, jcrPath); + } catch (RepositoryException e) { + throw new JcrException( + "Cannot check whether " + relativePath + " is open for edit in workspace " + jcrWorkspace, e); } } @@ -198,4 +252,33 @@ public class JcrContentProvider implements ContentProvider, NamespaceContext { } } + + /* + * UTILITIES + */ + /** + * Just adds a '/' so that it becomes an absolute JCR path within the JCR + * workspace of this provider. + */ + private String toJcrPath(String relativePath) { + return '/' + relativePath; + } + + /* + * TRANSITIONAL, WHILE MIGRATING FROM JCR TO ACR + */ + @Deprecated + public synchronized T doInAdminSession(Function toDo) { + try { + return toDo.apply(adminSession); + } finally { + try { + if (adminSession.hasPendingChanges()) + adminSession.save(); + } catch (RepositoryException e) { + throw new JcrException("Cannot save admin session", e); + } + } + } + }