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.forum.setup;
35
36 import info.magnolia.jcr.util.NodeUtil;
37 import info.magnolia.module.InstallContext;
38 import info.magnolia.module.delta.TaskExecutionException;
39 import info.magnolia.ui.dialog.setup.DialogMigrationTask;
40 import info.magnolia.ui.dialog.setup.migration.CheckBoxSwitchControlMigrator;
41 import info.magnolia.ui.dialog.setup.migration.ControlMigrator;
42 import info.magnolia.ui.dialog.setup.migration.EditControlMigrator;
43 import info.magnolia.ui.dialog.setup.migration.LinkControlMigrator;
44
45 import java.util.ArrayList;
46 import java.util.Collection;
47
48 import javax.jcr.Node;
49 import javax.jcr.Property;
50 import javax.jcr.RepositoryException;
51 import javax.jcr.Session;
52
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56
57
58
59 public class ForumDialogMigrationTask extends DialogMigrationTask {
60
61 private static Logger log = LoggerFactory.getLogger(ForumDialogMigrationTask.class);
62 private static final String DIALOG_PATH = "/modules/forum/dialogs";
63 private static final String TABS_PATH = "form/tabs";
64 private static final String NEW_TAB_NAME = "tabMain";
65 private static final String PROPERTY_JS = "jsExecutedAfterSaving";
66 private final Collection<Node> controlsToRemove = new ArrayList<Node>();
67
68 public ForumDialogMigrationTask(String moduleName) {
69 super("forum");
70 }
71
72 @Override
73 public void execute(InstallContext installContext) throws TaskExecutionException {
74 super.execute(installContext);
75 this.adjustTabs(installContext);
76 this.removeObsoleteControls();
77 }
78
79 private void adjustTabs(InstallContext installContext) {
80 try {
81 Session config = installContext.getConfigJCRSession();
82 if (!config.nodeExists(DIALOG_PATH)) {
83 log.warn("No dialogs found.");
84 return;
85 }
86 Node dialogs = config.getNode("/modules/forum/dialogs");
87 for (Node dialog : NodeUtil.getNodes(dialogs)) {
88
89 if (dialog.hasProperty(PROPERTY_JS)) {
90 dialog.getProperty(PROPERTY_JS).remove();
91 }
92
93 if (!dialog.hasNode(TABS_PATH)) {
94 continue;
95 }
96 Node firstTab = dialog.getNode(TABS_PATH).getNodes().nextNode();
97 NodeUtil.renameNode(firstTab, NEW_TAB_NAME);
98 }
99 } catch (RepositoryException e) {
100 log.warn("Cannot rename tabs in forum dialogs.", e);
101 }
102 }
103
104 private void removeObsoleteControls() {
105 for (Node node : controlsToRemove) {
106 try {
107 node.remove();
108 } catch (RepositoryException e) {
109 log.warn("Cannot remove old control: '{}'.", node);
110 }
111 }
112 }
113
114 private void removeObsoleteProperty(Node controlNode, String propertyName) throws RepositoryException {
115 this.removeObsoleteProperty(controlNode, propertyName, null);
116 }
117
118 private void removeObsoleteProperty(Node controlNode, String propertyName, String onlyIfHasThisValue) throws RepositoryException {
119 if (controlNode.hasProperty(propertyName)) {
120 if (onlyIfHasThisValue != null) {
121 if (!controlNode.getProperty(propertyName).getString().equals(onlyIfHasThisValue)) {
122 return;
123 }
124 }
125 controlNode.getProperty(propertyName).remove();
126 }
127 }
128
129 private void renameObsoleteProperty(Node controlNode, String oldName, String newName) throws RepositoryException {
130 if (!controlNode.hasProperty(oldName)) {
131 return;
132 }
133 Property property = controlNode.getProperty(oldName);
134 controlNode.setProperty(newName, property.getValue());
135 property.remove();
136 }
137
138
139
140
141 protected class ForumEditControlMigrator extends EditControlMigrator {
142 @Override
143 public void migrate(Node controlNode, InstallContext installContext) throws RepositoryException {
144 super.migrate(controlNode, installContext);
145 if (!controlNode.hasProperty("type")) {
146 controlNode.setProperty("type", "String");
147 }
148
149 if ("auhtor".equals(controlNode.getName())) {
150 NodeUtil.renameNode(controlNode, "author");
151 }
152 }
153 }
154
155
156
157
158 protected class ForumLinkControlMigrator extends LinkControlMigrator {
159
160 @Override
161 public void migrate(Node controlNode, InstallContext installContext) throws RepositoryException {
162 super.migrate(controlNode, installContext);
163
164 removeObsoleteProperty(controlNode, "tree");
165 renameObsoleteProperty(controlNode, "repository", "targetWorkspace");
166
167 String controlName = controlNode.getName();
168 if ("threadId".equals(controlName) || "forumId".equals(controlName)) {
169 controlNode.setProperty("appName", "forum");
170 controlNode.setProperty("targetWorkspace", "forum");
171 } else {
172 controlNode.setProperty("appName", "pages");
173 }
174 }
175 }
176
177
178
179
180 protected class RemoveControlMigrator implements ControlMigrator {
181 @Override
182 public void migrate(Node controlNode, InstallContext installContext) throws RepositoryException {
183 controlsToRemove.add(controlNode);
184 }
185 }
186
187
188
189
190 protected class ForumCheckBoxSwitchControlMigrator extends CheckBoxSwitchControlMigrator {
191 @Override
192 public void migrate(Node controlNode, InstallContext installContext) throws RepositoryException {
193 super.migrate(controlNode, installContext);
194 removeObsoleteProperty(controlNode, "defaultValue");
195 }
196 }
197 }
198
199