]> git.argeo.org Git - gpl/argeo-slc.git/blob - cms/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/commands/MergeWorkspaces.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 / MergeWorkspaces.java
1 package org.argeo.slc.client.ui.dist.commands;
2
3 import javax.jcr.Credentials;
4 import javax.jcr.NoSuchWorkspaceException;
5 import javax.jcr.Node;
6 import javax.jcr.Repository;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.RepositoryFactory;
9 import javax.jcr.Session;
10 import javax.jcr.query.Query;
11 import javax.jcr.query.QueryResult;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.argeo.api.security.Keyring;
16 import org.argeo.eclipse.ui.EclipseJcrMonitor;
17 import org.argeo.jcr.JcrMonitor;
18 import org.argeo.jcr.JcrUtils;
19 import org.argeo.slc.SlcException;
20 import org.argeo.slc.client.ui.dist.DistPlugin;
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
30 /** Merge two workspaces */
31 public class MergeWorkspaces extends AbstractHandler {
32 private final static Log log = LogFactory.getLog(MergeWorkspaces.class);
33
34 public final static String ID = DistPlugin.PLUGIN_ID + ".mergeWorkspaces";
35 public final static String DEFAULT_LABEL = "Merge";
36
37 public final static String PARAM_SOURCE_WORKSPACE_NAME = "srcWkspName";
38 public final static String PARAM_SOURCE_REPO_PATH = "srcRepoPath";
39 public final static String PARAM_TARGET_WORKSPACE_NAME = "targetWkspName";
40 public final static String PARAM_TARGET_REPO_PATH = "targetRepoPath";
41
42 // DEPENDENCY INJECTION
43 private RepositoryFactory repositoryFactory;
44 private Keyring keyring;
45 private Repository nodeRepository;
46
47 public Object execute(ExecutionEvent event) throws ExecutionException {
48 String targetRepoPath = event.getParameter(PARAM_TARGET_REPO_PATH);
49 String targetWkspName = event.getParameter(PARAM_TARGET_WORKSPACE_NAME);
50 String sourceRepoPath = event.getParameter(PARAM_SOURCE_REPO_PATH);
51 String sourceWkspName = event.getParameter(PARAM_SOURCE_WORKSPACE_NAME);
52
53 Session nodeSession = null;
54 try {
55 nodeSession = nodeRepository.login();
56 Node srcRepoNode = nodeSession.getNode(sourceRepoPath);
57 Repository srcRepository = RepoUtils.getRepository(repositoryFactory, keyring, srcRepoNode);
58 Credentials srcCredentials = RepoUtils.getRepositoryCredentials(keyring, srcRepoNode);
59
60 Node targetRepoNode = nodeSession.getNode(targetRepoPath);
61 Repository targetRepository = RepoUtils.getRepository(repositoryFactory, keyring, targetRepoNode);
62 Credentials targetCredentials = RepoUtils.getRepositoryCredentials(keyring, targetRepoNode);
63
64 // String msg = "Are you sure you want to merge distribution ["
65 // + sourceWkspName + "] in distribution [" + targetWkspName
66 // + "] ?";
67 //
68 // boolean result = MessageDialog.openConfirm(
69 // HandlerUtil.getActiveShell(event), "Confirm Merge", msg);
70
71 // if (result) {
72 // Open sessions here since the background thread
73 // won't necessarily be authenticated.
74 // Job should close the sessions.
75 Session sourceSession = srcRepository.login(srcCredentials, sourceWkspName);
76 Session targetSession;
77 try {
78 targetSession = targetRepository.login(targetCredentials, targetWkspName);
79 } catch (NoSuchWorkspaceException e) {
80 Session defaultSession = targetRepository.login(targetCredentials);
81 try {
82 defaultSession.getWorkspace().createWorkspace(targetWkspName);
83 } catch (Exception e1) {
84 throw new SlcException("Cannot create new workspace " + targetWkspName, e);
85 } finally {
86 JcrUtils.logoutQuietly(defaultSession);
87 }
88 targetSession = targetRepository.login(targetCredentials, targetWkspName);
89 }
90
91 Job workspaceMergeJob = new WorkspaceMergeJob(sourceSession, targetSession);
92 workspaceMergeJob.setUser(true);
93 workspaceMergeJob.schedule();
94 } catch (RepositoryException re) {
95 throw new SlcException("Unexpected error while merging workspaces.", re);
96 } finally {
97 JcrUtils.logoutQuietly(nodeSession);
98 }
99 return null;
100 }
101
102 private static class WorkspaceMergeJob extends Job {
103 private Session sourceSession;
104 private Session targetSession;
105
106 public WorkspaceMergeJob(Session sourceSession, Session targetSession) {
107 super("Workspace merge");
108 this.sourceSession = sourceSession;
109 this.targetSession = targetSession;
110 }
111
112 @Override
113 protected IStatus run(IProgressMonitor eclipseMonitor) {
114 long begin = System.currentTimeMillis();
115 try {
116 Query countQuery = sourceSession.getWorkspace().getQueryManager()
117 .createQuery("select file from [nt:file] as file", Query.JCR_SQL2);
118 QueryResult result = countQuery.execute();
119 Long expectedCount = result.getNodes().getSize();
120 if (log.isDebugEnabled())
121 log.debug("Will copy " + expectedCount + " files...");
122
123 JcrMonitor monitor = new EclipseJcrMonitor(eclipseMonitor);
124 eclipseMonitor.beginTask("Copy files", expectedCount.intValue());
125
126 Long count = JcrUtils.copyFiles(sourceSession.getRootNode(), targetSession.getRootNode(), true, monitor,
127 true);
128
129 monitor.done();
130 long duration = (System.currentTimeMillis() - begin) / 1000;// in
131 // s
132 if (log.isDebugEnabled())
133 log.debug("Copied " + count + " files in " + (duration / 60) + "min " + (duration % 60) + "s");
134
135 return Status.OK_STATUS;
136 } catch (RepositoryException e) {
137 return new Status(IStatus.ERROR, DistPlugin.PLUGIN_ID, "Cannot merge", e);
138 } finally {
139 JcrUtils.logoutQuietly(sourceSession);
140 JcrUtils.logoutQuietly(targetSession);
141 }
142 }
143 }
144
145 /* DEPENDENCY INJECTION */
146 public void setNodeRepository(Repository nodeRepository) {
147 this.nodeRepository = nodeRepository;
148 }
149
150 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
151 this.repositoryFactory = repositoryFactory;
152 }
153
154 public void setKeyring(Keyring keyring) {
155 this.keyring = keyring;
156 }
157 }