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