]> git.argeo.org Git - lgpl/argeo-commons.git/blob - RepositoriesElem.java
70922fe1bd8291e64c749fab21593e6126bcb3e0
[lgpl/argeo-commons.git] / RepositoriesElem.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.workbench.internal.jcr.model;
17
18 import java.util.Map;
19
20 import javax.jcr.Node;
21 import javax.jcr.NodeIterator;
22 import javax.jcr.Repository;
23 import javax.jcr.RepositoryException;
24 import javax.jcr.RepositoryFactory;
25 import javax.jcr.Session;
26
27 import org.argeo.cms.ArgeoNames;
28 import org.argeo.eclipse.ui.EclipseUiException;
29 import org.argeo.eclipse.ui.TreeParent;
30 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
31 import org.argeo.jcr.RepositoryRegister;
32 import org.argeo.node.NodeUtils;
33 import org.argeo.node.security.Keyring;
34
35 /**
36 * UI Tree component that implements the Argeo abstraction of a
37 * {@link RepositoryFactory} that enable a user to "mount" various repositories
38 * in a single Tree like View. It is usually meant to be at the root of the UI
39 * Tree and thus {@link getParent()} method will return null.
40 *
41 * The {@link RepositoryFactory} is injected at instantiation time and must be
42 * use get or register new {@link Repository} objects upon which a reference is
43 * kept here.
44 */
45
46 public class RepositoriesElem extends TreeParent implements ArgeoNames {
47 private final RepositoryRegister repositoryRegister;
48 private final RepositoryFactory repositoryFactory;
49
50 /**
51 * A session of the logged in user on the default workspace of the node
52 * repository.
53 */
54 private final Session userSession;
55 private final Keyring keyring;
56
57 public RepositoriesElem(String name, RepositoryRegister repositoryRegister, RepositoryFactory repositoryFactory,
58 TreeParent parent, Session userSession, Keyring keyring) {
59 super(name);
60 this.repositoryRegister = repositoryRegister;
61 this.repositoryFactory = repositoryFactory;
62 this.userSession = userSession;
63 this.keyring = keyring;
64 }
65
66 /**
67 * Override normal behavior to initialize the various repositories only at
68 * request time
69 */
70 @Override
71 public synchronized Object[] getChildren() {
72 if (isLoaded()) {
73 return super.getChildren();
74 } else {
75 // initialize current object
76 Map<String, Repository> refRepos = repositoryRegister.getRepositories();
77 for (String name : refRepos.keySet()) {
78 Repository repository = refRepos.get(name);
79 // if (repository instanceof MaintainedRepository)
80 // super.addChild(new MaintainedRepositoryElem(name,
81 // repository, this));
82 // else
83 super.addChild(new RepositoryElem(name, repository, this));
84 }
85
86 // remote
87 if (keyring != null) {
88 try {
89 addRemoteRepositories(keyring);
90 } catch (RepositoryException e) {
91 throw new EclipseUiException("Cannot browse remote repositories", e);
92 }
93 }
94 return super.getChildren();
95 }
96 }
97
98 protected void addRemoteRepositories(Keyring jcrKeyring) throws RepositoryException {
99 Node userHome = NodeUtils.getUserHome(userSession);
100 if (userHome != null && userHome.hasNode(ARGEO_REMOTE)) {
101 NodeIterator it = userHome.getNode(ARGEO_REMOTE).getNodes();
102 while (it.hasNext()) {
103 Node remoteNode = it.nextNode();
104 String uri = remoteNode.getProperty(ARGEO_URI).getString();
105 try {
106 RemoteRepositoryElem remoteRepositoryNode = new RemoteRepositoryElem(remoteNode.getName(),
107 repositoryFactory, uri, this, userSession, jcrKeyring, remoteNode.getPath());
108 super.addChild(remoteRepositoryNode);
109 } catch (Exception e) {
110 ErrorFeedback.show("Cannot add remote repository " + remoteNode, e);
111 }
112 }
113 }
114 }
115
116 public void registerNewRepository(String alias, Repository repository) {
117 // TODO: implement this
118 // Create a new RepositoryNode Object
119 // add it
120 // super.addChild(new RepositoriesNode(...));
121 }
122
123 /** Returns the {@link RepositoryRegister} wrapped by this object. */
124 public RepositoryRegister getRepositoryRegister() {
125 return repositoryRegister;
126 }
127 }