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.jcr.util.NodeTypes;
37 import info.magnolia.module.InstallContext;
38
39 import javax.jcr.Node;
40 import javax.jcr.NodeIterator;
41 import javax.jcr.RepositoryException;
42 import javax.jcr.query.Query;
43 import javax.jcr.query.QueryManager;
44
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48
49
50
51 public class FindAndChangeTemplateIdTask extends AbstractRepositoryTask {
52
53 private static final Logger log = LoggerFactory.getLogger(FindAndChangeTemplateIdTask.class);
54
55 private final String workspace;
56 private final String oldTemplateId;
57 private final String newTemplateId;
58
59 public FindAndChangeTemplateIdTask(String workspace, String oldTemplateId, String newTemplateId) {
60 this("Replace old template with new one in whole repository.",
61 String.format("Replace old templateId '%s' with '%s' in the whole '%s' workspace", oldTemplateId, newTemplateId, workspace),
62 workspace, oldTemplateId, newTemplateId);
63 }
64
65 public FindAndChangeTemplateIdTask(String name, String description, String workspace, String oldTemplateId, String newTemplateId) {
66 super(name, description);
67 this.workspace = workspace;
68 this.oldTemplateId = oldTemplateId;
69 this.newTemplateId = newTemplateId;
70 }
71
72 @Override
73 protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
74 final String statement = "select * from [nt:base] where [mgnl:template] = '" + oldTemplateId + "'";
75 QueryManager qm = installContext.getJCRSession(workspace).getWorkspace().getQueryManager();
76 Query query = qm.createQuery(statement, Query.JCR_SQL2);
77 NodeIterator iter = query.execute().getNodes();
78 while (iter.hasNext()) {
79 operateOnNode(installContext, iter.nextNode());
80 }
81 }
82
83 protected void operateOnNode(InstallContext installContext, Node node) {
84 try {
85 NodeTypes.Renderable.set(node, newTemplateId);
86 log.info("Node " + node.getPath() + ": old template " + oldTemplateId + " replaced by " + newTemplateId);
87 } catch (RepositoryException e) {
88 log.warn("Can't change old template id.", e);
89 }
90 }
91
92 }