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