]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jcr/JcrUtils.java
274d149dcd8c3d5dc1a39f843bf73423f2981e90
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / jcr / JcrUtils.java
1 package org.argeo.jcr;
2
3 import java.util.Calendar;
4 import java.util.StringTokenizer;
5
6 import javax.jcr.Node;
7 import javax.jcr.NodeIterator;
8 import javax.jcr.Property;
9 import javax.jcr.PropertyIterator;
10 import javax.jcr.RepositoryException;
11 import javax.jcr.Session;
12 import javax.jcr.Value;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.argeo.ArgeoException;
17
18 public class JcrUtils {
19 private final static Log log = LogFactory.getLog(JcrUtils.class);
20
21 public static String parentPath(String path) {
22 if (path.equals("/"))
23 throw new ArgeoException("Root path '/' has no parent path");
24 if (path.charAt(0) != '/')
25 throw new ArgeoException("Path " + path + " must start with a '/'");
26 String pathT = path;
27 if (pathT.charAt(pathT.length() - 1) == '/')
28 pathT = pathT.substring(0, pathT.length() - 2);
29
30 int index = pathT.lastIndexOf('/');
31 return pathT.substring(0, index);
32 }
33
34 public static String dateAsPath(Calendar cal) {
35 StringBuffer buf = new StringBuffer(11);
36 buf.append(cal.get(Calendar.YEAR));// 4
37 buf.append('/');// 1
38 int month = cal.get(Calendar.MONTH) + 1;
39 if (month < 10)
40 buf.append(0);
41 buf.append(month);// 2
42 buf.append('/');// 1
43 int day = cal.get(Calendar.DAY_OF_MONTH);
44 if (day < 10)
45 buf.append(0);
46 buf.append(day);// 2
47 buf.append('/');// 1
48 return buf.toString();
49
50 }
51
52 public static String lastPathElement(String path) {
53 if (path.charAt(path.length() - 1) == '/')
54 throw new ArgeoException("Path " + path + " cannot end with '/'");
55 int index = path.lastIndexOf('/');
56 if (index <= 0)
57 throw new ArgeoException("Cannot find last path element for "
58 + path);
59 return path.substring(index + 1);
60 }
61
62 public static Node mkdirs(Session session, String path) {
63 return mkdirs(session, path, null, false);
64 }
65
66 public static Node mkdirs(Session session, String path, String type,
67 Boolean versioning) {
68 try {
69 if (path.equals('/'))
70 return session.getRootNode();
71
72 StringTokenizer st = new StringTokenizer(path, "/");
73 StringBuffer current = new StringBuffer("/");
74 Node currentNode = session.getRootNode();
75 while (st.hasMoreTokens()) {
76 String part = st.nextToken();
77 current.append(part).append('/');
78 if (!session.itemExists(current.toString())) {
79 if (type != null)
80 currentNode = currentNode.addNode(part, type);
81 else
82 currentNode = currentNode.addNode(part);
83 if (versioning)
84 currentNode.addMixin(ArgeoJcrConstants.MIX_VERSIONABLE);
85 if (log.isTraceEnabled())
86 log.debug("Added folder " + part + " as " + current);
87 } else {
88 currentNode = (Node) session.getItem(current.toString());
89 }
90 }
91 session.save();
92 return currentNode;
93 } catch (RepositoryException e) {
94 throw new ArgeoException("Cannot mkdirs " + path, e);
95 }
96 }
97
98 /** Recursively outputs the contents of the given node. */
99 public static void debug(Node node) throws RepositoryException {
100 // First output the node path
101 log.debug(node.getPath());
102 // Skip the virtual (and large!) jcr:system subtree
103 if (node.getName().equals(ArgeoJcrConstants.JCR_SYSTEM)) {
104 return;
105 }
106
107 // Then the children nodes (recursive)
108 NodeIterator it = node.getNodes();
109 while (it.hasNext()) {
110 Node childNode = it.nextNode();
111 debug(childNode);
112 }
113
114 // Then output the properties
115 PropertyIterator properties = node.getProperties();
116 while (properties.hasNext()) {
117 Property property = properties.nextProperty();
118 if (property.getDefinition().isMultiple()) {
119 // A multi-valued property, print all values
120 Value[] values = property.getValues();
121 for (int i = 0; i < values.length; i++) {
122 log.debug(property.getPath() + "=" + values[i].getString());
123 }
124 } else {
125 // A single-valued property
126 log.debug(property.getPath() + "=" + property.getString());
127 }
128 }
129
130 }
131 }