]> 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
Clean session management.
[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 if (repoUri == null && repoNode != null)
86 repoUri = repoNode.getProperty(ArgeoNames.ARGEO_URI)
87 .getString();
88
89 } catch (RepositoryException e) {
90 throw new SlcException("Cannot log to workspace "
91 + workspaceName + " for repo defined in "
92 + repoNodePath, e);
93 } finally {
94 JcrUtils.logoutQuietly(localSession);
95 }
96
97 ModuleEditorInput wei = new ModuleEditorInput(repoNodePath,
98 repoUri, workspaceName, modulePath);
99 Node artifact = businessSession.getNode(modulePath);
100
101 // Choose correct editor based on the artifact mixin
102 if (artifact.isNodeType(SlcTypes.SLC_MODULAR_DISTRIBUTION))
103 HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
104 .openEditor(wei, ModularDistVersionEditor.ID);
105 else
106 HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
107 .openEditor(wei, ArtifactVersionEditor.ID);
108 } catch (RepositoryException e) {
109 throw new SlcException("Unexpected error while "
110 + "getting repoNode info for repoNode at path "
111 + repoNodePath, e);
112 } catch (PartInitException e) {
113 throw new SlcException("Unexpected error while "
114 + "opening editor for workspace " + workspaceName
115 + " with URI " + repoUri + " and repoNode at path "
116 + repoNodePath, e);
117 } finally {
118 JcrUtils.logoutQuietly(businessSession);
119 }
120 return null;
121 }
122
123 /* DEPENDENCY INJECTION */
124 public void setLocalRepository(Repository localRepository) {
125 this.localRepository = localRepository;
126 }
127
128 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
129 this.repositoryFactory = repositoryFactory;
130 }
131
132 public void setKeyring(Keyring keyring) {
133 this.keyring = keyring;
134 }
135
136 }