Clover icon

Google Sitemap Module 2.4.3

  1. Project Clover database Thu May 11 2017 16:52:32 CEST
  2. Package info.magnolia.module.googlesitemap.setup.for2_1

File UpdatePropertyNamesAndNodeStructure.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
28% of files have more coverage

Code metrics

20
32
6
2
155
83
17
0.53
5.33
3
2.83

Classes

Class Line # Actions
UpdatePropertyNamesAndNodeStructure 64 26 0% 14 9
0.8085106680.9%
UpdatePropertyNamesAndNodeStructure.SiteMapPropertyVisitor 86 6 0% 3 1
0.9090909490.9%
 

Contributing tests

This file is covered by 13 tests. .

Source view

1    /**
2    * This file Copyright (c) 2014-2017 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.lang3.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  13 toggle public UpdatePropertyNamesAndNodeStructure(String taskName, String taskDescription) {
69  13 super(taskName, taskDescription);
70    }
71   
 
72  4 toggle @Override
73    public void execute(InstallContext ctx) throws TaskExecutionException {
74  4 try {
75  4 siteMapSession = ctx.getJCRSession(GoogleSiteMapConfiguration.WORKSPACE);
76  4 NodeUtil.visit(siteMapSession.getRootNode(), new SiteMapPropertyVisitor());
77    } catch (RepositoryException re) {
78  0 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  5 toggle @Override
89    public void visit(Node siteMap) throws RepositoryException {
90  5 if (!StringUtils.equals(siteMap.getPrimaryNodeType().getName(), SiteMap.NAME)) {
91  4 return;
92    }
93    // rename properties
94  1 renameProperties(siteMap);
95    // create new default properties for default ChangeFreq and Priority
96  1 createDefaultProperties(siteMap);
97    // transform sub node 'sites' to a JCR multi value property
98  1 if (siteMap.hasNode("sites")) {
99  1 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  1 toggle private void renameProperties(Node siteMapNode) throws RepositoryException {
112  1 if (siteMapNode.hasProperty("displayName")) {
113  1 PropertyUtil.renameProperty(siteMapNode.getProperty("displayName"), SiteMap.DISPLAY_NAME);
114    }
115  1 if (siteMapNode.hasProperty("type")) {
116  1 PropertyUtil.renameProperty(siteMapNode.getProperty("type"), SiteMap.TYPE);
117    }
118  1 if (siteMapNode.hasProperty("url")) {
119  1 PropertyUtil.renameProperty(siteMapNode.getProperty("url"), SiteMap.URL);
120    }
121  1 if (siteMapNode.hasProperty("includeVirtualURIMappings")) {
122  1 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  1 toggle private void createDefaultProperties(Node siteMapNode) throws RepositoryException {
132  1 if (!siteMapNode.hasProperty(SiteMap.DEFAULT_CHANGEFREQ)) {
133  1 siteMapNode.setProperty(SiteMap.DEFAULT_CHANGEFREQ, GoogleSiteMapConfiguration.DEFAULT_CHANGE_FREQUENCY);
134    }
135  1 if (!siteMapNode.hasProperty(SiteMap.DEFAULT_PRIORITY)) {
136  0 siteMapNode.setProperty(SiteMap.DEFAULT_PRIORITY, GoogleSiteMapConfiguration.DEFAULT_PRIORITY);
137    }
138    }
139   
 
140  1 toggle private void transformSitesNodeIntoMultiValueProperty(Node siteMapNode) throws RepositoryException {
141  1 List<String> siteValues = new ArrayList<String>();
142    // Get all site properties of the sites sub node.
143  1 Node sitesNode = new JCRMgnlPropertiesFilteringNodeWrapper(siteMapNode.getNode("sites"));
144  1 PropertyIterator iterator = sitesNode.getProperties();
145  3 while (iterator.hasNext()) {
146  2 siteValues.add(iterator.nextProperty().getString());
147    }
148  1 if (!siteValues.isEmpty()) {
149  1 siteMapNode.setProperty(SiteMap.PAGES, siteValues.toArray(new String[siteValues.size()]));
150    }
151  1 log.info("Remove sub node '{}' containing the pages information from '{}' ", sitesNode.getPath(), siteMapNode.getPath());
152  1 sitesNode.remove();
153    }
154   
155    }