]> 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
26d32e70692e31e02eff11418d8376efa29d91e5
[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.ArtifactEditor;
29 import org.argeo.slc.client.ui.dist.editors.ModuleEditorInput;
30 import org.argeo.slc.client.ui.dist.editors.DistributionEditor;
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_ARTIFACT_PATH = "param.artifactPath";
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 artifactPath = event.getParameter(PARAM_ARTIFACT_PATH);
70
71 Session localSession = null;
72 Session businessSession = null;
73 Node repoNode = null;
74
75 try {
76 if (repoNodePath != null && repoUri == null) {
77 try {
78 localSession = localRepository.login();
79 if (repoNodePath != null
80 && localSession.nodeExists(repoNodePath))
81 repoNode = localSession.getNode(repoNodePath);
82
83 businessSession = RepoUtils.getCorrespondingSession(
84 repositoryFactory, keyring, repoNode, repoUri,
85 workspaceName);
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 }
98
99 ModuleEditorInput wei = new ModuleEditorInput(repoNodePath,
100 repoUri, workspaceName, artifactPath);
101 Node artifact = businessSession.getNode(artifactPath);
102
103 // Choose correct editor based on the artifact mixin
104 if (artifact.isNodeType(SlcTypes.SLC_MODULAR_DISTRIBUTION))
105 HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
106 .openEditor(wei, DistributionEditor.ID);
107 else
108 HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
109 .openEditor(wei, ArtifactEditor.ID);
110 } catch (RepositoryException e) {
111 throw new SlcException("Unexpected error while "
112 + "getting repoNode info for repoNode at path "
113 + repoNodePath, e);
114 } catch (PartInitException e) {
115 throw new SlcException("Unexpected error while "
116 + "opening editor for workspace " + workspaceName
117 + " with URI " + repoUri + " and repoNode at path "
118 + repoNodePath, e);
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 }