Clover Coverage Report - Google Sitemap Module 2.2
Coverage timestamp: Fri Jul 11 2014 19:21:34 CEST
../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
21   109   11   5.25
12   61   0.52   4
4     2.75  
1    
 
  RenameSiteMapeSitePagesPropertyName       Line # 60 21 0% 11 2 94.6% 0.9459459
 
No Tests
 
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  6 toggle public RenameSiteMapeSitePagesPropertyName(String name, String description, String workspace) {
67  6 super(name, description);
68  6 this.workspace = StringUtils.isNotBlank(workspace) ? workspace : RepositoryConstants.WEBSITE;
69    }
70   
 
71  5 toggle @Override
72    protected void doExecute(InstallContext ctx) throws RepositoryException, TaskExecutionException {
73  5 try {
74  5 siteSession = ctx.getJCRSession(workspace);
75  5 for (String toRename : propertiesToRename) {
76  20 NodeIterator nodesToHandle = search(toRename);
77  24 while (nodesToHandle.hasNext()) {
78  4 renameProperties(nodesToHandle.nextNode());
79    }
80    }
81   
82    } catch (RepositoryException re) {
83  0 ctx.error("Could not update the siteMap site properties ", re);
84    }
85    }
86   
 
87  20 toggle private NodeIterator search(String toRename) throws RepositoryException {
88  20 final String statement = "SELECT * FROM [" + NodeTypes.Page.NAME + "] AS t WHERE t." + toRename + " is not null";
89  20 QueryManager manager = siteSession.getWorkspace().getQueryManager();
90  20 Query query = manager.createQuery(statement, Query.JCR_SQL2);
91  20 return NodeUtil.filterDuplicates(query.execute().getNodes());
92    }
93   
 
94  4 toggle private void renameProperties(Node page) throws RepositoryException {
95  4 if (page.hasProperty("googleSitemapChangefreq")) {
96  1 PropertyUtil.renameProperty(page.getProperty("googleSitemapChangefreq"), GoogleSiteMap.SITEMAP_CHANGEFREQ);
97    }
98  4 if (page.hasProperty("googleSitemapHide")) {
99  1 PropertyUtil.renameProperty(page.getProperty("googleSitemapHide"), GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP);
100    }
101  4 if (page.hasProperty("googleSitemapPriority")) {
102  1 PropertyUtil.renameProperty(page.getProperty("googleSitemapPriority"), GoogleSiteMap.SITEMAP_PRIORITY);
103    }
104  4 if (page.hasProperty("googleSitemapHideChildren")) {
105  1 PropertyUtil.renameProperty(page.getProperty("googleSitemapHideChildren"), GoogleSiteMap.SITEMAP_HIDE_IN_GOOGLESITEMAP_CHILDREN);
106    }
107    }
108   
109    }