]> 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/RepositoryNode.java
Change profile and user home
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / model / RepositoryNode.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.Repository;
19 import javax.jcr.RepositoryException;
20 import javax.jcr.Session;
21
22 import org.argeo.ArgeoException;
23 import org.argeo.eclipse.ui.TreeParent;
24
25 /**
26 * UI Tree component. Wraps a JCR {@link Repository}. It also keeps a reference
27 * to its parent Tree Ui component; typically the unique {@link Repositories}
28 * object of the current view to enable bi-directionnal browsing in the tree.
29 */
30
31 public class RepositoryNode extends TreeParent implements UiNode {
32 private String alias;
33 private final Repository repository;
34 private Session defaultSession = null;
35
36 /** Create a new repository with alias = name */
37 public RepositoryNode(String alias, Repository repository, TreeParent parent) {
38 this(alias, alias, repository, parent);
39 }
40
41 /** Create a new repository with distinct name & alias */
42 public RepositoryNode(String alias, String name, Repository repository,
43 TreeParent parent) {
44 super(name);
45 this.repository = repository;
46 setParent(parent);
47 this.alias = alias;
48 }
49
50 public void login() {
51 try {
52 defaultSession = repositoryLogin(null);
53 String[] wkpNames = defaultSession.getWorkspace()
54 .getAccessibleWorkspaceNames();
55 for (String wkpName : wkpNames) {
56 if (wkpName.equals(defaultSession.getWorkspace().getName()))
57 addChild(new WorkspaceNode(this, wkpName, defaultSession));
58 else
59 addChild(new WorkspaceNode(this, wkpName));
60 }
61 } catch (RepositoryException e) {
62 throw new ArgeoException("Cannot connect to repository " + alias, e);
63 }
64 }
65
66 /**
67 * Actual call to the
68 * {@link Repository#login(javax.jcr.Credentials, String)} method. To be
69 * overridden.
70 */
71 protected Session repositoryLogin(String workspaceName)
72 throws RepositoryException {
73 return repository.login(workspaceName);
74 }
75
76 public String[] getAccessibleWorkspaceNames() {
77 try {
78 return defaultSession.getWorkspace().getAccessibleWorkspaceNames();
79 } catch (RepositoryException e) {
80 throw new ArgeoException("Cannot retrieve workspace names", e);
81 }
82 }
83
84 /** returns the {@link Repository} referenced by the current UI Node */
85 public Repository getRepository() {
86 return repository;
87 }
88
89 public String getAlias() {
90 return alias;
91 }
92
93 public Boolean isConnected() {
94 if (defaultSession != null && defaultSession.isLive())
95 return true;
96 else
97 return false;
98 }
99 }