1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
60
61
62
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
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
94 renameProperties(siteMap);
95
96 createDefaultProperties(siteMap);
97
98 if (siteMap.hasNode("sites")) {
99 transformSitesNodeIntoMultiValueProperty(siteMap);
100 }
101 }
102 }
103
104
105
106
107
108
109
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
128
129
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
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 }