/* Copyright (c) 2008, Centre for Educational Technology, Tallinn University Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ package ee.tlu.htk.waramu.fire; import ee.tlu.htk.waramu.base.BaseType; import ee.tlu.htk.waramu.db.DataLoaderLocal; import ee.tlu.htk.waramu.server.TypesBeanLocal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.eun.fire.sqi.FireSQISource; import org.eun.fire.sqi.FireSQITarget; import org.eun.fire.sqi.SQIFault; import net.sourceforge.limbs.client.utils.UUIDGenerator; /** * * @author vahur */ @WebService public class LocalSQI implements FireSQITarget { private static final String S2QL = "http://fire.eun.org/xsd/s2ql-2.0"; private static final String PLQL = "http://www.prolearn-project.org/PLQL/l2"; private static final String LOM = "http://fire.eun.org/xsd/strictLreResults-1.0"; private static final String PLF = "http://www.prolearn-project.org/PLRF/0/lom"; private Map sessionInfos = null; /** Creates a new instance of LocalSQI */ public LocalSQI() { sessionInfos = new HashMap(); } @WebMethod() public synchronized void setQueryLanguage(@WebParam(name="sessionID") String sessionId, @WebParam(name="queryLanguageID")String queryLanguageID) throws SQIFault { checkSession(sessionId); checkQueryLanguage(queryLanguageID); SQISession session = (SQISession) sessionInfos.get(sessionId); session.setQueryLanguage(queryLanguageID); } @WebMethod public synchronized void setResultsFormat(@WebParam(name="sessionID") String targetSessionID, @WebParam(name="resultsFormat") String resultsFormat) throws SQIFault { checkSession(targetSessionID); checkResultFormat(resultsFormat); SQISession session = (SQISession)sessionInfos.get(targetSessionID); session.setResultFormat(resultsFormat); } public synchronized void setSourceLocation(String targetSessionID, FireSQISource sourceLocation) throws SQIFault { checkSession(targetSessionID); SQISession session = (SQISession) sessionInfos.get(targetSessionID); session.setSourceLocation(sourceLocation.toString()); } public synchronized void asynchronousQuery(String targetSessionID, String queryStatement, String queryID) throws SQIFault { throw new SQIFault(SQIFault.QUERY_MODE_NOT_SUPPORTED,""); } @WebMethod public synchronized String synchronousQuery(@WebParam(name="sessionID")String targetSessionID, @WebParam(name="queryStatement") String queryStatement) throws SQIFault { checkSession(targetSessionID); SQISession session = (SQISession) sessionInfos.get(targetSessionID); ParserBase parser = null; TypesBeanLocal typesBean = lookupTypesBean(); List samples = typesBean.getSamples(); if ( session.getQueryLanguage().equals(S2QL)) { parser = new S2QLParser(samples); } if ( session.getQueryLanguage().equals(PLQL) ) { parser = new PLQLParser(samples); } if ( parser == null ) { throw new SQIFault(SQIFault.QUERY_LANGUAGE_NOT_SUPPORTED, session.getQueryLanguage()); } List res = new ArrayList(); DataLoaderLocal dloader = lookupDataLoaderBean(); String[] sql = parser.parse(queryStatement); for ( String s: sql) { List midres = dloader.customQuery(s); res.addAll(midres); } String ql = ""; if ( session.getResultFormat().equals(LOM) ) { ql = "lom"; } StringBuffer formatted = new StringBuffer(); for ( BaseType obj: res) { formatted.append(obj.getTransformedValue(ql)); } return formatted.toString(); } /* * Session management */ @WebMethod public synchronized String createAnonymousSession() throws SQIFault { String key = UUIDGenerator.getInstance().genUUID("session"); SQISession session = new SQISession(); session.setKey(key); sessionInfos.put(key, session); return key; } public synchronized String createSession(String userID, String password) throws SQIFault { return createAnonymousSession(); } @WebMethod public synchronized void destroySession(@WebParam(name="sessionID") String sessionID) throws SQIFault { checkSession(sessionID); sessionInfos.remove(sessionID); } /* * Helper methods */ private void checkSession(String targetSessionID) throws SQIFault { if( !sessionInfos.containsKey(targetSessionID) ) { throw new SQIFault( SQIFault.NO_SUCH_SESSION, targetSessionID ) ; } } private void checkQueryLanguage(String queryLanguageID) throws SQIFault { if ( queryLanguageID.equals(S2QL) || queryLanguageID.equals(PLQL) ) return; throw new SQIFault( SQIFault.QUERY_LANGUAGE_NOT_SUPPORTED, queryLanguageID ) ; } private void checkResultFormat(String resultsFormat) throws SQIFault { if ( resultsFormat.equals(LOM) || resultsFormat.equals(PLF) ) return; throw new SQIFault(SQIFault.RESULTS_FORMAT_NOT_SUPPORTED, resultsFormat); } private DataLoaderLocal lookupDataLoaderBean() { try { Context c = new InitialContext(); return (DataLoaderLocal) c.lookup("java:comp/env/DataLoaderBean"); } catch (NamingException ne) { java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne); throw new RuntimeException(ne); } } private TypesBeanLocal lookupTypesBean() { try { Context c = new InitialContext(); return (TypesBeanLocal) c.lookup("java:comp/env/TypesBean"); } catch (NamingException ne) { java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne); throw new RuntimeException(ne); } } }