/*
 * Copyright 2010 Collaborative Research Centre SFB 632
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package annis.frontend.servlets;

import annis.exceptions.AnnisCorpusAccessException;
import annis.exceptions.AnnisQLSemanticsException;
import annis.exceptions.AnnisQLSyntaxException;
import annis.exceptions.AnnisServiceFactoryException;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import annis.model.AnnisNode;
import annis.service.AnnisService;
import annis.service.AnnisServiceFactory;
import annis.service.ifaces.AnnisResult;
import annis.service.ifaces.AnnisResultSet;
import java.io.*;
import java.util.LinkedList;
import java.util.List;

public class TextExporterServlet extends HttpServlet {

  private static final long serialVersionUID = -8182635617256833563L;

  @SuppressWarnings("unchecked")
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // BEGIN getting the needed parameters from the query
    HttpSession session = request.getSession();

    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");

    /** The selected corpora */
    List<Long> corpusIdList = new LinkedList<Long>();
    /** The AQL query which should be executed */
    String queryAnnisQL = request.getParameter(SubmitQueryServlet.PARAM_ANNIS_QL);

    // the left an right context size
    int contextLeft = 0;
    int contextRight = 0;
    try {
      String p = request.getParameter(SubmitQueryServlet.PARAM_CONTEXT_LEFT);
      if (p != null) {
        contextLeft = Integer.parseInt(p);
      }
      p = request.getParameter(SubmitQueryServlet.PARAM_CONTEXT_RIGHT);
      if (p != null) {
        contextRight = Integer.parseInt(p);
      }
    }
    catch (NumberFormatException ex) {
      // ignore
    }
    // get a "real" list of corpora from the comma-delimited parameter
    String corpusListAsString = request.getParameter(SubmitQueryServlet.PARAM_CORPUS_ID);
    if (queryAnnisQL == null) {
      response.getWriter().println("missing parameter for the AQL query (" + SubmitQueryServlet.PARAM_ANNIS_QL + ")");
      return;
    }
    if (corpusListAsString == null) {
      response.getWriter()
          .println("missing parameter for the corpus list (" + SubmitQueryServlet.PARAM_CORPUS_ID + ")");
      return;
    }

    for (String corpusId : corpusListAsString.split(",")) {
      try {
        corpusIdList.add(Long.parseLong(corpusId));
      }
      catch (NumberFormatException ex) {
        // ignore
      }
    }
    // END getting the needed parameters from the query

    try {
      AnnisService service = AnnisServiceFactory.getClient(this.getServletContext().getInitParameter(
          "AnnisRemoteService.URL"));

      int count = service.getCount(corpusIdList, queryAnnisQL);
      AnnisResultSet queryResult = service
          .getResultSet(corpusIdList, queryAnnisQL, count, 0, contextLeft, contextRight);

      int counter = 0;
      for (AnnisResult annisResult : queryResult) {
        counter++;
        response.getWriter().append("" + counter + ". ");
        List<AnnisNode> tok = annisResult.getGraph().getTokens();
        for (AnnisNode annisNode : tok) {

          response.getWriter().append(annisNode.getSpannedText());
          response.getWriter().append(" ");
        }
        response.getWriter().append("\n");

      }

    }
    catch (AnnisServiceFactoryException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (AnnisQLSemanticsException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (AnnisQLSyntaxException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (AnnisCorpusAccessException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

@@ -3275,32 +3275,35 @@
 e.URL%22));%0A%0A     
+ //
  int count = ser
@@ -3384,26 +3384,168 @@
 t = 
-service%0A          
+null;%0A      int counter = 0;%0A      int offset = 0;%0A      while (offset == 0 %7C%7C (queryResult != null && queryResult.size() %3E 0)) %7B%0A%0A        queryResult = service
 .get
@@ -3586,16 +3586,18 @@
 QL, 
-count, 0
+50, offset
 , co
@@ -3631,33 +3631,12 @@
     
-  int counter = 0;%0A
     
-  
 for 
@@ -3673,24 +3673,26 @@
 ryResult) %7B%0A
+  
         coun
@@ -3698,32 +3698,34 @@
 nter++;%0A        
+  
 response.getWrit
@@ -3754,24 +3754,26 @@
 er + %22. %22);%0A
+  
         List
@@ -3826,32 +3826,34 @@
 kens();%0A        
+  
 for (AnnisNode a
@@ -3867,24 +3867,26 @@
 e : tok) %7B%0A%0A
+  
           re
@@ -3942,32 +3942,34 @@
 t());%0A          
+  
 response.getWrit
@@ -3990,26 +3990,30 @@
 %22);%0A        
+ 
+ 
 %7D%0A
+  
         resp
@@ -4040,24 +4040,66 @@
 end(%22%5Cn%22);%0A%0A
+        %7D%0A%0A        offset = offset + 50;%0A%0A
       %7D%0A%0A   

allow bigger results in TextExporter