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