]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/model/RepositoryElem.java
Prepare next development cycle
[lgpl/argeo-commons.git] / jcr / model / 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.cms.ui.jcr.model;
17
18 import javax.jcr.Repository;
19 import javax.jcr.RepositoryException;
20 import javax.jcr.Session;
21
22 import org.argeo.eclipse.ui.EclipseUiException;
23 import org.argeo.eclipse.ui.TreeParent;
24 import org.argeo.jcr.JcrUtils;
25
26 /**
27 * UI Tree component that wraps a JCR {@link Repository}. It also keeps a
28 * reference to its parent Tree Ui component; typically the unique
29 * {@link RepositoriesElem} object of the current view to enable bi-directionnal
30 * browsing in the tree.
31 */
32
33 public class RepositoryElem extends TreeParent {
34 private String alias;
35 protected Repository repository;
36 private Session defaultSession = null;
37
38 /** Create a new repository with distinct name and alias */
39 public RepositoryElem(String alias, Repository repository, TreeParent parent) {
40 super(alias);
41 this.repository = repository;
42 setParent(parent);
43 this.alias = alias;
44 }
45
46 public void login() {
47 try {
48 defaultSession = repositoryLogin("main");
49 String[] wkpNames = defaultSession.getWorkspace().getAccessibleWorkspaceNames();
50 for (String wkpName : wkpNames) {
51 if (wkpName.equals(defaultSession.getWorkspace().getName()))
52 addChild(new WorkspaceElem(this, wkpName, defaultSession));
53 else
54 addChild(new WorkspaceElem(this, wkpName));
55 }
56 } catch (RepositoryException e) {
57 throw new EclipseUiException("Cannot connect to repository " + alias, e);
58 }
59 }
60
61 public synchronized void logout() {
62 for (Object child : getChildren()) {
63 if (child instanceof WorkspaceElem)
64 ((WorkspaceElem) child).logout();
65 }
66 clearChildren();
67 JcrUtils.logoutQuietly(defaultSession);
68 defaultSession = null;
69 }
70
71 /**
72 * Actual call to the {@link Repository#login(javax.jcr.Credentials, String)}
73 * method. To be overridden.
74 */
75 protected Session repositoryLogin(String workspaceName) throws RepositoryException {
76 return repository.login(workspaceName);
77 }
78
79 public String[] getAccessibleWorkspaceNames() {
80 try {
81 return defaultSession.getWorkspace().getAccessibleWorkspaceNames();
82 } catch (RepositoryException e) {
83 throw new EclipseUiException("Cannot retrieve workspace names", e);
84 }
85 }
86
87 public void createWorkspace(String workspaceName) {
88 if (!isConnected())
89 login();
90 try {
91 defaultSession.getWorkspace().createWorkspace(workspaceName);
92 } catch (RepositoryException e) {
93 throw new EclipseUiException("Cannot create workspace", e);
94 }
95 }
96
97 /** returns the {@link Repository} referenced by the current UI Node */
98 public Repository getRepository() {
99 return repository;
100 }
101
102 public String getAlias() {
103 return alias;
104 }
105
106 public Boolean isConnected() {
107 if (defaultSession != null && defaultSession.isLive())
108 return true;
109 else
110 return false;
111 }
112 }