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.templatingkit.setup.for2_9;
35
36 import info.magnolia.module.InstallContext;
37 import info.magnolia.module.delta.AbstractRepositoryTask;
38 import info.magnolia.module.delta.TaskExecutionException;
39 import info.magnolia.module.templatingkit.templates.pages.STKPage;
40
41 import javax.jcr.Node;
42 import javax.jcr.NodeIterator;
43 import javax.jcr.RepositoryException;
44 import javax.jcr.Session;
45
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49
50
51
52 public class MigrateTemplateTypesTask extends AbstractRepositoryTask {
53
54 private final static Logger log = LoggerFactory.getLogger(MigrateTemplateTypesTask.class);
55
56 protected final static String MESSAGE_FORMAT = "Checked %d STK page definitions modified %d and copied %d category and %d subcategory properties";
57
58 protected final static String PROPERTY_NAME_CLASS = "class";
59 protected final static String PROPERTY_VALUE_STK_PAGE_CLASS = STKPage.class.getName();
60
61 protected final static String PROPERTY_NAME_CATEGORY = "category";
62 protected final static String PROPERTY_NAME_SUBCATEGORY = "subcategory";
63
64 protected final static String PROPERTY_NAME_TYPE = "type";
65 protected final static String PROPERTY_NAME_SUBTYPE = "subtype";
66
67 protected static final String PROPERTY_NAME_EXTENDS = "extends";
68
69 private Session session;
70 private int templates = 0;
71 private int templatesModified = 0;
72 private int categories = 0;
73 private int subcategories = 0;
74
75 public MigrateTemplateTypesTask() {
76 super("Migrate template categories to template types", "This task will copy template properties 'category' and 'subcategory' to new template types");
77 }
78
79 @Override
80 protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
81 session = installContext.getConfigJCRSession();
82 final Node startNode = session.getNode("/modules");
83 final NodeIterator nodeIterator = startNode.getNodes();
84 while (nodeIterator.hasNext()) {
85 Node moduleNode = nodeIterator.nextNode();
86 if (moduleNode.hasNode("templates")) {
87 operateOnNode(moduleNode.getNode("templates"));
88 }
89 }
90
91 final String message = String.format(MESSAGE_FORMAT, templates, templatesModified, categories, subcategories);
92 log.info(message);
93 installContext.info(message);
94 }
95
96 private void operateOnNode(Node templatesNode) throws RepositoryException {
97 if (isSTKPage(templatesNode) || extendsSTKPage(templatesNode)) {
98 templates++;
99 boolean modified = false;
100
101 if (templatesNode.hasProperty(PROPERTY_NAME_CATEGORY) && !templatesNode.hasProperty(PROPERTY_NAME_TYPE)) {
102 final String category = templatesNode.getProperty(PROPERTY_NAME_CATEGORY).getString();
103 templatesNode.setProperty(PROPERTY_NAME_TYPE, category);
104 categories++;
105 modified = true;
106 }
107
108 if (templatesNode.hasProperty(PROPERTY_NAME_SUBCATEGORY) && !templatesNode.hasProperty(PROPERTY_NAME_SUBTYPE)) {
109 final String category = templatesNode.getProperty(PROPERTY_NAME_SUBCATEGORY).getString();
110 templatesNode.setProperty(PROPERTY_NAME_SUBTYPE, category);
111 subcategories++;
112 modified = true;
113 }
114 if (modified)
115 templatesModified++;
116 } else {
117 if (templatesNode.hasNodes()) {
118 for (final NodeIterator nodeIterator = templatesNode.getNodes(); nodeIterator.hasNext(); ) {
119 operateOnNode(nodeIterator.nextNode());
120 }
121 }
122 }
123 }
124
125
126
127
128 private boolean isSTKPage(Node node) {
129 try {
130 return node.hasProperty(PROPERTY_NAME_CLASS) && PROPERTY_VALUE_STK_PAGE_CLASS.equals(node.getProperty(PROPERTY_NAME_CLASS).getString());
131 } catch (RepositoryException e) {
132 return false;
133 }
134 }
135
136
137
138
139 private boolean extendsSTKPage(Node node) {
140 try {
141 return node.hasProperty(PROPERTY_NAME_EXTENDS) && isSTKPage(session.getNode(node.getProperty(PROPERTY_NAME_EXTENDS).getString()));
142 } catch (RepositoryException e) {
143 return false;
144 }
145 }
146
147 }