]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jcr/JcrUtils.java
First draft of DAO implementation for JCR.
[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 hostAsPath(String host) {
74 // TODO : inverse order of the elements (to have org/argeo/test IO
75 // test/argeo/org
76 return host.replace('.', '/');
77 }
78
79 public static String lastPathElement(String path) {
80 if (path.charAt(path.length() - 1) == '/')
81 throw new ArgeoException("Path " + path + " cannot end with '/'");
82 int index = path.lastIndexOf('/');
83 if (index < 0)
84 throw new ArgeoException("Cannot find last path element for "
85 + path);
86 return path.substring(index + 1);
87 }
88
89 public static Node mkdirs(Session session, String path) {
90 return mkdirs(session, path, null, false);
91 }
92
93 public static Node mkdirs(Session session, String path, String type,
94 Boolean versioning) {
95 try {
96 if (path.equals('/'))
97 return session.getRootNode();
98
99 StringTokenizer st = new StringTokenizer(path, "/");
100 StringBuffer current = new StringBuffer("/");
101 Node currentNode = session.getRootNode();
102 while (st.hasMoreTokens()) {
103 String part = st.nextToken();
104 current.append(part).append('/');
105 if (!session.itemExists(current.toString())) {
106 if (type != null)
107 currentNode = currentNode.addNode(part, type);
108 else
109 currentNode = currentNode.addNode(part);
110 if (versioning)
111 currentNode.addMixin(ArgeoJcrConstants.MIX_VERSIONABLE);
112 if (log.isTraceEnabled())
113 log.debug("Added folder " + part + " as " + current);
114 } else {
115 currentNode = (Node) session.getItem(current.toString());
116 }
117 }
118 session.save();
119 return currentNode;
120 } catch (RepositoryException e) {
121 throw new ArgeoException("Cannot mkdirs " + path, e);
122 }
123 }
124
125 /** Recursively outputs the contents of the given node. */
126 public static void debug(Node node) throws RepositoryException {
127 // First output the node path
128 log.debug(node.getPath());
129 // Skip the virtual (and large!) jcr:system subtree
130 if (node.getName().equals(ArgeoJcrConstants.JCR_SYSTEM)) {
131 return;
132 }
133
134 // Then the children nodes (recursive)
135 NodeIterator it = node.getNodes();
136 while (it.hasNext()) {
137 Node childNode = it.nextNode();
138 debug(childNode);
139 }
140
141 // Then output the properties
142 PropertyIterator properties = node.getProperties();
143 //log.debug("Property are : ");
144
145 while (properties.hasNext()) {
146 Property property = properties.nextProperty();
147 if (property.getDefinition().isMultiple()) {
148 // A multi-valued property, print all values
149 Value[] values = property.getValues();
150 for (int i = 0; i < values.length; i++) {
151 log.debug(property.getPath() + "=" + values[i].getString());
152 }
153 } else {
154 // A single-valued property
155 log.debug(property.getPath() + "=" + property.getString());
156 }
157 }
158
159 }
160 }