]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.akb.ui/src/main/java/org/argeo/slc/akb/ui/commands/OpenAkbNodeEditor.java
Add first draft for Connector Alias and Template environment editors.
[gpl/argeo-slc.git] / plugins / org.argeo.slc.akb.ui / src / main / java / org / argeo / slc / akb / ui / commands / OpenAkbNodeEditor.java
1 package org.argeo.slc.akb.ui.commands;
2
3 import javax.jcr.Node;
4 import javax.jcr.Property;
5 import javax.jcr.Repository;
6 import javax.jcr.RepositoryException;
7 import javax.jcr.Session;
8
9 import org.argeo.eclipse.ui.dialogs.SingleValue;
10 import org.argeo.jcr.JcrUtils;
11 import org.argeo.slc.akb.AkbException;
12 import org.argeo.slc.akb.AkbService;
13 import org.argeo.slc.akb.AkbTypes;
14 import org.argeo.slc.akb.ui.AkbUiPlugin;
15 import org.argeo.slc.akb.ui.editors.AkbConnectorAliasEditor;
16 import org.argeo.slc.akb.ui.editors.AkbEnvTemplateEditor;
17 import org.argeo.slc.akb.ui.editors.AkbNodeEditorInput;
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.ui.IWorkbenchPage;
22 import org.eclipse.ui.PartInitException;
23 import org.eclipse.ui.handlers.HandlerUtil;
24
25 /**
26 * Opens or show an AKB specific node in a single repository / single workspace
27 * environment given some parameters, namely :
28 * <ul>
29 * <li>PARAM_NODE_JCR_ID: the corresponding JCR ID might be null to create a new
30 * one</li>
31 * <li>PARAM_NODE_TYPE: jcr type of the node to create</li>
32 * <li>PARAM_PARENT_NODE_JCR_ID: Only used in the case of the creation of a new
33 * node.</li>
34 * </ul>
35 */
36 public class OpenAkbNodeEditor extends AbstractHandler {
37 public final static String ID = AkbUiPlugin.PLUGIN_ID
38 + ".openAkbNodeEditor";
39
40 /* DEPENDENCY INJECTION */
41 private Repository repository;
42 private AkbService akbService;
43
44 public final static String PARAM_NODE_JCR_ID = "param.nodeJcrId";
45 public final static String PARAM_NODE_TYPE = "param.nodeType";
46 public final static String PARAM_PARENT_NODE_JCR_ID = "param.parentNodeJcrId";
47
48 public Object execute(ExecutionEvent event) throws ExecutionException {
49
50 String nodeType = event.getParameter(PARAM_NODE_TYPE);
51 String nodeJcrId = event.getParameter(PARAM_NODE_JCR_ID);
52 String parentNodeJcrId = event.getParameter(PARAM_PARENT_NODE_JCR_ID);
53
54 Session session = null;
55 try {
56 // caches current Page
57 IWorkbenchPage currentPage = HandlerUtil.getActiveWorkbenchWindow(
58 event).getActivePage();
59
60 session = repository.login();
61 Node node = null;
62
63 if (nodeJcrId == null)
64 if (parentNodeJcrId == null)
65 throw new AkbException(
66 "Define a parent node to create a new node");
67 else
68 node = createNewNode(session, nodeType, parentNodeJcrId);
69 else
70 node = session.getNodeByIdentifier(nodeJcrId);
71
72 // no node has been found or created, return
73 if (node == null)
74 return null;
75
76 String editorId = getEditorForNode(node);
77
78 // no editor has been found, return silently
79 if (editorId == null)
80 return null;
81
82 AkbNodeEditorInput eei = new AkbNodeEditorInput(
83 node.getIdentifier());
84
85 currentPage.openEditor(eei, editorId);
86 } catch (PartInitException pie) {
87 throw new AkbException(
88 "Unexpected PartInitException while opening akb node editor",
89 pie);
90 } catch (RepositoryException e) {
91 throw new AkbException("unexpected JCR error while opening "
92 + nodeType + " editor", e);
93 } finally {
94 JcrUtils.logoutQuietly(session);
95 }
96 return null;
97 }
98
99 private Node createNewNode(Session session, String nodeType,
100 String parentNodeJcrId) throws RepositoryException {
101 Node node = null;
102 String name = SingleValue.ask("New name", "Create AKB item");
103
104 if (name == null)
105 return null;
106 if (AkbTypes.AKB_ENV_TEMPLATE.equals(nodeType)) {
107 node = akbService.createAkbTemplate(
108 session.getNodeByIdentifier(parentNodeJcrId), name);
109 } else {
110 Node parentNode = session.getNodeByIdentifier(parentNodeJcrId);
111 node = parentNode.addNode(name, nodeType);
112 node.setProperty(Property.JCR_TITLE, name);
113 }
114 // corresponding node is saved but not checked in, in order to ease
115 // cancel actions.
116 session.save();
117 return node;
118 }
119
120 private String getEditorForNode(Node node) throws RepositoryException {
121 String editorId = null;
122 if (node.isNodeType(AkbTypes.AKB_CONNECTOR_ALIAS))
123 editorId = AkbConnectorAliasEditor.ID;
124 else if (node.isNodeType(AkbTypes.AKB_ENV_TEMPLATE))
125 editorId = AkbEnvTemplateEditor.ID;
126 // else
127 // throw new AkbException("Editor is undefined for node " + node);
128 return editorId;
129 }
130
131 /* DEPENDENCY INJECTION */
132 public void setRepository(Repository repository) {
133 this.repository = repository;
134 }
135
136 public void setAkbService(AkbService akbService) {
137 this.akbService = akbService;
138 }
139 }