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