]> 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
Add anonymous perspective
[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.Session;
23 import javax.jcr.security.Privilege;
24
25 import org.argeo.ArgeoException;
26 import org.argeo.jcr.JcrUtils;
27 import org.argeo.slc.client.ui.dist.DistPlugin;
28 import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
29 import org.argeo.slc.client.ui.dist.views.DistributionsView;
30 import org.argeo.slc.client.ui.dist.views.DistributionsView.DistributionViewSelectedElement;
31 import org.argeo.slc.repo.RepoUtils;
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.IWorkbenchPart;
37 import org.eclipse.ui.IWorkbenchWindow;
38
39 /**
40 * Create a copy of the chosen workspace in the current repository.
41 */
42
43 public class CopyWorkspace extends AbstractHandler {
44 // private static final Log log = LogFactory.getLog(CopyWorkspace.class);
45 public final static String ID = DistPlugin.ID + ".copyWorkspace";
46 public final static String DEFAULT_LABEL = "Duplicate";
47 public final static String DEFAULT_ICON_PATH = "icons/addItem.gif";
48
49 private Repository repository;
50 private Credentials credentials;
51 private String wkspName;
52 private String slcRole = "ROLE_SLC";
53
54 public Object execute(ExecutionEvent event) throws ExecutionException {
55 IWorkbenchWindow iww = DistPlugin.getDefault().getWorkbench()
56 .getActiveWorkbenchWindow();
57 IWorkbenchPart view = iww.getActivePage().getActivePart();
58 if (view instanceof DistributionsView) {
59 DistributionViewSelectedElement dvse = ((DistributionsView) view)
60 .getSelectedElement();
61 if (dvse != null && (dvse.isWorkspace)) {
62 repository = dvse.repository;
63 credentials = dvse.credentials;
64 wkspName = dvse.wkspName;
65 }
66 }
67 if (repository == null || wkspName == null)
68 return null;
69
70 InputDialog inputDialog = new InputDialog(iww.getShell(),
71 "New copy of workspace " + wkspName,
72 "Choose a name for the workspace to create", "", null);
73 inputDialog.open();
74 String newWorkspaceName = inputDialog.getValue();
75 Session srcSession = null;
76 Session newSession = null;
77 try {
78 srcSession = repository.login(credentials, wkspName);
79
80 // Create the workspace
81 srcSession.getWorkspace().createWorkspace(newWorkspaceName);
82 Node srcRootNode = srcSession.getRootNode();
83 // log in the newly created workspace
84 newSession = repository.login(credentials, newWorkspaceName);
85 Node newRootNode = newSession.getRootNode();
86 RepoUtils.copy(srcRootNode, newRootNode);
87 newSession.save();
88 JcrUtils.addPrivilege(newSession, "/", slcRole, Privilege.JCR_ALL);
89 CommandHelpers.callCommand(RefreshDistributionsView.ID);
90 } catch (RepositoryException re) {
91 throw new ArgeoException(
92 "Unexpected error while creating the new workspace.", re);
93 } finally {
94 if (srcSession != null)
95 srcSession.logout();
96 if (newSession != null)
97 newSession.logout();
98 }
99 return null;
100 }
101
102 /* DEPENDENCY INJECTION */
103 public void setRepository(Repository repository) {
104 this.repository = repository;
105 }
106 }