1 /************************************************************************************
3 * Copyright (c) 2008 Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel.
5 * Ce logiciel est diffuse sous les termes et conditions de la licence CECILL.
6 * Voir le fichier joint Licence_CeCILL_V2-fr.txt pour plus de details.
8 ************************************************************************************/
9 package com.pentila.evalcomp.ariadne;
11 import java.io.IOException;
12 import java.io.Reader;
13 import java.io.StringReader;
14 import java.rmi.RemoteException;
15 import java.util.HashSet;
16 import java.util.List;
19 import org.apache.axis2.AxisFault;
20 import org.jdom.Element;
21 import org.jdom.JDOMException;
22 import org.jdom.Namespace;
23 import org.jdom.input.SAXBuilder;
25 import be.cenorm.www.CreateAnonymousSession;
26 import be.cenorm.www.CreateAnonymousSessionResponse;
27 import be.cenorm.www.GetTotalResultsCount;
28 import be.cenorm.www.GetTotalResultsCountResponse;
29 import be.cenorm.www.SetQueryLanguage;
30 import be.cenorm.www.SetResultsFormat;
31 import be.cenorm.www.SetResultsSetSize;
32 import be.cenorm.www.SqiSessionManagementStub;
33 import be.cenorm.www.SqiTargetStub;
34 import be.cenorm.www.SynchronousQuery;
35 import be.cenorm.www.SynchronousQueryResponse;
36 import be.cenorm.www._SQIFaultException;
38 import com.pentila.evalcomp.domain.transit.RessourceFile;
39 import com.pentila.evalcomp.domain.transit.RessourceItem;
41 // TODO: Auto-generated Javadoc
43 * The Class AriadneImpl.
50 public class AriadneImpl {
53 private String host = "ariadne.cs.kuleuven.be";
56 private String service = "ariadne-dev-ws";
58 /** The sqi session stub. */
59 private static SqiSessionManagementStub sqiSessionStub;
62 private static SqiTargetStub sqiStub;
65 * Creates the anonymous session.
67 * @param target the target
71 public static String createAnonymousSession(String target) {
73 sqiSessionStub = new SqiSessionManagementStub(target);
74 CreateAnonymousSession createASession = new CreateAnonymousSession();
75 CreateAnonymousSessionResponse sessionResponse = sqiSessionStub
76 .createAnonymousSession(createASession);
77 String sessionId = sessionResponse
78 .getCreateAnonymousSessionReturn();
80 } catch (Exception e1) {
81 // TODO Auto-generated catch block
88 * Sets the query language.
90 * @param sessionId the session id
91 * @param language the language
93 * @throws RemoteException the remote exception
94 * @throws _SQIFaultException the _ sqi fault exception
96 public static void setQueryLanguage(String sessionId, String language)
97 throws RemoteException, _SQIFaultException {
98 SetQueryLanguage queryLanguage = new SetQueryLanguage();
99 queryLanguage.setQueryLanguageID(language);
100 queryLanguage.setTargetSessionID(sessionId);
101 sqiStub.setQueryLanguage(queryLanguage);
106 * Sets the result set size.
108 * @param sessionId the session id
109 * @param resultSize the result size
111 * @throws RemoteException the remote exception
112 * @throws _SQIFaultException the _ sqi fault exception
114 public static void setResultSetSize(String sessionId, int resultSize)
115 throws RemoteException, _SQIFaultException {
116 SetResultsSetSize resultsSetSize = new SetResultsSetSize();
117 resultsSetSize.setResultsSetSize(resultSize);
118 resultsSetSize.setTargetSessionID(sessionId);
119 sqiStub.setResultsSetSize(resultsSetSize);
123 * Sets the result set format.
125 * @param sessionId the session id
126 * @param format the format
128 * @throws RemoteException the remote exception
129 * @throws _SQIFaultException the _ sqi fault exception
131 public static void setResultSetFormat(String sessionId, String format)
132 throws RemoteException, _SQIFaultException {
133 SetResultsFormat resultsSetFormat = new SetResultsFormat();
134 resultsSetFormat.setResultsFormat(format);
135 resultsSetFormat.setTargetSessionID(sessionId);
136 sqiStub.setResultsFormat(resultsSetFormat);
142 * @param sessionId the session id
143 * @param query the query
144 * @param startResult the start result
148 * @throws RemoteException the remote exception
149 * @throws _SQIFaultException the _ sqi fault exception
151 public static String query(String sessionId, String query, int startResult)
152 throws RemoteException, _SQIFaultException {
153 SynchronousQuery syncQuery = new SynchronousQuery();
154 syncQuery.setQueryStatement(query);
155 syncQuery.setStartResult(startResult);
156 syncQuery.setTargetSessionID(sessionId);
158 SynchronousQueryResponse result = sqiStub.synchronousQuery(syncQuery);
159 String synchronousQueryReturn = result.getSynchronousQueryReturn();
160 return synchronousQueryReturn;
167 * @param sessionId the session id
168 * @param query the query
172 * @throws RemoteException the remote exception
173 * @throws _SQIFaultException the _ sqi fault exception
175 public static int countResults(String sessionId, String query)
176 throws RemoteException, _SQIFaultException {
177 GetTotalResultsCount getTotalResultsCount = new GetTotalResultsCount();
178 getTotalResultsCount.setQueryStatement(query);
179 getTotalResultsCount.setTargetSessionID(sessionId);
180 GetTotalResultsCountResponse countResponse = sqiStub
181 .getTotalResultsCount(getTotalResultsCount);
182 return countResponse.getGetTotalResultsCountReturn();
186 * Parses the results.
188 * @param results the results
190 * @return the list< element>
192 public static List<Element> parseResults(String results) {
194 SAXBuilder builder = new SAXBuilder();
195 Reader in = new StringReader(results);
196 org.jdom.Document doc;
197 doc = builder.build(in);
198 List<Element> mdInstances = doc.getRootElement().getChildren();
200 } catch (JDOMException e) {
201 // TODO Auto-generated catch block
203 } catch (IOException e) {
204 // TODO Auto-generated catch block
214 * @param mdInstances the md instances
216 public static void printIds(List mdInstances) {
217 Element lom, general, identifier, entry;
218 Namespace ns = Namespace.getNamespace("http://ltsc.ieee.org/xsd/LOM");
219 for (int i = 0; i < mdInstances.size(); i++) {
220 lom = (Element) mdInstances.get(i);
221 general = lom.getChild("general", ns);
222 identifier = general.getChild("identifier", ns);
223 entry = identifier.getChild("entry", ns);
224 System.out.println(entry.getText());
231 * @param query the query
232 * @param resultSize the result size
233 * @param startResult the start result
235 * @return the set< ressource item>
237 public Set<RessourceItem> query(String query, Integer resultSize,
238 Integer startResult) {
240 String sessionId, format, language;
242 sessionId = createAnonymousSession("http://" + host + "/" + service
243 + "/services/SqiSessionManagement");
244 sqiStub = new SqiTargetStub("http://" + host + "/" + service
245 + "/services/SqiTarget");
247 // Set the Query language
248 // language = "vslq";
249 // language = "plql0";
251 // language = "plql2";
252 setQueryLanguage(sessionId, language);
254 // Set the Result set size
256 if (resultSize != null) {
257 setResultSetSize(sessionId, resultSize);
259 // Set the result set format
263 setResultSetFormat(sessionId, format);
266 if (startResult == null) {
270 Set<RessourceItem> sri = new HashSet<RessourceItem>();
272 String result = query(sessionId, query, startResult);
273 // System.out.println(result);
275 // Get the total count of results
276 // int count = countResults(sessionId, query);
277 // System.out.println(count);
278 Namespace ns = Namespace
279 .getNamespace("http://ltsc.ieee.org/xsd/LOM");
280 List<Element> mdInstances = parseResults(result);
281 for (Element e : mdInstances) {
283 Element g = e.getChild("general", ns);
284 Element title = g.getChild("title", ns);
286 Element t = e.getChild("technical", ns);
287 Element loc = t.getChild("location", ns);
289 Set<String> uu = new HashSet<String>();
291 Element metaMetadata = e.getChild("metaMetadata", ns);
292 List<Element> contributes = metaMetadata.getChildren(
295 for (Element cont : contributes) {
296 List<Element> le = cont.getChildren("entity", ns);
297 for (Element ent : le) {
298 String vcard = ent.getValue();
299 String[] lines = vcard.split("\n");
300 for (String line : lines) {
301 String[] v = line.split(":");
303 if (v[0].equals("FN")) {
312 } catch (Exception ee) {
313 ee.printStackTrace();
316 RessourceFile r = new RessourceFile();
318 r.setName(title.getValue());
319 r.setPath(loc.getValue());
328 } catch (AxisFault e) {
329 // TODO Auto-generated catch block
331 } catch (RemoteException e) {
332 // TODO Auto-generated catch block
334 } catch (_SQIFaultException e) {
335 // TODO Auto-generated catch block
338 return new HashSet<RessourceItem>();
347 public String getHost() {
354 * @param host the new host
356 public void setHost(String host) {
363 * @return the service
365 public String getService() {
372 * @param service the new service
374 public void setService(String service) {
375 this.service = service;