]> git.argeo.org Git - lgpl/argeo-commons.git/blob - model/WorkspaceElem.java
Prepare next development cycle
[lgpl/argeo-commons.git] / model / WorkspaceElem.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.cms.ui.workbench.internal.jcr.model;
17
18 import javax.jcr.Node;
19 import javax.jcr.NodeIterator;
20 import javax.jcr.RepositoryException;
21 import javax.jcr.Session;
22 // import javax.jcr.Workspace;
23 import javax.jcr.Workspace;
24
25 import org.argeo.eclipse.ui.EclipseUiException;
26 import org.argeo.eclipse.ui.TreeParent;
27 import org.argeo.jcr.JcrUtils;
28
29 /**
30 * UI Tree component. Wraps the root node of a JCR {@link Workspace}. It also
31 * keeps a reference to its parent {@link RepositoryElem}, to be able to
32 * retrieve alias of the current used repository
33 */
34 public class WorkspaceElem extends TreeParent {
35 private Session session = null;
36
37 public WorkspaceElem(RepositoryElem parent, String name) {
38 this(parent, name, null);
39 }
40
41 public WorkspaceElem(RepositoryElem parent, String name, Session session) {
42 super(name);
43 this.session = session;
44 setParent(parent);
45 }
46
47 public synchronized Session getSession() {
48 return session;
49 }
50
51 public synchronized Node getRootNode() {
52 try {
53 if (session != null)
54 return session.getRootNode();
55 else
56 return null;
57 } catch (RepositoryException e) {
58 throw new EclipseUiException("Cannot get root node of workspace "
59 + 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 "
68 + getName(), e);
69 }
70 }
71
72 public Boolean isConnected() {
73 if (session != null && session.isLive())
74 return true;
75 else
76 return false;
77 }
78
79 @Override
80 public synchronized void dispose() {
81 logout();
82 super.dispose();
83 }
84
85 /** Logouts the session, does not nothing if there is no live session. */
86 public synchronized void logout() {
87 clearChildren();
88 JcrUtils.logoutQuietly(session);
89 session = null;
90 }
91
92 @Override
93 public synchronized boolean hasChildren() {
94 try {
95 if (isConnected())
96 return session.getRootNode().hasNodes();
97 else
98 return false;
99 } catch (RepositoryException re) {
100 throw new EclipseUiException(
101 "Unexpected error while checking children node existence",
102 re);
103 }
104 }
105
106 /** Override normal behaviour to initialize display of the workspace */
107 @Override
108 public synchronized Object[] getChildren() {
109 if (isLoaded()) {
110 return super.getChildren();
111 } else {
112 // initialize current object
113 try {
114 Node rootNode;
115 if (session == null)
116 return null;
117 else
118 rootNode = session.getRootNode();
119 NodeIterator ni = rootNode.getNodes();
120 while (ni.hasNext()) {
121 Node node = ni.nextNode();
122 addChild(new SingleJcrNodeElem(this, node, node.getName()));
123 }
124 return super.getChildren();
125 } catch (RepositoryException e) {
126 throw new EclipseUiException(
127 "Cannot initialize WorkspaceNode UI object."
128 + getName(), e);
129 }
130 }
131 }
132 }