]> 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/CopyWorkspace.java
fix grouping issue for workspaces that have the same prefix.
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / CopyWorkspace.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.Credentials;
19 import javax.jcr.Node;
20 import javax.jcr.Repository;
21 import javax.jcr.RepositoryException;
22 import javax.jcr.RepositoryFactory;
23 import javax.jcr.Session;
24 import javax.jcr.security.Privilege;
25
26 import org.argeo.ArgeoException;
27 import org.argeo.jcr.JcrUtils;
28 import org.argeo.slc.SlcConstants;
29 import org.argeo.slc.client.ui.dist.DistPlugin;
30 import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
31 import org.argeo.slc.repo.RepoUtils;
32 import org.argeo.util.security.Keyring;
33 import org.eclipse.core.commands.AbstractHandler;
34 import org.eclipse.core.commands.ExecutionEvent;
35 import org.eclipse.core.commands.ExecutionException;
36 import org.eclipse.jface.dialogs.InputDialog;
37 import org.eclipse.jface.resource.ImageDescriptor;
38 import org.eclipse.jface.window.Window;
39 import org.eclipse.ui.handlers.HandlerUtil;
40
41 /**
42 * Create a copy of the chosen workspace in the current repository.
43 */
44
45 public class CopyWorkspace extends AbstractHandler {
46 // private static final Log log = LogFactory.getLog(CopyWorkspace.class);
47 public final static String ID = DistPlugin.ID + ".copyWorkspace";
48 public final static String DEFAULT_LABEL = "Duplicate...";
49 public final static String PARAM_SOURCE_WORKSPACE_NAME = "srcWkspName";
50 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
51 public final static ImageDescriptor DEFAULT_ICON = DistPlugin
52 .getImageDescriptor("icons/addItem.gif");
53
54 // DEPENDENCY INJECTION
55 private RepositoryFactory repositoryFactory;
56 private Keyring keyring;
57 private Repository nodeRepository;
58
59 public Object execute(ExecutionEvent event) throws ExecutionException {
60
61 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
62 String wkspName = event.getParameter(PARAM_SOURCE_WORKSPACE_NAME);
63
64 Session nodeSession = null;
65 Session srcSession = null;
66 Session newSession = null;
67 try {
68 nodeSession = nodeRepository.login();
69 Node repoNode = nodeSession.getNode(targetRepoPath);
70 Repository repository = RepoUtils.getRepository(repositoryFactory,
71 keyring, repoNode);
72 Credentials credentials = RepoUtils.getRepositoryCredentials(
73 keyring, repoNode);
74
75 InputDialog inputDialog = new InputDialog(HandlerUtil
76 .getActiveWorkbenchWindow(event).getShell(),
77 "New copy of workspace " + wkspName,
78 "Choose a name for the workspace to create", "", null);
79 int result = inputDialog.open();
80 if (result == Window.OK) {
81 String newWorkspaceName = inputDialog.getValue();
82 srcSession = repository.login(credentials, wkspName);
83
84 // Create the workspace
85 srcSession.getWorkspace().createWorkspace(newWorkspaceName);
86 Node srcRootNode = srcSession.getRootNode();
87 // log in the newly created workspace
88 newSession = repository.login(credentials, newWorkspaceName);
89 Node newRootNode = newSession.getRootNode();
90 RepoUtils.copy(srcRootNode, newRootNode);
91 newSession.save();
92 JcrUtils.addPrivilege(newSession, "/", SlcConstants.ROLE_SLC,
93 Privilege.JCR_ALL);
94 CommandHelpers.callCommand(RefreshDistributionsView.ID);
95 }
96 } catch (RepositoryException re) {
97 throw new ArgeoException(
98 "Unexpected error while creating the new workspace.", re);
99 } finally {
100 JcrUtils.logoutQuietly(newSession);
101 JcrUtils.logoutQuietly(srcSession);
102 JcrUtils.logoutQuietly(nodeSession);
103 }
104 return null;
105 }
106
107 /* DEPENDENCY INJECTION */
108 public void setNodeRepository(Repository nodeRepository) {
109 this.nodeRepository = nodeRepository;
110 }
111
112 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
113 this.repositoryFactory = repositoryFactory;
114 }
115
116 public void setKeyring(Keyring keyring) {
117 this.keyring = keyring;
118 }
119 }