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.InvalidQueryException;
8 import javax.jcr.query.QueryManager;
9 import javax.jcr.query.qom.QueryObjectModel;
10 import javax.jcr.query.qom.QueryObjectModelFactory;
11 import javax.jcr.query.qom.Selector;
12
13 import org.argeo.jcr.JcrUtils;
14 import org.argeo.slc.SlcException;
15 import org.argeo.slc.SlcNames;
16 import org.argeo.slc.SlcTypes;
17
18
19 public class WorkspaceElem extends DistParentElem {
20 private final RepoElem repoElem;
21 private String workspaceName;
22 private Session currSession;
23
24 public WorkspaceElem(WkspGroupElem parent, RepoElem repoElem,
25 String workspaceName) {
26 super(workspaceName, repoElem.inHome(), repoElem.isReadOnly());
27 this.repoElem = repoElem;
28 this.workspaceName = workspaceName;
29 setParent(parent);
30 }
31
32 public String getWorkspaceName() {
33 return workspaceName;
34 }
35
36 public RepoElem getRepoElem() {
37 return repoElem;
38 }
39
40 public Boolean isConnected() {
41 if (currSession != null && currSession.isLive())
42 return true;
43 else
44 return false;
45 }
46
47 public void login() {
48 currSession = repoElem.repositoryLogin(getName());
49 }
50
51
52 public Session getNewSession() {
53 return repoElem.repositoryLogin(getName());
54 }
55
56 public boolean hasChildren() {
57 try {
58 if (isConnected())
59 return currSession.getRootNode().hasNodes();
60 else
61 return true;
62 } catch (RepositoryException re) {
63 throw new SlcException(
64 "Unexpected error while checking children node existence",
65 re);
66 }
67 }
68
69
70 @Override
71 public synchronized Object[] getChildren() {
72 if (isLoaded()) {
73 return super.getChildren();
74 } else {
75
76 try {
77
78 if (currSession == null)
79 login();
80
81
82
83
84
85
86 QueryManager queryManager = currSession.getWorkspace()
87 .getQueryManager();
88 QueryObjectModelFactory factory = queryManager.getQOMFactory();
89 Selector selector = factory.selector(
90 SlcTypes.SLC_MODULAR_DISTRIBUTION,
91 SlcTypes.SLC_MODULAR_DISTRIBUTION);
92
93
94
95
96
97 QueryObjectModel query = factory.createQuery(selector, null,
98 null, null);
99
100
101
102
103
104
105
106
107 NodeIterator distributions = null;
108 try {
109 distributions = query.execute().getNodes();
110 } catch (InvalidQueryException iqe) {
111
112
113
114
115 }
116 distribs: while (distributions != null
117 && distributions.hasNext()) {
118 Node currDist = distributions.nextNode();
119 Node distBase = currDist.getParent().getParent();
120 if (!distBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE))
121 continue distribs;
122 String groupId = distBase
123 .getProperty(SlcNames.SLC_GROUP_ID).getString();
124 String artifactId = distBase.getProperty(
125 SlcNames.SLC_ARTIFACT_ID).getString();
126
127 String name;
128 String type;
129 if (ModularDistVersionBaseElem.AETHER_BINARIES_TYPE
130 .equals(artifactId)) {
131 name = groupId;
132 type = ModularDistVersionBaseElem.AETHER_BINARIES_TYPE;
133 } else {
134 name = artifactId;
135 type = ModularDistVersionBaseElem.AETHER_DEP_TYPE;
136 }
137 if (getChildByName(name) == null)
138 addChild(new ModularDistVersionBaseElem(
139 WorkspaceElem.this, name, distBase, type));
140 }
141 return super.getChildren();
142 } catch (RepositoryException e) {
143 throw new SlcException(
144 "Cannot initialize WorkspaceNode UI object."
145 + getName(), e);
146 }
147 }
148 }
149
150 @Override
151 public synchronized void dispose() {
152 JcrUtils.logoutQuietly(currSession);
153 super.dispose();
154 }
155 }