View Javadoc
1   package org.argeo.slc.client.ui.dist.model;
2   
3   import javax.jcr.Node;
4   import javax.jcr.NodeIterator;
5   import javax.jcr.RepositoryException;
6   import javax.jcr.Session;
7   import javax.jcr.query.InvalidQueryException;
8   import javax.jcr.query.QueryManager;
9   import javax.jcr.query.qom.QueryObjectModel;
10  import javax.jcr.query.qom.QueryObjectModelFactory;
11  import javax.jcr.query.qom.Selector;
12  
13  import org.argeo.jcr.JcrUtils;
14  import org.argeo.slc.SlcException;
15  import org.argeo.slc.SlcNames;
16  import org.argeo.slc.SlcTypes;
17  
18  /** Abstract a workspace that contains a software distribution */
19  public class WorkspaceElem extends DistParentElem {
20  	private final RepoElem repoElem;
21  	private String workspaceName;
22  	private Session currSession;
23  
24  	public WorkspaceElem(WkspGroupElem parent, RepoElem repoElem,
25  			String workspaceName) {
26  		super(workspaceName, repoElem.inHome(), repoElem.isReadOnly());
27  		this.repoElem = repoElem;
28  		this.workspaceName = workspaceName;
29  		setParent(parent);
30  	}
31  
32  	public String getWorkspaceName() {
33  		return workspaceName;
34  	}
35  
36  	public RepoElem getRepoElem() {
37  		return repoElem;
38  	}
39  
40  	public Boolean isConnected() {
41  		if (currSession != null && currSession.isLive())
42  			return true;
43  		else
44  			return false;
45  	}
46  
47  	public void login() {
48  		currSession = repoElem.repositoryLogin(getName());
49  	}
50  
51  	/** Utility to create a new Session with correct credential in this context */
52  	public Session getNewSession() {
53  		return repoElem.repositoryLogin(getName());
54  	}
55  
56  	public boolean hasChildren() {
57  		try {
58  			if (isConnected())
59  				return currSession.getRootNode().hasNodes();
60  			else
61  				return true;
62  		} catch (RepositoryException re) {
63  			throw new SlcException(
64  					"Unexpected error while checking children node existence",
65  					re);
66  		}
67  	}
68  
69  	/** Override normal behaviour to initialize display of the workspace */
70  	@Override
71  	public synchronized Object[] getChildren() {
72  		if (isLoaded()) {
73  			return super.getChildren();
74  		} else {
75  			// initialize current object
76  			try {
77  				// Lazy connect the first time we retrieve children
78  				if (currSession == null)
79  					login();
80  
81  				// Retrieve already existing distribution
82  
83  				// Use QOM rather than SQL2 - it seems more robust for remoting
84  				// with JCR 2.2 (might also be some model refresh issue with the
85  				// remoting)
86  				QueryManager queryManager = currSession.getWorkspace()
87  						.getQueryManager();
88  				QueryObjectModelFactory factory = queryManager.getQOMFactory();
89  				Selector selector = factory.selector(
90  						SlcTypes.SLC_MODULAR_DISTRIBUTION,
91  						SlcTypes.SLC_MODULAR_DISTRIBUTION);
92  				// Curiously this works...
93  				// Selector selector = factory.selector(
94  				// SlcTypes.SLC_JAR_FILE,
95  				// SlcTypes.SLC_JAR_FILE);
96  
97  				QueryObjectModel query = factory.createQuery(selector, null,
98  						null, null);
99  
100 				// Query groupQuery = currSession
101 				// .getWorkspace()
102 				// .getQueryManager()
103 				// .createQuery(
104 				// "select * from ["
105 				// + SlcTypes.SLC_MODULAR_DISTRIBUTION
106 				// + "]", Query.JCR_SQL2);
107 				NodeIterator distributions = null;
108 				try {
109 					distributions = query.execute().getNodes();
110 				} catch (InvalidQueryException iqe) {
111 					// For legacy only does not throw an exception while
112 					// browsing
113 					// legacy repositories that does not know
114 					// SLC_MODULAR_DISTRIBUTION type
115 				}
116 				distribs: while (distributions != null
117 						&& distributions.hasNext()) {
118 					Node currDist = distributions.nextNode();
119 					Node distBase = currDist.getParent().getParent();
120 					if (!distBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE))
121 						continue distribs;
122 					String groupId = distBase
123 							.getProperty(SlcNames.SLC_GROUP_ID).getString();
124 					String artifactId = distBase.getProperty(
125 							SlcNames.SLC_ARTIFACT_ID).getString();
126 
127 					String name;
128 					String type;
129 					if (ModularDistVersionBaseElem.AETHER_BINARIES_TYPE
130 							.equals(artifactId)) {
131 						name = groupId;
132 						type = ModularDistVersionBaseElem.AETHER_BINARIES_TYPE;
133 					} else {
134 						name = artifactId;
135 						type = ModularDistVersionBaseElem.AETHER_DEP_TYPE;
136 					}
137 					if (getChildByName(name) == null)
138 						addChild(new ModularDistVersionBaseElem(
139 								WorkspaceElem.this, name, distBase, type));
140 				}
141 				return super.getChildren();
142 			} catch (RepositoryException e) {
143 				throw new SlcException(
144 						"Cannot initialize WorkspaceNode UI object."
145 								+ getName(), e);
146 			}
147 		}
148 	}
149 
150 	@Override
151 	public synchronized void dispose() {
152 		JcrUtils.logoutQuietly(currSession);
153 		super.dispose();
154 	}
155 }