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.migration; |
35 |
|
|
36 |
|
import info.magnolia.cms.util.QueryUtil; |
37 |
|
import info.magnolia.jcr.util.NodeTypes; |
38 |
|
import info.magnolia.jcr.util.NodeUtil; |
39 |
|
import info.magnolia.jcr.wrapper.JCRMgnlPropertiesFilteringNodeWrapper; |
40 |
|
import info.magnolia.jcr.wrapper.JCRPropertiesFilteringNodeWrapper; |
41 |
|
import info.magnolia.module.InstallContext; |
42 |
|
import info.magnolia.module.delta.AbstractRepositoryTask; |
43 |
|
import info.magnolia.module.delta.TaskExecutionException; |
44 |
|
import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration; |
45 |
|
import info.magnolia.module.googlesitemap.config.SiteMapType; |
46 |
|
import info.magnolia.repository.RepositoryConstants; |
47 |
|
|
48 |
|
import java.util.ArrayList; |
49 |
|
import java.util.Iterator; |
50 |
|
import java.util.List; |
51 |
|
|
52 |
|
import javax.jcr.Node; |
53 |
|
import javax.jcr.NodeIterator; |
54 |
|
import javax.jcr.Property; |
55 |
|
import javax.jcr.PropertyIterator; |
56 |
|
import javax.jcr.RepositoryException; |
57 |
|
import javax.jcr.Session; |
58 |
|
import javax.jcr.query.Query; |
59 |
|
|
60 |
|
import org.apache.commons.lang.StringUtils; |
61 |
|
import org.slf4j.Logger; |
62 |
|
import org.slf4j.LoggerFactory; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
|
|
| 89.4% |
Uncovered Elements: 11 (104) |
Complexity: 24 |
Complexity Density: 0.36 |
|
74 |
|
public class SiteMapDefinitionMigrationTask extends AbstractRepositoryTask { |
75 |
|
|
76 |
|
private final Logger log = LoggerFactory.getLogger(SiteMapDefinitionMigrationTask.class); |
77 |
|
|
78 |
|
private final String sourceWorkspace; |
79 |
|
private final String searchRootPath; |
80 |
|
private final String templateName = "google-sitemap:pages/siteMapsConfiguration"; |
81 |
|
private final String siteDefinitionTemplateName = "google-sitemap:components/content/siteComponent"; |
82 |
|
private final String virtualUriTemplateName = "google-sitemap:components/content/virtualUriComponent"; |
83 |
|
private Session siteMapSession; |
84 |
|
|
85 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 3 |
Complexity Density: 1 |
|
86 |
4
|
public SiteMapDefinitionMigrationTask(String name, String description, String sourceWorkspace, String searchRootPath) {... |
87 |
4
|
super(name, description); |
88 |
4
|
this.sourceWorkspace = (StringUtils.isNotBlank(sourceWorkspace)) ? sourceWorkspace : RepositoryConstants.WEBSITE; |
89 |
4
|
this.searchRootPath = (StringUtils.isNotBlank(searchRootPath)) ? searchRootPath : "/"; |
90 |
|
} |
91 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
92 |
3
|
@Override... |
93 |
|
protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException { |
94 |
|
|
95 |
3
|
siteMapSession = installContext.getJCRSession(GoogleSiteMapConfiguration.WORKSPACE); |
96 |
3
|
try { |
97 |
|
|
98 |
3
|
NodeIterator nodeIterator = getQueryResult(); |
99 |
|
|
100 |
8
|
while (nodeIterator.hasNext()) { |
101 |
5
|
handleSimeMapDefinitionMigration(nodeIterator.nextNode()); |
102 |
|
} |
103 |
|
|
104 |
|
} catch (RepositoryException re) { |
105 |
0
|
installContext.error("Unable to perform Migration task " + getName(), re); |
106 |
0
|
throw new TaskExecutionException(re.getMessage()); |
107 |
|
} |
108 |
|
} |
109 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
110 |
5
|
private void handleSimeMapDefinitionMigration(Node siteMapNodeDefinition) throws RepositoryException {... |
111 |
|
|
112 |
5
|
Node targetRootSiteMapFolder = getOrCreateRootSiteMapDefinitionNode(siteMapNodeDefinition); |
113 |
|
|
114 |
|
|
115 |
5
|
Node targetSiteMapDefinition = migrateSiteMapDefinition(siteMapNodeDefinition, targetRootSiteMapFolder); |
116 |
5
|
log.info("New M5 siteMapDefinition created {} based on the previous definition {} ", targetSiteMapDefinition.getPath(), siteMapNodeDefinition.getPath()); |
117 |
|
|
118 |
|
|
119 |
5
|
siteMapNodeDefinition.remove(); |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
130 |
5
|
private Node migrateSiteMapDefinition(Node siteMapNodeDefinition, Node targetRootSiteMapFolder) throws RepositoryException {... |
131 |
|
|
132 |
5
|
Node targetSiteMapDefinition = targetRootSiteMapFolder.addNode(siteMapNodeDefinition.getName(), GoogleSiteMapConfiguration.NODE_TYPE); |
133 |
|
|
134 |
5
|
copyOrUpdateProperty(siteMapNodeDefinition, targetSiteMapDefinition); |
135 |
|
|
136 |
|
|
137 |
5
|
populateSiteMapDefinition(targetSiteMapDefinition, siteMapNodeDefinition); |
138 |
|
|
139 |
5
|
return targetSiteMapDefinition; |
140 |
|
} |
141 |
|
|
|
|
| 93.1% |
Uncovered Elements: 2 (29) |
Complexity: 6 |
Complexity Density: 0.32 |
|
142 |
5
|
private void populateSiteMapDefinition(Node targetSiteMapDefinition, Node sourceSiteMapNodeDefinition) throws RepositoryException {... |
143 |
5
|
List<String> siteMapDefinitionList = new ArrayList<String>(); |
144 |
5
|
boolean insertVirtualUri = false; |
145 |
5
|
Iterable<Node> children = NodeUtil.collectAllChildren(sourceSiteMapNodeDefinition); |
146 |
5
|
Iterator<Node> childrenIterator = children.iterator(); |
147 |
|
|
148 |
27
|
while (childrenIterator.hasNext()) { |
149 |
22
|
Node child = childrenIterator.next(); |
150 |
22
|
if (StringUtils.isNotBlank(NodeTypes.Renderable.getTemplate(child))) { |
151 |
10
|
String templateName = NodeTypes.Renderable.getTemplate(child); |
152 |
10
|
if (virtualUriTemplateName.equals(templateName)) { |
153 |
3
|
insertVirtualUri = true; |
154 |
7
|
} else if (siteDefinitionTemplateName.equals(templateName)) { |
155 |
7
|
siteMapDefinitionList.addAll(extractSiteMapDefinition(child)); |
156 |
|
} |
157 |
|
} |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
5
|
targetSiteMapDefinition.setProperty(GoogleSiteMapConfiguration.INCLUDE_VIRTUAL_URI_MAPPINGS_PROPERTIES, insertVirtualUri); |
162 |
|
|
163 |
5
|
if (!siteMapDefinitionList.isEmpty()) { |
164 |
5
|
Node sites = targetSiteMapDefinition.addNode("sites", NodeTypes.ContentNode.NAME); |
165 |
5
|
int pos = 0; |
166 |
5
|
for (String definition : siteMapDefinitionList) { |
167 |
12
|
sites.setProperty("" + pos, definition); |
168 |
12
|
pos += 1; |
169 |
|
} |
170 |
|
} |
171 |
|
} |
172 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
173 |
7
|
private List<String> extractSiteMapDefinition(Node parent) throws RepositoryException {... |
174 |
7
|
List<String> siteDefinitions = new ArrayList<String>(); |
175 |
7
|
if (parent.hasNode(GoogleSiteMapConfiguration.SITE_DIALOG_CONFIGURATION_NAME)) { |
176 |
7
|
Node filteredNode = new JCRMgnlPropertiesFilteringNodeWrapper(parent.getNode(GoogleSiteMapConfiguration.SITE_DIALOG_CONFIGURATION_NAME)); |
177 |
7
|
PropertyIterator iterator = filteredNode.getProperties(); |
178 |
19
|
while (iterator.hasNext()) { |
179 |
12
|
Property property = iterator.nextProperty(); |
180 |
12
|
siteDefinitions.add(property.getString()); |
181 |
|
} |
182 |
|
} else { |
183 |
0
|
log.info("Node '{}' do not have a 'sites' child node.", parent.getPath()); |
184 |
|
} |
185 |
7
|
return siteDefinitions; |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
192 |
5
|
private void copyOrUpdateProperty(Node source, Node target) throws RepositoryException {... |
193 |
5
|
Node filteredSource = new JCRPropertiesFilteringNodeWrapper(source); |
194 |
5
|
PropertyIterator iterator = filteredSource.getProperties(); |
195 |
40
|
while (iterator.hasNext()) { |
196 |
35
|
Property property = iterator.nextProperty(); |
197 |
35
|
if (target.hasProperty(property.getName())) { |
198 |
0
|
target.getProperty(property.getName()).setValue(property.getValue()); |
199 |
|
} else { |
200 |
35
|
target.setProperty(property.getName(), property.getValue()); |
201 |
|
} |
202 |
|
} |
203 |
|
|
204 |
5
|
target.setProperty(GoogleSiteMapConfiguration.SITE_MAP_URL_PROPERTY_NAME, source.getPath()); |
205 |
|
|
206 |
5
|
target.setProperty(GoogleSiteMapConfiguration.SITE_MAP_TYPE_PROPERTY_NAME, SiteMapType.Standard.name()); |
207 |
|
|
208 |
5
|
target.setProperty(GoogleSiteMapConfiguration.SITE_MAP_DISPLAY_NAME_PROPERTY_NAME, source.getName()); |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
214 |
5
|
Node getOrCreateRootSiteMapDefinitionNode(Node siteMapNodeDefinition) throws RepositoryException {... |
215 |
|
|
216 |
5
|
if (siteMapNodeDefinition.getDepth() <= 1) { |
217 |
2
|
return siteMapSession.getRootNode(); |
218 |
|
} |
219 |
|
|
220 |
3
|
String targetPath = siteMapNodeDefinition.getParent().getPath(); |
221 |
3
|
if (siteMapSession.nodeExists(targetPath)) { |
222 |
0
|
return siteMapSession.getNode(targetPath); |
223 |
|
} |
224 |
|
|
225 |
3
|
return NodeUtil.createPath(siteMapSession.getRootNode(), targetPath, NodeTypes.Folder.NAME, false); |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
231 |
3
|
private NodeIterator getQueryResult() throws RepositoryException {... |
232 |
3
|
NodeIterator nodeIterator = null; |
233 |
3
|
String query = "SELECT * FROM [nt:base] AS t WHERE (ISSAMENODE(t, '" + searchRootPath + "') OR ISDESCENDANTNODE(t, '" + searchRootPath + "')) " + |
234 |
|
"AND t.[mgnl:template] is not null AND contains(t.[" + NodeTypes.Renderable.TEMPLATE + "],'" + templateName + "')"; |
235 |
3
|
nodeIterator = QueryUtil.search(sourceWorkspace, query, Query.JCR_SQL2); |
236 |
3
|
log.info("{} google site map definitions will be handled", nodeIterator.getSize()); |
237 |
3
|
return nodeIterator; |
238 |
|
} |
239 |
|
} |