1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.jcr.util;
35
36 import info.magnolia.cms.core.FileSystemHelper;
37 import info.magnolia.cms.security.AccessDeniedException;
38 import info.magnolia.cms.security.PermissionUtil;
39 import info.magnolia.context.MgnlContext;
40 import info.magnolia.jcr.RuntimeRepositoryException;
41 import info.magnolia.jcr.iterator.NodeIterableAdapter;
42 import info.magnolia.jcr.predicate.AbstractPredicate;
43 import info.magnolia.jcr.wrapper.DelegateNodeWrapper;
44 import info.magnolia.jcr.wrapper.JCRPropertiesFilteringNodeWrapper;
45 import info.magnolia.objectfactory.Components;
46
47 import java.io.File;
48 import java.io.FileInputStream;
49 import java.io.FileOutputStream;
50 import java.io.IOException;
51 import java.util.ArrayList;
52 import java.util.Collection;
53 import java.util.Iterator;
54 import java.util.LinkedHashSet;
55 import java.util.List;
56 import java.util.NoSuchElementException;
57
58 import javax.jcr.ImportUUIDBehavior;
59 import javax.jcr.Node;
60 import javax.jcr.NodeIterator;
61 import javax.jcr.PathNotFoundException;
62 import javax.jcr.Property;
63 import javax.jcr.RepositoryException;
64 import javax.jcr.Session;
65 import javax.jcr.nodetype.NodeType;
66 import javax.jcr.nodetype.NodeTypeManager;
67 import javax.jcr.query.Row;
68 import javax.jcr.query.RowIterator;
69
70 import org.apache.commons.io.IOUtils;
71 import org.apache.commons.lang3.RandomStringUtils;
72 import org.apache.commons.lang3.StringUtils;
73 import org.apache.jackrabbit.JcrConstants;
74 import org.apache.jackrabbit.commons.iterator.FilteringNodeIterator;
75 import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
76 import org.apache.jackrabbit.commons.predicate.NodeTypePredicate;
77 import org.apache.jackrabbit.commons.predicate.Predicate;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
80
81
82
83
84 public class NodeUtil {
85
86 private static final Logger log = LoggerFactory.getLogger(NodeUtil.class);
87
88
89
90
91
92
93 @Deprecated
94 public static AbstractPredicate<Property> ALL_PROPERTIES_EXCEPT_JCR_AND_MGNL_FILTER = new AbstractPredicate<Property>() {
95
96 @Override
97 public boolean evaluateTyped(Property property) {
98 try {
99 String name = property.getName();
100 return !name.startsWith(NodeTypes.JCR_PREFIX) && !name.startsWith(NodeTypes.MGNL_PREFIX);
101 } catch (RepositoryException e) {
102 String path;
103 try {
104 path = property.getPath();
105 } catch (RepositoryException e1) {
106 path = "<path not available>";
107 }
108 log.error("Unable to read name of property {}", path);
109
110 return false;
111 }
112 }
113 };
114
115
116
117
118
119
120 @Deprecated
121 public static Predicate ALL_NODES_EXCEPT_JCR_FILTER = new AbstractPredicate<Node>() {
122 @Override
123 public boolean evaluateTyped(Node node) {
124 try {
125 return !node.getName().startsWith(NodeTypes.JCR_PREFIX);
126 } catch (RepositoryException e) {
127 log.error("Unable to read name for node {}", getNodePathIfPossible(node));
128 return false;
129 }
130 }
131 };
132
133
134
135
136
137
138 @Deprecated
139 public static AbstractPredicate<Node> EXCLUDE_META_DATA_FILTER = new AbstractPredicate<Node>() {
140
141 @Override
142 public boolean evaluateTyped(Node node) {
143 try {
144 return !node.getName().startsWith(NodeTypes.JCR_PREFIX)
145 && !NodeUtil.isNodeType(node, NodeTypes.MetaData.NAME);
146 } catch (RepositoryException e) {
147 log.error("Unable to read name or nodeType for node {}", getNodePathIfPossible(node));
148 return false;
149 }
150 }
151 };
152
153
154
155
156 public static AbstractPredicate<Node> MAGNOLIA_FILTER = new AbstractPredicate<Node>() {
157
158 @Override
159 public boolean evaluateTyped(Node node) {
160
161 try {
162 String nodeTypeName = node.getPrimaryNodeType().getName();
163
164 return nodeTypeName.startsWith(NodeTypes.MGNL_PREFIX);
165 } catch (RepositoryException e) {
166 log.error("Unable to read nodeType for node {}", getNodePathIfPossible(node));
167 }
168 return false;
169 }
170 };
171
172
173
174
175 public static Node getNodeByIdentifier(String workspace, String identifier) throws RepositoryException {
176 if (workspace == null || identifier == null) {
177 return null;
178 }
179
180 final Session jcrSession = MgnlContext.getJCRSession(workspace);
181 return (jcrSession == null) ? null : jcrSession.getNodeByIdentifier(identifier);
182 }
183
184
185
186
187
188
189 public static boolean hasMixin(Node node, String mixinName) throws RepositoryException {
190 if (StringUtils.isBlank(mixinName)) {
191 throw new IllegalArgumentException("Mixin name can't be empty.");
192 }
193 for (NodeType type : node.getMixinNodeTypes()) {
194 if (mixinName.equals(type.getName())) {
195 return true;
196 }
197 }
198 return false;
199 }
200
201
202
203
204
205 public static boolean isNodeType(Node node, String nodeTypeName) throws RepositoryException {
206 node = NodeUtil.deepUnwrap(node, JCRPropertiesFilteringNodeWrapper.class);
207 final String actualType = node.getProperty(JcrConstants.JCR_PRIMARYTYPE).getString();
208
209
210 if (JcrConstants.NT_FROZENNODE.equals(actualType) && !(JcrConstants.NT_FROZENNODE.equals(nodeTypeName))) {
211 final Property p = node.getProperty(JcrConstants.JCR_FROZENPRIMARYTYPE);
212 final String s = p.getString();
213 NodeTypeManager ntManager = node.getSession().getWorkspace().getNodeTypeManager();
214 NodeType primaryNodeType = ntManager.getNodeType(s);
215 return primaryNodeType.isNodeType(nodeTypeName);
216 }
217 return node.isNodeType(nodeTypeName);
218 }
219
220 public static Node unwrap(Node node) throws RepositoryException {
221 Node unwrappedNode = node;
222 while (unwrappedNode instanceof DelegateNodeWrapper) {
223 unwrappedNode = ((DelegateNodeWrapper) unwrappedNode).getWrappedNode();
224 }
225 return unwrappedNode;
226 }
227
228
229
230
231
232 public static Node deepUnwrap(Node node, Class<? extends DelegateNodeWrapper> wrapper) {
233 if (node instanceof DelegateNodeWrapper) {
234 return ((DelegateNodeWrapper) node).deepUnwrap(wrapper);
235 }
236 return node;
237 }
238
239
240
241
242
243 public static Node deepUnwrapAll(Node node, Class<? extends DelegateNodeWrapper> wrapperClass) {
244 while (node instanceof DelegateNodeWrapper) {
245 Node unwrapped = ((DelegateNodeWrapper) node).deepUnwrap(wrapperClass);
246
247 if (unwrapped == node) {
248 break;
249 }
250 node = unwrapped;
251 }
252 return node;
253 }
254
255
256
257
258 public static boolean isWrappedWith(Node node, Class<? extends DelegateNodeWrapper> wrapper) {
259 if (wrapper.isInstance(node)) {
260 return true;
261 }
262
263 if (node instanceof DelegateNodeWrapper) {
264 return isWrappedWith(((DelegateNodeWrapper) node).getWrappedNode(), wrapper);
265 }
266 return false;
267 }
268
269
270
271
272
273 public static void orderBefore(Node node, String siblingName) throws RepositoryException {
274 node.getParent().orderBefore(node.getName(), siblingName);
275 }
276
277
278
279
280 public static void orderAfter(Node node, String siblingName) throws RepositoryException {
281
282 if (siblingName == null) {
283 orderFirst(node);
284 return;
285 }
286
287 Node parent = node.getParent();
288 Node sibling = parent.getNode(siblingName);
289 Node siblingAfter = getSiblingAfter(sibling);
290
291 if (siblingAfter == null) {
292 orderLast(node);
293 return;
294 }
295
296
297 parent.orderBefore(node.getName(), siblingAfter.getName());
298 }
299
300
301
302
303 public static void orderFirst(Node node) throws RepositoryException {
304 Node parent = node.getParent();
305 NodeIterator siblings = parent.getNodes();
306 Node firstSibling = siblings.nextNode();
307 if (!firstSibling.isSame(node)) {
308 parent.orderBefore(node.getName(), firstSibling.getName());
309 }
310 }
311
312
313
314
315 public static void orderLast(Node node) throws RepositoryException {
316 node.getParent().orderBefore(node.getName(), null);
317 }
318
319
320
321
322
323 public static void orderNodeUp(Node node) throws RepositoryException {
324 Node siblingBefore = getSiblingBefore(node);
325 if (siblingBefore != null) {
326 node.getParent().orderBefore(node.getName(), siblingBefore.getName());
327 }
328 }
329
330
331
332
333
334 public static void orderNodeDown(Node node) throws RepositoryException {
335 Node siblingAfter = getSiblingAfter(node);
336 if (siblingAfter != null) {
337 node.getParent().orderBefore(siblingAfter.getName(), node.getName());
338 }
339 }
340
341 public static Node getSiblingBefore(Node node) throws RepositoryException {
342 Node parent = node.getParent();
343 NodeIterator siblings = parent.getNodes();
344 Node previousSibling = null;
345 while (siblings.hasNext()) {
346 Node sibling = siblings.nextNode();
347 if (isSame(node, sibling)) {
348 return previousSibling;
349 }
350 previousSibling = sibling;
351 }
352 return null;
353 }
354
355 public static Node getSiblingAfter(Node node) throws RepositoryException {
356 Node parent = node.getParent();
357 NodeIterator siblings = parent.getNodes();
358 while (siblings.hasNext()) {
359 Node sibling = siblings.nextNode();
360 if (isSame(node, sibling)) {
361 break;
362 }
363 }
364 return siblings.hasNext() ? siblings.nextNode() : null;
365 }
366
367
368
369
370
371
372
373 public static Iterable<Node> getSiblings(Node node) throws RepositoryException {
374 Node parent = node.getParent();
375 Iterable<Node> allSiblings = NodeUtil.getNodes(parent);
376 List<Node> siblings = new ArrayList<Node>();
377
378 for (Node sibling : allSiblings) {
379 if (!NodeUtil.isSame(node, sibling)) {
380 siblings.add(sibling);
381 }
382 }
383 return siblings;
384 }
385
386
387
388
389
390
391
392
393 public static Iterable<Node> getSiblings(Node node, String nodeTypeName) throws RepositoryException {
394 Node parent = node.getParent();
395 Iterable<Node> allSiblings = NodeUtil.getNodes(parent, nodeTypeName);
396 List<Node> sameTypeSiblings = new ArrayList<Node>();
397
398 for (Node sibling : allSiblings) {
399 if (!NodeUtil.isSame(node, sibling)) {
400 sameTypeSiblings.add(sibling);
401 }
402 }
403 return sameTypeSiblings;
404 }
405
406
407
408
409
410
411
412
413 public static Iterable<Node> getSiblings(Node node, Predicate predicate) throws RepositoryException {
414 Node parent = node.getParent();
415 Iterable<Node> allSiblings = NodeUtil.getNodes(parent, predicate);
416 List<Node> sameTypeSiblings = new ArrayList<Node>();
417
418 for (Node sibling : allSiblings) {
419 if (!NodeUtil.isSame(node, sibling)) {
420 sameTypeSiblings.add(sibling);
421 }
422 }
423 return sameTypeSiblings;
424 }
425
426
427
428
429
430
431
432 public static Iterable<Node> getSiblingsBefore(Node node) throws RepositoryException {
433 int toIndex = 0;
434 Node parent = node.getParent();
435 List<Node> allSiblings = NodeUtil.asList(NodeUtil.getNodes(parent));
436
437 for (Node sibling : allSiblings) {
438 if (NodeUtil.isSame(node, sibling)) {
439 break;
440 }
441 toIndex++;
442 }
443 return allSiblings.subList(0, toIndex);
444 }
445
446
447
448
449
450
451
452 public static Iterable<Node> getSiblingsAfter(Node node) throws RepositoryException {
453 int fromIndex = 0;
454 Node parent = node.getParent();
455 List<Node> allSiblings = NodeUtil.asList(NodeUtil.getNodes(parent));
456
457 for (Node sibling : allSiblings) {
458 if (NodeUtil.isSame(node, sibling)) {
459 fromIndex++;
460 break;
461 }
462 fromIndex++;
463 }
464 return allSiblings.subList(fromIndex, allSiblings.size());
465 }
466
467
468
469
470
471
472
473
474 public static Iterable<Node> getSiblingsBefore(Node node, String nodeTypeName) throws RepositoryException {
475 Node parent = node.getParent();
476 Iterable<Node> allSiblings = NodeUtil.getNodes(parent);
477 List<Node> sameTypeSiblings = new ArrayList<Node>();
478
479 for (Node sibling : allSiblings) {
480 if (NodeUtil.isSame(node, sibling)) {
481 break;
482 }
483 if (isNodeType(sibling, nodeTypeName)) {
484 sameTypeSiblings.add(sibling);
485 }
486 }
487 return sameTypeSiblings;
488 }
489
490
491
492
493
494
495
496
497 public static Iterable<Node> getSiblingsAfter(Node node, String nodeTypeName) throws RepositoryException {
498 Node parent = node.getParent();
499 List<Node> allSiblings = NodeUtil.asList(NodeUtil.getNodes(parent));
500 int fromIndex = 0;
501
502 for (Node sibling : allSiblings) {
503 fromIndex++;
504 if (NodeUtil.isSame(node, sibling)) {
505 break;
506 }
507 }
508
509 List<Node> sameTypeSiblings = new ArrayList<Node>();
510 for (Node sibling : allSiblings.subList(fromIndex, allSiblings.size())) {
511 if (isNodeType(sibling, nodeTypeName)) {
512 sameTypeSiblings.add(sibling);
513 }
514 }
515 return sameTypeSiblings;
516 }
517
518 public static void moveNode(Node nodeToMove, Node newParent) throws RepositoryException {
519
520
521 if (!newParent.getPath().equals(nodeToMove.getParent().getPath())) {
522 String newPath = combinePathAndName(newParent.getPath(), nodeToMove.getName());
523 nodeToMove.getSession().move(nodeToMove.getPath(), newPath);
524 }
525 }
526
527 public static void moveNodeBefore(Node nodeToMove, Node target) throws RepositoryException {
528 Node targetParent = target.getParent();
529 moveNode(nodeToMove, targetParent);
530 targetParent.orderBefore(nodeToMove.getName(), target.getName());
531 }
532
533 public static void moveNodeAfter(Node nodeToMove, Node target) throws RepositoryException {
534 Node targetParent = target.getParent();
535 moveNode(nodeToMove, targetParent);
536 orderAfter(nodeToMove, target.getName());
537 }
538
539 public static boolean isFirstSibling(Node node) throws RepositoryException {
540 Node parent = node.getParent();
541 NodeIterator nodes = parent.getNodes();
542 return isSame(nodes.nextNode(), node);
543 }
544
545
546
547
548 public static boolean isSameNameSiblings(Node node1, Node node2) throws RepositoryException {
549 Node parent1 = node1.getParent();
550 Node parent2 = node2.getParent();
551 return isSame(parent1, parent2) && node1.getName().equals(node2.getName());
552 }
553
554 public static boolean isLastSibling(Node node) throws RepositoryException {
555 Node parent = node.getParent();
556 NodeIterator nodes = parent.getNodes();
557 Node last = null;
558 while (nodes.hasNext()) {
559 last = nodes.nextNode();
560 }
561 return isSame(last, node);
562 }
563
564 public static void renameNode(Node node, String newName) throws RepositoryException {
565 if (node.getName().equals(newName)) {
566 return;
567 }
568 final Node parent = node.getParent();
569 final String newPath = combinePathAndName(parent.getPath(), newName);
570 final Node siblingAfter = NodeUtil.getSiblingAfter(node);
571
572 node.getSession().move(node.getPath(), newPath);
573
574 if (siblingAfter != null) {
575 parent.orderBefore(newName, siblingAfter.getName());
576 }
577 }
578
579
580
581
582
583 public static boolean isGranted(Node node, long permissions) {
584 try {
585 return PermissionUtil.isGranted(node, permissions);
586 } catch (RepositoryException e) {
587 throw new RuntimeRepositoryException(e);
588 }
589 }
590
591
592
593
594
595 public static boolean isSame(Node lhs, Node rhs) throws RepositoryException {
596 return unwrap(lhs).isSame(unwrap(rhs));
597 }
598
599
600
601
602 public static String combinePathAndName(String path, String name) {
603 if ("/".equals(path)) {
604 return "/" + name;
605 }
606 return path + "/" + name;
607 }
608
609
610
611
612
613 public static Node createPath(Node parent, String relPath, String primaryNodeTypeName) throws RepositoryException, PathNotFoundException, AccessDeniedException {
614 return createPath(parent, relPath, primaryNodeTypeName, false);
615 }
616
617
618
619
620
621 public static Node createPath(Node parent, String relPath, String primaryNodeTypeName, boolean save) throws RepositoryException, PathNotFoundException, AccessDeniedException {
622
623 String currentPath = StringUtils.removeStart(relPath, "/");
624
625 if (StringUtils.isEmpty(currentPath)) {
626 return parent;
627 }
628
629 Node root = parent;
630 String[] names = currentPath.split("/");
631
632 for (int i = 0; i < names.length; i++) {
633 String name = names[i];
634 if (root.hasNode(name)) {
635 root = root.getNode(name);
636 } else {
637 final Node newNode = root.addNode(name, primaryNodeTypeName);
638 if (newNode.canAddMixin(JcrConstants.MIX_LOCKABLE)) {
639 newNode.addMixin(JcrConstants.MIX_LOCKABLE);
640 }
641 if (save) {
642 root.getSession().save();
643 }
644 root = newNode;
645 }
646 }
647 return root;
648 }
649
650
651
652
653 public static void visit(Node node, NodeVisitor visitor) throws RepositoryException {
654 visit(node, visitor, EXCLUDE_META_DATA_FILTER);
655 }
656
657 public static void visit(Node node, NodeVisitor visitor, Predicate predicate) throws RepositoryException {
658
659 visitor.visit(node);
660 for (Node child : getNodes(node, predicate)) {
661 visit(child, visitor, predicate);
662 }
663 if (visitor instanceof PostNodeVisitor) {
664 ((PostNodeVisitor) visitor).postVisit(node);
665 }
666 }
667
668 public static Iterable<Node> getNodes(Node parent, Predicate predicate) throws RepositoryException {
669 return asIterable(new FilteringNodeIterator(parent.getNodes(), predicate));
670 }
671
672 public static Iterable<Node> getNodes(Node parent) throws RepositoryException {
673 return getNodes(parent, EXCLUDE_META_DATA_FILTER);
674 }
675
676 public static Iterable<Node> getNodes(Node parent, String nodeTypeName) throws RepositoryException {
677 return getNodes(parent, new NodeTypePredicate(nodeTypeName, false));
678 }
679
680 public static Iterable<Node> asIterable(NodeIterator iterator) {
681 return new NodeIterableAdapter(iterator);
682 }
683
684 public static List<Node> asList(Iterable<Node> nodes) {
685 List<Node> nodesList = new ArrayList<Node>();
686 for (Node node : nodes) {
687 nodesList.add(node);
688 }
689 return nodesList;
690 }
691
692
693
694
695 public static String getName(Node content) {
696 try {
697 return content.getName();
698 } catch (RepositoryException e) {
699 throw new RuntimeRepositoryException(e);
700 }
701 }
702
703
704
705
706 public static Iterable<Node> collectAllChildren(Node node) throws RepositoryException {
707 List<Node> nodes = new ArrayList<Node>();
708 return collectAllChildren(nodes, node, MAGNOLIA_FILTER);
709 }
710
711
712
713
714 public static Iterable<Node> collectAllChildren(Node node, Predicate predicate) throws RepositoryException {
715 List<Node> nodes = new ArrayList<Node>();
716 return collectAllChildren(nodes, node, predicate);
717 }
718
719
720
721
722
723 public static Iterable<Node> collectAllChildren(List<Node> nodes, Node parent, Predicate predicate) throws RepositoryException {
724
725 nodes.addAll(asList(getNodes(parent, predicate)));
726
727
728 Iterator<Node> allChildren = getNodes(parent, EXCLUDE_META_DATA_FILTER).iterator();
729
730
731 while (allChildren.hasNext()) {
732 collectAllChildren(nodes, allChildren.next(), predicate);
733 }
734
735 return nodes;
736 }
737
738
739
740
741 public static Collection<Node> getAncestors(Node node) throws RepositoryException {
742 List<Node> allAncestors = new ArrayList<Node>();
743 int level = node.getDepth();
744 while (level != 0) {
745 try {
746 allAncestors.add((Node) node.getAncestor(--level));
747 } catch (AccessDeniedException e) {
748 log.debug("Node {} didn't allow access to Ancestor's ", node.getIdentifier());
749 }
750 }
751 return allAncestors;
752 }
753
754
755
756
757
758
759
760
761
762 public static Node getNearestAncestorOfType(Node node, String nodeTypeName) throws RepositoryException {
763 if (node.getDepth() == 0) {
764 return null;
765 }
766
767 node = node.getParent();
768
769 while (true) {
770 if (NodeUtil.isNodeType(node, nodeTypeName)) {
771 return node;
772 }
773 if (node.getDepth() == 0) {
774 break;
775 }
776 node = node.getParent();
777 }
778 return null;
779 }
780
781
782
783
784 public static String getNodeIdentifierIfPossible(Node content) {
785 try {
786 return content.getIdentifier();
787 } catch (RepositoryException e) {
788 return "<not available>";
789 }
790 }
791
792 public static String getNodePathIfPossible(Node node) {
793 try {
794 return node.getPath();
795 } catch (RepositoryException e) {
796 return "<not available>";
797 }
798 }
799
800
801
802
803
804
805 public static String getPathIfPossible(Node node) {
806 try {
807 return node.getPath();
808 } catch (RepositoryException e) {
809 log.error("Failed to get handle: {}", e.getMessage(), e);
810 return "";
811 }
812 }
813
814 public static NodeIterator filterNodeType(NodeIterator iterator, String nodeType) {
815 return new FilteringNodeIterator(iterator, new info.magnolia.jcr.predicate.NodeTypePredicate(nodeType));
816 }
817
818 public static NodeIterator filterDuplicates(NodeIterator iterator) {
819 return new FilteringNodeIterator(iterator, new info.magnolia.jcr.predicate.DuplicateNodePredicate());
820 }
821
822 public static NodeIterator filterParentNodeType(NodeIterator iterator, final String nodeType) throws RepositoryException {
823 return new FilteringNodeIterator(iterator, new info.magnolia.jcr.predicate.NodeTypeParentPredicate(nodeType)) {
824 @Override
825 public Node nextNode() {
826 Node node = super.nextNode();
827 try {
828 while (node.getDepth() != 0 && !node.isNodeType(nodeType)) {
829 if (node.getDepth() != 0) {
830 node = node.getParent();
831 }
832 }
833 } catch (RepositoryException e) {
834 throw new RuntimeException(e.getMessage(), e);
835 }
836 return node;
837 }
838 };
839 }
840
841
842
843
844
845
846
847
848
849 public static NodeIterator filterParentNodeType(RowIterator iterator, final String selector) throws RepositoryException {
850 return new NodeIteratorAdapter(iterator) {
851 @Override
852 public Node nextNode() throws NoSuchElementException {
853 Row row = (Row) next();
854 try {
855 return row.getNode(selector);
856 } catch (RepositoryException e) {
857
858 log.debug(e.getMessage(), e);
859 throw new NoSuchElementException(e.getMessage());
860 }
861 }
862 };
863 }
864
865 public static Collection<Node> getCollectionFromNodeIterator(NodeIterator iterator) {
866 Collection<Node> nodeCollection = new LinkedHashSet<Node>(150);
867 while (iterator.hasNext()) {
868 nodeCollection.add(iterator.nextNode());
869 }
870 return nodeCollection;
871 }
872
873
874
875
876 @Deprecated
877 public static Collection<Node> getSortedCollectionFromNodeIterator(NodeIterator iterator) {
878 return getCollectionFromNodeIterator(iterator);
879 }
880
881
882
883
884
885
886
887
888
889 public static void copyInSession(Node src, String destAbsPath) throws RepositoryException {
890 final Session session = src.getSession();
891 final String destTmpNodeName = Components.getComponent(NodeNameHelper.class).getUniqueName(session, src.getParent().getPath(), "tmp_" + RandomStringUtils.randomAlphabetic(12));
892 final String destTmpParentPath = combinePathAndName(src.getParent().getPath(), destTmpNodeName);
893 final Node destTmpNode = createPath(src.getParent(), destTmpNodeName, src.getPrimaryNodeType().getName());
894 FileInputStream inStream = null;
895 FileOutputStream outStream = null;
896 File file = null;
897 try {
898 file = File.createTempFile("mgnl", null, Components.getComponent(FileSystemHelper.class).getTempDirectory());
899 outStream = new FileOutputStream(file);
900 session.exportSystemView(src.getPath(), outStream, false, false);
901 outStream.flush();
902 IOUtils.closeQuietly(outStream);
903 inStream = new FileInputStream(file);
904 session.importXML(
905 destTmpParentPath,
906 inStream,
907 ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
908
909 String currentPath = destTmpParentPath + "/" + src.getName();
910 session.move(currentPath, destAbsPath);
911
912 } catch (IOException e) {
913 throw new RepositoryException("Can't copy node " + src + " to " + destAbsPath, e);
914 } finally {
915 IOUtils.closeQuietly(inStream);
916 IOUtils.closeQuietly(outStream);
917 if (file != null) {
918 file.delete();
919 }
920 destTmpNode.remove();
921 }
922 }
923
924 public static void moveProperty(Property source, Node targetNode) throws RepositoryException {
925
926 if (source.isMultiple()) {
927 targetNode.setProperty(source.getName(), source.getValues());
928 } else {
929 targetNode.setProperty(source.getName(), source.getValue());
930 }
931 source.remove();
932 }
933
934
935 public static String getAbsolutePath(String path, String label) {
936 if (StringUtils.isEmpty(path) || (path.equals("/"))) {
937 return "/" + label;
938 }
939 return path + "/" + label;
940 }
941 }