]> git.argeo.org Git - gpl/argeo-jcr.git/blob - org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java
Fix submodule definition
[gpl/argeo-jcr.git] / org.argeo.cms.jcr / src / org / argeo / jcr / Jcr.java
1 package org.argeo.jcr;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.math.BigDecimal;
7 import java.text.MessageFormat;
8 import java.time.Instant;
9 import java.util.ArrayList;
10 import java.util.Arrays;
11 import java.util.Calendar;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.GregorianCalendar;
15 import java.util.Iterator;
16 import java.util.List;
17
18 import javax.jcr.Binary;
19 import javax.jcr.ItemNotFoundException;
20 import javax.jcr.Node;
21 import javax.jcr.NodeIterator;
22 import javax.jcr.Property;
23 import javax.jcr.PropertyType;
24 import javax.jcr.Repository;
25 import javax.jcr.RepositoryException;
26 import javax.jcr.Session;
27 import javax.jcr.Value;
28 import javax.jcr.Workspace;
29 import javax.jcr.nodetype.NodeType;
30 import javax.jcr.query.Query;
31 import javax.jcr.query.QueryManager;
32 import javax.jcr.query.Row;
33 import javax.jcr.security.Privilege;
34 import javax.jcr.version.Version;
35 import javax.jcr.version.VersionHistory;
36 import javax.jcr.version.VersionIterator;
37 import javax.jcr.version.VersionManager;
38
39 import org.apache.commons.io.IOUtils;
40
41 /**
42 * Utility class whose purpose is to make using JCR less verbose by
43 * systematically using unchecked exceptions and returning <code>null</code>
44 * when something is not found. This is especially useful when writing user
45 * interfaces (such as with SWT) where listeners and callbacks expect unchecked
46 * exceptions. Loosely inspired by Java's <code>Files</code> singleton.
47 */
48 public class Jcr {
49 /**
50 * The name of a node which will be serialized as XML text, as per section 7.3.1
51 * of the JCR 2.0 specifications.
52 */
53 public final static String JCR_XMLTEXT = "jcr:xmltext";
54 /**
55 * The name of a property which will be serialized as XML text, as per section
56 * 7.3.1 of the JCR 2.0 specifications.
57 */
58 public final static String JCR_XMLCHARACTERS = "jcr:xmlcharacters";
59 /**
60 * <code>jcr:name</code>, when used in another context than
61 * {@link Property#JCR_NAME}, typically to name a node rather than a property.
62 */
63 public final static String JCR_NAME = "jcr:name";
64 /**
65 * <code>jcr:path</code>, when used in another context than
66 * {@link Property#JCR_PATH}, typically to name a node rather than a property.
67 */
68 public final static String JCR_PATH = "jcr:path";
69 /**
70 * <code>jcr:primaryType</code> with prefix instead of namespace (as in
71 * {@link Property#JCR_PRIMARY_TYPE}.
72 */
73 public final static String JCR_PRIMARY_TYPE = "jcr:primaryType";
74 /**
75 * <code>jcr:mixinTypes</code> with prefix instead of namespace (as in
76 * {@link Property#JCR_MIXIN_TYPES}.
77 */
78 public final static String JCR_MIXIN_TYPES = "jcr:mixinTypes";
79 /**
80 * <code>jcr:uuid</code> with prefix instead of namespace (as in
81 * {@link Property#JCR_UUID}.
82 */
83 public final static String JCR_UUID = "jcr:uuid";
84 /**
85 * <code>jcr:created</code> with prefix instead of namespace (as in
86 * {@link Property#JCR_CREATED}.
87 */
88 public final static String JCR_CREATED = "jcr:created";
89 /**
90 * <code>jcr:createdBy</code> with prefix instead of namespace (as in
91 * {@link Property#JCR_CREATED_BY}.
92 */
93 public final static String JCR_CREATED_BY = "jcr:createdBy";
94 /**
95 * <code>jcr:lastModified</code> with prefix instead of namespace (as in
96 * {@link Property#JCR_LAST_MODIFIED}.
97 */
98 public final static String JCR_LAST_MODIFIED = "jcr:lastModified";
99 /**
100 * <code>jcr:lastModifiedBy</code> with prefix instead of namespace (as in
101 * {@link Property#JCR_LAST_MODIFIED_BY}.
102 */
103 public final static String JCR_LAST_MODIFIED_BY = "jcr:lastModifiedBy";
104
105 /**
106 * @see Node#isNodeType(String)
107 * @throws JcrException caused by {@link RepositoryException}
108 */
109 public static boolean isNodeType(Node node, String nodeTypeName) {
110 try {
111 return node.isNodeType(nodeTypeName);
112 } catch (RepositoryException e) {
113 throw new JcrException("Cannot get whether " + node + " is of type " + nodeTypeName, e);
114 }
115 }
116
117 /**
118 * @see Node#hasNodes()
119 * @throws JcrException caused by {@link RepositoryException}
120 */
121 public static boolean hasNodes(Node node) {
122 try {
123 return node.hasNodes();
124 } catch (RepositoryException e) {
125 throw new JcrException("Cannot get whether " + node + " has children.", e);
126 }
127 }
128
129 /**
130 * @see Node#getParent()
131 * @throws JcrException caused by {@link RepositoryException}
132 */
133 public static Node getParent(Node node) {
134 try {
135 return isRoot(node) ? null : node.getParent();
136 } catch (RepositoryException e) {
137 throw new JcrException("Cannot get parent of " + node, e);
138 }
139 }
140
141 /**
142 * @see Node#getParent()
143 * @throws JcrException caused by {@link RepositoryException}
144 */
145 public static String getParentPath(Node node) {
146 return getPath(getParent(node));
147 }
148
149 /**
150 * Whether this node is the root node.
151 *
152 * @throws JcrException caused by {@link RepositoryException}
153 */
154 public static boolean isRoot(Node node) {
155 try {
156 return node.getDepth() == 0;
157 } catch (RepositoryException e) {
158 throw new JcrException("Cannot get depth of " + node, e);
159 }
160 }
161
162 /**
163 * @see Node#getPath()
164 * @throws JcrException caused by {@link RepositoryException}
165 */
166 public static String getPath(Node node) {
167 try {
168 return node.getPath();
169 } catch (RepositoryException e) {
170 throw new JcrException("Cannot get path of " + node, e);
171 }
172 }
173
174 /**
175 * @see Node#getSession()
176 * @see Session#getWorkspace()
177 * @see Workspace#getName()
178 */
179 public static String getWorkspaceName(Node node) {
180 return session(node).getWorkspace().getName();
181 }
182
183 /**
184 * @see Node#getIdentifier()
185 * @throws JcrException caused by {@link RepositoryException}
186 */
187 public static String getIdentifier(Node node) {
188 try {
189 return node.getIdentifier();
190 } catch (RepositoryException e) {
191 throw new JcrException("Cannot get identifier of " + node, e);
192 }
193 }
194
195 /**
196 * @see Node#getName()
197 * @throws JcrException caused by {@link RepositoryException}
198 */
199 public static String getName(Node node) {
200 try {
201 return node.getName();
202 } catch (RepositoryException e) {
203 throw new JcrException("Cannot get name of " + node, e);
204 }
205 }
206
207 /**
208 * Returns the node name with its current index (useful for re-ordering).
209 *
210 * @see Node#getName()
211 * @see Node#getIndex()
212 * @throws JcrException caused by {@link RepositoryException}
213 */
214 public static String getIndexedName(Node node) {
215 try {
216 return node.getName() + "[" + node.getIndex() + "]";
217 } catch (RepositoryException e) {
218 throw new JcrException("Cannot get name of " + node, e);
219 }
220 }
221
222 /**
223 * @see Node#getProperty(String)
224 * @throws JcrException caused by {@link RepositoryException}
225 */
226 public static Property getProperty(Node node, String property) {
227 try {
228 if (node.hasProperty(property))
229 return node.getProperty(property);
230 else
231 return null;
232 } catch (RepositoryException e) {
233 throw new JcrException("Cannot get property " + property + " of " + node, e);
234 }
235 }
236
237 /**
238 * @see Node#getIndex()
239 * @throws JcrException caused by {@link RepositoryException}
240 */
241 public static int getIndex(Node node) {
242 try {
243 return node.getIndex();
244 } catch (RepositoryException e) {
245 throw new JcrException("Cannot get index of " + node, e);
246 }
247 }
248
249 /**
250 * If node has mixin {@link NodeType#MIX_TITLE}, return
251 * {@link Property#JCR_TITLE}, otherwise return {@link #getName(Node)}.
252 */
253 public static String getTitle(Node node) {
254 if (Jcr.isNodeType(node, NodeType.MIX_TITLE))
255 return get(node, Property.JCR_TITLE);
256 else
257 return Jcr.getName(node);
258 }
259
260 /** Accesses a {@link NodeIterator} as an {@link Iterable}. */
261 @SuppressWarnings("unchecked")
262 public static Iterable<Node> iterate(NodeIterator nodeIterator) {
263 return new Iterable<Node>() {
264
265 @Override
266 public Iterator<Node> iterator() {
267 return nodeIterator;
268 }
269 };
270 }
271
272 /**
273 * @return the children as an {@link Iterable} for use in for-each llops.
274 * @see Node#getNodes()
275 * @throws JcrException caused by {@link RepositoryException}
276 */
277 public static Iterable<Node> nodes(Node node) {
278 try {
279 return iterate(node.getNodes());
280 } catch (RepositoryException e) {
281 throw new JcrException("Cannot get children of " + node, e);
282 }
283 }
284
285 /**
286 * @return the children as a (possibly empty) {@link List}.
287 * @see Node#getNodes()
288 * @throws JcrException caused by {@link RepositoryException}
289 */
290 public static List<Node> getNodes(Node node) {
291 List<Node> nodes = new ArrayList<>();
292 try {
293 if (node.hasNodes()) {
294 NodeIterator nit = node.getNodes();
295 while (nit.hasNext())
296 nodes.add(nit.nextNode());
297 return nodes;
298 } else
299 return nodes;
300 } catch (RepositoryException e) {
301 throw new JcrException("Cannot get children of " + node, e);
302 }
303 }
304
305 /**
306 * @return the child or <code>null</node> if not found
307 * @see Node#getNode(String)
308 * @throws JcrException caused by {@link RepositoryException}
309 */
310 public static Node getNode(Node node, String child) {
311 try {
312 if (node.hasNode(child))
313 return node.getNode(child);
314 else
315 return null;
316 } catch (RepositoryException e) {
317 throw new JcrException("Cannot get child of " + node, e);
318 }
319 }
320
321 /**
322 * @return the node at this path or <code>null</node> if not found
323 * @see Session#getNode(String)
324 * @throws JcrException caused by {@link RepositoryException}
325 */
326 public static Node getNode(Session session, String path) {
327 try {
328 if (session.nodeExists(path))
329 return session.getNode(path);
330 else
331 return null;
332 } catch (RepositoryException e) {
333 throw new JcrException("Cannot get node " + path, e);
334 }
335 }
336
337 /**
338 * Add a node to this parent, setting its primary type and its mixins.
339 *
340 * @param parent the parent node
341 * @param name the name of the node, if <code>null</code>, the primary
342 * type will be used (typically for XML structures)
343 * @param primaryType the primary type, if <code>null</code>
344 * {@link NodeType#NT_UNSTRUCTURED} will be used.
345 * @param mixins the mixins
346 * @return the created node
347 * @see Node#addNode(String, String)
348 * @see Node#addMixin(String)
349 */
350 public static Node addNode(Node parent, String name, String primaryType, String... mixins) {
351 if (name == null && primaryType == null)
352 throw new IllegalArgumentException("Both node name and primary type cannot be null");
353 try {
354 Node newNode = parent.addNode(name == null ? primaryType : name,
355 primaryType == null ? NodeType.NT_UNSTRUCTURED : primaryType);
356 for (String mixin : mixins) {
357 newNode.addMixin(mixin);
358 }
359 return newNode;
360 } catch (RepositoryException e) {
361 throw new JcrException("Cannot add node " + name + " to " + parent, e);
362 }
363 }
364
365 /**
366 * Add an {@link NodeType#NT_BASE} node to this parent.
367 *
368 * @param parent the parent node
369 * @param name the name of the node, cannot be <code>null</code>
370 * @return the created node
371 *
372 * @see Node#addNode(String)
373 */
374 public static Node addNode(Node parent, String name) {
375 if (name == null)
376 throw new IllegalArgumentException("Node name cannot be null");
377 try {
378 Node newNode = parent.addNode(name);
379 return newNode;
380 } catch (RepositoryException e) {
381 throw new JcrException("Cannot add node " + name + " to " + parent, e);
382 }
383 }
384
385 /**
386 * Add mixins to a node.
387 *
388 * @param node the node
389 * @param mixins the mixins
390 * @see Node#addMixin(String)
391 */
392 public static void addMixin(Node node, String... mixins) {
393 try {
394 for (String mixin : mixins) {
395 node.addMixin(mixin);
396 }
397 } catch (RepositoryException e) {
398 throw new JcrException("Cannot add mixins " + Arrays.asList(mixins) + " to " + node, e);
399 }
400 }
401
402 /**
403 * Removes this node.
404 *
405 * @see Node#remove()
406 */
407 public static void remove(Node node) {
408 try {
409 node.remove();
410 } catch (RepositoryException e) {
411 throw new JcrException("Cannot remove node " + node, e);
412 }
413 }
414
415 /**
416 * @return the node with htis id or <code>null</node> if not found
417 * @see Session#getNodeByIdentifier(String)
418 * @throws JcrException caused by {@link RepositoryException}
419 */
420 public static Node getNodeById(Session session, String id) {
421 try {
422 return session.getNodeByIdentifier(id);
423 } catch (ItemNotFoundException e) {
424 return null;
425 } catch (RepositoryException e) {
426 throw new JcrException("Cannot get node with id " + id, e);
427 }
428 }
429
430 /**
431 * Set a property to the given value, or remove it if the value is
432 * <code>null</code>.
433 *
434 * @throws JcrException caused by {@link RepositoryException}
435 */
436 public static void set(Node node, String property, Object value) {
437 try {
438 if (!node.hasProperty(property)) {
439 if (value != null) {
440 if (value instanceof List) {// multiple
441 List<?> lst = (List<?>) value;
442 String[] values = new String[lst.size()];
443 for (int i = 0; i < lst.size(); i++) {
444 values[i] = lst.get(i).toString();
445 }
446 node.setProperty(property, values);
447 } else {
448 node.setProperty(property, value.toString());
449 }
450 }
451 return;
452 }
453 Property prop = node.getProperty(property);
454 if (value == null) {
455 prop.remove();
456 return;
457 }
458
459 // multiple
460 if (value instanceof List) {
461 List<?> lst = (List<?>) value;
462 String[] values = new String[lst.size()];
463 // TODO better cast?
464 for (int i = 0; i < lst.size(); i++) {
465 values[i] = lst.get(i).toString();
466 }
467 if (!prop.isMultiple())
468 prop.remove();
469 node.setProperty(property, values);
470 return;
471 }
472
473 // single
474 if (prop.isMultiple()) {
475 prop.remove();
476 node.setProperty(property, value.toString());
477 return;
478 }
479
480 if (value instanceof String)
481 prop.setValue((String) value);
482 else if (value instanceof Long)
483 prop.setValue((Long) value);
484 else if (value instanceof Integer)
485 prop.setValue(((Integer) value).longValue());
486 else if (value instanceof Double)
487 prop.setValue((Double) value);
488 else if (value instanceof Float)
489 prop.setValue(((Float) value).doubleValue());
490 else if (value instanceof Calendar)
491 prop.setValue((Calendar) value);
492 else if (value instanceof BigDecimal)
493 prop.setValue((BigDecimal) value);
494 else if (value instanceof Boolean)
495 prop.setValue((Boolean) value);
496 else if (value instanceof byte[])
497 JcrUtils.setBinaryAsBytes(prop, (byte[]) value);
498 else if (value instanceof Instant) {
499 Instant instant = (Instant) value;
500 GregorianCalendar calendar = new GregorianCalendar();
501 calendar.setTime(Date.from(instant));
502 prop.setValue(calendar);
503 } else // try with toString()
504 prop.setValue(value.toString());
505 } catch (RepositoryException e) {
506 throw new JcrException("Cannot set property " + property + " of " + node + " to " + value, e);
507 }
508 }
509
510 /**
511 * Get property as {@link String}.
512 *
513 * @return the value of
514 * {@link Node#getProperty(String)}.{@link Property#getString()} or
515 * <code>null</code> if the property does not exist.
516 * @throws JcrException caused by {@link RepositoryException}
517 */
518 public static String get(Node node, String property) {
519 return get(node, property, null);
520 }
521
522 /**
523 * Get property as a {@link String}. If the property is multiple it returns the
524 * first value.
525 *
526 * @return the value of
527 * {@link Node#getProperty(String)}.{@link Property#getString()} or
528 * <code>defaultValue</code> if the property does not exist.
529 * @throws JcrException caused by {@link RepositoryException}
530 */
531 public static String get(Node node, String property, String defaultValue) {
532 try {
533 if (node.hasProperty(property)) {
534 Property p = node.getProperty(property);
535 if (!p.isMultiple())
536 return p.getString();
537 else {
538 Value[] values = p.getValues();
539 if (values.length == 0)
540 return defaultValue;
541 else
542 return values[0].getString();
543 }
544 } else
545 return defaultValue;
546 } catch (RepositoryException e) {
547 throw new JcrException("Cannot retrieve property " + property + " from " + node, e);
548 }
549 }
550
551 /**
552 * Get property as a {@link Value}.
553 *
554 * @return {@link Node#getProperty(String)} or <code>null</code> if the property
555 * does not exist.
556 * @throws JcrException caused by {@link RepositoryException}
557 */
558 public static Value getValue(Node node, String property) {
559 try {
560 if (node.hasProperty(property))
561 return node.getProperty(property).getValue();
562 else
563 return null;
564 } catch (RepositoryException e) {
565 throw new JcrException("Cannot retrieve property " + property + " from " + node, e);
566 }
567 }
568
569 /**
570 * Get property doing a best effort to cast it as the target object.
571 *
572 * @return the value of {@link Node#getProperty(String)} or
573 * <code>defaultValue</code> if the property does not exist.
574 * @throws IllegalArgumentException if the value could not be cast
575 * @throws JcrException in case of unexpected
576 * {@link RepositoryException}
577 */
578 @SuppressWarnings("unchecked")
579 public static <T> T getAs(Node node, String property, T defaultValue) {
580 try {
581 // TODO deal with multiple
582 if (node.hasProperty(property)) {
583 Property p = node.getProperty(property);
584 try {
585 if (p.isMultiple()) {
586 throw new UnsupportedOperationException("Multiple values properties are not supported");
587 }
588 Value value = p.getValue();
589 return (T) get(value);
590 } catch (ClassCastException e) {
591 throw new IllegalArgumentException(
592 "Cannot cast property of type " + PropertyType.nameFromValue(p.getType()), e);
593 }
594 } else {
595 return defaultValue;
596 }
597 } catch (RepositoryException e) {
598 throw new JcrException("Cannot retrieve property " + property + " from " + node, e);
599 }
600 }
601
602 public static <T> T getAs(Node node, String property, Class<T> clss) {
603 if (String.class.isAssignableFrom(clss)) {
604 return (T) get(node, property);
605 } else if (Long.class.isAssignableFrom(clss)) {
606 return (T) get(node, property);
607 } else {
608 throw new IllegalArgumentException("Unsupported format " + clss);
609 }
610 }
611
612 /**
613 * Get a multiple property as a list, doing a best effort to cast it as the
614 * target list.
615 *
616 * @return the value of {@link Node#getProperty(String)}.
617 * @throws IllegalArgumentException if the value could not be cast
618 * @throws JcrException in case of unexpected
619 * {@link RepositoryException}
620 */
621 public static <T> List<T> getMultiple(Node node, String property) {
622 try {
623 if (node.hasProperty(property)) {
624 Property p = node.getProperty(property);
625 return getMultiple(p);
626 } else {
627 return null;
628 }
629 } catch (RepositoryException e) {
630 throw new JcrException("Cannot retrieve multiple values property " + property + " from " + node, e);
631 }
632 }
633
634 /**
635 * Get a multiple property as a list, doing a best effort to cast it as the
636 * target list.
637 */
638 @SuppressWarnings("unchecked")
639 public static <T> List<T> getMultiple(Property p) {
640 try {
641 List<T> res = new ArrayList<>();
642 if (!p.isMultiple()) {
643 res.add((T) get(p.getValue()));
644 return res;
645 }
646 Value[] values = p.getValues();
647 for (Value value : values) {
648 res.add((T) get(value));
649 }
650 return res;
651 } catch (ClassCastException | RepositoryException e) {
652 throw new IllegalArgumentException("Cannot get property " + p, e);
653 }
654 }
655
656 /** Cast a {@link Value} to a standard Java object. */
657 public static Object get(Value value) {
658 Binary binary = null;
659 try {
660 switch (value.getType()) {
661 case PropertyType.STRING:
662 return value.getString();
663 case PropertyType.DOUBLE:
664 return (Double) value.getDouble();
665 case PropertyType.LONG:
666 return (Long) value.getLong();
667 case PropertyType.BOOLEAN:
668 return (Boolean) value.getBoolean();
669 case PropertyType.DATE:
670 return value.getDate();
671 case PropertyType.BINARY:
672 binary = value.getBinary();
673 byte[] arr = null;
674 try (InputStream in = binary.getStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();) {
675 IOUtils.copy(in, out);
676 arr = out.toByteArray();
677 } catch (IOException e) {
678 throw new RuntimeException("Cannot read binary from " + value, e);
679 }
680 return arr;
681 default:
682 return value.getString();
683 }
684 } catch (RepositoryException e) {
685 throw new JcrException("Cannot cast value from " + value, e);
686 } finally {
687 if (binary != null)
688 binary.dispose();
689 }
690 }
691
692 /**
693 * Retrieves the {@link Session} related to this node.
694 *
695 * @deprecated Use {@link #getSession(Node)} instead.
696 */
697 @Deprecated
698 public static Session session(Node node) {
699 return getSession(node);
700 }
701
702 /** Retrieves the {@link Session} related to this node. */
703 public static Session getSession(Node node) {
704 try {
705 return node.getSession();
706 } catch (RepositoryException e) {
707 throw new JcrException("Cannot retrieve session related to " + node, e);
708 }
709 }
710
711 /** Retrieves the root node related to this session. */
712 public static Node getRootNode(Session session) {
713 try {
714 return session.getRootNode();
715 } catch (RepositoryException e) {
716 throw new JcrException("Cannot get root node for " + session, e);
717 }
718 }
719
720 /** Whether this item exists. */
721 public static boolean itemExists(Session session, String path) {
722 try {
723 return session.itemExists(path);
724 } catch (RepositoryException e) {
725 throw new JcrException("Cannot check whether " + path + " exists", e);
726 }
727 }
728
729 /**
730 * Saves the {@link Session} related to this node. Note that all other unrelated
731 * modifications in this session will also be saved.
732 */
733 public static void save(Node node) {
734 try {
735 Session session = node.getSession();
736 // if (node.isNodeType(NodeType.MIX_LAST_MODIFIED)) {
737 // set(node, Property.JCR_LAST_MODIFIED, Instant.now());
738 // set(node, Property.JCR_LAST_MODIFIED_BY, session.getUserID());
739 // }
740 if (session.hasPendingChanges())
741 session.save();
742 } catch (RepositoryException e) {
743 throw new JcrException("Cannot save session related to " + node + " in workspace "
744 + session(node).getWorkspace().getName(), e);
745 }
746 }
747
748 /** Login to a JCR repository. */
749 public static Session login(Repository repository, String workspace) {
750 try {
751 return repository.login(workspace);
752 } catch (RepositoryException e) {
753 throw new IllegalArgumentException("Cannot login to repository", e);
754 }
755 }
756
757 /** Safely and silently logs out a session. */
758 public static void logout(Session session) {
759 try {
760 if (session != null)
761 if (session.isLive())
762 session.logout();
763 } catch (Exception e) {
764 // silent
765 }
766 }
767
768 /** Safely and silently logs out the underlying session. */
769 public static void logout(Node node) {
770 Jcr.logout(session(node));
771 }
772
773 /*
774 * SECURITY
775 */
776 /**
777 * Add a single privilege to a node.
778 *
779 * @see Privilege
780 */
781 public static void addPrivilege(Node node, String principal, String privilege) {
782 try {
783 Session session = node.getSession();
784 JcrUtils.addPrivilege(session, node.getPath(), principal, privilege);
785 } catch (RepositoryException e) {
786 throw new JcrException("Cannot add privilege " + privilege + " to " + node, e);
787 }
788 }
789
790 /*
791 * VERSIONING
792 */
793 /** Get checked out status. */
794 public static boolean isCheckedOut(Node node) {
795 try {
796 return node.isCheckedOut();
797 } catch (RepositoryException e) {
798 throw new JcrException("Cannot retrieve checked out status of " + node, e);
799 }
800 }
801
802 /** @see VersionManager#checkpoint(String) */
803 public static void checkpoint(Node node) {
804 try {
805 versionManager(node).checkpoint(node.getPath());
806 } catch (RepositoryException e) {
807 throw new JcrException("Cannot check in " + node, e);
808 }
809 }
810
811 /** @see VersionManager#checkin(String) */
812 public static void checkin(Node node) {
813 try {
814 versionManager(node).checkin(node.getPath());
815 } catch (RepositoryException e) {
816 throw new JcrException("Cannot check in " + node, e);
817 }
818 }
819
820 /** @see VersionManager#checkout(String) */
821 public static void checkout(Node node) {
822 try {
823 versionManager(node).checkout(node.getPath());
824 } catch (RepositoryException e) {
825 throw new JcrException("Cannot check out " + node, e);
826 }
827 }
828
829 /** Get the {@link VersionManager} related to this node. */
830 public static VersionManager versionManager(Node node) {
831 try {
832 return node.getSession().getWorkspace().getVersionManager();
833 } catch (RepositoryException e) {
834 throw new JcrException("Cannot get version manager from " + node, e);
835 }
836 }
837
838 /** Get the {@link VersionHistory} related to this node. */
839 public static VersionHistory getVersionHistory(Node node) {
840 try {
841 return versionManager(node).getVersionHistory(node.getPath());
842 } catch (RepositoryException e) {
843 throw new JcrException("Cannot get version history from " + node, e);
844 }
845 }
846
847 /**
848 * The linear versions of this version history in reverse order and without the
849 * root version.
850 */
851 public static List<Version> getLinearVersions(VersionHistory versionHistory) {
852 try {
853 List<Version> lst = new ArrayList<>();
854 VersionIterator vit = versionHistory.getAllLinearVersions();
855 while (vit.hasNext())
856 lst.add(vit.nextVersion());
857 lst.remove(0);
858 Collections.reverse(lst);
859 return lst;
860 } catch (RepositoryException e) {
861 throw new JcrException("Cannot get linear versions from " + versionHistory, e);
862 }
863 }
864
865 /** The frozen node related to this {@link Version}. */
866 public static Node getFrozenNode(Version version) {
867 try {
868 return version.getFrozenNode();
869 } catch (RepositoryException e) {
870 throw new JcrException("Cannot get frozen node from " + version, e);
871 }
872 }
873
874 /** Get the base {@link Version} related to this node. */
875 public static Version getBaseVersion(Node node) {
876 try {
877 return versionManager(node).getBaseVersion(node.getPath());
878 } catch (RepositoryException e) {
879 throw new JcrException("Cannot get base version from " + node, e);
880 }
881 }
882
883 /*
884 * FILES
885 */
886 /**
887 * Returns the size of this file.
888 *
889 * @see NodeType#NT_FILE
890 */
891 public static long getFileSize(Node fileNode) {
892 try {
893 if (!fileNode.isNodeType(NodeType.NT_FILE))
894 throw new IllegalArgumentException(fileNode + " must be a file.");
895 return getBinarySize(fileNode.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary());
896 } catch (RepositoryException e) {
897 throw new JcrException("Cannot get file size of " + fileNode, e);
898 }
899 }
900
901 /** Returns the size of this {@link Binary}. */
902 public static long getBinarySize(Binary binaryArg) {
903 try {
904 try (Bin binary = new Bin(binaryArg)) {
905 return binary.getSize();
906 }
907 } catch (RepositoryException e) {
908 throw new JcrException("Cannot get file size of binary " + binaryArg, e);
909 }
910 }
911
912 // QUERY
913 /** Creates a JCR-SQL2 query using {@link MessageFormat}. */
914 public static Query createQuery(QueryManager qm, String sql, Object... args) {
915 // fix single quotes
916 sql = sql.replaceAll("'", "''");
917 String query = MessageFormat.format(sql, args);
918 try {
919 return qm.createQuery(query, Query.JCR_SQL2);
920 } catch (RepositoryException e) {
921 throw new JcrException("Cannot create JCR-SQL2 query from " + query, e);
922 }
923 }
924
925 /** Executes a JCR-SQL2 query using {@link MessageFormat}. */
926 public static NodeIterator executeQuery(QueryManager qm, String sql, Object... args) {
927 Query query = createQuery(qm, sql, args);
928 try {
929 return query.execute().getNodes();
930 } catch (RepositoryException e) {
931 throw new JcrException("Cannot execute query " + sql + " with arguments " + Arrays.asList(args), e);
932 }
933 }
934
935 /** Executes a JCR-SQL2 query using {@link MessageFormat}. */
936 public static NodeIterator executeQuery(Session session, String sql, Object... args) {
937 QueryManager queryManager;
938 try {
939 queryManager = session.getWorkspace().getQueryManager();
940 } catch (RepositoryException e) {
941 throw new JcrException("Cannot get query manager from session " + session, e);
942 }
943 return executeQuery(queryManager, sql, args);
944 }
945
946 /**
947 * Executes a JCR-SQL2 query using {@link MessageFormat}, which must return a
948 * single node at most.
949 *
950 * @return the node or <code>null</code> if not found.
951 */
952 public static Node getNode(QueryManager qm, String sql, Object... args) {
953 NodeIterator nit = executeQuery(qm, sql, args);
954 if (nit.hasNext()) {
955 Node node = nit.nextNode();
956 if (nit.hasNext())
957 throw new IllegalStateException(
958 "Query " + sql + " with arguments " + Arrays.asList(args) + " returned more than one node.");
959 return node;
960 } else {
961 return null;
962 }
963 }
964
965 /**
966 * Executes a JCR-SQL2 query using {@link MessageFormat}, which must return a
967 * single node at most.
968 *
969 * @return the node or <code>null</code> if not found.
970 */
971 public static Node getNode(Session session, String sql, Object... args) {
972 QueryManager queryManager;
973 try {
974 queryManager = session.getWorkspace().getQueryManager();
975 } catch (RepositoryException e) {
976 throw new JcrException("Cannot get query manager from session " + session, e);
977 }
978 return getNode(queryManager, sql, args);
979 }
980
981 public static Node getRowNode(Row row, String selectorName) {
982 try {
983 return row.getNode(selectorName);
984 } catch (RepositoryException e) {
985 throw new JcrException("Cannot get node " + selectorName + " from row", e);
986 }
987 }
988
989 /** Singleton. */
990 private Jcr() {
991
992 }
993 }