View Javadoc
1   /**
2    * This file Copyright (c) 2016-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.ui.contentapp.setup;
35  
36  import info.magnolia.cms.core.Path;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.module.InstallContext;
39  import info.magnolia.module.delta.AbstractRepositoryTask;
40  import info.magnolia.module.delta.TaskExecutionException;
41  import info.magnolia.repository.RepositoryConstants;
42  import info.magnolia.ui.contentapp.browser.action.DelegateByNodeTypeActionDefinition;
43  import info.magnolia.ui.contentapp.browser.action.ExpandNodeActionDefinition;
44  
45  import java.util.Map;
46  
47  import javax.jcr.Node;
48  import javax.jcr.RepositoryException;
49  import javax.jcr.Session;
50  
51  /**
52   * Task that replaces defaultAction in the actionbar with a delegate action.
53   *
54   * @see DelegateByNodeTypeActionDefinition
55   * @see ExpandNodeActionDefinition
56   */
57  public class DefaultActionToDelegateActionTask extends AbstractRepositoryTask {
58  
59      private static final String EXPAND_NODE_ACTION_NAME = "expandNodeAction";
60      private static final String DELEGATE_BY_NODE_TYPE_ACTION_NAME = "delegateByNodeTypeAction";
61      private static final String NODE_TYPE_TO_ACTION_MAPPINGS_NAME = "nodeTypeToActionMappings";
62  
63      private static final String CLASS_PROPERTY = "class";
64      private static final String NODE_TYPE = "nodeType";
65      private static final String ACTION = "action";
66      private static final String DEFAULT_ACTION = "defaultAction";
67  
68      private static final String ACTIONS_PATH = "/modules/%s/apps/%s/subApps/%s/actions";
69      private static final String ACTIONBAR_PATH = "/modules/%s/apps/%s/subApps/%s/actionbar";
70  
71      private final String appName;
72      private final String subAppName;
73      private final Map<String, String> nodeTypeToActionMapping;
74  
75      public DefaultActionToDelegateActionTask(String name, String description, String appName, String subAppName, Map<String, String> nodeTypeToActionMapping) {
76          super(name, description);
77          this.appName = appName;
78          this.subAppName = subAppName;
79          this.nodeTypeToActionMapping = nodeTypeToActionMapping;
80      }
81  
82      @Override
83      protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
84          String moduleName = installContext.getCurrentModuleDefinition().getName();
85          Session session = installContext.getJCRSession(RepositoryConstants.CONFIG);
86          setupExpandNodeAction(session, moduleName);
87          setupDelegateAction(session, moduleName);
88          linkDefaultActionToDelegateAction(session, moduleName);
89      }
90  
91      private void setupDelegateAction(Session session, String moduleName) throws RepositoryException {
92          String actionsPath = String.format(ACTIONS_PATH, moduleName, appName, subAppName);
93          if (!session.nodeExists(actionsPath + "/" + DELEGATE_BY_NODE_TYPE_ACTION_NAME)) {
94              Node actionsNode = session.getNode(actionsPath);
95              Node delegateActionNode = actionsNode.addNode(DELEGATE_BY_NODE_TYPE_ACTION_NAME, NodeTypes.ContentNode.NAME);
96              delegateActionNode.setProperty(CLASS_PROPERTY, DelegateByNodeTypeActionDefinition.class.getCanonicalName());
97              Node mappingsNode = delegateActionNode.addNode(NODE_TYPE_TO_ACTION_MAPPINGS_NAME, NodeTypes.ContentNode.NAME);
98              for (Map.Entry<String, String> entry : nodeTypeToActionMapping.entrySet()) {
99                  Node node = mappingsNode.addNode(Path.getValidatedLabel(entry.getKey()), NodeTypes.ContentNode.NAME);
100                 node.setProperty(NODE_TYPE, entry.getKey());
101                 node.setProperty(ACTION, entry.getValue());
102             }
103         }
104     }
105 
106     private void setupExpandNodeAction(Session session, String moduleName) throws RepositoryException {
107         String actionsPath = String.format(ACTIONS_PATH, moduleName, appName, subAppName);
108         if (!session.nodeExists(actionsPath + "/" + EXPAND_NODE_ACTION_NAME)) {
109             Node actionsNode = session.getNode(actionsPath);
110             Node expandActionNode = actionsNode.addNode(EXPAND_NODE_ACTION_NAME, NodeTypes.ContentNode.NAME);
111             expandActionNode.setProperty(CLASS_PROPERTY, ExpandNodeActionDefinition.class.getCanonicalName());
112         }
113     }
114 
115     private void linkDefaultActionToDelegateAction(Session session, String moduleName) throws RepositoryException {
116         Node actionbarNode = session.getNode(String.format(ACTIONBAR_PATH, moduleName, appName, subAppName));
117         actionbarNode.setProperty(DEFAULT_ACTION, DELEGATE_BY_NODE_TYPE_ACTION_NAME);
118     }
119 }