From bf024d3e4a7859a2a70e2110dffddb4f424b0e9e Mon Sep 17 00:00:00 2001
From: Schankula Christopher <schankuc@mcmaster.ca>
Date: Mon, 2 Apr 2018 20:51:45 -0400
Subject: [PATCH] add new server code for clustering

---
 src/search/RecordCluster.java        |  7 ++++
 tomcat/webapps/Trawl/WEB-INF/web.xml |  1 +
 tomcat/webapps/Trawl/cluster.jsp     | 49 ++++++++++++++++++++++++++++
 tomcat/webapps/Trawl/map.jsp         |  3 +-
 4 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 tomcat/webapps/Trawl/cluster.jsp

diff --git a/src/search/RecordCluster.java b/src/search/RecordCluster.java
index 6bfe630..ebccd09 100644
--- a/src/search/RecordCluster.java
+++ b/src/search/RecordCluster.java
@@ -8,14 +8,17 @@ import sandbox.Point;
 public class RecordCluster {
 	private ArrayList<Record> records;
 	private Point centroid;
+	private int individualCount;
 	
 	public RecordCluster() {
 		records = new ArrayList<Record>();
 		centroid = new Point(0,0);
+		individualCount = 0;
 	}
 	
 	public void addRecord(Record r) {
 		records.add(r);
+		individualCount++;
 	}
 	
 	public Iterable<Record> records(){
@@ -26,6 +29,10 @@ public class RecordCluster {
 		return records.size();
 	}
 	
+	public int getCount() {
+		return records.size();
+	}
+	
 	public Point centroid() {
 		double x = 0;
 		double y = 0;
diff --git a/tomcat/webapps/Trawl/WEB-INF/web.xml b/tomcat/webapps/Trawl/WEB-INF/web.xml
index 6b212a2..dc7d6ca 100644
--- a/tomcat/webapps/Trawl/WEB-INF/web.xml
+++ b/tomcat/webapps/Trawl/WEB-INF/web.xml
@@ -14,5 +14,6 @@
         <url-pattern>/doBioLookup.do</url-pattern>
         <url-pattern>/doHist.do</url-pattern>
         <url-pattern>/doMap.do</url-pattern>
+        <url-pattern>/doCluster.do</url-pattern>
     </servlet-mapping>
 </web-app>
\ No newline at end of file
diff --git a/tomcat/webapps/Trawl/cluster.jsp b/tomcat/webapps/Trawl/cluster.jsp
new file mode 100644
index 0000000..13c26d6
--- /dev/null
+++ b/tomcat/webapps/Trawl/cluster.jsp
@@ -0,0 +1,49 @@
+<%@ page import="java.util.*, data.Record, model.TrawlExpert, search.BST, search.BasicSearchResult,data.BioTree,data.TaxonNode,search.RecordCluster,sandbox.Point" %>
+<%@page import="org.json.simple.JSONArray"%>
+<%@page import="org.json.simple.JSONObject"%>
+<%@page import="org.json.simple.parser.JSONParser"%>
+
+<%
+	// Sample result data containing an iterable of records
+		TrawlExpert te = (TrawlExpert)request.getServletContext().getAttribute("trawl");
+		JSONParser parser = new JSONParser();
+		JSONObject req = (JSONObject) parser.parse(request.getReader().readLine());
+		
+		Integer taxonId = (int) (long) req.get("taxId");
+		Integer yearLo = (int) (long) req.get("yearF");
+		Integer yearHi = (int) (long) req.get("yearT");
+		double area = (double) req.get("area");
+
+		BasicSearchResult result = te.rangeSearch(taxonId, yearLo, yearHi);
+		
+		Iterable<RecordCluster> clusters = result.cluster(area);
+		
+		// Initialize JSON Object and Arrays
+		JSONObject js = new JSONObject();
+		JSONArray longitude = new JSONArray();
+		JSONArray latitude = new JSONArray();
+		JSONArray count = new JSONArray();
+		
+		
+		// Update value of each JSON Object/Array at the same index as the corresponding Record in Result input
+		for (RecordCluster rc: clusters){
+			Point centroid = rc.centroid();
+			
+			longitude.add(centroid.getX());
+			latitude.add(centroid.getY());
+			JSONObject dateobj = new JSONObject();
+			count.add(rc.getCount());
+		}
+	
+		// Insert JSON Array and Objects into main Object
+		js.put("latitude", latitude);
+		js.put("longitude", longitude);
+		js.put("n", result.n());
+		js.put("individualCount", count);
+		js.put("time", result.time());
+		
+	
+		out.print(js.toJSONString());
+%>
+
+	
\ No newline at end of file
diff --git a/tomcat/webapps/Trawl/map.jsp b/tomcat/webapps/Trawl/map.jsp
index 6681bdb..79cd8c7 100644
--- a/tomcat/webapps/Trawl/map.jsp
+++ b/tomcat/webapps/Trawl/map.jsp
@@ -42,7 +42,8 @@
 		js.put("longitude", longitude);
 		js.put("name", name);
 		js.put("date", date);
-		js.put("individual count", count);
+		js.put("n", result.n());
+		js.put("individualCount", count);
 		js.put("time", result.time());
 		
 	
-- 
GitLab