View Javadoc
1   /**
2    * This file Copyright (c) 2013-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.googlesitemap;
35  
36  import info.magnolia.jcr.util.NodeTypes;
37  
38  import java.util.ArrayList;
39  import java.util.LinkedList;
40  import java.util.List;
41  
42  import javax.jcr.Node;
43  import javax.jcr.RepositoryException;
44  import javax.jcr.Value;
45  
46  /**
47   * Constants and convenience methods for node types introduced by Google Site Map module.
48   */
49  public class SiteMapNodeTypes {
50      /**
51       * Represents the node type mgnl:siteMap.
52       */
53      public static class SiteMap {
54          public static final String NAME = NodeTypes.MGNL_PREFIX + "siteMap";
55          public static final String TYPE = NodeTypes.MGNL_PREFIX + "googleSiteMapType";
56          public static final String DISPLAY_NAME = NodeTypes.MGNL_PREFIX + "googleSiteMapDisplayName";
57          public static final String INCLUDE_VIRTUAL_URI = NodeTypes.MGNL_PREFIX + "googleSiteMapIncludeVirtualUri";
58          public static final String URL = NodeTypes.MGNL_PREFIX + "googleSiteMapURL";
59          public static final String PAGES = NodeTypes.MGNL_PREFIX + "googleSiteMapPages";
60          public static final String DEFAULT_CHANGEFREQ = NodeTypes.MGNL_PREFIX + "googleSiteMapDefaultChangeFreq";
61          public static final String DEFAULT_PRIORITY = NodeTypes.MGNL_PREFIX + "googleSiteMapDefaultPriority";
62  
63          /**
64           * Returns the type of the site map or null if no type has been stored on the node.
65           */
66          public static String getType(Node node) throws RepositoryException {
67              return node.hasProperty(TYPE) ? node.getProperty(TYPE).getString() : null;
68          }
69  
70          /**
71           * Returns the displayName of the site map or null if no displayName has been stored on the node.
72           */
73          public static String getDisplayName(Node node) throws RepositoryException {
74              return node.hasProperty(DISPLAY_NAME) ? node.getProperty(DISPLAY_NAME).getString() : null;
75          }
76  
77          /**
78           * Returns the value of {@link #INCLUDE_VIRTUAL_URI} stored or false in the property is empty.
79           */
80          public static boolean isVirtualUriMappingIncluded(Node node) throws RepositoryException {
81              return node.hasProperty(INCLUDE_VIRTUAL_URI) ? node.getProperty(INCLUDE_VIRTUAL_URI).getBoolean() : Boolean.FALSE;
82          }
83  
84          /**
85           * Returns the URL of the site map or null if no URL has been stored on the node.
86           */
87          public static String getUrl(Node node) throws RepositoryException {
88              return node.hasProperty(URL) ? node.getProperty(URL).getString() : null;
89          }
90  
91          /**
92           * Returns the change frequency of the site map page or null if no change frequency has been stored on the node.
93           */
94          public static String getDefaultChangeFreq(Node node) throws RepositoryException {
95              return node.hasProperty(DEFAULT_CHANGEFREQ) ? node.getProperty(DEFAULT_CHANGEFREQ).getString() : null;
96          }
97  
98          /**
99           * Returns the priority of the site map page or null if no priority has been stored on the node.
100          */
101         public static Double getDefaultPriority(Node node) throws RepositoryException {
102             return node.hasProperty(DEFAULT_PRIORITY) ? node.getProperty(DEFAULT_PRIORITY).getDouble() : null;
103         }
104 
105         /**
106          * Returns the pages linked to the site map or null if no pages has been stored on the node.
107          */
108         public static List<String> getPages(Node node) throws RepositoryException {
109             if (!node.hasProperty(PAGES)) {
110                 return new ArrayList<String>();
111             }
112 
113             Value[] values = node.getProperty(PAGES).getValues();
114             List<String> res = new LinkedList<String>();
115             for (Value value : values) {
116                 res.add(value.getString());
117             }
118             return res;
119         }
120 
121         public static void update(Node node, String type, String url, String displayName, boolean includeVirtualUri, String defaultChangeFrq, Double defaultPriority, List<String> pages) throws RepositoryException {
122             NodeTypes.checkNodeType(node, NAME, TYPE, URL, DISPLAY_NAME, INCLUDE_VIRTUAL_URI, PAGES);
123             node.setProperty(TYPE, type);
124             node.setProperty(URL, url);
125             node.setProperty(DISPLAY_NAME, displayName);
126             node.setProperty(INCLUDE_VIRTUAL_URI, includeVirtualUri);
127             node.setProperty(DEFAULT_CHANGEFREQ, defaultChangeFrq);
128             node.setProperty(DEFAULT_PRIORITY, defaultPriority);
129             if (pages != null) {
130                 node.setProperty(PAGES, pages.toArray(new String[pages.size()]));
131             }
132         }
133 
134     }
135 
136     /**
137      * Represents the mixIn node type mgnl:googleSiteMap.
138      */
139     public static class GoogleSiteMap {
140         public static final String NAME = NodeTypes.MGNL_PREFIX + "googleSiteMap";
141         public static final String SITEMAP_CHANGEFREQ = NodeTypes.MGNL_PREFIX + "googleSiteMapChangeFreq";
142         public static final String SITEMAP_PRIORITY = NodeTypes.MGNL_PREFIX + "googleSiteMapPriority";
143         public static final String SITEMAP_HIDE_IN_GOOGLESITEMAP = NodeTypes.MGNL_PREFIX + "googleSiteMapHide";
144         public static final String SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN = NodeTypes.MGNL_PREFIX + "googleSiteMapHideChildren";
145 
146         /**
147          * Returns the change frequency of the site map page or null if no change frequency has been stored on the node.
148          */
149         public static String getChangeFreq(Node node) throws RepositoryException {
150             return node.hasProperty(SITEMAP_CHANGEFREQ) ? node.getProperty(SITEMAP_CHANGEFREQ).getString() : null;
151         }
152 
153         /**
154          * Returns the priority of the site map page or null if no priority has been stored on the node.
155          */
156         public static Double getPriority(Node node) throws RepositoryException {
157             return node.hasProperty(SITEMAP_PRIORITY) ? node.getProperty(SITEMAP_PRIORITY).getDouble() : null;
158         }
159 
160         /**
161          * Returns the hide value of the site map page or false if no value is stored on the node.
162          */
163         public static boolean isHide(Node node) throws RepositoryException {
164             return node.hasProperty(SITEMAP_HIDE_IN_GOOGLESITEMAP) ? node.getProperty(SITEMAP_HIDE_IN_GOOGLESITEMAP).getBoolean() : Boolean.FALSE;
165         }
166 
167         /**
168          * Returns the hide children value of the site map page or false if no value is stored on the node.
169          */
170         public static boolean isHideChildren(Node node) throws RepositoryException {
171             return node.hasProperty(SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN) ? node.getProperty(SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN).getBoolean() : Boolean.FALSE;
172         }
173 
174         public static void update(Node node, String changeFrq, Double priority, boolean hide, boolean hideChildren) throws RepositoryException {
175             NodeTypes.checkNodeType(node, NAME, SITEMAP_CHANGEFREQ, SITEMAP_PRIORITY, SITEMAP_HIDE_IN_GOOGLESITEMAP, SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN);
176             node.setProperty(SITEMAP_CHANGEFREQ, changeFrq);
177             node.setProperty(SITEMAP_PRIORITY, priority);
178             node.setProperty(SITEMAP_HIDE_IN_GOOGLESITEMAP, hide);
179             node.setProperty(SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN, hideChildren);
180         }
181     }
182 }