View Javadoc
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.RepositoryException;
20  import javax.jcr.Session;
21  
22  import org.argeo.jcr.JcrUtils;
23  import org.argeo.slc.SlcException;
24  import org.argeo.slc.SlcTypes;
25  import org.argeo.slc.client.ui.dist.DistPlugin;
26  import org.argeo.slc.client.ui.dist.editors.ArtifactVersionEditor;
27  import org.argeo.slc.client.ui.dist.editors.ModularDistVersionEditor;
28  import org.argeo.slc.client.ui.dist.editors.ModuleEditorInput;
29  import org.argeo.slc.repo.RepoService;
30  import org.eclipse.core.commands.AbstractHandler;
31  import org.eclipse.core.commands.ExecutionEvent;
32  import org.eclipse.core.commands.ExecutionException;
33  import org.eclipse.ui.PartInitException;
34  import org.eclipse.ui.handlers.HandlerUtil;
35  
36  /**
37   * Open the relevant editor for a given module node of a given repository
38   * workspace. For the time being, modules can be artifacts or
39   * modularDistributions
40   */
41  public class OpenModuleEditor extends AbstractHandler {
42  	public final static String ID = DistPlugin.PLUGIN_ID + ".openModuleEditor";
43  	public final static String DEFAULT_LABEL = "Open relevant editor";
44  
45  	// use local node repo and repository factory to retrieve and log to
46  	// relevant repository
47  	public final static String PARAM_REPO_NODE_PATH = "param.repoNodePath";
48  	// use URI and repository factory to retrieve and ANONYMOUSLY log in
49  	// relevant repository
50  	public final static String PARAM_REPO_URI = "param.repoUri";
51  	public final static String PARAM_WORKSPACE_NAME = "param.workspaceName";
52  	public final static String PARAM_MODULE_PATH = "param.modulePath";
53  
54  	/* DEPENDENCY INJECTION */
55  	private RepoService repoService;
56  
57  	public Object execute(ExecutionEvent event) throws ExecutionException {
58  		String repoNodePath = event.getParameter(PARAM_REPO_NODE_PATH);
59  		String repoUri = event.getParameter(PARAM_REPO_URI);
60  		String workspaceName = event.getParameter(PARAM_WORKSPACE_NAME);
61  		String modulePath = event.getParameter(PARAM_MODULE_PATH);
62  
63  		Session businessSession = null;
64  		try {
65  			businessSession = repoService.getRemoteSession(repoNodePath,
66  					repoUri, workspaceName);
67  
68  			Node module = businessSession.getNode(modulePath);
69  			ModuleEditorInput mei = new ModuleEditorInput(repoNodePath,
70  					repoUri, workspaceName, modulePath);
71  
72  			// Choose correct editor based on its mixin
73  			if (module.isNodeType(SlcTypes.SLC_MODULAR_DISTRIBUTION))
74  				HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
75  						.openEditor(mei, ModularDistVersionEditor.ID);
76  			else
77  				HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
78  						.openEditor(mei, ArtifactVersionEditor.ID);
79  		} catch (RepositoryException e) {
80  			throw new SlcException("Unexpected error while "
81  					+ "getting repoNode info for repoNode at path "
82  					+ repoNodePath, e);
83  		} catch (PartInitException e) {
84  			throw new SlcException("Unexpected error while "
85  					+ "opening editor for workspace " + workspaceName
86  					+ " with URI " + repoUri + " and repoNode at path "
87  					+ repoNodePath, e);
88  		} finally {
89  			JcrUtils.logoutQuietly(businessSession);
90  		}
91  		return null;
92  	}
93  
94  	/* DEPENDENCY INJECTION */
95  	public void setRepoService(RepoService repoService) {
96  		this.repoService = repoService;
97  	}
98  }