/*
 *    AbstractGS2FieldSearch.java
 *    Copyright (C) 2006 New Zealand Digital Library, http://www.nzdl.org
 *
 *    This program is free software; you can redistribute it and/or modify
 *   the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.greenstone.gsdl3.service;

// Greenstone classes
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.greenstone.gsdl3.util.FacetWrapper;
import org.greenstone.gsdl3.util.GSXML;
import org.greenstone.gsdl3.util.XMLConverter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

abstract public class AbstractGS2FieldSearch extends AbstractGS2TextSearch
{
	// extra services offered by these indexers that use fields
	public static final String FIELD_QUERY_SERVICE = "FieldQuery";
	public static final String ADVANCED_FIELD_QUERY_SERVICE = "AdvancedFieldQuery";
	public static final String RAW_QUERY_SERVICE = "RawQuery";

	// extra parameters used
	protected static final String LEVEL_PARAM = "level";
	protected static final String RANK_PARAM = "sortBy";
	protected static final String RANK_PARAM_RANK = "1";
	protected static final String RANK_PARAM_NONE = "0";
	protected static final String SIMPLE_FIELD_PARAM = "simpleField";
	protected static final String ADVANCED_FIELD_PARAM = "complexField";

	protected static final String RAW_PARAM = "rawquery";

  // param for highlighting
  protected static final String HL_DOC_OID_PARAM = "hldocOID";
	// more params for field query
	public static final String FIELD_QUERY_PARAM = "fqv";
	protected static final String FIELD_STEM_PARAM = "fqs";
	protected static final String FIELD_CASE_PARAM = "fqc";
	protected static final String FIELD_ACCENT_PARAM = "fqa";
	protected static final String FIELD_FIELD_PARAM = "fqf";
	protected static final String FIELD_COMBINE_PARAM = "fqk";
	protected static final String FIELD_COMBINE_PARAM_AND = "0";
	protected static final String FIELD_COMBINE_PARAM_OR = "1";
	protected static final String FIELD_COMBINE_PARAM_NOT = "2";

	// some stuff for config files
  // extra search types we get with field searching
  protected static final String SEARCH_TYPE_SIMPLE_FORM = "simpleform";
	protected static final String SEARCH_TYPE_ADVANCED_FORM = "advancedform";
	protected static final String SEARCH_TYPE_RAW = "raw";

	protected static final String DEFAULT_LEVEL_ELEM = "defaultLevel";
	protected static final String DEFAULT_DB_LEVEL_ELEM = "defaultDBLevel";
	protected static final String LEVEL_ELEM = "level";
	protected static final String FIELD_ATT = "field";

	protected String AND_OPERATOR = "&";
	protected String OR_OPERATOR = "|";
	protected String NOT_OPERATOR = "!";

        // what levels we support
        protected ArrayList<String> level_ids = null;
	// the default level for searching
	protected String default_level = null;
	// default level for collection db
	protected String default_db_level = null;
        // metadata for service, such as does_paging = true
        protected Element service_metadata_list = null;
	// which additional search services will we offer??
  // base class has quick and plain
	protected boolean simple_form_search = false;
	protected boolean advanced_form_search = false;
	protected boolean raw_search = false;

	static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractGS2FieldSearch.class.getName());

	/** constructor */
	public AbstractGS2FieldSearch()
	{
	  super();
	  paramDefaults.put(RANK_PARAM, RANK_PARAM_RANK);
	  paramDefaults.put(FIELD_COMBINE_PARAM, FIELD_COMBINE_PARAM_AND);

	}

	public void cleanUp()
	{
		super.cleanUp();
	}

	/** configure this service */
	public boolean configure(Element info, Element extra_info)
	{
		if (!super.configure(info, extra_info))
		{
			return false;
		}

		// set up which params to save
		this.save_params.add(LEVEL_PARAM);
		this.save_params.add(RANK_PARAM);
		this.save_params.add(RAW_PARAM);
		this.save_params.add(FIELD_QUERY_PARAM);
		this.save_params.add(FIELD_STEM_PARAM);
		this.save_params.add(FIELD_CASE_PARAM);
		this.save_params.add(FIELD_ACCENT_PARAM);
		this.save_params.add(FIELD_FIELD_PARAM);
		this.save_params.add(FIELD_COMBINE_PARAM);

                Element config_search = null;
                // could it be null?
                if (extra_info != null) {
                  config_search =(Element) GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
                }
                
                // get the list of levels from buildConfig, with displayItems from
                // extra_info (collectionConfig)

                Document owner = info.getOwnerDocument();

                level_ids = new ArrayList<String>();
                NodeList levels = info.getElementsByTagName(GSXML.LEVEL_ELEM);
			
                for (int i = 0; i < levels.getLength(); i++)
                {
                  Element lev = (Element) levels.item(i);
                  String shortname = lev.getAttribute(GSXML.SHORTNAME_ATT);
                  if (shortname.equals(""))
                  {
                    continue;
                  }
                  level_ids.add(shortname);
                  
                  String name = lev.getAttribute(GSXML.NAME_ATT);
                  Element node_extra = (config_search == null ? null : GSXML.getNamedElement(config_search, GSXML.LEVEL_ELEM, GSXML.NAME_ATT, name));
                  if (node_extra == null)
                  {
                    logger.error("haven't found extra info for level named " + name);
                    continue;
                  }

                  // get the display elements if any - displayName
                  NodeList display_names = node_extra.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
                  if (display_names != null)
                  {
                    for (int j = 0; j < display_names.getLength(); j++)
                    {
                      Element e = (Element) display_names.item(j);
                      lev.appendChild(owner.importNode(e, true));
                    }
                  }
                }// for each level
                
		// Get the default level out of <defaultLevel> (buildConfig.xml)
		Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_LEVEL_ELEM);
		if (def != null)
		{
			this.default_level = def.getAttribute(GSXML.SHORTNAME_ATT);
		}
		if (this.default_level == null || this.default_level.equals(""))
		{
                  logger.error("default level not specified!, assuming the first specified level"+level_ids.get(0));
                  this.default_level = level_ids.get(0);
		}

		// Get the default DB level
		def = (Element) GSXML.getChildByTagName(info, DEFAULT_DB_LEVEL_ELEM);
		if (def != null)
		{
			this.default_db_level = def.getAttribute(GSXML.SHORTNAME_ATT);
		}
		if (this.default_db_level == null || this.default_db_level.equals(""))
		{
			logger.error("default database level (defaultDBLevel) not specified!, assuming Sec");
			this.default_db_level = "Sec";
		}

		
		// the format info is the same for all services - it was saved by the base class
		Element format_info = (Element) format_info_map.get(QUERY_SERVICE);

		// set up the extra services which are available for this collection
		if (this.simple_form_search)
		{
			// set up short_service_info_ - for now just has id and type - name will be added in on the fly
			Element fq_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
			fq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
			fq_service.setAttribute(GSXML.NAME_ATT, FIELD_QUERY_SERVICE);
			this.short_service_info.appendChild(fq_service);

			if (format_info != null)
			{
				this.format_info_map.put(FIELD_QUERY_SERVICE, format_info);
			}
		}

		if (this.advanced_form_search)
		{
			Element afq_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
			afq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
			afq_service.setAttribute(GSXML.NAME_ATT, ADVANCED_FIELD_QUERY_SERVICE);
			this.short_service_info.appendChild(afq_service);

			if (format_info != null)
			{
				this.format_info_map.put(ADVANCED_FIELD_QUERY_SERVICE, format_info);
			}
		}

		if (this.raw_search)
		{
			Element rq_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
			rq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
			rq_service.setAttribute(GSXML.NAME_ATT, RAW_QUERY_SERVICE);
			this.short_service_info.appendChild(rq_service);

			if (format_info != null)
			{
				this.format_info_map.put(RAW_QUERY_SERVICE, format_info);
			}
		}

		return true;
	}

  // todo, in future may need to pass in the element if we have more config info
  protected void saveCustomSearchType(String type_name, boolean enabled) {
    if (enabled) {
      if (type_name.equals(SEARCH_TYPE_SIMPLE_FORM))
      {
        this.simple_form_search = true;
      }
      else if (type_name.equals(SEARCH_TYPE_ADVANCED_FORM))
      {
        this.advanced_form_search = true;
      }
      else if (type_name.equals(SEARCH_TYPE_RAW))
      {
        this.raw_search = true;
      }
    }
  }
  // if field case/stem/accent defaults are not specified explicitly, we should use case/stem/accent defaults
  protected void getSearchParamDefaults(Element search_elem) {
    
    boolean found_f_stem = false;
    boolean found_f_case = false;
    boolean found_f_accent = false;
    NodeList param_defaults_list = search_elem.getElementsByTagName(GSXML.PARAM_DEFAULT_ELEM); //GSXML.getChildrenByTagName(search_elem, GSXML.PARAM_DEFAULT_ELEM);
    for (int i=0; i<param_defaults_list.getLength(); i++) {
      Element paramdef = (Element)param_defaults_list.item(i);
      String name = paramdef.getAttribute(GSXML.NAME_ATT);
      String val = paramdef.getAttribute(GSXML.VALUE_ATT);
      if (!name.equals("") && !val.equals("")) {
	paramDefaults.put(name, val);
	if (name.equals(FIELD_STEM_PARAM)) {
	  found_f_stem = true;
	} else if (name.equals(FIELD_CASE_PARAM)) {
	  found_f_case = true;
	} else if (name.equals(FIELD_ACCENT_PARAM)) {
	  found_f_accent = true;
	}
      }
    }
    if (!found_f_stem) {
      paramDefaults.put (FIELD_STEM_PARAM, paramDefaults.get(STEM_PARAM));
    }
    if (!found_f_case) {
      paramDefaults.put (FIELD_CASE_PARAM, paramDefaults.get(CASE_PARAM));
    }
    if (!found_f_accent) {
      paramDefaults.put (FIELD_ACCENT_PARAM, paramDefaults.get(ACCENT_PARAM));
    }

    
  }
  
  /** set up our query params */
  protected void addQueryParams(String service_id, Element param_list, String lang)
  {
    if (service_id.equals(TEXT_QUERY_SERVICE)) {
      // TextQuery needs a level param
      createParameter(LEVEL_PARAM, param_list, lang);
    }
    if (service_id.equals(TEXT_QUERY_SERVICE) || service_id.equals(QUICK_QUERY_SERVICE)) {
      // apart from the above, we just use the same params as super class
      super.addQueryParams(service_id, param_list, lang);
      return;
    }
    
    // raw query has limited params
    if (service_id.equals(RAW_QUERY_SERVICE))
    {
      if (does_chunking)
      {
        createParameter(MAXDOCS_PARAM, param_list, lang);
      }
      if (does_paging)
      {
        createParameter(HITS_PER_PAGE_PARAM, param_list, lang);
        createParameter(START_PAGE_PARAM, param_list, lang);
      }
      createParameter(RAW_PARAM, param_list, lang);
      return;
    }

    // FIELD_QUERY and ADVANCED_FIELD_QUERY 
    if (!default_index_subcollection.equals(""))
    {
      createParameter(INDEX_SUBCOLLECTION_PARAM, param_list, lang);
    }
    if (!default_index_language.equals(""))
    {
      createParameter(INDEX_LANGUAGE_PARAM, param_list, lang);
    }
    createParameter(LEVEL_PARAM, param_list, lang);
    createParameter(RANK_PARAM, param_list, lang);
    if (does_chunking) {
      createParameter(MAXDOCS_PARAM, param_list, lang);
    }
    if (does_paging)
    {
      createParameter(HITS_PER_PAGE_PARAM, param_list, lang);
      createParameter(START_PAGE_PARAM, param_list, lang);
    }
    
    // create a multi param for the fields etc
    // text box, field
    Document doc = param_list.getOwnerDocument();
    Element multiparam = null;
    Element param = null;
    String multi_param_name = SIMPLE_FIELD_PARAM;
    if (service_id.equals(ADVANCED_FIELD_QUERY_SERVICE)) {
      multi_param_name = ADVANCED_FIELD_PARAM;
    }
    multiparam = GSXML.createParameterDescription(doc, multi_param_name, "", GSXML.PARAM_TYPE_MULTI, null, null, null);
    multiparam.setAttribute("occurs", "4");
    param_list.appendChild(multiparam);
    
    // the components
    if (service_id.equals(FIELD_QUERY_SERVICE)) {
      //simple form just has value and field
      createParameter(FIELD_QUERY_PARAM, multiparam, lang);
      createParameter(FIELD_FIELD_PARAM, multiparam, lang);
    } else {
      // adv field have combiners, stem case etc
      createParameter(FIELD_COMBINE_PARAM, multiparam, lang);
      createParameter(FIELD_QUERY_PARAM, multiparam, lang);
      if (this.does_case)
      {
        createParameter(FIELD_CASE_PARAM, multiparam, lang);
      }
      if (this.does_stem)
      {
        createParameter(FIELD_STEM_PARAM, multiparam, lang);
      }
      if (this.does_accent)
      {
        createParameter(FIELD_ACCENT_PARAM, multiparam, lang);
      }
      createParameter(FIELD_FIELD_PARAM, multiparam, lang);
    }
  }
  

	/** create a param and add to the list */
	protected void createParameter(String name, Element param_list, String lang)
	{
	  Document doc = param_list.getOwnerDocument();
		Element param = null;
		String param_default = paramDefaults.get(name);
		if (name.equals(LEVEL_PARAM))
		{
			ArrayList<String> level_names = new ArrayList<String>();
			getLevelData(level_names, lang);
			if (level_ids.size() > 1)
			{
				param = GSXML.createParameterDescription2(doc, LEVEL_PARAM, getTextString("param." + LEVEL_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, this.default_level, level_ids, level_names);
			}
			else
			{
				// we need to set the level, but hidden, in case there is an invalid level saved
				param = GSXML.createParameterDescription(doc, LEVEL_PARAM, "", GSXML.PARAM_TYPE_INVISIBLE, this.default_level, null, null);
			}
		}
		else if (name.equals(RANK_PARAM))
		{
			String[] vals1 = { RANK_PARAM_RANK, RANK_PARAM_NONE };
			String[] vals1_texts = { getTextString("param." + RANK_PARAM + "." + RANK_PARAM_RANK, lang), getTextString("param." + RANK_PARAM + "." + RANK_PARAM_NONE, lang) };

			param = GSXML.createParameterDescription(doc, RANK_PARAM, getTextString("param." + RANK_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, param_default, vals1, vals1_texts);

		}
		else if (name.equals(FIELD_QUERY_PARAM))
		{
			param = GSXML.createParameterDescription(doc, FIELD_QUERY_PARAM, getTextString("param." + FIELD_QUERY_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);

		}
		else if (name.equals(FIELD_CASE_PARAM) || name.equals(FIELD_STEM_PARAM) || name.equals(FIELD_ACCENT_PARAM))
		{
			String[] bool_ops = { "0", "1" };
			String[] bool_texts = { getTextString("param.boolean.off", lang), getTextString("param.boolean.on", lang) };
			param = GSXML.createParameterDescription(doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_BOOLEAN, param_default, bool_ops, bool_texts);

		}
		else if (name.equals(FIELD_FIELD_PARAM))
		{
			ArrayList<String> fields = new ArrayList<String>();
			ArrayList<String> field_names = new ArrayList<String>();
			getIndexData(fields, field_names, lang);
			// the field list -  read from config file

			// Fix for http://trac.greenstone.org/ticket/245 "java crash, index out of bounds"
			// org.greenstone.gsdl3.service.AbstractGS2FieldSearch.createParameter(AbstractGS2FieldSearch.java:362)
			// Changed from:
			// param = GSXML.createParameterDescription2(doc, name, getTextString("param."+name, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, (String)fields.get(0), fields, field_names );
			String default_value = (fields.size() > 0) ? fields.get(0) : null;
			// don't want to access element 0 if fields.size()==0, and
			// GSXML.createParameterDescription2 checks for default_value==null condition
			param = GSXML.createParameterDescription2(doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, default_value, fields, field_names);

		}
		else if (name.equals(FIELD_COMBINE_PARAM))
		{

			String[] vals = { FIELD_COMBINE_PARAM_AND, FIELD_COMBINE_PARAM_OR, FIELD_COMBINE_PARAM_NOT };
			String[] val_texts = { getTextString("param." + FIELD_COMBINE_PARAM + "." + FIELD_COMBINE_PARAM_AND, lang), getTextString("param." + FIELD_COMBINE_PARAM + "." + FIELD_COMBINE_PARAM_OR, lang), getTextString("param." + FIELD_COMBINE_PARAM + "." + FIELD_COMBINE_PARAM_NOT, lang) };

			param = GSXML.createParameterDescription(doc, FIELD_COMBINE_PARAM, "", GSXML.PARAM_TYPE_ENUM_SINGLE, param_default, vals, val_texts);
			param.setAttribute(GSXML.PARAM_IGNORE_POS_ATT, "0");
		}

		if (param != null)
		{
			param_list.appendChild(param);
		}
		else
		{
			super.createParameter(name, param_list, lang);
		}
	}

	// should cache some of this
  protected void getLevelData(ArrayList<String> level_names, String lang)
	{
		Element level_list = (Element) GSXML.getChildByTagName(this.config_info, LEVEL_ELEM + GSXML.LIST_MODIFIER);
		for (int i=0; i<level_ids.size(); i++) {
		  Element level = GSXML.getNamedElement(level_list, GSXML.LEVEL_ELEM, GSXML.SHORTNAME_ATT, level_ids.get(i));
			String display_name = getDisplayText(level, GSXML.DISPLAY_TEXT_NAME, lang, "en");
			if (display_name.equals(""))
			{
				// we'll use the name, and look it up in the dictionary
				display_name = level.getAttribute(GSXML.NAME_ATT);
				if (display_name.equals(""))
				{
				  display_name = level_ids.get(i); // shortname
				}
				else
				{
					display_name = getTextString("level." + display_name, lang);
				}
			}
			level_names.add(display_name);
		}
	}


	// the following functions are needed so the base class can 
	// call the process+SERVICE_NAME methods

	/** process a field query */
	protected Element processFieldQuery(Element request)
	{
		return processAnyQuery(request, FIELD_QUERY_SERVICE);
	}

	/** process an advanced field query */
	protected Element processAdvancedFieldQuery(Element request)
	{
		return processAnyQuery(request, ADVANCED_FIELD_QUERY_SERVICE);
	}

	protected Element processRawQuery(Element request)
	{
		return processAnyQuery(request, RAW_QUERY_SERVICE);
	}

	/** process a query */
	protected Element processAnyQuery(Element request, String service_name)
	{
				
		String empty_query_test_param = null;
		// set up the type specific bits
		switch (service_name)
		{
                case QUICK_QUERY_SERVICE:
                case TEXT_QUERY_SERVICE:
                  empty_query_test_param = QUERY_PARAM;
                  break;
                case FIELD_QUERY_SERVICE:
                case ADVANCED_FIELD_QUERY_SERVICE:
                  empty_query_test_param = FIELD_QUERY_PARAM;
                  break;
		
		case RAW_QUERY_SERVICE:
                  empty_query_test_param = RAW_PARAM;
                  break;
		default:
                  // should never get here
                  logger.error("wrong query type!!");
                  return null;
		}

		// Create a new (empty) result message
		Document result_doc = XMLConverter.newDOM();
		Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
		result.setAttribute(GSXML.FROM_ATT, service_name);
		result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);

		// Get the parameters of the request
		Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
		if (param_list == null)
		{
			logger.error("TextQuery request had no paramList.");
			return result; // Return the empty result
		}

		// Process the request parameters
		
		HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
                // have we got highlight doc param? we'll use it later
		String hldocOID = (String) params.get(HL_DOC_OID_PARAM);

                String level;
                if (service_name.equals(QUICK_QUERY_SERVICE)) {
                  level = default_level;
                } else {
                  level = (String) params.get(LEVEL_PARAM);
                  if (level == null) {
                    level = default_level;
                  }
                }
		//System.out.println("@@@@@Extracted hldocOID is " + hldocOID);
		// Make sure a query has been specified
		String query = (String) params.get(empty_query_test_param);
		if (query == null || query.equals(""))
		{
			return result; // Return the empty result
		}

		// If a field hasn't been specified, use the default - for textQuery
		String field = (String) params.get(INDEX_PARAM);
		
		 if (field == null)
		 {
		 	field = default_index;
		 }
                 
		// indexField = field;
		// set up the appropriate query system
                 Object queryObject = setUpQueryer(params, service_name);
		if (queryObject == null)
		{			
			return result;
		}

		// if field search, create the query string
		switch (service_name)
		{
                case QUICK_QUERY_SERVICE:
		case TEXT_QUERY_SERVICE:
			query = addFieldInfo(query, field);
			break;
		case FIELD_QUERY_SERVICE:
			query = parseFieldQueryParams(params);
			break;
		case ADVANCED_FIELD_QUERY_SERVICE:
			query = parseAdvancedFieldQueryParams(params);
			break;
		}		
		
		// run the query		
		Object query_result = runQuery(queryObject, query);
		
		
		// We want highlighted text to be returned right now!
		if (hldocOID != null && does_full_field_highlighting)
		{

                  if (query_result == null ) {
                    // got no highlighted section, return empty result
                    return result;
                  }
                  Element nodeContentElement = result_doc.createElement(GSXML.NODE_CONTENT_ELEM);
                  nodeContentElement.setTextContent((String) query_result);
                  result.appendChild(nodeContentElement);
                  return result;
		}
		
			
		// build up the response

		// Create a metadata list to store information about the query results
		// should we be using metadataList? or something else?
		Element metadata_list = result_doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
		result.appendChild(metadata_list);

		// Add a metadata element specifying the number of matching documents
		long totalDocs = numDocsMatched(query_result);

		GSXML.addMetadata(metadata_list, "numDocsMatched", "" + totalDocs);
                // this metadata specified what level the results are (section/document)
                GSXML.addMetadata(metadata_list, "docLevel", level);
		// Create a document list to store the matching documents, and add them
		String[] docs = getDocIDs(query_result);
		String[] doc_ranks = getDocRanks(query_result);

		// add a metadata item to specify docs returned
		if (does_chunking) // this means we have a max docs param, and might ask for only a subset of results
		{
		  logger.error("does_chunking = true");
		        int docs_returned = docs.length;
			String maxdocs_str = (String) params.get(MAXDOCS_PARAM);
			if (maxdocs_str != null)
			{
				int maxdocs = Integer.parseInt(maxdocs_str);
				if (maxdocs > 0) { // maxdocs==-1 means return all
				  docs_returned = (maxdocs < (int) totalDocs ? maxdocs : (int) totalDocs);
				}
			}
			GSXML.addMetadata(metadata_list, "numDocsReturned", "" + docs_returned);
		}
		
		Map<String, Map<String, List<String>>> hlResults = null;
		if (does_highlight_snippets)
		{
			hlResults = getHighlightSnippets(query_result);
		}
		
		// add a metadata item to specify what actual query was done - eg if stuff was stripped out etc. and then we can use the query later, cos we don't know which parameter was the query
		GSXML.addMetadata(metadata_list, "query", query);
		if (docs.length > 0)
		{
			Element document_list = result_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
			result.appendChild(document_list);
			Element snippet_list = result_doc.createElement(GSXML.HL_SNIPPET_ELEM + GSXML.LIST_MODIFIER);
			result.appendChild(snippet_list);
			for (int d = 0; d < docs.length; d++)
			{
				String doc_id = internalNum2OID(docs[d]);
				Element doc_node = createDocNode(result_doc, doc_id, doc_ranks[d]);
				if (hlResults != null && hlResults.get(docs[d]) != null && hlResults.get(docs[d]).get(field) != null) {
					for (String snippet : hlResults.get(docs[d]).get(field)){
						//remove html tags
						snippet = snippet.replaceAll("\\<.*?>", "");
						//remove truncated tags
						snippet = snippet.replaceAll(".*>", "");
						snippet = snippet.replaceAll("\\<.*", "");
						//remove unwanted symbols at start of line
						snippet = snippet.replaceAll("^[ .,»):;-–]+", "");
						//highlighting tags transformation
						snippet = snippet.replaceAll("&lt;", "<");
						snippet = snippet.replaceAll("&gt;", ">");
						Element snippet_node = result_doc.createElement(GSXML.HL_SNIPPET_ELEM);
						snippet_node.setAttribute(GSXML.NODE_ID_ATT, doc_id);
						snippet_node.setTextContent(snippet);
						snippet_list.appendChild(snippet_node);
					}
				}
				document_list.appendChild(doc_node);
			}
		}

		// Create a term list to store the term information, and add it
		Element term_list = result_doc.createElement(GSXML.TERM_ELEM + GSXML.LIST_MODIFIER);
		result.appendChild(term_list);
		addTermInfo(term_list, params, query_result);

		if(does_faceting)
		{
		  String lang = request.getAttribute(GSXML.LANG_ATT);
		  ArrayList<FacetWrapper> facets = getFacets(query_result, lang);
			if(facets != null)
			{
				Element facet_list = result_doc.createElement(GSXML.FACET_ELEM + GSXML.LIST_MODIFIER);
				result.appendChild(facet_list);

				for(FacetWrapper currentFacet : facets)
				{
					Element facet_elem = result_doc.createElement(GSXML.FACET_ELEM);
					facet_elem.setAttribute(GSXML.NAME_ATT, currentFacet.getName());
					String display_name = currentFacet.getDisplayName();
					if (display_name != null && !display_name.equals("")) {
					  facet_elem.appendChild(GSXML.createDisplayTextElement(result_doc, GSXML.DISPLAY_TEXT_NAME, display_name));
					}
					facet_list.appendChild(facet_elem);
					
					List<FacetWrapper.FacetCount> countList = currentFacet.getCounts();
					for (FacetWrapper.FacetCount thisCount : countList) 
					{
					    String countName = thisCount.name;
					    long countValue = thisCount.count;
						if(countValue > 0)
						{
							Element count_elem = result_doc.createElement(GSXML.COUNT_ELEM);
							count_elem.setAttribute(GSXML.NAME_ATT, countName);
							count_elem.setTextContent("" + countValue);

							facet_elem.appendChild(count_elem);
						}
					}
				}
			}
		}
		
		queryObject = null;
		return result;

	}

	/** methods to handle actually doing the query */
	/** do any initialisation of the query object. Call before runQuery()
	  * @return the queryObject (e.g. GS2LuceneQuery)
	*/	
  abstract protected Object setUpQueryer(HashMap<String, Serializable> params, String service_name);

	/** do the query 
	  * The queryObject parameter is the return value of setUpQueryer.
	*/
	abstract protected Object runQuery(Object queryObject, String query);

	/** get the total number of docs that match */
	abstract protected long numDocsMatched(Object query_result);

	/** get the list of doc ids */
	abstract protected String[] getDocIDs(Object query_result);

	/** get the list of doc ranks */
	abstract protected String[] getDocRanks(Object query_result);
	
	/** get the list of facets */
  abstract protected ArrayList<FacetWrapper> getFacets(Object query_result, String lang);
	
	/** get the map of highlighting snippets */
	abstract protected Map<String, Map<String, List<String>>> getHighlightSnippets(Object query_result);

	/** add in term info if available */
	abstract protected boolean addTermInfo(Element term_list, HashMap<String, Serializable> params, Object query_result);

	/**
	 * combines all the field params into a single query - for simple field
	 * query
	 */
	/** We assume the combination (AND/OR) is done by the match param */
	protected String parseFieldQueryParams(HashMap<String, Serializable> params)
	{

		StringBuffer final_query = new StringBuffer(256);
		String text_line = (String) params.get(FIELD_QUERY_PARAM);
		String[] texts = text_line.split(",", -1);
		String field_line = (String) params.get(FIELD_FIELD_PARAM);
		String[] fields = field_line.split(",", -1);

		for (int i = 0; i < texts.length; i++)
		{
			String q = texts[i].trim();
			if (!q.equals(""))
			{
				final_query.append(" " + addFieldInfo(q, fields[i]));
			}
		}

		return final_query.toString();
	}

	abstract protected String addFieldInfo(String query, String field);

	/**
	 * combines all the field params into a single query - for advanced field
	 * query
	 */
	protected String parseAdvancedFieldQueryParams(HashMap<String, Serializable> params)
	{

		StringBuffer final_query = new StringBuffer(256);
		String text_line = (String) params.get(FIELD_QUERY_PARAM);
		String[] texts = text_line.split(",", -1);
		String field_line = (String) params.get(FIELD_FIELD_PARAM);
		String[] fields = field_line.split(",", -1);
		String[] cases = null;
		String[] stems = null;
		String[] accents = null;
		if (does_case)
		{
			String case_line = (String) params.get(FIELD_CASE_PARAM);
			if (case_line != null)
				cases = case_line.split(",", -1);
		}
		if (does_stem)
		{
			String stem_line = (String) params.get(FIELD_STEM_PARAM);
			if (stem_line != null)
				stems = stem_line.split(",", -1);
		}
		if (does_accent)
		{
			String accent_line = (String) params.get(FIELD_ACCENT_PARAM);
			if (accent_line != null)
				accents = accent_line.split(",", -1);
		}
		String combine_line = (String) params.get(FIELD_COMBINE_PARAM);
		String[] combines = combine_line.split(",", -1);
		String combine = "";
		for (int i = 0; i < texts.length; i++)
		{
			if (i == 0)
			{// assume first one is blank
				combine = "";
			}
			else
			{
				String x = combines[i];
				if (x.equals(FIELD_COMBINE_PARAM_AND))
				{
					combine = AND_OPERATOR;
				}
				else if (x.equals(FIELD_COMBINE_PARAM_OR))
				{
					combine = OR_OPERATOR;
				}
				else if (x.equals(FIELD_COMBINE_PARAM_NOT))
				{
					combine = NOT_OPERATOR;
				}

			}

			String q = texts[i].trim();
			boolean modified = false;
			if (!q.equals(""))
			{
				String c = null;
				String s = null;
				String a = null;
				if (does_case)
				{
					modified = true;
					c = cases[i];
				}
				if (does_stem)
				{
					modified = true;
					s = stems[i];
				}
				if (does_accent)
				{
					modified = true;
					a = accents[i];
				}
				if (modified)
				{
					q = addStemOptions(q, s, c, a);
				}
				addQueryElem(final_query, q, fields[i], combine);
			}
		}
		return final_query.toString();
	}

	abstract protected void addQueryElem(StringBuffer final_query, String query, String field, String combine);

	abstract protected String addStemOptions(String query, String stem, String casef, String accent);

}
