View Javadoc

1   /**
2    * This file Copyright (c) 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.setup.for2_1;
35  
36  import info.magnolia.jcr.util.NodeTypes;
37  import info.magnolia.jcr.util.NodeUtil;
38  import info.magnolia.jcr.util.PropertyUtil;
39  import info.magnolia.module.InstallContext;
40  import info.magnolia.module.delta.AbstractRepositoryTask;
41  import info.magnolia.module.delta.TaskExecutionException;
42  import info.magnolia.module.googlesitemap.SiteMapNodeTypes.GoogleSiteMap;
43  import info.magnolia.repository.RepositoryConstants;
44  
45  import java.util.Arrays;
46  import java.util.List;
47  
48  import javax.jcr.Node;
49  import javax.jcr.NodeIterator;
50  import javax.jcr.RepositoryException;
51  import javax.jcr.Session;
52  import javax.jcr.query.Query;
53  import javax.jcr.query.QueryManager;
54  
55  import org.apache.commons.lang.StringUtils;
56  
57  /**
58   * Rename siteMap properties stored under pages nodes.
59   */
60  public class RenameSiteMapeSitePagesPropertyName extends AbstractRepositoryTask {
61  
62      private Session siteSession;
63      private final String workspace;
64      private final List<String> propertiesToRename = Arrays.asList("googleSitemapChangefreq", "googleSitemapHide", "googleSitemapPriority", "googleSitemapHideChildren");
65  
66      public RenameSiteMapeSitePagesPropertyName(String name, String description, String workspace) {
67          super(name, description);
68          this.workspace = StringUtils.isNotBlank(workspace) ? workspace : RepositoryConstants.WEBSITE;
69      }
70  
71      @Override
72      protected void doExecute(InstallContext ctx) throws RepositoryException, TaskExecutionException {
73          try {
74              siteSession = ctx.getJCRSession(workspace);
75              for (String toRename : propertiesToRename) {
76                  NodeIterator nodesToHandle = search(toRename);
77                  while (nodesToHandle.hasNext()) {
78                      renameProperties(nodesToHandle.nextNode());
79                  }
80              }
81  
82          } catch (RepositoryException re) {
83              ctx.error("Could not update the siteMap site properties ", re);
84          }
85      }
86  
87      private NodeIterator search(String toRename) throws RepositoryException {
88          final String statement = "SELECT * FROM [" + NodeTypes.Page.NAME + "] AS t WHERE  t." + toRename + " is not null";
89          QueryManager manager = siteSession.getWorkspace().getQueryManager();
90          Query query = manager.createQuery(statement, Query.JCR_SQL2);
91          return NodeUtil.filterDuplicates(query.execute().getNodes());
92      }
93  
94      private void renameProperties(Node page) throws RepositoryException {
95          if (page.hasProperty("googleSitemapChangefreq")) {
96              PropertyUtil.renameProperty(page.getProperty("googleSitemapChangefreq"), GoogleSiteMap.SITEMAP_CHANGEFREQ);
97          }
98          if (page.hasProperty("googleSitemapHide")) {
99              PropertyUtil.renameProperty(page.getProperty("googleSitemapHide"), GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP);
100         }
101         if (page.hasProperty("googleSitemapPriority")) {
102             PropertyUtil.renameProperty(page.getProperty("googleSitemapPriority"), GoogleSiteMap.SITEMAP_PRIORITY);
103         }
104         if (page.hasProperty("googleSitemapHideChildren")) {
105             PropertyUtil.renameProperty(page.getProperty("googleSitemapHideChildren"), GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN);
106         }
107     }
108 
109 }