+ /** Listens to drag */
+ class ViewDragListener extends DragSourceAdapter {
+ public void dragSetData(DragSourceEvent event) {
+ IStructuredSelection selection = (IStructuredSelection) viewer
+ .getSelection();
+ if (selection.getFirstElement() instanceof DistributionElem) {
+ DistributionElem de = (DistributionElem) selection
+ .getFirstElement();
+ if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
+ event.data = de.getWorkspacePath();
+ }
+ }
+ }
+ }
+
+ /** Listens to drop */
+ class ViewDropListener extends ViewerDropAdapter {
+
+ public ViewDropListener(Viewer viewer) {
+ super(viewer);
+ }
+
+ @Override
+ public boolean performDrop(Object data) {
+ DistributionElem sourceDist = (DistributionElem) getSelectedObject();
+ RepoElem targetRepo = (RepoElem) getCurrentTarget();
+
+ Boolean ok = MessageDialog.openConfirm(getSite().getShell(),
+ "Confirm distribution merge", "Do you want to merge "
+ + sourceDist.getWorkspaceName() + " (from repo "
+ + sourceDist.getRepoElem().getLabel()
+ + ") to repo " + targetRepo.getLabel() + "?");
+ if (!ok)
+ return false;
+
+ try {
+ String sourceWorkspace = sourceDist.getWorkspaceName();
+ Repository sourceRepository = RepoUtils.getRepository(
+ repositoryFactory, keyring, sourceDist
+ .getWorkspaceNode().getParent());
+ Credentials sourceCredentials = RepoUtils
+ .getRepositoryCredentials(keyring, sourceDist
+ .getWorkspaceNode().getParent());
+
+ String targetWorkspace = sourceWorkspace;
+ Repository targetRepository = RepoUtils.getRepository(
+ repositoryFactory, keyring, targetRepo.getRepoNode());
+ Credentials targetCredentials = RepoUtils
+ .getRepositoryCredentials(keyring,
+ targetRepo.getRepoNode());
+
+ // Open sessions here since the background thread
+ // won't necessarily be authenticated.
+ // Job should close the sessions.
+ Session sourceSession = sourceRepository.login(
+ sourceCredentials, sourceWorkspace);
+ Session targetSession;
+ try {
+ targetSession = targetRepository.login(targetCredentials,
+ targetWorkspace);
+ } catch (NoSuchWorkspaceException e) {
+ Session defaultSession = targetRepository
+ .login(targetCredentials);
+ try {
+ defaultSession.getWorkspace().createWorkspace(
+ targetWorkspace);
+ } catch (Exception e1) {
+ throw new SlcException("Cannot create new workspace "
+ + targetWorkspace, e);
+ } finally {
+ JcrUtils.logoutQuietly(defaultSession);
+ }
+ targetSession = targetRepository.login(targetCredentials,
+ targetWorkspace);
+ }
+
+ Job workspaceMergeJob = new WorkspaceMergeJob(sourceSession,
+ targetSession);
+ workspaceMergeJob.setUser(true);
+ workspaceMergeJob.schedule();
+ return true;
+ } catch (RepositoryException e) {
+ throw new SlcException("Cannot process drop from " + sourceDist
+ + " to " + targetRepo, e);
+ }
+ }
+
+ @Override
+ public boolean validateDrop(Object target, int operation,
+ TransferData transferType) {
+ if (target instanceof RepoElem) {
+ if (getSelectedObject() instanceof DistributionElem) {
+ // check if not same repository
+ String srcRepoPath = ((DistributionElem) getSelectedObject())
+ .getRepoPath();
+ String targetRepoPath = ((RepoElem) target).getRepoPath();
+ return !targetRepoPath.equals(srcRepoPath);
+ }
+ }
+ return false;
+ }
+ }
+
+ private static class WorkspaceMergeJob extends Job {
+ private Session sourceSession;
+ private Session targetSession;
+
+ public WorkspaceMergeJob(Session sourceSession, Session targetSession) {
+ super("Workspace merge");
+ this.sourceSession = sourceSession;
+ this.targetSession = targetSession;
+ }
+
+ @Override
+ protected IStatus run(IProgressMonitor eclipseMonitor) {
+ long begin = System.currentTimeMillis();
+ try {
+ Query countQuery = sourceSession
+ .getWorkspace()
+ .getQueryManager()
+ .createQuery("select file from [nt:file] as file",
+ Query.JCR_SQL2);
+ QueryResult result = countQuery.execute();
+ Long expectedCount = result.getNodes().getSize();
+ if (log.isDebugEnabled())
+ log.debug("Will copy " + expectedCount + " files...");
+
+ ArgeoMonitor monitor = new EclipseArgeoMonitor(eclipseMonitor);
+ eclipseMonitor
+ .beginTask("Copy files", expectedCount.intValue());
+
+ Long count = JcrUtils.copyFiles(sourceSession.getRootNode(),
+ targetSession.getRootNode(), true, monitor);
+
+ monitor.done();
+ long duration = (System.currentTimeMillis() - begin) / 1000;// in
+ // s
+ if (log.isDebugEnabled())
+ log.debug("Copied " + count + " files in "
+ + (duration / 60) + "min " + (duration % 60) + "s");
+
+ return Status.OK_STATUS;
+ } catch (RepositoryException e) {
+ return new Status(IStatus.ERROR, DistPlugin.ID, "Cannot merge",
+ e);
+ } finally {
+ JcrUtils.logoutQuietly(sourceSession);
+ JcrUtils.logoutQuietly(targetSession);
+ }
+ }