]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/model/WorkspaceNode.java
38470332208bdc4b06887e87192867d77266e305
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / model / WorkspaceNode.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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.jcr.ui.explorer.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
24 import org.argeo.ArgeoException;
25 import org.argeo.eclipse.ui.TreeParent;
26 import org.argeo.jcr.JcrUtils;
27
28 /**
29 * UI Tree component. Wraps the root node of a JCR {@link Workspace}. It also
30 * keeps a reference to its parent {@link RepositoryNode}, to be able to
31 * retrieve alias of the current used repository
32 */
33 public class WorkspaceNode extends TreeParent implements UiNode {
34 private Session session = null;
35
36 public WorkspaceNode(RepositoryNode parent, String name) {
37 this(parent, name, null);
38 }
39
40 public WorkspaceNode(RepositoryNode parent, String name, Session session) {
41 super(name);
42 this.session = session;
43 setParent(parent);
44 }
45
46 public Session getSession() {
47 return session;
48 }
49
50 public Node getRootNode() {
51 try {
52 if (session != null)
53 return session.getRootNode();
54 else
55 return null;
56 } catch (RepositoryException e) {
57 throw new ArgeoException("Cannot get root node of workspace "
58 + getName(), e);
59 }
60 }
61
62 public void login() {
63 try {
64 session = ((RepositoryNode) getParent()).repositoryLogin(getName());
65 } catch (RepositoryException e) {
66 throw new ArgeoException("Cannot connect to repository "
67 + 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 /** Logouts the session, does not nothing if there is no live session. */
85 public void logout() {
86 clearChildren();
87 JcrUtils.logoutQuietly(session);
88 }
89
90 /** Returns the alias of the parent Repository */
91 public String getAlias() {
92 return ((UiNode) getParent()).getAlias();
93 }
94
95 @Override
96 public boolean hasChildren() {
97 try {
98 if (isConnected())
99 return session.getRootNode().hasNodes();
100 else
101 return false;
102 } catch (RepositoryException re) {
103 throw new ArgeoException(
104 "Unexpected error while checking children node existence",
105 re);
106 }
107 }
108
109 /** Override normal behaviour to initialize display of the workspace */
110 @Override
111 public synchronized Object[] getChildren() {
112 if (isLoaded()) {
113 return super.getChildren();
114 } else {
115 // initialize current object
116 try {
117 Node rootNode;
118 if (session == null)
119 return null;
120 else
121 rootNode = session.getRootNode();
122 NodeIterator ni = rootNode.getNodes();
123 while (ni.hasNext()) {
124 Node node = ni.nextNode();
125 addChild(new SingleJcrNode(this, node, node.getName()));
126 }
127 return super.getChildren();
128 } catch (RepositoryException e) {
129 throw new ArgeoException(
130 "Cannot initialize WorkspaceNode UI object."
131 + getName(), e);
132 }
133 }
134 }
135 }