From 4c2a9530a61fceb4867578b98ae7d8dabacefa97 Mon Sep 17 00:00:00 2001 From: tttzach Date: Mon, 15 Jun 2020 20:01:18 -0400 Subject: [PATCH 1/3] close #47 --- portfolio/pom.xml | 5 +++ .../sps/servlets/CoronavirusDataServlet.java | 37 +++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/portfolio/pom.xml b/portfolio/pom.xml index ca7257a..cef3db2 100644 --- a/portfolio/pom.xml +++ b/portfolio/pom.xml @@ -33,6 +33,11 @@ appengine-api-1.0-sdk 1.9.59 + + com.opencsv + opencsv + 4.5 + diff --git a/portfolio/src/main/java/com/google/sps/servlets/CoronavirusDataServlet.java b/portfolio/src/main/java/com/google/sps/servlets/CoronavirusDataServlet.java index 703d51b..a32297f 100644 --- a/portfolio/src/main/java/com/google/sps/servlets/CoronavirusDataServlet.java +++ b/portfolio/src/main/java/com/google/sps/servlets/CoronavirusDataServlet.java @@ -14,10 +14,13 @@ package com.google.sps.servlets; +import com.opencsv.CSVReaderHeaderAware; import com.google.gson.Gson; + +import java.io.FileReader; import java.io.IOException; import java.util.LinkedHashMap; -import java.util.Scanner; +import java.util.Map; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -31,21 +34,8 @@ public class CoronavirusDataServlet extends HttpServlet { @Override public void init() { - Scanner scanner = new Scanner(getServletContext().getResourceAsStream( - "/WEB-INF/coronavirus-stats-by-country.csv")); - // Ignore header row = "Country/Region,Confirmed,Active,..." - if (scanner.hasNextLine()) { - scanner.nextLine(); - } - while (scanner.hasNextLine()) { - // line = "Afghanistan,20917,369.0,..." - String line = scanner.nextLine(); - String[] cells = line.split(","); - String country = String.valueOf(cells[0]); - Integer confirmedCases = Integer.valueOf(cells[1]); - coronavirusCases.put(country, confirmedCases); - } - scanner.close(); + String filePath = "/home/tanzachary/step/portfolio/src/main/webapp/WEB-INF/coronavirus-stats-by-country.csv"; + readCsv(filePath); } @Override @@ -55,5 +45,20 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro String json = gson.toJson(coronavirusCases); response.getWriter().println(json); } + + private void readCsv(String filePath) { + try { + CSVReaderHeaderAware csvReader = new CSVReaderHeaderAware(new FileReader(filePath)); + Map line; + while ((line = csvReader.readMap()) != null) { + String country = line.get("Country/Region"); + Integer confirmedCases = Integer.valueOf(line.get("Confirmed")); + coronavirusCases.put(country, confirmedCases); + } + csvReader.close(); + } catch (IOException exception) { + System.out.println("File not found."); + } + } } From abdaa24a5cf092eb4718938a4426a87530cbeb85 Mon Sep 17 00:00:00 2001 From: tttzach Date: Tue, 16 Jun 2020 19:01:58 -0400 Subject: [PATCH 2/3] Use relative file path and remove redundant newline --- .../java/com/google/sps/servlets/CoronavirusDataServlet.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/portfolio/src/main/java/com/google/sps/servlets/CoronavirusDataServlet.java b/portfolio/src/main/java/com/google/sps/servlets/CoronavirusDataServlet.java index a32297f..b3f8970 100644 --- a/portfolio/src/main/java/com/google/sps/servlets/CoronavirusDataServlet.java +++ b/portfolio/src/main/java/com/google/sps/servlets/CoronavirusDataServlet.java @@ -16,7 +16,6 @@ import com.opencsv.CSVReaderHeaderAware; import com.google.gson.Gson; - import java.io.FileReader; import java.io.IOException; import java.util.LinkedHashMap; @@ -34,7 +33,7 @@ public class CoronavirusDataServlet extends HttpServlet { @Override public void init() { - String filePath = "/home/tanzachary/step/portfolio/src/main/webapp/WEB-INF/coronavirus-stats-by-country.csv"; + String filePath = "../../src/main/webapp/WEB-INF/coronavirus-stats-by-country.csv"; readCsv(filePath); } @@ -60,5 +59,5 @@ private void readCsv(String filePath) { System.out.println("File not found."); } } - + } From 5f1f235ccd43cd9c5ee403faefa91b0e962ecc30 Mon Sep 17 00:00:00 2001 From: tttzach Date: Tue, 16 Jun 2020 19:03:37 -0400 Subject: [PATCH 3/3] Use relative file path and remove redundant newline --- .../java/com/google/sps/servlets/CoronavirusDataServlet.java | 1 - 1 file changed, 1 deletion(-) diff --git a/portfolio/src/main/java/com/google/sps/servlets/CoronavirusDataServlet.java b/portfolio/src/main/java/com/google/sps/servlets/CoronavirusDataServlet.java index b7f8225..a5fe861 100644 --- a/portfolio/src/main/java/com/google/sps/servlets/CoronavirusDataServlet.java +++ b/portfolio/src/main/java/com/google/sps/servlets/CoronavirusDataServlet.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.Scanner; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest;