package de.uni_tuebingen.ub.ixTheo.timeAspectRangeSearch;


import org.apache.lucene.search.Query;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.SyntaxError;


public class TimeAspectRangeParser extends QParser {
    private final static String QUERY_SEPARATOR = " ";
    private final static String DB_FIELD_SEPARATOR = ",";
    //private static Logger logger = LoggerFactory.getLogger(TimeAspectRangeParser.class);

    /**
     * @param qstr        The part of the query string specific to this parser
     * @param localParams The set of parameters that are specific to this QParser.
     *                    See http://wiki.apache.org/solr/LocalParams
     * @param params      The rest of the {@link SolrParams}
     * @param req         The original {@link SolrQueryRequest}.
     */
    public TimeAspectRangeParser(final String qstr, final SolrParams localParams, final SolrParams params, final SolrQueryRequest req) {
        super(qstr, localParams, params, req);
    }

    static TimeAspectRange[] getRangesFromDatabaseField(final String db_field) {
        return TimeAspectRange.getRanges(db_field, DB_FIELD_SEPARATOR);
    }

    private TimeAspectRange[] getRangesFromQuery() {
        return TimeAspectRange.getRanges(getFieldsFromQuery());
    }

    private String[] getFieldsFromQuery() {
        return qstr.split(QUERY_SEPARATOR);
    }
    
    @Override
    public Query parse() throws SyntaxError {
        
        String queryString = "time_aspect_ranges:*";
        final TimeAspectRange[] ranges = getRangesFromQuery();
        
        //if more than one time range become relevant, minimum lower and 
        //maximum upper need to be implemented for pre-selection
        if (ranges.length != 1) {
            throw new SyntaxError("Unsupported format");
        }
        TimeAspectRange range = ranges[0];
        String lower = String.format("%08d", range.getLower());
        String upper = String.format("%08d", range.getUpper());
        queryString = "time_range_end:[" + lower + " TO " + "*" + "] AND time_range_start:[" + "*" + " TO " + upper + "]";
        final QParser parser = getParser(queryString, "multiLanguageQueryParser", getReq());        
        return new TimeAspectRangeQuery(parser.parse(), ranges);
    }
}

@@ -1989,33 +1989,34 @@
 tring.format(%22%250
-8
+12
 d%22, range.getLow
@@ -2066,9 +2066,10 @@
 (%22%250
-8
+12
 d%22, 

changed format string for b.c. values