]> 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/model/WorkspaceElem.java
Clean session management.
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / model / WorkspaceElem.java
1 package org.argeo.slc.client.ui.dist.model;
2
3 import javax.jcr.Node;
4 import javax.jcr.NodeIterator;
5 import javax.jcr.RepositoryException;
6 import javax.jcr.Session;
7 import javax.jcr.query.Query;
8
9 import org.argeo.ArgeoException;
10 import org.argeo.jcr.JcrUtils;
11 import org.argeo.slc.jcr.SlcNames;
12 import org.argeo.slc.jcr.SlcTypes;
13
14 /** Abstract a workspace that contains a software distribution */
15 public class WorkspaceElem extends DistParentElem {
16 private final RepoElem repoElem;
17 private String workspaceName;
18 private Session currSession;
19
20 public WorkspaceElem(WkspGroupElem parent, RepoElem repoElem,
21 String workspaceName) {
22 super(workspaceName, repoElem.inHome(), repoElem.isReadOnly());
23 this.repoElem = repoElem;
24 this.workspaceName = workspaceName;
25 setParent(parent);
26 }
27
28 public String getWorkspaceName() {
29 return workspaceName;
30 }
31
32 public RepoElem getRepoElem() {
33 return repoElem;
34 }
35
36 public Boolean isConnected() {
37 if (currSession != null && currSession.isLive())
38 return true;
39 else
40 return false;
41 }
42
43 public void login() {
44 currSession = repoElem.repositoryLogin(getName());
45 }
46
47 /** Utility to create a new Session with correct credential in this context */
48 public Session getNewSession() {
49 return repoElem.repositoryLogin(getName());
50 }
51
52 public boolean hasChildren() {
53 try {
54 if (isConnected())
55 return currSession.getRootNode().hasNodes();
56 else
57 return true;
58 } catch (RepositoryException re) {
59 throw new ArgeoException(
60 "Unexpected error while checking children node existence",
61 re);
62 }
63 }
64
65 /** Override normal behaviour to initialize display of the workspace */
66 @Override
67 public synchronized Object[] getChildren() {
68 if (isLoaded()) {
69 return super.getChildren();
70 } else {
71 // initialize current object
72 try {
73 // Lazy connect the first time we retrieve children
74 if (currSession == null)
75 login();
76
77 // Retrieve already existing distribution
78 Query groupQuery = currSession
79 .getWorkspace()
80 .getQueryManager()
81 .createQuery(
82 "select * from ["
83 + SlcTypes.SLC_MODULAR_DISTRIBUTION
84 + "]", Query.JCR_SQL2);
85 NodeIterator distributions = groupQuery.execute().getNodes();
86 distribs: while (distributions.hasNext()) {
87 Node currDist = distributions.nextNode();
88 Node distBase = currDist.getParent().getParent();
89 if (!distBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE))
90 continue distribs;
91 String groupId = distBase
92 .getProperty(SlcNames.SLC_GROUP_ID).getString();
93 String artifactId = distBase.getProperty(
94 SlcNames.SLC_ARTIFACT_ID).getString();
95
96 String name;
97 String type;
98 if (ModularDistBaseElem.AETHER_BINARIES_TYPE
99 .equals(artifactId)) {
100 name = groupId;
101 type = ModularDistBaseElem.AETHER_BINARIES_TYPE;
102 } else {
103 name = artifactId;
104 type = ModularDistBaseElem.AETHER_DEP_TYPE;
105 }
106 if (getChildByName(name) == null)
107 addChild(new ModularDistBaseElem(WorkspaceElem.this,
108 name, distBase, type));
109 }
110 // Add empty group base that have been marked as relevant
111 groupQuery = currSession
112 .getWorkspace()
113 .getQueryManager()
114 .createQuery(
115 "select * from ["
116 + SlcTypes.SLC_RELEVANT_CATEGORY + "]",
117 Query.JCR_SQL2);
118 distributions = groupQuery.execute().getNodes();
119 while (distributions.hasNext()) {
120 Node distBase = distributions.nextNode();
121 String groupBaseId = distBase.getProperty(
122 SlcNames.SLC_GROUP_BASE_ID).getString();
123 if (getChildByName(groupBaseId) == null)
124 addChild(new ModularDistBaseElem(WorkspaceElem.this,
125 groupBaseId, distBase,
126 ModularDistBaseElem.AETHER_CATEGORY_BASE));
127 }
128 return super.getChildren();
129 } catch (RepositoryException e) {
130 throw new ArgeoException(
131 "Cannot initialize WorkspaceNode UI object."
132 + getName(), e);
133 }
134 }
135 }
136
137 @Override
138 public synchronized void dispose() {
139 JcrUtils.logoutQuietly(currSession);
140 super.dispose();
141 }
142 }