Merged support to spql 2.0

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/speciesdiscovery@74255 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Federico De Faveri 2013-04-30 12:30:18 +00:00
parent a3b93375a9
commit 55bc2edf0e
3 changed files with 56 additions and 79 deletions

View File

@ -62,7 +62,7 @@
<dependency>
<groupId>org.gcube.data.spd</groupId>
<artifactId>spql-parser</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>

View File

@ -15,32 +15,35 @@ import org.gcube.portlets.user.speciesdiscovery.shared.SearchType;
public class QueryBuilder {
protected static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("d/MM/yyyy");
protected static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
public static String buildQuery(String searchTerm, SearchType searchType, SearchFilters searchFilters){
StringBuilder query = new StringBuilder();
query.append("SEARCH BY ");
//ADD search type;
switch (searchType) {
case BY_SCIENTIFIC_NAME: query.append("SN "); break;
case BY_COMMON_NAME: query.append("CN "); break;
default: break;
}
//ADD search term;
query.append('\'');
query.append(searchTerm);
query.append('\'');
//ADD search type;
switch (searchType) {
case BY_SCIENTIFIC_NAME: query.append(" as ScientificName "); break;
case BY_COMMON_NAME: query.append(" as CommonName "); break;
}
query.append("' ");
//ADD data sources;
if (searchFilters.getListDataSources()!=null && searchFilters.getListDataSources().size()>0) {
query.append(" in ");
query.append("in ");
Iterator<DataSourceModel> dsIterator = searchFilters.getListDataSources().iterator();
while(dsIterator.hasNext()) {
DataSourceModel ds = dsIterator.next();
query.append(" ");
query.append(ds.getId());
if (dsIterator.hasNext()) query.append(", ");
else query.append(" ");
@ -52,7 +55,7 @@ public class QueryBuilder {
//ADD filters
if(properties.size()>0){
query.append(" where ");
query.append("WHERE ");
Iterator<Property> pIterator = properties.iterator();
while (pIterator.hasNext()) {
@ -67,17 +70,14 @@ public class QueryBuilder {
}
//ADD return type
query.append(" return ");
query.append("RETURN ");
if (searchFilters.getResultType()!=null) {
switch (searchFilters.getResultType()) {
// case RESULTITEM: query.append("*"); break; //Modify by Francesco
case RESULTITEM: query.append("* having Occurrence"); break;
case TAXONOMYITEM: query.append("Taxon"); break;
case RESULTITEM: query.append("PRODUCT HAVING xpath(\"//product[type='OCCURRENCE' and counter>0]\")"); break;
case TAXONOMYITEM: query.append("TAXON"); break;
}
} else {
// query.append("*"); Modify by Francesco
query.append("* having Occurrence");
query.append("PRODUCT HAVING xpath(\"//product[type='OCCURRENCE' and counter>0]\")");
}
return query.toString();
@ -88,19 +88,24 @@ public class QueryBuilder {
String statement = null;
Properties prop = property.getProp();
if(prop.equals(Properties.CoordinateTo)){
org.gcube.data.spd.model.Coordinate coordinate = (org.gcube.data.spd.model.Coordinate)property.getValue();
statement= "lowerBound IS " + coordinate.getLatitude() + " , "+coordinate.getLongitude();
}else if(prop.equals(Properties.CoordinateFrom)){
org.gcube.data.spd.model.Coordinate coordinate = (org.gcube.data.spd.model.Coordinate)property.getValue();
statement= "upperBound IS " + coordinate.getLatitude() + " , "+coordinate.getLongitude();
}else if(prop.equals(Properties.DateFrom)){
Calendar calendar = (Calendar) property.getValue();
statement= "fromDate IS " + DATE_FORMAT.format(calendar.getTime());
}else if(prop.equals(Properties.DateTo)){
Calendar calendar = (Calendar) property.getValue();
statement= "toDate IS " + DATE_FORMAT.format(calendar.getTime());
switch (prop) {
case CoordinateTo: {
org.gcube.data.spd.model.Coordinate coordinate = (org.gcube.data.spd.model.Coordinate)property.getValue();
statement= "coordinate <= " + coordinate.getLatitude() + " , "+coordinate.getLongitude();
} break;
case CoordinateFrom: {
org.gcube.data.spd.model.Coordinate coordinate = (org.gcube.data.spd.model.Coordinate)property.getValue();
statement= "coordinate >= " + coordinate.getLatitude() + " , "+coordinate.getLongitude();
} break;
case DateFrom: {
Calendar calendar = (Calendar) property.getValue();
statement= "eventDate >= '" + DATE_FORMAT.format(calendar.getTime())+"'";
} break;
case DateTo: {
Calendar calendar = (Calendar) property.getValue();
statement= "eventDate <= '" + DATE_FORMAT.format(calendar.getTime())+"'";
}
}
return statement;

View File

@ -4,15 +4,11 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.apache.log4j.Logger;
import org.gcube.dataaccess.spql.SPQLLexer;
import org.gcube.dataaccess.spql.SPQLParser;
import org.gcube.dataaccess.spql.model.ParsingResult;
import org.gcube.dataaccess.spql.model.ReturnType;
import org.gcube.dataaccess.spql.model.TermType;
import org.gcube.dataaccess.spql.SPQLQueryParser;
import org.gcube.dataaccess.spql.model.Query;
import org.gcube.dataaccess.spql.model.Term;
import org.gcube.dataaccess.spql.model.ret.ReturnType;
import org.gcube.portlets.user.speciesdiscovery.shared.SearchByQueryParameter;
import org.gcube.portlets.user.speciesdiscovery.shared.SearchFilters;
import org.gcube.portlets.user.speciesdiscovery.shared.SearchResultType;
@ -29,56 +25,32 @@ public class QueryUtil {
protected static Logger logger = Logger.getLogger(QueryUtil.class);
public static SearchByQueryParameter getQueryResultType(String query) throws SearchServiceException
public static SearchByQueryParameter getQueryResultType(String queryText) throws SearchServiceException
{
try {
SPQLLexer lexer = new SPQLLexer(new ANTLRStringStream(query));
CommonTokenStream tokens = new CommonTokenStream(lexer);
SPQLParser parser = new SPQLParser(tokens);
ParsingResult result = parser.query();
Query query = SPQLQueryParser.parse(queryText);
Map<SearchType, List<String>> terms = new HashMap<SearchType, List<String>>();
for (TermType key : result.getTerms().keySet()) {
if(key.equals(TermType.COMMON_NAME)){
terms.put(SearchType.BY_COMMON_NAME, terms.get(key));
}else if(key.equals(TermType.SCIENTIFIC_NAME)){
terms.put(SearchType.BY_COMMON_NAME, terms.get(key));
for (Term term:query.getTerms()) {
switch (term.getType()) {
case COMMON_NAME: terms.put(SearchType.BY_COMMON_NAME, term.getWords()); break;
case SCIENTIFIC_NAME: terms.put(SearchType.BY_SCIENTIFIC_NAME, term.getWords()); break;
}
}
ReturnType type = result.getReturnExpression().getType();
ReturnType returnType = query.getReturnType();
SearchResultType searchResultType = SearchResultType.SPECIES_PRODUCT;
SearchResultType searchResultType = null;
if (type == null) searchResultType = SearchResultType.SPECIES_PRODUCT;
switch (type) {
case ALL:{
searchResultType = SearchResultType.SPECIES_PRODUCT;
break;
}
case OCCURRENCE:{
searchResultType = SearchResultType.OCCURRENCE_POINT;
break;
}
case TAXON: {
searchResultType = SearchResultType.TAXONOMY_ITEM;
break;
}
default: {
logger.error("Unknow return type: "+type + " setting default "+SearchResultType.SPECIES_PRODUCT);
searchResultType = SearchResultType.SPECIES_PRODUCT;
}
switch (returnType) {
case OCCURRENCE: searchResultType = SearchResultType.OCCURRENCE_POINT; break;
case PRODUCT: searchResultType = SearchResultType.SPECIES_PRODUCT; break;
case TAXON: searchResultType = SearchResultType.TAXONOMY_ITEM; break;
}
return new SearchByQueryParameter(terms, searchResultType);
} catch (RecognitionException e) {
} catch (Exception e) {
logger.warn("Error parsing the user query", e);
throw new SearchServiceException("Wrong query: "+e.getMessage());
}