]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jcr/JcrUtils.java
Add node mapper abstraction
[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 parentPath(String path) {
43 if (path.equals("/"))
44 throw new ArgeoException("Root path '/' has no parent path");
45 if (path.charAt(0) != '/')
46 throw new ArgeoException("Path " + path + " must start with a '/'");
47 String pathT = path;
48 if (pathT.charAt(pathT.length() - 1) == '/')
49 pathT = pathT.substring(0, pathT.length() - 2);
50
51 int index = pathT.lastIndexOf('/');
52 return pathT.substring(0, index);
53 }
54
55 public static String dateAsPath(Calendar cal) {
56 StringBuffer buf = new StringBuffer(11);
57 buf.append(cal.get(Calendar.YEAR));// 4
58 buf.append('/');// 1
59 int month = cal.get(Calendar.MONTH) + 1;
60 if (month < 10)
61 buf.append(0);
62 buf.append(month);// 2
63 buf.append('/');// 1
64 int day = cal.get(Calendar.DAY_OF_MONTH);
65 if (day < 10)
66 buf.append(0);
67 buf.append(day);// 2
68 buf.append('/');// 1
69 return buf.toString();
70
71 }
72
73 public static String lastPathElement(String path) {
74 if (path.charAt(path.length() - 1) == '/')
75 throw new ArgeoException("Path " + path + " cannot end with '/'");
76 int index = path.lastIndexOf('/');
77 if (index < 0)
78 throw new ArgeoException("Cannot find last path element for "
79 + path);
80 return path.substring(index + 1);
81 }
82
83 public static Node mkdirs(Session session, String path) {
84 return mkdirs(session, path, null, false);
85 }
86
87 public static Node mkdirs(Session session, String path, String type,
88 Boolean versioning) {
89 try {
90 if (path.equals('/'))
91 return session.getRootNode();
92
93 StringTokenizer st = new StringTokenizer(path, "/");
94 StringBuffer current = new StringBuffer("/");
95 Node currentNode = session.getRootNode();
96 while (st.hasMoreTokens()) {
97 String part = st.nextToken();
98 current.append(part).append('/');
99 if (!session.itemExists(current.toString())) {
100 if (type != null)
101 currentNode = currentNode.addNode(part, type);
102 else
103 currentNode = currentNode.addNode(part);
104 if (versioning)
105 currentNode.addMixin(ArgeoJcrConstants.MIX_VERSIONABLE);
106 if (log.isTraceEnabled())
107 log.debug("Added folder " + part + " as " + current);
108 } else {
109 currentNode = (Node) session.getItem(current.toString());
110 }
111 }
112 session.save();
113 return currentNode;
114 } catch (RepositoryException e) {
115 throw new ArgeoException("Cannot mkdirs " + path, e);
116 }
117 }
118
119 /** Recursively outputs the contents of the given node. */
120 public static void debug(Node node) throws RepositoryException {
121 // First output the node path
122 log.debug(node.getPath());
123 // Skip the virtual (and large!) jcr:system subtree
124 if (node.getName().equals(ArgeoJcrConstants.JCR_SYSTEM)) {
125 return;
126 }
127
128 // Then the children nodes (recursive)
129 NodeIterator it = node.getNodes();
130 while (it.hasNext()) {
131 Node childNode = it.nextNode();
132 debug(childNode);
133 }
134
135 // Then output the properties
136 PropertyIterator properties = node.getProperties();
137 while (properties.hasNext()) {
138 Property property = properties.nextProperty();
139 if (property.getDefinition().isMultiple()) {
140 // A multi-valued property, print all values
141 Value[] values = property.getValues();
142 for (int i = 0; i < values.length; i++) {
143 log.debug(property.getPath() + "=" + values[i].getString());
144 }
145 } else {
146 // A single-valued property
147 log.debug(property.getPath() + "=" + property.getString());
148 }
149 }
150
151 }
152 }