]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/OpenModuleEditor.java
b1b9619eb64ef69e8ea36c94e1e5de100d7c337a
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / OpenModuleEditor.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.client.ui.dist.commands;
17
18 import javax.jcr.Node;
19 import javax.jcr.Repository;
20 import javax.jcr.RepositoryException;
21 import javax.jcr.RepositoryFactory;
22 import javax.jcr.Session;
23
24 import org.argeo.jcr.ArgeoNames;
25 import org.argeo.jcr.JcrUtils;
26 import org.argeo.slc.SlcException;
27 import org.argeo.slc.client.ui.dist.DistPlugin;
28 import org.argeo.slc.client.ui.dist.editors.ArtifactVersionEditor;
29 import org.argeo.slc.client.ui.dist.editors.ModularDistVersionEditor;
30 import org.argeo.slc.client.ui.dist.editors.ModuleEditorInput;
31 import org.argeo.slc.jcr.SlcTypes;
32 import org.argeo.slc.repo.RepoUtils;
33 import org.argeo.util.security.Keyring;
34 import org.eclipse.core.commands.AbstractHandler;
35 import org.eclipse.core.commands.ExecutionEvent;
36 import org.eclipse.core.commands.ExecutionException;
37 import org.eclipse.ui.PartInitException;
38 import org.eclipse.ui.handlers.HandlerUtil;
39
40 /**
41 * Open the relevant editor for a given module node of a given repository
42 * workspace. For the time being, modules can be artifacts or
43 * modularDistributions
44 */
45 public class OpenModuleEditor extends AbstractHandler {
46 public final static String ID = DistPlugin.ID + ".openModuleEditor";
47 public final static String DEFAULT_LABEL = "Open relevant editor";
48
49 // use local node repo and repository factory to retrieve and log to
50 // relevant repository
51 public final static String PARAM_REPO_NODE_PATH = "param.repoNodePath";
52 // use URI and repository factory to retrieve and ANONYMOUSLY log in
53 // relevant repository
54 public final static String PARAM_REPO_URI = "param.repoUri";
55 public final static String PARAM_WORKSPACE_NAME = "param.workspaceName";
56 public final static String PARAM_MODULE_PATH = "param.modulePath";
57
58 /* DEPENDENCY INJECTION */
59 private Repository localRepository;
60 // We must log in the corresponding repository to get the node and be able
61 // to decide which editor to open depending on its mixin
62 private RepositoryFactory repositoryFactory;
63 private Keyring keyring;
64
65 public Object execute(ExecutionEvent event) throws ExecutionException {
66 String repoNodePath = event.getParameter(PARAM_REPO_NODE_PATH);
67 String repoUri = event.getParameter(PARAM_REPO_URI);
68 String workspaceName = event.getParameter(PARAM_WORKSPACE_NAME);
69 String modulePath = event.getParameter(PARAM_MODULE_PATH);
70
71 Session localSession = null;
72 Session businessSession = null;
73 Node repoNode = null;
74
75 try {
76 try {
77 localSession = localRepository.login();
78 if (repoNodePath != null
79 && localSession.nodeExists(repoNodePath))
80 repoNode = localSession.getNode(repoNodePath);
81
82 businessSession = RepoUtils.getCorrespondingSession(
83 repositoryFactory, keyring, repoNode, repoUri,
84 workspaceName);
85 repoUri = repoNode.getProperty(ArgeoNames.ARGEO_URI)
86 .getString();
87
88 } catch (RepositoryException e) {
89 throw new SlcException("Cannot log to workspace "
90 + workspaceName + " for repo defined in "
91 + repoNodePath, e);
92 } finally {
93 JcrUtils.logoutQuietly(localSession);
94 }
95
96 ModuleEditorInput wei = new ModuleEditorInput(repoNodePath,
97 repoUri, workspaceName, modulePath);
98 Node artifact = businessSession.getNode(modulePath);
99
100 // Choose correct editor based on the artifact mixin
101 if (artifact.isNodeType(SlcTypes.SLC_MODULAR_DISTRIBUTION))
102 HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
103 .openEditor(wei, ModularDistVersionEditor.ID);
104 else
105 HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
106 .openEditor(wei, ArtifactVersionEditor.ID);
107 } catch (RepositoryException e) {
108 throw new SlcException("Unexpected error while "
109 + "getting repoNode info for repoNode at path "
110 + repoNodePath, e);
111 } catch (PartInitException e) {
112 throw new SlcException("Unexpected error while "
113 + "opening editor for workspace " + workspaceName
114 + " with URI " + repoUri + " and repoNode at path "
115 + repoNodePath, e);
116 } finally {
117 JcrUtils.logoutQuietly(businessSession);
118 }
119 return null;
120 }
121
122 /* DEPENDENCY INJECTION */
123 public void setLocalRepository(Repository localRepository) {
124 this.localRepository = localRepository;
125 }
126
127 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
128 this.repositoryFactory = repositoryFactory;
129 }
130
131 public void setKeyring(Keyring keyring) {
132 this.keyring = keyring;
133 }
134
135 }