/* 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 java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * @author vahur */ public class S2QLParser extends ParserBase { private List sampleObjects; Pattern kwpat = Pattern.compile("(.*?)"); Pattern lpat = Pattern.compile("(.*?)"); Pattern minpat = Pattern.compile("(.*?)"); Pattern maxpat = Pattern.compile("(.*?)"); public S2QLParser(List samples) { this.sampleObjects = samples; } /* QUERY 1 * en et key1 key2 1 4 * * QUERY 2 * en et test kala * * QUERY 3 * en et key1 key2 1 4 */ @Override public String[] parse(String query) { /* * eg. minAge -> fieldName -> columnName * -> tableName * */ List res = new ArrayList(); for (BaseType obj : sampleObjects) { List joined = new ArrayList(); String q = "SELECT INTERNAL_ID FROM "; String joins = ""; String wheres = " WHERE 1=1 "; HashMap mapp = obj.getQLMapping("s2ql"); if (query.contains("keyword")) { // Keywords HashMap hm = (HashMap) mapp.get("keyword"); if (hm == null) { // this object doesn't have required field mapping. skip this object. continue; } Matcher kwmat = kwpat.matcher(query); String tablename = (String) hm.get("tableName"); String column = (String) hm.get("column"); while (kwmat.find()) { if (!joined.contains(tablename)) { joins += " JOIN " + tablename + " USING (INTERNAL_ID) "; joined.add(tablename); } wheres += " AND " + column + " LIKE '%" + kwmat.group(1).trim() + "%' "; } } if (query.contains("language")) { // language HashMap hm = (HashMap) mapp.get("language"); if (hm == null) { continue; } Matcher lmat = lpat.matcher(query); String tablename = (String) hm.get("tableName"); String column = (String) hm.get("column"); while (lmat.find()) { if (!joined.contains(tablename)) { joins += " JOIN " + tablename + " USING (INTERNAL_ID) "; joined.add(tablename); } wheres += " AND " + column + " = '"+lmat.group(1).trim() + "'"; } } if ( query.contains("ageRange")) { // age range // min HashMap hm = (HashMap) mapp.get("minAge"); if ( hm == null) { continue; } Matcher minmat = minpat.matcher(query); String tablename = (String) hm.get("tableName"); String column = (String) hm.get("column"); while ( minmat.find()) { if (!joined.contains(tablename)) { joins += " JOIN " + tablename + " USING (INTERNAL_ID) "; joined.add(tablename); } wheres += " AND "+column+ " > " + minmat.group(1).trim()+" "; } // max hm = (HashMap) mapp.get("maxAge"); if ( hm == null) { continue; } Matcher maxmat = maxpat.matcher(query); tablename = (String) hm.get("tableName"); column = (String) hm.get("column"); while ( maxmat.find()) { if (!joined.contains(tablename)) { joins += " JOIN " + tablename + " USING (INTERNAL_ID) "; joined.add(tablename); } wheres += " AND "+column+ " < " + maxmat.group(1).trim()+" "; } } q = q + joins + wheres; res.add(q); } return res.toArray(new String[]{}); } }