1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.onehippo.forge.channelmanager.pagesupport.channel.event;
17
18 import javax.jcr.Node;
19 import javax.jcr.RepositoryException;
20 import javax.jcr.Session;
21
22 import org.hippoecm.hst.util.NodeUtils;
23 import org.hippoecm.repository.HippoStdNodeType;
24 import org.hippoecm.repository.api.HippoNodeType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29
30
31 public final class HippoFolderDocumentUtils {
32
33 private static final Logger log = LoggerFactory.getLogger(HippoFolderDocumentUtils.class);
34
35 private HippoFolderDocumentUtils() {
36 }
37
38
39
40
41
42
43
44
45 public static boolean folderExists(final Session session, final String folderLocation) throws RepositoryException {
46 if (!session.nodeExists(folderLocation)) {
47 return false;
48 }
49
50 final Node node = session.getNode(folderLocation);
51
52 return NodeUtils.isNodeType(node, HippoStdNodeType.NT_FOLDER);
53 }
54
55
56
57
58
59
60
61
62 public static boolean documentExists(final Session session, final String documentLocation) throws RepositoryException {
63 if (!session.nodeExists(documentLocation)) {
64 return false;
65 }
66
67 final Node node = session.getNode(documentLocation);
68
69 if (NodeUtils.isNodeType(node, HippoNodeType.NT_HANDLE)) {
70 return true;
71 } else if (NodeUtils.isNodeType(node, HippoNodeType.NT_DOCUMENT)) {
72 if (!session.getRootNode().isSame(node)) {
73 Node parentNode = node.getParent();
74
75 return NodeUtils.isNodeType(parentNode, HippoNodeType.NT_HANDLE);
76 }
77 }
78
79 return false;
80 }
81
82
83
84
85
86
87
88 public static String getHippoTranslationLanguage(final Node node) {
89 try {
90 if (node.hasProperty("hippotranslation:locale")) {
91 return node.getProperty("hippotranslation:locale").getString();
92 }
93 } catch (RepositoryException e) {
94 log.error("Failed to retrieve hippotranslation:locale property.", e);
95 }
96
97 return null;
98 }
99
100
101
102
103
104
105
106
107 public static Node getHippoDocumentHandle(Node node) throws RepositoryException {
108 if (node.isNodeType("hippo:handle")) {
109 return node;
110 } else if (node.isNodeType("hippo:document")) {
111 if (!node.getSession().getRootNode().isSame(node)) {
112 Node parentNode = node.getParent();
113
114 if (parentNode.isNodeType("hippo:handle")) {
115 return parentNode;
116 }
117 }
118 }
119
120 return null;
121 }
122 }