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.delta;
35
36 import info.magnolia.cms.core.MgnlNodeType;
37 import info.magnolia.cms.core.MetaData;
38 import info.magnolia.module.InstallContext;
39 import info.magnolia.repository.RepositoryConstants;
40
41 import javax.jcr.Node;
42 import javax.jcr.RepositoryException;
43 import javax.jcr.Session;
44
45
46
47
48 public class ChangeTemplateTask extends AbstractRepositoryTask {
49 private final String workspace;
50 private final String path;
51 private final String templateId;
52
53 private static final String TEMPLATE_PROPERTY = MgnlNodeType.MGNL_PREFIX + MetaData.TEMPLATE;
54
55 public ChangeTemplateTask(String taskName, String taskDescription, String path, String templateId) {
56 super(taskName, taskDescription);
57 this.workspace = RepositoryConstants.WEBSITE;
58 this.path = path;
59 this.templateId = templateId;
60 }
61
62 @Override
63 public void doExecute(InstallContext installContext) throws RepositoryException {
64 final Session session = installContext.getJCRSession(workspace);
65 if (session.itemExists(path)) {
66 Node page = session.getNode(path);
67 if (!page.hasNode(MetaData.DEFAULT_META_NODE)) {
68 page.addNode(MetaData.DEFAULT_META_NODE, MgnlNodeType.NT_METADATA);
69 }
70 page.getNode(MetaData.DEFAULT_META_NODE).setProperty(TEMPLATE_PROPERTY, templateId);
71 } else {
72 installContext.warn("The template of page " + path + " was to be changed but it was not found.");
73 }
74 }
75 }