]> 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
Clean, update comments
[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.ui.handlers.HandlerUtil;
39
40 /**
41 * Create a copy of the chosen workspace in the current repository.
42 */
43
44 public class CopyWorkspace extends AbstractHandler {
45 // private static final Log log = LogFactory.getLog(CopyWorkspace.class);
46 public final static String ID = DistPlugin.ID + ".copyWorkspace";
47 public final static String DEFAULT_LABEL = "Duplicate";
48 public final static String PARAM_SOURCE_WORKSPACE_NAME = "srcWkspName";
49 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
50 public final static ImageDescriptor DEFAULT_ICON = DistPlugin
51 .getImageDescriptor("icons/addItem.gif");
52
53 // DEPENDENCY INJECTION
54 private RepositoryFactory repositoryFactory;
55 private Keyring keyring;
56 private Repository nodeRepository;
57
58 public Object execute(ExecutionEvent event) throws ExecutionException {
59
60 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
61 String wkspName = event.getParameter(PARAM_SOURCE_WORKSPACE_NAME);
62
63 Session nodeSession = null;
64 Session srcSession = null;
65 Session newSession = null;
66 try {
67 nodeSession = nodeRepository.login();
68 Node repoNode = nodeSession.getNode(targetRepoPath);
69 Repository repository = RepoUtils.getRepository(repositoryFactory,
70 keyring, repoNode);
71 Credentials credentials = RepoUtils.getRepositoryCredentials(
72 keyring, repoNode);
73
74 InputDialog inputDialog = new InputDialog(HandlerUtil
75 .getActiveWorkbenchWindow(event).getShell(),
76 "New copy of workspace " + wkspName,
77 "Choose a name for the workspace to create", "", null);
78 inputDialog.open();
79 String newWorkspaceName = inputDialog.getValue();
80 srcSession = repository.login(credentials, wkspName);
81
82 // Create the workspace
83 srcSession.getWorkspace().createWorkspace(newWorkspaceName);
84 Node srcRootNode = srcSession.getRootNode();
85 // log in the newly created workspace
86 newSession = repository.login(credentials, newWorkspaceName);
87 Node newRootNode = newSession.getRootNode();
88 RepoUtils.copy(srcRootNode, newRootNode);
89 newSession.save();
90 JcrUtils.addPrivilege(newSession, "/", SlcConstants.ROLE_SLC,
91 Privilege.JCR_ALL);
92 CommandHelpers.callCommand(RefreshDistributionsView.ID);
93 } catch (RepositoryException re) {
94 throw new ArgeoException(
95 "Unexpected error while creating the new workspace.", re);
96 } finally {
97 JcrUtils.logoutQuietly(newSession);
98 JcrUtils.logoutQuietly(srcSession);
99 JcrUtils.logoutQuietly(nodeSession);
100 }
101 return null;
102 }
103
104 /* DEPENDENCY INJECTION */
105 public void setNodeRepository(Repository nodeRepository) {
106 this.nodeRepository = nodeRepository;
107 }
108
109 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
110 this.repositoryFactory = repositoryFactory;
111 }
112
113 public void setKeyring(Keyring keyring) {
114 this.keyring = keyring;
115 }
116 }