/**   
 * License Agreement for OpenSearchServer
 *
 * Copyright (C) 2014 Emmanuel Keller / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of OpenSearchServer.
 *
 * OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * OpenSearchServer 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 OpenSearchServer. 
 *  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.jaeksoft.searchlib.webservice.query.search;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.jaeksoft.searchlib.Client;
import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.request.AbstractSearchRequest;
import com.jaeksoft.searchlib.request.RequestTypeEnum;
import com.jaeksoft.searchlib.request.SearchFieldRequest;
import com.jaeksoft.searchlib.request.SearchPatternRequest;
import com.jaeksoft.searchlib.result.AbstractResultSearch;
import com.jaeksoft.searchlib.webservice.query.CommonQuery;

@JsonInclude(Include.NON_NULL)
@XmlAccessorType(XmlAccessType.FIELD)
public class SearchQueryBatch {

	@XmlElements({
			@XmlElement(name = "SearchField", type = SearchFieldQuery.class),
			@XmlElement(name = "SearchPattern", type = SearchPatternQuery.class),
			@XmlElement(name = "SearchFieldTemplate", type = SearchFieldTemplateQuery.class),
			@XmlElement(name = "SearchPatternTemplate", type = SearchPatternTemplateQuery.class) })
	@JsonTypeInfo(use = Id.NAME, property = "type")
	@JsonSubTypes({
			@JsonSubTypes.Type(value = SearchFieldQuery.class, name = "SearchField"),
			@JsonSubTypes.Type(value = SearchPatternQuery.class, name = "SearchPattern"),
			@JsonSubTypes.Type(value = SearchFieldTemplateQuery.class, name = "SearchFieldTemplate"),
			@JsonSubTypes.Type(value = SearchPatternTemplateQuery.class, name = "SearchPatternTemplate") })
	final public List<SearchQueryAbstract> queries;

	public SearchQueryBatch() {
		queries = null;
	}

	@XmlAccessorType(XmlAccessType.FIELD)
	@JsonInclude(Include.NON_NULL)
	public static class SearchFieldTemplateQuery extends SearchFieldQuery {

		public final String template;

		public SearchFieldTemplateQuery() {
			template = null;
		}

		public AbstractSearchRequest getNewRequest(Client client)
				throws SearchLibException {
			return (AbstractSearchRequest) CommonQuery.getNewRequest(client,
					template, RequestTypeEnum.SearchFieldRequest);
		}
	}

	@XmlAccessorType(XmlAccessType.FIELD)
	@JsonInclude(Include.NON_NULL)
	public static class SearchPatternTemplateQuery extends SearchPatternQuery {

		public final String template;

		public SearchPatternTemplateQuery() {
			template = null;
		}

		public AbstractSearchRequest getNewRequest(Client client)
				throws SearchLibException {
			return (AbstractSearchRequest) CommonQuery.getNewRequest(client,
					template, RequestTypeEnum.SearchRequest);
		}
	}

	public List<SearchResult> result(Client client) throws SearchLibException {
		if (queries == null)
			return null;
		List<SearchResult> searchResult = new ArrayList<SearchResult>(
				queries.size());
		for (SearchQueryAbstract query : queries) {
			AbstractSearchRequest searchRequest = null;
			if (query instanceof SearchFieldTemplateQuery)
				searchRequest = ((SearchFieldTemplateQuery) query)
						.getNewRequest(client);
			else if (query instanceof SearchPatternTemplateQuery)
				searchRequest = ((SearchPatternTemplateQuery) query)
						.getNewRequest(client);
			else if (query instanceof SearchFieldQuery)
				searchRequest = new SearchFieldRequest(client);
			else if (query instanceof SearchPatternQuery)
				searchRequest = new SearchPatternRequest(client);
			query.apply(searchRequest);
			searchResult.add(new SearchResult((AbstractResultSearch) client
					.request(searchRequest)));
		}
		return searchResult;
	}
}

@@ -1977,16 +1977,79 @@
 atch %7B%0A%0A
+%09public enum Mode %7B%0A%09%09all, first%0A%09%7D%0A%0A%09final public Mode mode;%0A%0A
 %09@XmlEle
@@ -2887,16 +2887,35 @@
 = null;%0A
+%09%09mode = Mode.all;%0A
 %09%7D%0A%0A%09@Xm
@@ -3982,16 +3982,17 @@
 chResult
+s
  = new A
@@ -4644,17 +4644,17 @@
 st);%0A%09%09%09
-s
+S
 earchRes
@@ -4660,13 +4660,24 @@
 sult
-.add(
+ searchResult = 
 new 
@@ -4689,16 +4689,22 @@
 hResult(
+%0A%09%09%09%09%09
 (Abstrac
@@ -4724,22 +4724,16 @@
 ) client
-%0A%09%09%09%09%09
 .request
@@ -4748,17 +4748,142 @@
 equest))
-)
+;%0A%09%09%09searchResults.add(searchResult);%0A%09%09%09if (mode != null && mode == Mode.first)%0A%09%09%09%09if (searchResult.numFound %3E 0)%0A%09%09%09%09%09break
 ;%0A%09%09%7D%0A%09%09
@@ -4901,15 +4901,16 @@
 chResult
+s
 ;%0A%09%7D%0A%7D%0A

Add mode: all|first to #1255