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