]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/views/DistributionsView.java
Fix commands in dist UI
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / views / DistributionsView.java
index 29a1ca9c57b988b20c448e24405d7cdeee1d1969..5c6fcdfda140ddcd007dbc07234577b89a181568 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2012 Mathieu Baudier
+ * Copyright (C) 2007-2012 Argeo GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -30,6 +30,8 @@ import javax.jcr.RepositoryException;
 import javax.jcr.RepositoryFactory;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
+import javax.jcr.query.Query;
+import javax.jcr.query.QueryResult;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -44,7 +46,6 @@ import org.argeo.jcr.JcrUtils;
 import org.argeo.jcr.UserJcrUtils;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.client.ui.dist.DistPlugin;
-import org.argeo.slc.client.ui.dist.DistUiUtils;
 import org.argeo.slc.client.ui.dist.commands.CopyWorkspace;
 import org.argeo.slc.client.ui.dist.commands.CreateWorkspace;
 import org.argeo.slc.client.ui.dist.commands.DeleteWorkspace;
@@ -55,6 +56,7 @@ import org.argeo.slc.client.ui.dist.editors.DistributionEditorInput;
 import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
 import org.argeo.slc.jcr.SlcNames;
 import org.argeo.slc.repo.RepoConstants;
+import org.argeo.slc.repo.RepoUtils;
 import org.argeo.util.security.Keyring;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
@@ -109,7 +111,6 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
 
                TreeViewerColumn col = new TreeViewerColumn(viewer, SWT.NONE);
                col.getColumn().setWidth(200);
-               // col.getColumn().setText("Workspace");
                col.setLabelProvider(new ColumnLabelProvider() {
                        @Override
                        public String getText(Object element) {
@@ -160,9 +161,10 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
                                if (!repos.hasNode(alias)) {
                                        Node repoNode = repos.addNode(alias,
                                                        ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
-                                       repoNode.setProperty(ARGEO_URI, alias);
+                                       repoNode.setProperty(ARGEO_URI, "vm:///" + alias);
                                        repoNode.addMixin(NodeType.MIX_TITLE);
-                                       repoNode.setProperty(Property.JCR_TITLE, "vm://" + alias);
+                                       repoNode.setProperty(Property.JCR_TITLE, "Internal "
+                                                       + alias + " repository");
                                        nodeSession.save();
                                }
                        }
@@ -184,19 +186,17 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
                Object firstElement = ((IStructuredSelection) viewer.getSelection())
                                .getFirstElement();
 
-               if (firstElement instanceof TreeParent) {
-                       TreeParent tp = (TreeParent) firstElement;
-                       String wsName = tp.getName();
+               if (firstElement instanceof TreeParent
+                               || firstElement instanceof RepoElem) {
+                       String wsName = null;
 
                        // Build conditions depending on element type (repo or distribution
                        // for the time being)
                        boolean isDistribElem = false; // , isRepoElem = false;
 
-                       // if (tp instanceof RepositoryElem){
-                       // isRepoElem = true;
-                       // } else
-                       if (tp instanceof DistributionElem) {
+                       if (firstElement instanceof DistributionElem) {
                                isDistribElem = true;
+                               wsName = ((DistributionElem) firstElement).getName();
                        }
 
                        // create workspace
@@ -345,10 +345,9 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
                protected void connect() {
                        if (repository != null)
                                return;
-                       repository = DistUiUtils.getRepository(repositoryFactory, keyring,
-                                       repoNode);
-                       credentials = DistUiUtils.getRepositoryCredentials(keyring,
+                       repository = RepoUtils.getRepository(repositoryFactory, keyring,
                                        repoNode);
+                       credentials = RepoUtils.getRepositoryCredentials(keyring, repoNode);
                }
 
                public String getLabel() {
@@ -375,10 +374,15 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
                                String[] workspaceNames = session.getWorkspace()
                                                .getAccessibleWorkspaceNames();
                                List<DistributionElem> distributionElems = new ArrayList<DistributionElem>();
-                               for (String workspace : workspaceNames)
-                                       distributionElems.add(new DistributionElem(this, workspace,
-                                                       credentials));
-                               // FIXME remove deleted workspaces
+                               for (String workspaceName : workspaceNames) {
+                                       Node workspaceNode = repoNode.hasNode(workspaceName) ? repoNode
+                                                       .getNode(workspaceName) : repoNode
+                                                       .addNode(workspaceName);
+                                       repoNode.getSession().save();
+                                       distributionElems.add(new DistributionElem(this,
+                                                       workspaceNode));
+                                       // FIXME remove deleted workspaces
+                               }
                                return distributionElems.toArray();
                        } catch (RepositoryException e) {
                                throw new SlcException(
@@ -407,32 +411,22 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
                        connect();
                        return repository;
                }
+
+               public Credentials getCredentials() {
+                       return credentials;
+               }
+
        }
 
        /** Abstracts a distribution, that is a workspace */
        private static class DistributionElem extends TreeParent {
                private final RepoElem repoElem;
                private final Node workspaceNode;
-               private final String workspaceName;
-               private final Credentials credentials;
 
-               public DistributionElem(RepoElem repoElem, String workspaceName,
-                               Credentials credentials) {
-                       super(workspaceName);
+               public DistributionElem(RepoElem repoElem, Node workspaceNode) {
+                       super(JcrUtils.getNameQuietly(workspaceNode));
                        this.repoElem = repoElem;
-                       try {
-                               // TODO move it to repo elem
-                               this.workspaceNode = repoElem.getRepoNode().hasNode(
-                                               workspaceName) ? repoElem.getRepoNode().getNode(
-                                               workspaceName) : repoElem.getRepoNode().addNode(
-                                               workspaceName);
-                               repoElem.getRepoNode().getSession().save();
-                       } catch (RepositoryException e) {
-                               throw new SlcException("Cannot get or add workspace node "
-                                               + workspaceName, e);
-                       }
-                       this.workspaceName = workspaceName;
-                       this.credentials = credentials;
+                       this.workspaceNode = workspaceNode;
                }
 
                public Node getWorkspaceNode() {
@@ -440,7 +434,7 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
                }
 
                public String getWorkspaceName() {
-                       return workspaceName;
+                       return JcrUtils.getNameQuietly(workspaceNode);
                }
 
                public String getWorkspacePath() {
@@ -448,7 +442,7 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
                                return workspaceNode.getPath();
                        } catch (RepositoryException e) {
                                throw new SlcException("Cannot get or add workspace path "
-                                               + workspaceName, e);
+                                               + getWorkspaceName(), e);
                        }
                }
 
@@ -457,7 +451,7 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
                                return workspaceNode.getParent().getPath();
                        } catch (RepositoryException e) {
                                throw new SlcException("Cannot get or add workspace path "
-                                               + workspaceName, e);
+                                               + getWorkspaceName(), e);
                        }
                }
 
@@ -466,7 +460,7 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
                }
 
                public Credentials getCredentials() {
-                       return credentials;
+                       return repoElem.getCredentials();
                }
        }
 
@@ -480,8 +474,6 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
                                                .getFirstElement();
                                if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
                                        event.data = de.getWorkspacePath();
-                                       if (log.isDebugEnabled())
-                                               log.debug("Distribution drag for " + event.data);
                                }
                        }
                }
