package yanagishima.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.fluent.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import yanagishima.config.YanagishimaConfig;
import yanagishima.util.AccessControlUtil;
import yanagishima.util.HttpRequestUtil;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Singleton
public class QueryServlet extends HttpServlet {

	private static Logger LOGGER = LoggerFactory.getLogger(QueryServlet.class);

	private static final long serialVersionUID = 1L;

	private YanagishimaConfig yanagishimaConfig;

	private static final int LIMIT = 100;

	@Inject
	public QueryServlet(YanagishimaConfig yanagishimaConfig) {
		this.yanagishimaConfig = yanagishimaConfig;
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String datasource = HttpRequestUtil.getParam(request, "datasource");
		AccessControlUtil.checkDatasource(request, datasource);
		String prestoCoordinatorServer = yanagishimaConfig
				.getPrestoCoordinatorServer(datasource);
		response.setContentType("application/json");
		PrintWriter writer = response.getWriter();
		String originalJson = Request.Get(prestoCoordinatorServer + "/v1/query")
				.execute().returnContent().asString(StandardCharsets.UTF_8);
		ObjectMapper mapper = new ObjectMapper();
		List<Map> list = mapper.readValue(originalJson, List.class);
		if(list.size() > LIMIT) {
			list.sort((a,b)-> String.class.cast(b.get("queryId")).compareTo(String.class.cast(a.get("queryId"))));
			String json = mapper.writeValueAsString(list.subList(0, LIMIT));
			writer.println(json);
		} else {
			writer.println(originalJson);
		}
	}

}

@@ -71,24 +71,56 @@
 jectMapper;%0A
+import me.geso.tinyorm.TinyORM;%0A
 import org.a
@@ -149,24 +149,24 @@
 nt.Request;%0A
-
 import org.s
@@ -255,16 +255,46 @@
 Config;%0A
+import yanagishima.row.Query;%0A
 import y
@@ -1007,16 +1007,47 @@
 onfig;%0A%0A
+%09@Inject%0A%09private TinyORM db;%0A%0A
 %09private
@@ -1901,37 +1901,8 @@
 s);%0A
-%09%09if(list.size() %3E LIMIT) %7B%0A%09
 %09%09li
@@ -2008,49 +2008,32 @@
 ;%0A%09%09
-%09String json = mapper.writeValueAsString(
+List%3CMap%3E limitedList = 
 list
@@ -2054,36 +2054,239 @@
 MIT)
-)
 ;%0A%09%09
-%09writer.println(json
+for(Map m : limitedList) %7B%0A%09%09%09Optional%3CQuery%3E queryOptional = db.single(Query.class).where(%22query_id=? and datasource=?%22, m.get(%22queryId%22), datasource).execute();%0A%09%09%09if(queryOptional.isPresent()) %7B%0A%09%09%09%09m.put(%22existdb%22, true
 );%0A
+%09
 %09%09%7D 
@@ -2299,42 +2299,123 @@
 %0A%09%09%09
-writer.println(originalJ
+%09m.put(%22existdb%22, false);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09String json = mapper.writeValueAsString(limitedList);%0A%09%09writer.println(j
 son);%0A
-%09%09%7D%0A
 %09%7D%0A%0A

Add existdb flag