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