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