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.resources.setup; |
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.Task; |
42 |
|
import info.magnolia.module.delta.TaskExecutionException; |
43 |
|
import info.magnolia.objectfactory.Components; |
44 |
|
import info.magnolia.repository.RepositoryManager; |
45 |
|
import info.magnolia.resourceloader.Resource; |
46 |
|
import info.magnolia.resourceloader.ResourceOrigin; |
47 |
|
import info.magnolia.resourceloader.jcr.JcrResourceOrigin; |
48 |
|
import info.magnolia.resourceloader.layered.LayeredResource; |
49 |
|
import info.magnolia.resourceloader.layered.LayeredResourceOrigin; |
50 |
|
|
51 |
|
import java.io.IOException; |
52 |
|
import java.io.Reader; |
53 |
|
import java.util.Iterator; |
54 |
|
|
55 |
|
import javax.inject.Provider; |
56 |
|
import javax.jcr.Node; |
57 |
|
import javax.jcr.Property; |
58 |
|
import javax.jcr.PropertyIterator; |
59 |
|
import javax.jcr.RepositoryException; |
60 |
|
import javax.jcr.Session; |
61 |
|
|
62 |
|
import org.apache.commons.io.IOUtils; |
63 |
|
import org.apache.commons.lang3.ObjectUtils; |
64 |
|
import org.apache.commons.lang3.StringUtils; |
65 |
|
import org.apache.jackrabbit.commons.predicate.NodeTypePredicate; |
66 |
|
import org.slf4j.Logger; |
67 |
|
import org.slf4j.LoggerFactory; |
68 |
|
|
69 |
|
|
70 |
|
@value |
71 |
|
@value |
72 |
|
|
73 |
|
@value |
74 |
|
|
75 |
|
@value |
76 |
|
@link |
77 |
|
|
78 |
|
|
79 |
|
@value |
80 |
|
@link |
81 |
|
|
82 |
|
|
83 |
|
|
|
|
| 95.4% |
Uncovered Elements: 5 (108) |
Complexity: 34 |
Complexity Density: 0.51 |
|
84 |
|
public class TemplateMigrationTask extends AbstractRepositoryTask { |
85 |
|
|
86 |
|
private static final Logger log = LoggerFactory.getLogger(TemplateMigrationTask.class); |
87 |
|
|
88 |
|
protected static final String TEMPLATES_WORKSPACE = "templates"; |
89 |
|
|
90 |
|
protected static final String PROPERTY_AUTO_IMPORT = "autoImport"; |
91 |
|
protected static final String PROPERTY_TEXT = "text"; |
92 |
|
protected static final String PROPERTY_ENABLED = "enabled"; |
93 |
|
|
94 |
|
private final Provider<ResourceOrigin> resourceOriginProvider; |
95 |
|
|
96 |
|
private Session templatesSession = null; |
97 |
|
private Session resourcesSession = null; |
98 |
|
|
99 |
|
private final Task onSuccess; |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
101 |
24 |
public TemplateMigrationTask(Task onSuccess) {... |
102 |
24 |
super("Migrates templates from 'templates' workspace", "Migrates templates from 'templates' workspace to 'resources' workspace"); |
103 |
|
|
104 |
24 |
this.resourceOriginProvider = new Provider<ResourceOrigin>() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
105 |
11 |
@Override... |
106 |
|
public ResourceOrigin get() { |
107 |
11 |
return Components.getComponent(ResourceOrigin.class); |
108 |
|
} |
109 |
|
}; |
110 |
|
|
111 |
24 |
this.onSuccess = onSuccess; |
112 |
|
} |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
13 |
public TemplateMigrationTask() {... |
115 |
13 |
this(null); |
116 |
|
} |
117 |
|
|
|
|
| 98.2% |
Uncovered Elements: 1 (57) |
Complexity: 14 |
Complexity Density: 0.4 |
|
118 |
22 |
@Override... |
119 |
|
protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException { |
120 |
22 |
final RepositoryManager repositoryManager = Components.getComponent(RepositoryManager.class); |
121 |
22 |
String infoMessage = null; |
122 |
22 |
if (repositoryManager.hasWorkspace(TEMPLATES_WORKSPACE)) { |
123 |
12 |
try { |
124 |
12 |
templatesSession = installContext.getJCRSession(TEMPLATES_WORKSPACE); |
125 |
|
} catch (RepositoryException e) { |
126 |
1 |
infoMessage = String.format("Could not get session for workspace '%s'. " + |
127 |
|
"Not running template migration task.", TEMPLATES_WORKSPACE); |
128 |
|
} |
129 |
|
} else { |
130 |
10 |
infoMessage = String.format("Inplace-templating might not be installed. Not running template migration task."); |
131 |
|
} |
132 |
|
|
133 |
22 |
if (infoMessage != null) { |
134 |
11 |
installContext.info(infoMessage); |
135 |
11 |
log.info(infoMessage); |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
} |
140 |
|
|
141 |
22 |
if (templatesSession != null) { |
142 |
11 |
resourcesSession = installContext.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE); |
143 |
|
|
144 |
11 |
final LayeredResourceOrigin resourceOrigin = (LayeredResourceOrigin) resourceOriginProvider.get(); |
145 |
|
|
146 |
11 |
final Iterator<Node> nodeIterator = NodeUtil.collectAllChildren(templatesSession.getRootNode(), |
147 |
|
new NodeTypePredicate(NodeTypes.Content.NAME, false)).iterator(); |
148 |
|
|
149 |
19 |
while (nodeIterator.hasNext()) { |
150 |
10 |
final Node templateNode = nodeIterator.next(); |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
10 |
final boolean enabled = templateNode.hasProperty(PROPERTY_ENABLED) && templateNode.getProperty(PROPERTY_ENABLED).getBoolean(); |
156 |
10 |
if (!enabled) { |
157 |
1 |
continue; |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
9 |
final boolean autoImport = templateNode.hasProperty(PROPERTY_AUTO_IMPORT) && templateNode.getProperty(PROPERTY_AUTO_IMPORT).getBoolean(); |
162 |
|
|
163 |
9 |
try { |
164 |
|
|
165 |
9 |
final String resourceTemplatePath = templateNode.getPath() + ".ftl"; |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
9 |
if (autoImport) { |
170 |
3 |
if (!resourceOrigin.hasPath(resourceTemplatePath)) { |
171 |
|
|
172 |
2 |
migrateTemplate(templateNode, resourceTemplatePath); |
173 |
|
} |
174 |
|
} else { |
175 |
|
|
176 |
6 |
if (resourceOrigin.hasPath(resourceTemplatePath)) { |
177 |
4 |
final LayeredResource resource = resourceOrigin.getByPath(resourceTemplatePath); |
178 |
|
|
179 |
|
|
180 |
4 |
if (resource.getFirst().getOrigin() instanceof JcrResourceOrigin) { |
181 |
|
|
182 |
3 |
if (!templateEqualsResource(templateNode, resource)) { |
183 |
|
|
184 |
|
|
185 |
|
|
186 |
2 |
throw new TaskExecutionException(String.format("Contents of '%s:%s' and resource '%s:%s' were not equal but autoImport was set to 'false'. " + |
187 |
|
"Cannot proceed with template migration.", TEMPLATES_WORKSPACE, resourceTemplatePath, |
188 |
|
JcrResourceOrigin.RESOURCES_WORKSPACE, resource.getPath())); |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
} else { |
193 |
|
|
194 |
1 |
migrateTemplate(templateNode, resourceTemplatePath); |
195 |
|
} |
196 |
|
} else { |
197 |
|
|
198 |
2 |
migrateTemplate(templateNode, resourceTemplatePath); |
199 |
|
} |
200 |
|
} |
201 |
|
|
202 |
|
} catch (RepositoryException e) { |
203 |
0 |
log.info("An error occurred when handling template '{}:{}'. Will not migrate this template.", |
204 |
|
TEMPLATES_WORKSPACE, NodeUtil.getPathIfPossible(templateNode), e); |
205 |
|
} |
206 |
|
} |
207 |
|
|
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
20 |
if (onSuccess != null) { |
213 |
10 |
onSuccess.execute(installContext); |
214 |
|
} |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
@link@value |
219 |
|
@link@link |
220 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 4 |
Complexity Density: 0.57 |
|
221 |
3 |
private boolean templateEqualsResource(final Node templateNode, final Resource resource) {... |
222 |
3 |
try (final Reader reader = resource.openReader()) { |
223 |
3 |
final String contentResource = IOUtils.toString(reader); |
224 |
3 |
final String contentTemplate = PropertyUtil.getString(templateNode, PROPERTY_TEXT, null); |
225 |
|
|
226 |
3 |
return ObjectUtils.equals(contentResource, contentTemplate); |
227 |
|
} catch (IOException e) { |
228 |
0 |
log.error("Cannot read contents of '{}:{}'.", resource.getOrigin().getName(), resource.getName(), e); |
229 |
|
} |
230 |
|
|
231 |
0 |
return false; |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
|
@link@value |
236 |
|
|
237 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
238 |
5 |
private void migrateTemplate(final Node templateNode, final String newTemplatePath) throws RepositoryException {... |
239 |
5 |
final String parentPath = StringUtils.substringBeforeLast(newTemplatePath, "/"); |
240 |
5 |
if (StringUtils.isNotBlank(parentPath) && !resourcesSession.nodeExists(parentPath)) { |
241 |
1 |
NodeUtil.createPath(resourcesSession.getRootNode(), parentPath, NodeTypes.Folder.NAME); |
242 |
|
|
243 |
1 |
log.info("Created path '{}:{}'.", JcrResourceOrigin.RESOURCES_WORKSPACE, parentPath); |
244 |
|
} |
245 |
|
|
246 |
5 |
copyTemplateNode(templateNode, newTemplatePath); |
247 |
|
|
248 |
5 |
log.info("Migrated template '{}:{}' to '{}:{}'.", TEMPLATES_WORKSPACE, NodeUtil.getPathIfPossible(templateNode), |
249 |
|
JcrResourceOrigin.RESOURCES_WORKSPACE, newTemplatePath); |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
|
@link |
254 |
|
@value |
255 |
|
@value |
256 |
|
|
257 |
|
@link |
258 |
|
@link |
259 |
|
@link@link |
260 |
|
|
|
|
| 91.7% |
Uncovered Elements: 2 (24) |
Complexity: 10 |
Complexity Density: 0.71 |
|
261 |
5 |
private void copyTemplateNode(final Node templateNode, final String newTemplatePath) throws RepositoryException {... |
262 |
5 |
final Node newTemplateNode = NodeUtil.createPath(resourcesSession.getRootNode(), newTemplatePath, NodeTypes.Content.NAME); |
263 |
5 |
final PropertyIterator propertyIterator = templateNode.getProperties(); |
264 |
|
|
265 |
24 |
while (propertyIterator.hasNext()) { |
266 |
19 |
final Property property = propertyIterator.nextProperty(); |
267 |
19 |
final String propertyName = property.getName(); |
268 |
|
|
269 |
|
|
270 |
19 |
if (PROPERTY_ENABLED.equals(propertyName)) { |
271 |
5 |
continue; |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
14 |
if (propertyName.startsWith(NodeTypes.REP_PREFIX) || propertyName.startsWith(NodeTypes.JCR_PREFIX)) { |
276 |
0 |
continue; |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
|
|
281 |
14 |
if (NodeTypes.LastModified.LAST_MODIFIED.equals(propertyName) || NodeTypes.LastModified.LAST_MODIFIED_BY.equals(propertyName)) { |
282 |
2 |
continue; |
283 |
|
} |
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
|
288 |
12 |
if (NodeTypes.Activatable.LAST_ACTIVATED.equals(propertyName) || NodeTypes.Activatable.LAST_ACTIVATED_BY.equals(propertyName) || |
289 |
|
NodeTypes.Activatable.ACTIVATION_STATUS.equals(propertyName)) { |
290 |
2 |
continue; |
291 |
|
} |
292 |
|
|
293 |
10 |
newTemplateNode.setProperty(propertyName, property.getValue()); |
294 |
|
} |
295 |
|
} |
296 |
|
|
297 |
|
} |