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.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
53
54
55
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 }