]> git.argeo.org Git - lgpl/argeo-commons.git/blob - RepositoryElem.java
935bac12b00eadb16db2756cc484d638e4f845dc
[lgpl/argeo-commons.git] / RepositoryElem.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.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 RepositoryElem extends TreeParent {
32 private String alias;
33 protected Repository repository;
34 private Session defaultSession = null;
35
36 /** Create a new repository with distinct name & alias */
37 public RepositoryElem(String alias, Repository repository, TreeParent parent) {
38 super(alias);
39 this.repository = repository;
40 setParent(parent);
41 this.alias = alias;
42 }
43
44 public void login() {
45 try {
46 defaultSession = repositoryLogin(null);
47 String[] wkpNames = defaultSession.getWorkspace()
48 .getAccessibleWorkspaceNames();
49 for (String wkpName : wkpNames) {
50 if (wkpName.equals(defaultSession.getWorkspace().getName()))
51 addChild(new WorkspaceElem(this, wkpName, defaultSession));
52 else
53 addChild(new WorkspaceElem(this, wkpName));
54 }
55 } catch (RepositoryException e) {
56 throw new ArgeoException("Cannot connect to repository " + alias, e);
57 }
58 }
59
60 /**
61 * Actual call to the
62 * {@link Repository#login(javax.jcr.Credentials, String)} method. To be
63 * overridden.
64 */
65 protected Session repositoryLogin(String workspaceName)
66 throws RepositoryException {
67 return repository.login(workspaceName);
68 }
69
70 public String[] getAccessibleWorkspaceNames() {
71 try {
72 return defaultSession.getWorkspace().getAccessibleWorkspaceNames();
73 } catch (RepositoryException e) {
74 throw new ArgeoException("Cannot retrieve workspace names", e);
75 }
76 }
77
78 public void createWorkspace(String workspaceName) {
79 if (!isConnected())
80 login();
81 try {
82 defaultSession.getWorkspace().createWorkspace(workspaceName);
83 } catch (RepositoryException e) {
84 throw new ArgeoException("Cannot create workspace", e);
85 }
86 }
87
88 /** returns the {@link Repository} referenced by the current UI Node */
89 public Repository getRepository() {
90 return repository;
91 }
92
93 public String getAlias() {
94 return alias;
95 }
96
97 public Boolean isConnected() {
98 if (defaultSession != null && defaultSession.isLive())
99 return true;
100 else
101 return false;
102 }
103 }