View Javadoc
1   /**
2    * This file Copyright (c) 2014-2015 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.setup.for2_1;
35  
36  import info.magnolia.jcr.util.NodeUtil;
37  import info.magnolia.jcr.util.NodeVisitor;
38  import info.magnolia.jcr.util.PropertyUtil;
39  import info.magnolia.jcr.wrapper.JCRMgnlPropertiesFilteringNodeWrapper;
40  import info.magnolia.module.InstallContext;
41  import info.magnolia.module.delta.AbstractTask;
42  import info.magnolia.module.delta.TaskExecutionException;
43  import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration;
44  import info.magnolia.module.googlesitemap.SiteMapNodeTypes.SiteMap;
45  
46  import java.util.ArrayList;
47  import java.util.List;
48  
49  import javax.jcr.Node;
50  import javax.jcr.PropertyIterator;
51  import javax.jcr.RepositoryException;
52  import javax.jcr.Session;
53  
54  import org.apache.commons.lang.StringUtils;
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  /**
59   * Rename the {@link SiteMap} properties (from displayName to mgnl:googleSiteMapDisplayName,...) and <br>
60   * change the node structure (used to store the related SiteMap pages):<br>
61   * - from a 'sites' sub node
62   * - to a {@link SiteMap#PAGES} JCR multi value property.
63   */
64  public class UpdatePropertyNamesAndNodeStructure extends AbstractTask {
65      private static final Logger log = LoggerFactory.getLogger(UpdatePropertyNamesAndNodeStructure.class);
66      private Session siteMapSession;
67  
68      public UpdatePropertyNamesAndNodeStructure(String taskName, String taskDescription) {
69          super(taskName, taskDescription);
70      }
71  
72      @Override
73      public void execute(InstallContext ctx) throws TaskExecutionException {
74          try {
75              siteMapSession = ctx.getJCRSession(GoogleSiteMapConfiguration.WORKSPACE);
76              NodeUtil.visit(siteMapSession.getRootNode(), new SiteMapPropertyVisitor());
77          } catch (RepositoryException re) {
78              ctx.error("Could not update the site map properties ", re);
79          }
80  
81      }
82  
83      /**
84       * Rename properties, and change the node structure.
85       */
86      private class SiteMapPropertyVisitor implements NodeVisitor {
87  
88          @Override
89          public void visit(Node siteMap) throws RepositoryException {
90              if (!StringUtils.equals(siteMap.getPrimaryNodeType().getName(), SiteMap.NAME)) {
91                  return;
92              }
93              // rename properties
94              renameProperties(siteMap);
95              // create new default properties for default ChangeFreq and Priority
96              createDefaultProperties(siteMap);
97              // transform sub node 'sites' to a JCR multi value property
98              if (siteMap.hasNode("sites")) {
99                  transformSitesNodeIntoMultiValueProperty(siteMap);
100             }
101         }
102     }
103 
104     /**
105      * Rename property: <br>
106      * - displayName to mgnl:googleSiteMapDisplayName<br>
107      * - type to mgnl:googleSiteMapType<br>
108      * - url to mgnl:googleSiteMapURL<br>
109      * - includeVirtualURIMappings to mgnl:googleSiteMapIncludeVirtualUri.
110      */
111     private void renameProperties(Node siteMapNode) throws RepositoryException {
112         if (siteMapNode.hasProperty("displayName")) {
113             PropertyUtil.renameProperty(siteMapNode.getProperty("displayName"), SiteMap.DISPLAY_NAME);
114         }
115         if (siteMapNode.hasProperty("type")) {
116             PropertyUtil.renameProperty(siteMapNode.getProperty("type"), SiteMap.TYPE);
117         }
118         if (siteMapNode.hasProperty("url")) {
119             PropertyUtil.renameProperty(siteMapNode.getProperty("url"), SiteMap.URL);
120         }
121         if (siteMapNode.hasProperty("includeVirtualURIMappings")) {
122             PropertyUtil.renameProperty(siteMapNode.getProperty("includeVirtualURIMappings"), SiteMap.INCLUDE_VIRTUAL_URI);
123         }
124     }
125 
126     /**
127      * Create : <br>
128      * - mgnl:googleSiteMapDefaultChangeFreq based on the default values<br>
129      * - mgnl:googleSiteMapDefaultPriority based on the default values.
130      */
131     private void createDefaultProperties(Node siteMapNode) throws RepositoryException {
132         if (!siteMapNode.hasProperty(SiteMap.DEFAULT_CHANGEFREQ)) {
133             siteMapNode.setProperty(SiteMap.DEFAULT_CHANGEFREQ, GoogleSiteMapConfiguration.DEFAULT_CHANGE_FREQUENCY);
134         }
135         if (!siteMapNode.hasProperty(SiteMap.DEFAULT_PRIORITY)) {
136             siteMapNode.setProperty(SiteMap.DEFAULT_PRIORITY, GoogleSiteMapConfiguration.DEFAULT_PRIORITY);
137         }
138     }
139 
140     private void transformSitesNodeIntoMultiValueProperty(Node siteMapNode) throws RepositoryException {
141         List<String> siteValues = new ArrayList<String>();
142         // Get all site properties of the sites sub node.
143         Node sitesNode = new JCRMgnlPropertiesFilteringNodeWrapper(siteMapNode.getNode("sites"));
144         PropertyIterator iterator = sitesNode.getProperties();
145         while (iterator.hasNext()) {
146             siteValues.add(iterator.nextProperty().getString());
147         }
148         if (!siteValues.isEmpty()) {
149             siteMapNode.setProperty(SiteMap.PAGES, siteValues.toArray(new String[siteValues.size()]));
150         }
151         log.info("Remove sub node '{}' containing the pages information from '{}' ", sitesNode.getPath(), siteMapNode.getPath());
152         sitesNode.remove();
153     }
154 
155 }