]> git.argeo.org Git - gpl/argeo-slc.git/blob - cms/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/commands/CopyWorkspace.java
Adapt to changes in Argeo Commons.
[gpl/argeo-slc.git] / cms / org.argeo.slc.client.ui.dist / src / org / argeo / slc / client / ui / dist / commands / CopyWorkspace.java
1 package org.argeo.slc.client.ui.dist.commands;
2
3 import javax.jcr.Credentials;
4 import javax.jcr.Node;
5 import javax.jcr.Repository;
6 import javax.jcr.RepositoryException;
7 import javax.jcr.RepositoryFactory;
8 import javax.jcr.Session;
9 import javax.jcr.security.Privilege;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.api.security.Keyring;
14 import org.argeo.cms.ui.workbench.util.PrivilegedJob;
15 import org.argeo.eclipse.ui.EclipseJcrMonitor;
16 import org.argeo.jcr.JcrMonitor;
17 import org.argeo.jcr.JcrUtils;
18 import org.argeo.slc.SlcConstants;
19 import org.argeo.slc.SlcException;
20 import org.argeo.slc.client.ui.dist.DistPlugin;
21 import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
22 import org.argeo.slc.repo.RepoUtils;
23 import org.eclipse.core.commands.AbstractHandler;
24 import org.eclipse.core.commands.ExecutionEvent;
25 import org.eclipse.core.commands.ExecutionException;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.core.runtime.jobs.Job;
30 import org.eclipse.jface.dialogs.ErrorDialog;
31 import org.eclipse.jface.dialogs.InputDialog;
32 import org.eclipse.jface.resource.ImageDescriptor;
33 import org.eclipse.jface.window.Window;
34 import org.eclipse.swt.widgets.Display;
35 import org.eclipse.ui.handlers.HandlerUtil;
36
37 /** Create a copy of the chosen workspace in a remote repository */
38 public class CopyWorkspace extends AbstractHandler {
39 private static final Log log = LogFactory.getLog(CopyWorkspace.class);
40
41 public final static String ID = DistPlugin.PLUGIN_ID + ".copyWorkspace";
42 public final static String DEFAULT_LABEL = "Duplicate...";
43 public final static ImageDescriptor DEFAULT_ICON = DistPlugin
44 .getImageDescriptor("icons/addItem.gif");
45
46 public final static String PARAM_SOURCE_WORKSPACE_NAME = "srcWkspName";
47 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
48
49 // DEPENDENCY INJECTION
50 private RepositoryFactory repositoryFactory;
51 private Keyring keyring;
52 private Repository nodeRepository;
53
54 public Object execute(ExecutionEvent event) throws ExecutionException {
55
56 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
57 String wkspName = event.getParameter(PARAM_SOURCE_WORKSPACE_NAME);
58
59 InputDialog inputDialog = new InputDialog(HandlerUtil
60 .getActiveWorkbenchWindow(event).getShell(),
61 "New copy of workspace " + wkspName,
62 "Choose a name for the workspace to create", "", null);
63 int result = inputDialog.open();
64 if (result == Window.OK) {
65 String newWorkspaceName = inputDialog.getValue();
66
67 if (newWorkspaceName == null || newWorkspaceName.trim().equals("")
68 || newWorkspaceName.trim().equals(wkspName.trim())) {
69 ErrorDialog
70 .openError(HandlerUtil.getActiveShell(event),
71 "Non valid workspace name", newWorkspaceName
72 + " is not a valid workspace name.",
73 new Status(IStatus.ERROR, "not valid", 0,
74 "Error", null));
75 return null;
76 }
77 Job copyWkspJob = new CopyWkspJob(repositoryFactory, keyring,
78 nodeRepository, targetRepoPath, wkspName, newWorkspaceName,
79 HandlerUtil.getActiveWorkbenchWindow(event).getShell()
80 .getDisplay());
81 copyWkspJob.setUser(true);
82 copyWkspJob.schedule();
83 }
84 return null;
85 }
86
87 private static class CopyWkspJob extends PrivilegedJob {
88
89 private RepositoryFactory repositoryFactory;
90 private Keyring keyring;
91 private Repository localRepository;
92 private String targetRepoPath;
93 private String srcWkspName;
94 private String targetWkspName;
95 private Display display;
96
97 public CopyWkspJob(RepositoryFactory repositoryFactory,
98 Keyring keyring, Repository localRepository,
99 String targetRepoPath, String srcWkspName,
100 String targetWkspName, Display display) {
101 super("Duplicate workspace");
102 this.repositoryFactory = repositoryFactory;
103 this.keyring = keyring;
104 this.localRepository = localRepository;
105 this.targetRepoPath = targetRepoPath;
106 this.srcWkspName = srcWkspName;
107 this.targetWkspName = targetWkspName;
108 this.display = display;
109 }
110
111 @Override
112 protected IStatus doRun(IProgressMonitor progressMonitor) {
113 long begin = System.currentTimeMillis();
114
115 JcrMonitor monitor = new EclipseJcrMonitor(progressMonitor);
116 monitor.beginTask("Copy workspace", -1);
117 monitor.subTask("Copying nodes");
118
119 Session nodeSession = null;
120 Session srcSession = null;
121 Session newSession = null;
122 try {
123 nodeSession = localRepository.login();
124 Node repoNode = nodeSession.getNode(targetRepoPath);
125 Repository repository = RepoUtils.getRepository(
126 repositoryFactory, keyring, repoNode);
127 Credentials credentials = RepoUtils.getRepositoryCredentials(
128 keyring, repoNode);
129
130 srcSession = repository.login(credentials, srcWkspName);
131
132 // Create the workspace
133 srcSession.getWorkspace().createWorkspace(targetWkspName);
134 Node srcRootNode = srcSession.getRootNode();
135 // log in the newly created workspace
136 newSession = repository.login(credentials, targetWkspName);
137 Node newRootNode = newSession.getRootNode();
138 RepoUtils.copy(srcRootNode, newRootNode, monitor);
139 newSession.save();
140 JcrUtils.addPrivilege(newSession, "/", SlcConstants.ROLE_SLC,
141 Privilege.JCR_ALL);
142
143 display.asyncExec(new Runnable() {
144 public void run() {
145 CommandHelpers.callCommand(RefreshDistributionsView.ID);
146 }
147 });
148 monitor.worked(1);
149
150 } catch (RepositoryException re) {
151 throw new SlcException(
152 "Unexpected error while creating the new workspace.",
153 re);
154 } finally {
155 JcrUtils.logoutQuietly(newSession);
156 JcrUtils.logoutQuietly(srcSession);
157 JcrUtils.logoutQuietly(nodeSession);
158 }
159
160 monitor.done();
161 long duration = (System.currentTimeMillis() - begin) / 1000;// in
162 // s
163 if (log.isDebugEnabled())
164 log.debug("Created workspace " + targetWkspName + " in "
165 + (duration / 60) + "min " + (duration % 60) + "s");
166 return Status.OK_STATUS;
167 }
168
169 }
170
171 /* DEPENDENCY INJECTION */
172 public void setNodeRepository(Repository nodeRepository) {
173 this.nodeRepository = nodeRepository;
174 }
175
176 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
177 this.repositoryFactory = repositoryFactory;
178 }
179
180 public void setKeyring(Keyring keyring) {
181 this.keyring = keyring;
182 }
183 }