View Javadoc
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.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  // import javax.jcr.Workspace;
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   * UI Tree component. Wraps the root node of a JCR {@link Workspace}. It also
32   * keeps a reference to its parent {@link RepositoryElem}, to be able to
33   * retrieve alias of the current used repository
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  	/** Logouts the session, does not nothing if there is no live session. */
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  					// current user may not have access to the root node
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 	/** Override normal behaviour to initialize display of the workspace */
109 	@Override
110 	public synchronized Object[] getChildren() {
111 		if (isLoaded()) {
112 			return super.getChildren();
113 		} else {
114 			// initialize current object
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 }