]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/jcr/model/WorkspaceElem.java
Make login shell styling more robust.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / jcr / model / WorkspaceElem.java
1 package org.argeo.cms.ui.jcr.model;
2
3 import javax.jcr.AccessDeniedException;
4 import javax.jcr.Node;
5 import javax.jcr.NodeIterator;
6 import javax.jcr.RepositoryException;
7 import javax.jcr.Session;
8 // import javax.jcr.Workspace;
9 import javax.jcr.Workspace;
10
11 import org.argeo.eclipse.ui.EclipseUiException;
12 import org.argeo.eclipse.ui.TreeParent;
13 import org.argeo.jcr.JcrUtils;
14
15 /**
16 * UI Tree component. Wraps the root node of a JCR {@link Workspace}. It also
17 * keeps a reference to its parent {@link RepositoryElem}, to be able to
18 * retrieve alias of the current used repository
19 */
20 public class WorkspaceElem extends TreeParent {
21 private Session session = null;
22
23 public WorkspaceElem(RepositoryElem parent, String name) {
24 this(parent, name, null);
25 }
26
27 public WorkspaceElem(RepositoryElem parent, String name, Session session) {
28 super(name);
29 this.session = session;
30 setParent(parent);
31 }
32
33 public synchronized Session getSession() {
34 return session;
35 }
36
37 public synchronized Node getRootNode() {
38 try {
39 if (session != null)
40 return session.getRootNode();
41 else
42 return null;
43 } catch (RepositoryException e) {
44 throw new EclipseUiException("Cannot get root node of workspace " + getName(), e);
45 }
46 }
47
48 public synchronized void login() {
49 try {
50 session = ((RepositoryElem) getParent()).repositoryLogin(getName());
51 } catch (RepositoryException e) {
52 throw new EclipseUiException("Cannot connect to repository " + getName(), e);
53 }
54 }
55
56 public Boolean isConnected() {
57 if (session != null && session.isLive())
58 return true;
59 else
60 return false;
61 }
62
63 @Override
64 public synchronized void dispose() {
65 logout();
66 super.dispose();
67 }
68
69 /** Logouts the session, does not nothing if there is no live session. */
70 public synchronized void logout() {
71 clearChildren();
72 JcrUtils.logoutQuietly(session);
73 session = null;
74 }
75
76 @Override
77 public synchronized boolean hasChildren() {
78 try {
79 if (isConnected())
80 try {
81 return session.getRootNode().hasNodes();
82 } catch (AccessDeniedException e) {
83 // current user may not have access to the root node
84 return false;
85 }
86 else
87 return false;
88 } catch (RepositoryException re) {
89 throw new EclipseUiException("Unexpected error while checking children node existence", re);
90 }
91 }
92
93 /** Override normal behaviour to initialize display of the workspace */
94 @Override
95 public synchronized Object[] getChildren() {
96 if (isLoaded()) {
97 return super.getChildren();
98 } else {
99 // initialize current object
100 try {
101 Node rootNode;
102 if (session == null)
103 return null;
104 else
105 rootNode = session.getRootNode();
106 NodeIterator ni = rootNode.getNodes();
107 while (ni.hasNext()) {
108 Node node = ni.nextNode();
109 addChild(new SingleJcrNodeElem(this, node, node.getName()));
110 }
111 return super.getChildren();
112 } catch (RepositoryException e) {
113 throw new EclipseUiException("Cannot initialize WorkspaceNode UI object." + getName(), e);
114 }
115 }
116 }
117 }