View Javadoc
1   /*
2    * Copyright 2024 Bloomreach (https://www.bloomreach.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Hippo Folder/Document Node Utilities.
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       * Returns true if a folder exists at {@code folderLocation}.
40       * @param session JCR session
41       * @param folderLocation folder location
42       * @return true if a folder exists at {@code folderLocation}
43       * @throws RepositoryException if repository exception occurs
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       * Returns true if a document exists at {@code documentLocation}.
57       * @param session JCR session
58       * @param documentLocation document handle or variant location
59       * @return true if a document exists at {@code documentLocation}
60       * @throws RepositoryException if repository exception occurs
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       * Returns {@code hippotranslation:locale} property value from the {@code node} if exists,
84       * or null if not existing.
85       * @param node JCR node
86       * @return {@code hippotranslation:locale} property value from the {@code node} if exists, or null if not existing
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      * Returns {@code node} if it is a document handle node, or its parent if it is a document variant node.
102      * Otherwise, returns null.
103      * @param node JCR node
104      * @return {@code node} if it is a document handle node, or its parent if it is a document variant node. Otherwise, returns null.
105      * @throws RepositoryException if repository exception occurs
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 }