1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.cms.ui.jcr.model;
17
18 import javax.jcr.AccessDeniedException;
19 import javax.jcr.Node;
20 import javax.jcr.NodeIterator;
21 import javax.jcr.RepositoryException;
22 import javax.jcr.Session;
23
24 import javax.jcr.Workspace;
25
26 import org.argeo.eclipse.ui.EclipseUiException;
27 import org.argeo.eclipse.ui.TreeParent;
28 import org.argeo.jcr.JcrUtils;
29
30
31
32
33
34
35 public class WorkspaceElem extends TreeParent {
36 private Session session = null;
37
38 public WorkspaceElem(RepositoryElem parent, String name) {
39 this(parent, name, null);
40 }
41
42 public WorkspaceElem(RepositoryElem parent, String name, Session session) {
43 super(name);
44 this.session = session;
45 setParent(parent);
46 }
47
48 public synchronized Session getSession() {
49 return session;
50 }
51
52 public synchronized Node getRootNode() {
53 try {
54 if (session != null)
55 return session.getRootNode();
56 else
57 return null;
58 } catch (RepositoryException e) {
59 throw new EclipseUiException("Cannot get root node of workspace " + getName(), e);
60 }
61 }
62
63 public synchronized void login() {
64 try {
65 session = ((RepositoryElem) getParent()).repositoryLogin(getName());
66 } catch (RepositoryException e) {
67 throw new EclipseUiException("Cannot connect to repository " + getName(), e);
68 }
69 }
70
71 public Boolean isConnected() {
72 if (session != null && session.isLive())
73 return true;
74 else
75 return false;
76 }
77
78 @Override
79 public synchronized void dispose() {
80 logout();
81 super.dispose();
82 }
83
84
85 public synchronized void logout() {
86 clearChildren();
87 JcrUtils.logoutQuietly(session);
88 session = null;
89 }
90
91 @Override
92 public synchronized boolean hasChildren() {
93 try {
94 if (isConnected())
95 try {
96 return session.getRootNode().hasNodes();
97 } catch (AccessDeniedException e) {
98
99 return false;
100 }
101 else
102 return false;
103 } catch (RepositoryException re) {
104 throw new EclipseUiException("Unexpected error while checking children node existence", re);
105 }
106 }
107
108
109 @Override
110 public synchronized Object[] getChildren() {
111 if (isLoaded()) {
112 return super.getChildren();
113 } else {
114
115 try {
116 Node rootNode;
117 if (session == null)
118 return null;
119 else
120 rootNode = session.getRootNode();
121 NodeIterator ni = rootNode.getNodes();
122 while (ni.hasNext()) {
123 Node node = ni.nextNode();
124 addChild(new SingleJcrNodeElem(this, node, node.getName()));
125 }
126 return super.getChildren();
127 } catch (RepositoryException e) {
128 throw new EclipseUiException("Cannot initialize WorkspaceNode UI object." + getName(), e);
129 }
130 }
131 }
132 }