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.Repository;
20  import javax.jcr.RepositoryException;
21  import javax.jcr.Session;
22  
23  import org.argeo.cms.ArgeoNames;
24  import org.argeo.jcr.JcrUtils;
25  import org.argeo.slc.SlcException;
26  import org.argeo.slc.client.ui.dist.DistPlugin;
27  import org.argeo.slc.client.ui.dist.editors.DistWkspEditorInput;
28  import org.argeo.slc.client.ui.dist.editors.DistWorkspaceEditor;
29  import org.eclipse.core.commands.AbstractHandler;
30  import org.eclipse.core.commands.ExecutionEvent;
31  import org.eclipse.core.commands.ExecutionException;
32  import org.eclipse.jface.resource.ImageDescriptor;
33  import org.eclipse.ui.PartInitException;
34  import org.eclipse.ui.handlers.HandlerUtil;
35  
36  /**
37   * Open a distribution workspace editor for a given workspace in a repository
38   */
39  public class OpenWorkspaceEditor extends AbstractHandler {
40  	public final static String ID = DistPlugin.PLUGIN_ID
41  			+ ".openWorkspaceEditor";
42  	public final static String DEFAULT_LABEL = "Open editor";
43  	public final static ImageDescriptor DEFAULT_ICON = DistPlugin
44  			.getImageDescriptor("icons/distribution_perspective.gif");
45  
46  	// Use local node repo and repository factory to retrieve and log to
47  	// relevant repository
48  	public final static String PARAM_REPO_NODE_PATH = "param.repoNodePath";
49  	// Use URI and repository factory to retrieve and ANONYMOUSLY log in
50  	// relevant repository
51  	public final static String PARAM_REPO_URI = "param.repoUri";
52  	public final static String PARAM_WORKSPACE_NAME = "param.workspaceName";
53  
54  	/* DEPENDENCY INJECTION */
55  	private Repository localRepository;
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  
62  		Session defaultSession = null;
63  		if (repoNodePath != null && repoUri == null) {
64  			try {
65  				defaultSession = localRepository.login();
66  				if (defaultSession.nodeExists(repoNodePath)) {
67  					Node repoNode = defaultSession.getNode(repoNodePath);
68  					repoUri = repoNode.getProperty(ArgeoNames.ARGEO_URI)
69  							.getString();
70  				}
71  			} catch (RepositoryException e) {
72  				throw new SlcException("Unexpected error while "
73  						+ "getting repoNode at path " + repoNodePath, e);
74  			} finally {
75  				JcrUtils.logoutQuietly(defaultSession);
76  			}
77  		}
78  
79  		DistWkspEditorInput wei = new DistWkspEditorInput(repoNodePath,
80  				repoUri, workspaceName);
81  		try {
82  			HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
83  					.openEditor(wei, DistWorkspaceEditor.ID);
84  		} catch (PartInitException e) {
85  			throw new SlcException("Unexpected error while "
86  					+ "opening editor for workspace " + workspaceName
87  					+ " with URI " + repoUri + " and repoNode at path "
88  					+ repoNodePath, e);
89  		}
90  		return null;
91  	}
92  
93  	/* DEPENDENCY INJECTION */
94  	public void setLocalRepository(Repository localRepository) {
95  		this.localRepository = localRepository;
96  	}
97  }