]> 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/model/WorkspaceElem.java
Full refactoring of the UI distribution view:
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / model / WorkspaceElem.java
index e4562ee71154ebb505a138b8da976762b2942d2d..52acdac7d7e0ba1e85c33c3734baca3673e29c27 100644 (file)
@@ -1,70 +1,93 @@
 package org.argeo.slc.client.ui.dist.model;
 
-import javax.jcr.Credentials;
-import javax.jcr.Node;
+import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.query.Query;
 
-import org.argeo.eclipse.ui.TreeParent;
-import org.argeo.jcr.JcrUtils;
-import org.argeo.slc.SlcException;
+import org.argeo.ArgeoException;
+import org.argeo.slc.jcr.SlcNames;
+import org.argeo.slc.jcr.SlcTypes;
 
-/** Abstracts a workspace that contains a given distribution */
-public class WorkspaceElem extends TreeParent {
+/** Abstract a workspace that contains a software distribution */
+public class WorkspaceElem extends DistParentElem {
        private final RepoElem repoElem;
-       private final Node workspaceNode;
+       private String workspaceName;
+       private Session defaultSession;
 
-       /**
-        * Helper to display only version when the workspace name is well formatted
-        */
-       private static String formatName(Node workspaceNode) {
-               String name = JcrUtils.getNameQuietly(workspaceNode);
-               if (name != null && name.lastIndexOf('-') > 0)
-                       return name.substring(name.lastIndexOf('-') + 1);
-               else
-                       return name;
-       }
-
-       public WorkspaceElem(RepoElem repoElem, Node workspaceNode) {
-               super(formatName(workspaceNode));
+       public WorkspaceElem(WkspGroupElem parent, RepoElem repoElem,
+                       String workspaceName) {
+               super(workspaceName, repoElem.inHome(), repoElem.isReadOnly());
                this.repoElem = repoElem;
-               this.workspaceNode = workspaceNode;
-       }
-
-       public Node getWorkspaceNode() {
-               return workspaceNode;
+               this.workspaceName = workspaceName;
+               setParent(parent);
        }
 
        public String getWorkspaceName() {
-               return JcrUtils.getNameQuietly(workspaceNode);
+               return workspaceName;
        }
 
-       public String getWorkspacePath() {
-               try {
-                       return workspaceNode.getPath();
-               } catch (RepositoryException e) {
-                       throw new SlcException("Cannot get or add workspace path "
-                                       + getWorkspaceName(), e);
-               }
+       public RepoElem getRepoElem() {
+               return repoElem;
        }
 
-       public String getRepoPath() {
-               try {
-                       return workspaceNode.getParent().getPath();
-               } catch (RepositoryException e) {
-                       throw new SlcException("Cannot get or add workspace path "
-                                       + getWorkspaceName(), e);
-               }
+       public Boolean isConnected() {
+               if (defaultSession != null && defaultSession.isLive())
+                       return true;
+               else
+                       return false;
        }
 
-       public RepoElem getRepoElem() {
-               return repoElem;
+       public void login() {
+               defaultSession = repoElem.repositoryLogin(getName());
        }
 
-       public Credentials getCredentials() {
-               return repoElem.getCredentials();
+       public boolean hasChildren() {
+               try {
+                       if (isConnected())
+                               return defaultSession.getRootNode().hasNodes();
+                       else
+                               return false;
+               } catch (RepositoryException re) {
+                       throw new ArgeoException(
+                                       "Unexpected error while checking children node existence",
+                                       re);
+               }
        }
 
-       public boolean isReadOnly() {
-               return repoElem.isReadOnly();
+       /** Override normal behaviour to initialize display of the workspace */
+       @Override
+       public synchronized Object[] getChildren() {
+               if (isLoaded()) {
+                       return super.getChildren();
+               } else {
+                       // initialize current object
+                       try {
+                               if (defaultSession == null)
+                                       return null;
+                               else {
+                                       Query groupQuery = defaultSession
+                                                       .getWorkspace()
+                                                       .getQueryManager()
+                                                       .createQuery(
+                                                                       "select * from [" + SlcTypes.SLC_GROUP_BASE
+                                                                                       + "] as group order by group.["
+                                                                                       + SlcNames.SLC_GROUP_BASE_ID + "]",
+                                                                       Query.JCR_SQL2);
+                                       NodeIterator groups = groupQuery.execute().getNodes();
+                                       while (groups.hasNext()) {
+                                               addChild(new GroupBaseElem(WorkspaceElem.this, groups
+                                                               .nextNode()
+                                                               .getProperty(SlcNames.SLC_GROUP_BASE_ID)
+                                                               .getString()));
+                                       }
+                               }
+                               return super.getChildren();
+                       } catch (RepositoryException e) {
+                               throw new ArgeoException(
+                                               "Cannot initialize WorkspaceNode UI object."
+                                                               + getName(), e);
+                       }
+               }
        }
 }