]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jcr/JcrUtils.java
Add utilities
[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 import javax.jcr.query.Query;
14 import javax.jcr.query.QueryResult;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.argeo.ArgeoException;
19
20 public class JcrUtils {
21 private final static Log log = LogFactory.getLog(JcrUtils.class);
22
23 public static Node querySingleNode(Query query) {
24 NodeIterator nodeIterator;
25 try {
26 QueryResult queryResult = query.execute();
27 nodeIterator = queryResult.getNodes();
28 } catch (RepositoryException e) {
29 throw new ArgeoException("Cannot execute query " + query, e);
30 }
31 Node node;
32 if (nodeIterator.hasNext())
33 node = nodeIterator.nextNode();
34 else
35 return null;
36
37 if (nodeIterator.hasNext())
38 throw new ArgeoException("Query returned more than one node.");
39 return node;
40 }
41
42 public static String removeForbiddenCharacters(String str){
43 return str.replace('[', '_').replace(']', '_')
44 .replace('/', '_').replace('*', '_');
45
46 }
47
48 public static String parentPath(String path) {
49 if (path.equals("/"))
50 throw new ArgeoException("Root path '/' has no parent path");
51 if (path.charAt(0) != '/')
52 throw new ArgeoException("Path " + path + " must start with a '/'");
53 String pathT = path;
54 if (pathT.charAt(pathT.length() - 1) == '/')
55 pathT = pathT.substring(0, pathT.length() - 2);
56
57 int index = pathT.lastIndexOf('/');
58 return pathT.substring(0, index);
59 }
60
61 public static String dateAsPath(Calendar cal) {
62 StringBuffer buf = new StringBuffer(11);
63 buf.append(cal.get(Calendar.YEAR));// 4
64 buf.append('/');// 1
65 int month = cal.get(Calendar.MONTH) + 1;
66 if (month < 10)
67 buf.append(0);
68 buf.append(month);// 2
69 buf.append('/');// 1
70 int day = cal.get(Calendar.DAY_OF_MONTH);
71 if (day < 10)
72 buf.append(0);
73 buf.append(day);// 2
74 buf.append('/');// 1
75 return buf.toString();
76
77 }
78
79 public static String hostAsPath(String host) {
80 // TODO : inverse order of the elements (to have org/argeo/test IO
81 // test/argeo/org
82 return host.replace('.', '/');
83 }
84
85 public static String lastPathElement(String path) {
86 if (path.charAt(path.length() - 1) == '/')
87 throw new ArgeoException("Path " + path + " cannot end with '/'");
88 int index = path.lastIndexOf('/');
89 if (index < 0)
90 throw new ArgeoException("Cannot find last path element for "
91 + path);
92 return path.substring(index + 1);
93 }
94
95 public static Node mkdirs(Session session, String path) {
96 return mkdirs(session, path, null, false);
97 }
98
99 public static Node mkdirs(Session session, String path, String type,
100 Boolean versioning) {
101 try {
102 if (path.equals('/'))
103 return session.getRootNode();
104
105 StringTokenizer st = new StringTokenizer(path, "/");
106 StringBuffer current = new StringBuffer("/");
107 Node currentNode = session.getRootNode();
108 while (st.hasMoreTokens()) {
109 String part = st.nextToken();
110 current.append(part).append('/');
111 if (!session.itemExists(current.toString())) {
112 if (type != null)
113 currentNode = currentNode.addNode(part, type);
114 else
115 currentNode = currentNode.addNode(part);
116 if (versioning)
117 currentNode.addMixin(ArgeoJcrConstants.MIX_VERSIONABLE);
118 if (log.isTraceEnabled())
119 log.debug("Added folder " + part + " as " + current);
120 } else {
121 currentNode = (Node) session.getItem(current.toString());
122 }
123 }
124 session.save();
125 return currentNode;
126 } catch (RepositoryException e) {
127 throw new ArgeoException("Cannot mkdirs " + path, e);
128 }
129 }
130
131 /** Recursively outputs the contents of the given node. */
132 public static void debug(Node node) throws RepositoryException {
133 // First output the node path
134 log.debug(node.getPath());
135 // Skip the virtual (and large!) jcr:system subtree
136 if (node.getName().equals(ArgeoJcrConstants.JCR_SYSTEM)) {
137 return;
138 }
139
140 // Then the children nodes (recursive)
141 NodeIterator it = node.getNodes();
142 while (it.hasNext()) {
143 Node childNode = it.nextNode();
144 debug(childNode);
145 }
146
147 // Then output the properties
148 PropertyIterator properties = node.getProperties();
149 //log.debug("Property are : ");
150
151 while (properties.hasNext()) {
152 Property property = properties.nextProperty();
153 if (property.getDefinition().isMultiple()) {
154 // A multi-valued property, print all values
155 Value[] values = property.getValues();
156 for (int i = 0; i < values.length; i++) {
157 log.debug(property.getPath() + "=" + values[i].getString());
158 }
159 } else {
160 // A single-valued property
161 log.debug(property.getPath() + "=" + property.getString());
162 }
163 }
164
165 }
166 }