--- /dev/null
+/************************************************************************************
+ *
+ * Copyright (c) 2008 Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel.
+ *
+ * Ce logiciel est diffuse sous les termes et conditions de la licence CECILL.
+ * Voir le fichier joint Licence_CeCILL_V2-fr.txt pour plus de details.
+ *
+ ************************************************************************************/
+package com.pentila.evalcomp.ariadne;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.rmi.RemoteException;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.axis2.AxisFault;
+import org.jdom.Element;
+import org.jdom.JDOMException;
+import org.jdom.Namespace;
+import org.jdom.input.SAXBuilder;
+
+import be.cenorm.www.CreateAnonymousSession;
+import be.cenorm.www.CreateAnonymousSessionResponse;
+import be.cenorm.www.GetTotalResultsCount;
+import be.cenorm.www.GetTotalResultsCountResponse;
+import be.cenorm.www.SetQueryLanguage;
+import be.cenorm.www.SetResultsFormat;
+import be.cenorm.www.SetResultsSetSize;
+import be.cenorm.www.SqiSessionManagementStub;
+import be.cenorm.www.SqiTargetStub;
+import be.cenorm.www.SynchronousQuery;
+import be.cenorm.www.SynchronousQueryResponse;
+import be.cenorm.www._SQIFaultException;
+
+import com.pentila.evalcomp.domain.transit.RessourceFile;
+import com.pentila.evalcomp.domain.transit.RessourceItem;
+
+// TODO: Auto-generated Javadoc
+/**
+ * The Class AriadneImpl.
+ */
+
+/**
+ * @author gonzalo
+ *
+ */
+public class AriadneImpl {
+
+ /** The host. */
+ private String host = "ariadne.cs.kuleuven.be";
+
+ /** The service. */
+ private String service = "ariadne-dev-ws";
+
+ /** The sqi session stub. */
+ private static SqiSessionManagementStub sqiSessionStub;
+
+ /** The sqi stub. */
+ private static SqiTargetStub sqiStub;
+
+ /**
+ * Creates the anonymous session.
+ *
+ * @param target the target
+ *
+ * @return the string
+ */
+ public static String createAnonymousSession(String target) {
+ try {
+ sqiSessionStub = new SqiSessionManagementStub(target);
+ CreateAnonymousSession createASession = new CreateAnonymousSession();
+ CreateAnonymousSessionResponse sessionResponse = sqiSessionStub
+ .createAnonymousSession(createASession);
+ String sessionId = sessionResponse
+ .getCreateAnonymousSessionReturn();
+ return sessionId;
+ } catch (Exception e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ return null;
+ }
+ }
+
+ /**
+ * Sets the query language.
+ *
+ * @param sessionId the session id
+ * @param language the language
+ *
+ * @throws RemoteException the remote exception
+ * @throws _SQIFaultException the _ sqi fault exception
+ */
+ public static void setQueryLanguage(String sessionId, String language)
+ throws RemoteException, _SQIFaultException {
+ SetQueryLanguage queryLanguage = new SetQueryLanguage();
+ queryLanguage.setQueryLanguageID(language);
+ queryLanguage.setTargetSessionID(sessionId);
+ sqiStub.setQueryLanguage(queryLanguage);
+
+ }
+
+ /**
+ * Sets the result set size.
+ *
+ * @param sessionId the session id
+ * @param resultSize the result size
+ *
+ * @throws RemoteException the remote exception
+ * @throws _SQIFaultException the _ sqi fault exception
+ */
+ public static void setResultSetSize(String sessionId, int resultSize)
+ throws RemoteException, _SQIFaultException {
+ SetResultsSetSize resultsSetSize = new SetResultsSetSize();
+ resultsSetSize.setResultsSetSize(resultSize);
+ resultsSetSize.setTargetSessionID(sessionId);
+ sqiStub.setResultsSetSize(resultsSetSize);
+ }
+
+ /**
+ * Sets the result set format.
+ *
+ * @param sessionId the session id
+ * @param format the format
+ *
+ * @throws RemoteException the remote exception
+ * @throws _SQIFaultException the _ sqi fault exception
+ */
+ public static void setResultSetFormat(String sessionId, String format)
+ throws RemoteException, _SQIFaultException {
+ SetResultsFormat resultsSetFormat = new SetResultsFormat();
+ resultsSetFormat.setResultsFormat(format);
+ resultsSetFormat.setTargetSessionID(sessionId);
+ sqiStub.setResultsFormat(resultsSetFormat);
+ }
+
+ /**
+ * Query.
+ *
+ * @param sessionId the session id
+ * @param query the query
+ * @param startResult the start result
+ *
+ * @return the string
+ *
+ * @throws RemoteException the remote exception
+ * @throws _SQIFaultException the _ sqi fault exception
+ */
+ public static String query(String sessionId, String query, int startResult)
+ throws RemoteException, _SQIFaultException {
+ SynchronousQuery syncQuery = new SynchronousQuery();
+ syncQuery.setQueryStatement(query);
+ syncQuery.setStartResult(startResult);
+ syncQuery.setTargetSessionID(sessionId);
+
+ SynchronousQueryResponse result = sqiStub.synchronousQuery(syncQuery);
+ String synchronousQueryReturn = result.getSynchronousQueryReturn();
+ return synchronousQueryReturn;
+
+ }
+
+ /**
+ * Count results.
+ *
+ * @param sessionId the session id
+ * @param query the query
+ *
+ * @return the int
+ *
+ * @throws RemoteException the remote exception
+ * @throws _SQIFaultException the _ sqi fault exception
+ */
+ public static int countResults(String sessionId, String query)
+ throws RemoteException, _SQIFaultException {
+ GetTotalResultsCount getTotalResultsCount = new GetTotalResultsCount();
+ getTotalResultsCount.setQueryStatement(query);
+ getTotalResultsCount.setTargetSessionID(sessionId);
+ GetTotalResultsCountResponse countResponse = sqiStub
+ .getTotalResultsCount(getTotalResultsCount);
+ return countResponse.getGetTotalResultsCountReturn();
+ }
+
+ /**
+ * Parses the results.
+ *
+ * @param results the results
+ *
+ * @return the list< element>
+ */
+ public static List<Element> parseResults(String results) {
+ try {
+ SAXBuilder builder = new SAXBuilder();
+ Reader in = new StringReader(results);
+ org.jdom.Document doc;
+ doc = builder.build(in);
+ List<Element> mdInstances = doc.getRootElement().getChildren();
+ return mdInstances;
+ } catch (JDOMException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return null;
+
+ }
+
+ /**
+ * Prints the ids.
+ *
+ * @param mdInstances the md instances
+ */
+ public static void printIds(List mdInstances) {
+ Element lom, general, identifier, entry;
+ Namespace ns = Namespace.getNamespace("http://ltsc.ieee.org/xsd/LOM");
+ for (int i = 0; i < mdInstances.size(); i++) {
+ lom = (Element) mdInstances.get(i);
+ general = lom.getChild("general", ns);
+ identifier = general.getChild("identifier", ns);
+ entry = identifier.getChild("entry", ns);
+ System.out.println(entry.getText());
+ }
+ }
+
+ /**
+ * Query.
+ *
+ * @param query the query
+ * @param resultSize the result size
+ * @param startResult the start result
+ *
+ * @return the set< ressource item>
+ */
+ public Set<RessourceItem> query(String query, Integer resultSize,
+ Integer startResult) {
+ try {
+ String sessionId, format, language;
+
+ sessionId = createAnonymousSession("http://" + host + "/" + service
+ + "/services/SqiSessionManagement");
+ sqiStub = new SqiTargetStub("http://" + host + "/" + service
+ + "/services/SqiTarget");
+
+ // Set the Query language
+ // language = "vslq";
+ // language = "plql0";
+ language = "plql1";
+ // language = "plql2";
+ setQueryLanguage(sessionId, language);
+
+ // Set the Result set size
+ // resultSize = 10;
+ if (resultSize != null) {
+ setResultSetSize(sessionId, resultSize);
+ }
+ // Set the result set format
+ format = "lom";
+ // format = "solr";
+ // format = "rlom";
+ setResultSetFormat(sessionId, format);
+
+ // Do the query
+ if (startResult == null) {
+ startResult = 1;
+ }
+
+ Set<RessourceItem> sri = new HashSet<RessourceItem>();
+
+ String result = query(sessionId, query, startResult);
+ // System.out.println(result);
+
+ // Get the total count of results
+ // int count = countResults(sessionId, query);
+ // System.out.println(count);
+ Namespace ns = Namespace
+ .getNamespace("http://ltsc.ieee.org/xsd/LOM");
+ List<Element> mdInstances = parseResults(result);
+ for (Element e : mdInstances) {
+
+ Element g = e.getChild("general", ns);
+ Element title = g.getChild("title", ns);
+
+ Element t = e.getChild("technical", ns);
+ Element loc = t.getChild("location", ns);
+
+ Set<String> uu = new HashSet<String>();
+ try {
+ Element metaMetadata = e.getChild("metaMetadata", ns);
+ List<Element> contributes = metaMetadata.getChildren(
+ "contribute", ns);
+
+ for (Element cont : contributes) {
+ List<Element> le = cont.getChildren("entity", ns);
+ for (Element ent : le) {
+ String vcard = ent.getValue();
+ String[] lines = vcard.split("\n");
+ for (String line : lines) {
+ String[] v = line.split(":");
+ if (v.length > 1) {
+ if (v[0].equals("FN")) {
+ uu.add(v[1]);
+ }
+ }
+
+ }
+
+ }
+ }
+ } catch (Exception ee) {
+ ee.printStackTrace();
+ }
+
+ RessourceFile r = new RessourceFile();
+
+ r.setName(title.getValue());
+ r.setPath(loc.getValue());
+ r.setOwners(uu);
+
+ sri.add(r);
+
+ }
+
+ return sri;
+
+ } catch (AxisFault e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (RemoteException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (_SQIFaultException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return new HashSet<RessourceItem>();
+
+ }
+
+ /**
+ * Gets the host.
+ *
+ * @return the host
+ */
+ public String getHost() {
+ return host;
+ }
+
+ /**
+ * Sets the host.
+ *
+ * @param host the new host
+ */
+ public void setHost(String host) {
+ this.host = host;
+ }
+
+ /**
+ * Gets the service.
+ *
+ * @return the service
+ */
+ public String getService() {
+ return service;
+ }
+
+ /**
+ * Sets the service.
+ *
+ * @param service the new service
+ */
+ public void setService(String service) {
+ this.service = service;
+ }
+
+}