OpenDataPhillyFinal / src / edu / upenn / cit594 / datamanagement / JSONCovidReader.java
JSONCovidReader.java
Raw
package edu.upenn.cit594.datamanagement;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import edu.upenn.cit594.ui.UserInterface;
import edu.upenn.cit594.util.CovidData;


public class JSONCovidReader implements CovidReader {

	protected static String filename;
	
	public JSONCovidReader (String name) {
		filename = name;
	}
	
	public static ArrayList<CovidData> readCovidJSON() {
		
		File jsonFile = new File(filename);
		if(!jsonFile.exists()) {
			UserInterface.print("error: covid json file does not exist.");
			return null;
		} else if (!jsonFile.canRead()) {
			UserInterface.print("error: covid json file can not be read.");
			return null;
		}
		
		//To store the CovidData Object Entries
		ArrayList<CovidData> covidData = null;
		//Stores BufferedReader line
		//String line = "";
		
		try {
			covidData = new ArrayList<CovidData>();
			Object obj = new JSONParser().parse(new FileReader(jsonFile));
			JSONArray ja = (JSONArray) obj;
			
			for (Object o : ja) {
				JSONObject jo = (JSONObject) o;
				
				//Get zip code
				long zipCodeLong = (Long) jo.get("zip_code");
				int zipCode = (int) zipCodeLong;				
				
				//if the zipcode is not 5 digits long, then skip row
				if (String.valueOf(zipCode).length() != 5) {
					continue;
				//else, valid zip code
				} else{ 
					//Get only time stamps that have the proper format
					String timeStamp = (String) jo.get("etl_timestamp");
					//If timestamp is not in specified format (“YYYY- MM-DD hh:mm:ss”), then skip row
					String pattern = "[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]";
					Pattern r = Pattern.compile(pattern);
					Matcher m = r.matcher(timeStamp);
					if (!m.find()) {continue;}
					
					//Get partially_vaccinated value
					int partialVax = 0;
					if (jo.containsKey("partially_vaccinated")) {
						long partialVaxLong = (Long)(jo.get("partially_vaccinated"));
						partialVax = (int) partialVaxLong;
					}
					//Get fully_vaccinated value
					int fullVax = 0;
					if (jo.containsKey("fully_vaccinated")) {
						long fullVaxLong = (Long)(jo.get("fully_vaccinated"));
						fullVax = (int) fullVaxLong;
						
					}
					//get boosted value
					int boosted = 0;
					if (jo.containsKey("boosted")) {
						long boostedLong = (Long)(jo.get("boosted"));
						boosted = (int) boostedLong;
					
					}
					
					//create new covid row data
					CovidData covidRow = new CovidData(
							zipCode, 
							partialVax, 
							fullVax, 
							boosted, 
							timeStamp);
					
					//add to CovidData Object ArrayList 
					covidData.add(covidRow);
				}
			}
		} catch (Exception e) {
			UserInterface.print(e.getMessage());
		}
		return covidData;	
	}



}