]> 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
Work on modular distributions - change lazy init policies, add observer on the catego...
[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..a163974072607d43659a534c250c200e7479c6d2 100644 (file)
 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.ArgeoException;
 import org.argeo.jcr.JcrUtils;
-import org.argeo.slc.SlcException;
+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 currSession;
 
-       /**
-        * 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(WkspGroupElem parent, RepoElem repoElem,
+                       String workspaceName) {
+               super(workspaceName, repoElem.inHome(), repoElem.isReadOnly());
+               this.repoElem = repoElem;
+               this.workspaceName = workspaceName;
+               setParent(parent);
        }
 
-       public WorkspaceElem(RepoElem repoElem, Node workspaceNode) {
-               super(formatName(workspaceNode));
-               this.repoElem = repoElem;
-               this.workspaceNode = workspaceNode;
+       public String getWorkspaceName() {
+               return workspaceName;
        }
 
-       public Node getWorkspaceNode() {
-               return workspaceNode;
+       public RepoElem getRepoElem() {
+               return repoElem;
        }
 
-       public String getWorkspaceName() {
-               return JcrUtils.getNameQuietly(workspaceNode);
+       public Boolean isConnected() {
+               if (currSession != null && currSession.isLive())
+                       return true;
+               else
+                       return false;
        }
 
-       public String getWorkspacePath() {
-               try {
-                       return workspaceNode.getPath();
-               } catch (RepositoryException e) {
-                       throw new SlcException("Cannot get or add workspace path "
-                                       + getWorkspaceName(), e);
-               }
+       public void login() {
+               currSession = repoElem.repositoryLogin(getName());
        }
 
-       public String getRepoPath() {
+       public boolean hasChildren() {
                try {
-                       return workspaceNode.getParent().getPath();
-               } catch (RepositoryException e) {
-                       throw new SlcException("Cannot get or add workspace path "
-                                       + getWorkspaceName(), e);
+                       if (isConnected())
+                               return currSession.getRootNode().hasNodes();
+                       else
+                               return true;
+               } catch (RepositoryException re) {
+                       throw new ArgeoException(
+                                       "Unexpected error while checking children node existence",
+                                       re);
                }
        }
 
-       public RepoElem getRepoElem() {
-               return repoElem;
-       }
+       /** Override normal behaviour to initialize display of the workspace */
+       @Override
+       public synchronized Object[] getChildren() {
+               if (isLoaded()) {
+                       return super.getChildren();
+               } else {
+                       // initialize current object
+                       try {
+                               // Lazy connect the first time we retrieve children
+                               if (currSession == null)
+                                       login();
+
+                               // Retrieve already existing distribution
+                               Query groupQuery = currSession
+                                               .getWorkspace()
+                                               .getQueryManager()
+                                               .createQuery(
+                                                               "select * from ["
+                                                                               + SlcTypes.SLC_MODULAR_DISTRIBUTION
+                                                                               + "]", Query.JCR_SQL2);
+                               NodeIterator distributions = groupQuery.execute().getNodes();
+                               distribs: while (distributions.hasNext()) {
+                                       Node currDist = distributions.nextNode();
+                                       Node distBase = currDist.getParent().getParent();
+                                       if (!distBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE))
+                                               continue distribs;
+                                       String groupId = distBase
+                                                       .getProperty(SlcNames.SLC_GROUP_ID).getString();
+                                       String artifactId = distBase.getProperty(
+                                                       SlcNames.SLC_ARTIFACT_ID).getString();
 
-       public Credentials getCredentials() {
-               return repoElem.getCredentials();
+                                       String name;
+                                       String type;
+                                       if (ModularDistBaseElem.AETHER_BINARIES_TYPE
+                                                       .equals(artifactId)) {
+                                               name = groupId;
+                                               type = ModularDistBaseElem.AETHER_BINARIES_TYPE;
+                                       } else {
+                                               name = artifactId;
+                                               type = ModularDistBaseElem.AETHER_DEP_TYPE;
+                                       }
+                                       if (getChildByName(name) == null)
+                                               addChild(new ModularDistBaseElem(WorkspaceElem.this,
+                                                               name, distBase, type));
+                               }
+                               // Add empty group base that have been marked as relevant
+                               groupQuery = currSession
+                                               .getWorkspace()
+                                               .getQueryManager()
+                                               .createQuery(
+                                                               "select * from ["
+                                                                               + SlcTypes.SLC_RELEVANT_CATEGORY + "]",
+                                                               Query.JCR_SQL2);
+                               distributions = groupQuery.execute().getNodes();
+                               while (distributions.hasNext()) {
+                                       Node distBase = distributions.nextNode();
+                                       String groupBaseId = distBase.getProperty(
+                                                       SlcNames.SLC_GROUP_BASE_ID).getString();
+                                       if (getChildByName(groupBaseId) == null)
+                                               addChild(new ModularDistBaseElem(WorkspaceElem.this,
+                                                               groupBaseId, distBase,
+                                                               ModularDistBaseElem.AETHER_CATEGORY_BASE));
+                               }
+                               return super.getChildren();
+                       } catch (RepositoryException e) {
+                               throw new ArgeoException(
+                                               "Cannot initialize WorkspaceNode UI object."
+                                                               + getName(), e);
+                       }
+               }
        }
 
-       public boolean isReadOnly() {
-               return repoElem.isReadOnly();
+       @Override
+       public synchronized void dispose() {
+               JcrUtils.logoutQuietly(currSession);
+               super.dispose();
        }
 }