package ae3.servlet.structuredquery;

import ae3.service.structuredquery.AtlasStructuredQueryService;
import ae3.service.structuredquery.AutoCompleteItem;
import ae3.service.structuredquery.IValueListHelper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pashky
 */
public class FactorValuesServlet extends JsonServlet {

    public JSONObject process(HttpServletRequest request) throws JSONException {

        AtlasStructuredQueryService service = ae3.service.ArrayExpressSearchService.instance()
                .getStructQueryService();
        
        List<IValueListHelper> listers = new ArrayList<IValueListHelper>();

        String type = request.getParameter("type");
        if("gene".equals(type)) {
            listers.add(service.getGeneListHelper());
        } else if("efv".equals(type)) {
            listers.add(service.getEfvListHelper());
        } else if("efo".equals(type)) {
            listers.add(service.getEfoListHelper());
        } else if("efoefv".equals(type)) {
            listers.add(service.getEfoListHelper());
            listers.add(service.getEfvListHelper());
        }

        JSONObject result = new JSONObject();

        if("all".equals(request.getParameter("mode")))
        {

            List<String> resultList = new ArrayList<String>();
            for(IValueListHelper lister : listers)
                resultList.addAll(lister.listAllValues(request.getParameter("factor")));
            result.put("values", new JSONArray(resultList));
        } else {
            int nlimit = 100;
            try {
                nlimit = Integer.parseInt(request.getParameter("limit"));
                if(nlimit > 1000)
                    nlimit = 1000;
            } catch(Exception e) {
                // just ignore
            }
            String factor = request.getParameter("factor");
            String query = request.getParameter("q");
            if (query == null)
                query = "";
            if (query.startsWith("\"")) {
                query = query.substring(1);
            }
            if (query.endsWith("\"")) {
                query = query.substring(0, query.length() - 1);
            }

            Map<String,String> filters = new HashMap<String,String>();
            String[] filtps = request.getParameterValues("f");
            if(filtps != null)
                for(String filter : filtps) {
                    filters.put(filter, request.getParameter(filter));
                }

            List<AutoCompleteItem> resultList = new ArrayList<AutoCompleteItem>();
            for(IValueListHelper lister : listers) {
                resultList.addAll(lister.autoCompleteValues(
                                factor,
                                query,
                                nlimit,
                                filters
                        ));
            }
            result.put("values", new JSONArray(resultList, true));
        }

        return result;
    }
}

@@ -1350,24 +1350,119 @@
 NObject();%0A%0A
+        String factor = request.getParameter(%22factor%22);%0A        result.put(%22factor%22, factor);%0A%0A
         if(%22
@@ -1688,38 +1688,14 @@
 ues(
-request.getParameter(%22
 factor
-%22)
 ));%0A
@@ -2053,39 +2053,42 @@
           String
- factor
+%5B%5D queries
  = request.getPa
@@ -2098,74 +2098,174 @@
 eter
-(%22factor%22);%0A            String query = request.getParameter(%22q%22);%0A
+Values(%22q%22);%0A            JSONObject values = new JSONObject();%0A            result.put(%22completions%22, values);%0A            %0A            for(String query : queries) %7B%0A 
     
@@ -2264,32 +2264,35 @@
  %7B%0A             
+   
 if (query == nul
@@ -2302,32 +2302,36 @@
                 
+    
 query = %22%22;%0A    
@@ -2330,32 +2330,36 @@
 %22%22;%0A            
+  
+  
 if (query.starts
@@ -2380,32 +2380,36 @@
                 
+    
 query = query.su
@@ -2424,34 +2424,42 @@
 1);%0A            
+  
-%7D%0A
+  %7D%0A    
             if (
@@ -2474,32 +2474,36 @@
 dsWith(%22%5C%22%22)) %7B%0A
+    
                 
@@ -2554,35 +2554,43 @@
 1);%0A            
-%7D%0A%0A
+    %7D%0A%0A    
             Map%3C
@@ -2648,32 +2648,36 @@
 ();%0A            
+    
 String%5B%5D filtps 
@@ -2723,16 +2723,20 @@
         
+    
 if(filtp
@@ -2754,32 +2754,36 @@
                 
+ 
+   
 for(String filte
@@ -2792,24 +2792,28 @@
 : filtps) %7B%0A
+    
             
@@ -2879,35 +2879,43 @@
                 
+ 
-%7D%0A%0A
+   %7D%0A%0A    
             List
@@ -2985,32 +2985,36 @@
 ();%0A            
+    
 for(IValueListHe
@@ -3035,19 +3035,78 @@
 listers)
- %7B%0A
+%0A                    if(resultList.size() %3C nlimit) %7B%0A        
         
@@ -3275,16 +3275,36 @@
   nlimit
+ - resultList.size()
 ,%0A      
@@ -3369,32 +3369,40 @@
 ));%0A            
+        
 %7D%0A            re
@@ -3399,35 +3399,36 @@
         
-result.put(%22values%22
+    values.put(query
 , new JS
@@ -3451,24 +3451,38 @@
 st, true));%0A
+            %7D%0A
         %7D%0A%0A 

Support for batch queries (multiple autocompletion searches in one request) Slightly change output structure