]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.jcr/src/org/argeo/cms/fs/CmsFsUtils.java
Admin session to the proper content provider workspace
[lgpl/argeo-commons.git] / jcr / org.argeo.cms.jcr / src / org / argeo / cms / fs / CmsFsUtils.java
1 package org.argeo.cms.fs;
2
3 import java.io.IOException;
4 import java.net.URI;
5 import java.net.URISyntaxException;
6 import java.nio.file.FileSystem;
7 import java.nio.file.Path;
8 import java.nio.file.spi.FileSystemProvider;
9
10 import javax.jcr.NoSuchWorkspaceException;
11 import javax.jcr.Node;
12 import javax.jcr.NodeIterator;
13 import javax.jcr.Repository;
14 import javax.jcr.RepositoryException;
15 import javax.jcr.Session;
16 import javax.jcr.query.Query;
17 import javax.jcr.query.QueryManager;
18
19 import org.argeo.api.cms.CmsConstants;
20 import org.argeo.jcr.Jcr;
21
22 /** Utilities around documents. */
23 public class CmsFsUtils {
24 // TODO make it more robust and configurable
25 private static String baseWorkspaceName = CmsConstants.SYS_WORKSPACE;
26
27 public static Node getNode(Repository repository, Path path) {
28 String workspaceName = path.getNameCount() == 0 ? baseWorkspaceName : path.getName(0).toString();
29 String jcrPath = '/' + path.subpath(1, path.getNameCount()).toString();
30 try {
31 Session newSession;
32 try {
33 newSession = repository.login(workspaceName);
34 } catch (NoSuchWorkspaceException e) {
35 // base workspace
36 newSession = repository.login(baseWorkspaceName);
37 jcrPath = path.toString();
38 }
39 return newSession.getNode(jcrPath);
40 } catch (RepositoryException e) {
41 throw new IllegalStateException("Cannot get node from path " + path, e);
42 }
43 }
44
45 public static NodeIterator getLastUpdatedDocuments(Session session) {
46 try {
47 String qStr = "//element(*, nt:file)";
48 qStr += " order by @jcr:lastModified descending";
49 QueryManager queryManager = session.getWorkspace().getQueryManager();
50 @SuppressWarnings("deprecation")
51 Query xpathQuery = queryManager.createQuery(qStr, Query.XPATH);
52 xpathQuery.setLimit(8);
53 NodeIterator nit = xpathQuery.execute().getNodes();
54 return nit;
55 } catch (RepositoryException e) {
56 throw new IllegalStateException("Unable to retrieve last updated documents", e);
57 }
58 }
59
60 public static Path getPath(FileSystemProvider nodeFileSystemProvider, URI uri) {
61 try {
62 FileSystem fileSystem = nodeFileSystemProvider.getFileSystem(uri);
63 if (fileSystem == null)
64 fileSystem = nodeFileSystemProvider.newFileSystem(uri, null);
65 String path = uri.getPath();
66 return fileSystem.getPath(path);
67 } catch (IOException e) {
68 throw new IllegalStateException("Unable to initialise file system for " + uri, e);
69 }
70 }
71
72 public static Path getPath(FileSystemProvider nodeFileSystemProvider, Node node) {
73 String workspaceName = Jcr.getWorkspaceName(node);
74 String fullPath = baseWorkspaceName.equals(workspaceName) ? Jcr.getPath(node)
75 : '/' + workspaceName + Jcr.getPath(node);
76 URI uri;
77 try {
78 uri = new URI(CmsConstants.SCHEME_NODE, null, fullPath, null);
79 } catch (URISyntaxException e) {
80 throw new IllegalArgumentException("Cannot interpret " + fullPath + " as an URI", e);
81 }
82 return getPath(nodeFileSystemProvider, uri);
83 }
84
85 /** Singleton. */
86 private CmsFsUtils() {
87 }
88 }