View Javadoc
1   /**
2    * This file Copyright (c) 2014-2018 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.pages.setup;
35  
36  import info.magnolia.jcr.util.NodeTypes;
37  import info.magnolia.jcr.util.NodeUtil;
38  import info.magnolia.module.InstallContext;
39  import info.magnolia.module.delta.AbstractRepositoryTask;
40  import info.magnolia.module.delta.ArrayDelegateTask;
41  import info.magnolia.module.delta.CheckAndModifyPropertyValueTask;
42  import info.magnolia.module.delta.NodeExistsDelegateTask;
43  import info.magnolia.module.delta.PartialBootstrapTask;
44  import info.magnolia.module.delta.RemoveNodeTask;
45  import info.magnolia.module.delta.SetPropertyTask;
46  import info.magnolia.module.delta.TaskExecutionException;
47  import info.magnolia.pages.app.action.DeleteComponentAction;
48  import info.magnolia.pages.app.action.DeletePageItemAction;
49  import info.magnolia.repository.RepositoryConstants;
50  
51  import javax.jcr.Node;
52  import javax.jcr.RepositoryException;
53  import javax.jcr.Session;
54  
55  /**
56   * Updates actions and actionbar section to use availability for all actions and remove unused actions.
57   */
58  public class UpdatePageEditorActionAvailability extends ArrayDelegateTask {
59  
60      private String bootstrapFile = "/mgnl-bootstrap/pages/config.modules.pages.apps.pages.xml";
61      private String[] actions = {"editArea", "addArea", "deleteArea", "editComponent", "addComponent", "deleteComponent", "startMoveComponent", "stopMoveComponent", "showPreviousVersion"};
62      private String[] actionbarSections = {"pagePreviewActions", "pageActions", "areaActions", "componentActions", "pageDeleteActions"};
63      private String[] obsoleteActionbarSections = {"editableAreaActions", "optionalAreaActions", "optionalEditableAreaActions"};
64      private String[] unusedActions = {"redo", "undo", "copyComponent", "pasteComponent"};
65  
66  
67      public UpdatePageEditorActionAvailability() {
68          super("Bootstrap availability rules to page editor actions and actionbar sections.");
69  
70          bootstrapActionAvailability();
71          bootstrapActionbarSectionAvailability();
72          deleteObsoleteActionbarSections();
73          deleteUnusedAction();
74  
75          addTask(new NodeExistsDelegateTask("Update deleteArea action.", "/modules/pages/apps/pages/subApps/detail/actions/deleteArea",
76                  new SetPropertyTask(RepositoryConstants.CONFIG, "/modules/pages/apps/pages/subApps/detail/actions/deleteArea", "implementationClass", DeletePageItemAction.class.getName())));
77  
78          addTask(new NodeExistsDelegateTask("Update deleteArea action.", "/modules/pages/apps/pages/subApps/detail/actions/deleteComponent",
79                  new CheckAndModifyPropertyValueTask("/modules/pages/apps/pages/subApps/detail/actions/deleteComponent", "implementationClass", DeleteComponentAction.class.getName(), DeletePageItemAction.class.getName())));
80  
81  
82          addTask(new UpdateAreaSectionActionsTask());
83  
84  
85          addTask(new NodeExistsDelegateTask("Bootstrap new pageNodeAreaActions.", "/modules/pages/apps/pages/subApps/detail/actionbar/sections",
86                  new PartialBootstrapTask("Bootstrap pageNodeAreaActions actionbar section.",
87                          bootstrapFile,
88                          "/pages/subApps/detail/actionbar/sections/pageNodeAreaActions"))
89          );
90  
91          addTask(new NodeExistsDelegateTask("Bootstrap new editPageNodeArea action.", "/modules/pages/apps/pages/subApps/detail/actions/",
92                  new PartialBootstrapTask("Bootstrap pageNodeAreaActions actionbar section.",
93                          bootstrapFile,
94                          "/pages/subApps/detail/actions/editPageNodeArea"))
95          );
96      }
97  
98      private void deleteUnusedAction() {
99          for (String action : unusedActions) {
100             String actionPath = "/modules/pages/apps/pages/subApps/detail/actions/" + action;
101 
102             addTask(new NodeExistsDelegateTask(String.format("Remove unused action: %s", action), actionPath,
103                     new RemoveNodeTask("", actionPath))
104             );
105         }
106     }
107 
108     private void bootstrapActionAvailability() {
109         for (String action : actions) {
110             String actionPath = "/pages/subApps/detail/actions/" + action;
111 
112             addTask(new NodeExistsDelegateTask(String.format("Update action availability for %s action.", action), "/modules/pages/apps" + actionPath,
113                     new PartialBootstrapTask(String.format("Bootstrap availability for %s action.", action),
114                             bootstrapFile,
115                             actionPath + "/availability"))
116             );
117         }
118     }
119 
120     private void bootstrapActionbarSectionAvailability() {
121         for (String section : actionbarSections) {
122             String sectionPath = "/pages/subApps/detail/actionbar/sections/" + section;
123 
124             addTask(new NodeExistsDelegateTask(String.format("Update action availability for %s section.", section), "/modules/pages/apps" + sectionPath,
125                     new PartialBootstrapTask(String.format("Bootstrap availability for %s action.", section),
126                             bootstrapFile,
127                             sectionPath + "/availability"))
128             );
129         }
130     }
131 
132     private void deleteObsoleteActionbarSections() {
133         for (String section : obsoleteActionbarSections) {
134             String sectionPath = "/modules/pages/apps/pages/subApps/detail/actionbar/sections/" + section;
135 
136             addTask(new NodeExistsDelegateTask(String.format("Remove obsolete page editor actionbar section: %s", section), sectionPath,
137                     new RemoveNodeTask("", sectionPath))
138             );
139         }
140     }
141 
142     /**
143      * Update tasks for areaActions section.
144      */
145     private class UpdateAreaSectionActionsTask extends AbstractRepositoryTask {
146 
147         public UpdateAreaSectionActionsTask() {
148             super("Update page editor actionbar", "Add missing actions to areaActions section.");
149         }
150 
151         @Override
152         protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
153             Session session = installContext.getJCRSession(RepositoryConstants.CONFIG);
154             String areaSection = "/modules/pages/apps/pages/subApps/detail/actionbar/sections/areaActions";
155 
156             if (!session.nodeExists(areaSection)) {
157                 return;
158             }
159             Node areaActions = session.getNode(areaSection);
160             Node groups = NodeUtil.createPath(areaActions, "groups", NodeTypes.ContentNode.NAME);
161 
162             NodeUtil.createPath(groups, "editingActions/items/editArea", NodeTypes.ContentNode.NAME);
163             Node addingActions = NodeUtil.createPath(groups, "addingActions/items", NodeTypes.ContentNode.NAME);
164 
165             NodeUtil.createPath(addingActions, "addArea", NodeTypes.ContentNode.NAME);
166             NodeUtil.createPath(addingActions, "deleteArea", NodeTypes.ContentNode.NAME);
167             NodeUtil.createPath(addingActions, "addComponent", NodeTypes.ContentNode.NAME);
168 
169         }
170     }
171 }