View Javadoc

1   /**
2    * This file Copyright (c) 2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * {@link DialogMigrationTask} for forum module.
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      * Forum specific {@link EditControlMigrator}.
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      * Forum specific {@link LinkControlMigrator}.
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      * RemoveControlMigrator.
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      * Forum specific {@link CheckBoxSwitchControlMigrator}.
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