]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.akb.ui/src/main/java/org/argeo/slc/akb/ui/commands/CreateAkbNode.java
Fix environment editor
[gpl/argeo-slc.git] / plugins / org.argeo.slc.akb.ui / src / main / java / org / argeo / slc / akb / ui / commands / CreateAkbNode.java
1 package org.argeo.slc.akb.ui.commands;
2
3 import javax.jcr.Node;
4 import javax.jcr.RepositoryException;
5 import javax.jcr.Session;
6
7 import org.argeo.jcr.JcrUtils;
8 import org.argeo.slc.akb.AkbException;
9 import org.argeo.slc.akb.AkbService;
10 import org.argeo.slc.akb.AkbTypes;
11 import org.argeo.slc.akb.ui.AkbUiPlugin;
12 import org.argeo.slc.akb.ui.dialogs.AddItemDialog;
13 import org.argeo.slc.akb.ui.wizards.CreateEnvInstanceWizard;
14 import org.eclipse.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.jface.wizard.WizardDialog;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.handlers.HandlerUtil;
20
21 /**
22 * Opens corresponding wizard to create a new AKB Node
23 */
24 public class CreateAkbNode extends AbstractHandler {
25 public final static String ID = AkbUiPlugin.PLUGIN_ID + ".createAkbNode";
26
27 /* DEPENDENCY INJECTION */
28 private AkbService akbService;
29
30 public final static String PARAM_PARENT_NODE_JCR_ID = "param.parentNodeJcrId";
31 public final static String PARAM_NODE_TYPE = "param.nodeType";
32
33 public Object execute(ExecutionEvent event) throws ExecutionException {
34
35 String parentNodeJcrId = event.getParameter(PARAM_PARENT_NODE_JCR_ID);
36 String nodeType = event.getParameter(PARAM_NODE_TYPE);
37 Session session = null;
38 try {
39 session = akbService.getRepository().login();
40 Node node = createNewNode(HandlerUtil.getActiveShell(event),
41 session, nodeType, parentNodeJcrId);
42 // no node has been created, return
43 if (node == null)
44 return null;
45 } catch (RepositoryException e) {
46 throw new AkbException("unexpected JCR error while opening "
47 + nodeType + " editor", e);
48 } finally {
49 JcrUtils.logoutQuietly(session);
50 }
51 return null;
52 }
53
54 private Node createNewNode(Shell shell, Session session, String nodeType,
55 String parentNodeJcrId) throws RepositoryException {
56 Node node = null;
57 if (AkbTypes.AKB_ITEM.equals(nodeType)) {
58 Node parNode = session.getNodeByIdentifier(parentNodeJcrId);
59 AddItemDialog dialog = new AddItemDialog(shell, "Add new item",
60 parNode);
61 dialog.open();
62 node = dialog.getNewNode();
63 } else if (AkbTypes.AKB_ENV.equals(nodeType)) {
64 CreateEnvInstanceWizard wizard = new CreateEnvInstanceWizard(
65 akbService, session);
66 WizardDialog dialog = new WizardDialog(shell, wizard);
67 dialog.open();
68 node = wizard.getCreatedNode();
69 } else
70 return null;
71 // {
72 // String name = SingleValue
73 // .ask("Create "
74 // + AkbMessages
75 // .getLabelForType(nodeSubtype == null ? nodeType
76 // : nodeSubtype),
77 // "Please enter a name for the corresponding "
78 // + AkbMessages
79 // .getLabelForType(nodeSubtype == null ? nodeType
80 // : nodeSubtype));
81 // if (name == null)
82 // return null;
83 // if (AkbTypes.AKB_ENV_TEMPLATE.equals(nodeType)) {
84 // node = akbService.createAkbTemplate(
85 // session.getNodeByIdentifier(parentNodeJcrId), name);
86 // } else if (AkbTypes.AKB_CONNECTOR_ALIAS.equals(nodeType)) {
87 // // the Jcr ID of the corresponding template must be passed to
88 // // create a new alias
89 // node = session.getNodeByIdentifier(parentNodeJcrId);
90 // akbService.createConnectorAlias(node, name, nodeSubtype);
91 // } else {
92 // Node parentNode = session.getNodeByIdentifier(parentNodeJcrId);
93 // node = parentNode.addNode(name, nodeType);
94 // node.setProperty(Property.JCR_TITLE, name);
95 // }
96 // }
97 // corresponding node is saved but not checked in, in order to ease
98 // cancel actions.
99 session.save();
100 return node;
101 }
102
103 public void setAkbService(AkbService akbService) {
104 this.akbService = akbService;
105 }
106 }