@@ -509,17 +501,17 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
 
                        try {
                                String sourceWorkspace = sourceDist.getWorkspaceName();
-                               Repository sourceRepository = DistUiUtils.getRepository(
+                               Repository sourceRepository = RepoUtils.getRepository(
                                                repositoryFactory, keyring, sourceDist
                                                                .getWorkspaceNode().getParent());
-                               Credentials sourceCredentials = DistUiUtils
+                               Credentials sourceCredentials = RepoUtils
                                                .getRepositoryCredentials(keyring, sourceDist
                                                                .getWorkspaceNode().getParent());
 
                                String targetWorkspace = sourceWorkspace;
-                               Repository targetRepository = DistUiUtils.getRepository(
+                               Repository targetRepository = RepoUtils.getRepository(
                                                repositoryFactory, keyring, targetRepo.getRepoNode());
-                               Credentials targetCredentials = DistUiUtils
+                               Credentials targetCredentials = RepoUtils
                                                .getRepositoryCredentials(keyring,
                                                                targetRepo.getRepoNode());
 
@@ -590,17 +582,31 @@ public class DistributionsView extends ViewPart implements SlcNames, ArgeoNames
                        long begin = System.currentTimeMillis();
                        try {
                                // Not implemented in Davex Jackrabbit v2.2
-                               // Query countQuery = sourceSession
-                               // .getWorkspace()
+                               // Query countQuery = sourceSession.getWorkspace()
                                // .getQueryManager()
-                               // .createQuery("select count(*) from [nt:file]",
-                               // Query.JCR_SQL2);
+                               // .createQuery("//element(*, nt:file)", Query.XPATH);
                                // QueryResult result = countQuery.execute();
-                               // Long fileCount = result.getRows().nextRow().getValues()[0]
+                               // Long expectedCount = result.getNodes().getSize();
+
+                               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();
+                               // Long expectedCount =
+                               // result.getRows().nextRow().getValues()[0]
                                // .getLong();
 
+                               // Long expectedCount = JcrUtils.countFiles(sourceSession
+                               // .getRootNode());
+                               if (log.isDebugEnabled())
+                                       log.debug("Will copy " + expectedCount + " files...");
+
                                ArgeoMonitor monitor = new EclipseArgeoMonitor(eclipseMonitor);
-                               eclipseMonitor.beginTask("Copy files", ArgeoMonitor.UNKNOWN);
+                               eclipseMonitor
+                                               .beginTask("Copy files", expectedCount.intValue());
 
                                Long count = JcrUtils.copyFiles(sourceSession.getRootNode(),
                                                targetSession.getRootNode(), true, monitor);