package com.thanglequoc.aqicalculator;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class PollutantsBreakpointGenerator {
	private PollutantsBreakpoint pollutantsBreakpoint;
	private PollutantBreakpointParser pollutantBreakpointParser;
	private static String breakpointFilePath = "./src/main/resources/AQIresource/aqi-breakpoint.json";
	
	private File breakpointFile;
	
	public PollutantsBreakpointGenerator() throws JsonProcessingException, IOException{
		pollutantBreakpointParser = new PollutantBreakpointParser();
		pollutantsBreakpoint = new PollutantsBreakpoint();
		File breakpointFile = new File(breakpointFilePath);
		setBreakpointFile(breakpointFile);
		
		ObjectMapper mapper = new ObjectMapper();
		
		JsonNode root = mapper.readTree(breakpointFile);
		
		for(JsonNode pollutantNode: root){
			PollutantBreakpoint pollutantBreakpoint = pollutantBreakpointParser.parseNode(pollutantNode);
			this.pollutantsBreakpoint.addPollutantBreakpoint(pollutantBreakpoint);
		}
		
		
	}

	public PollutantsBreakpoint getPollutantsBreakpoint() {
		return pollutantsBreakpoint;
	}

	public void setPollutantsBreakpoint(PollutantsBreakpoint pollutantsBreakpoint) {
		this.pollutantsBreakpoint = pollutantsBreakpoint;
	}

	public File getBreakpointFile() {
		return breakpointFile;
	}

	public void setBreakpointFile(File breakpointFile) {
		this.breakpointFile = breakpointFile;
	}
	
	
}

@@ -48,20 +48,27 @@
 java.io.
-File
+IOException
 ;%0Aimport
@@ -77,26 +77,26 @@
 ava.io.I
-OException
+nputStream
 ;%0A%0Aimpor
@@ -513,41 +513,8 @@
 n%22;%0A
-%09%0A%09private File breakpointFile;%0A%09
 %0A%09pu
@@ -715,149 +715,241 @@
 ();%0A
+%0A
 %09%09
-File breakpointFile = new File(breakpointFilePath
+ObjectMapper mapper = new ObjectMapper(
 );%0A
+%0A
 %09%09
-setBreakpointFile(breakpointFile
+ClassLoader classLoader = PollutantsBreakpointGenerator.class.getClassLoader(
 );%0A
-%09%09
 %0A%09%09
-ObjectMapper mapper = new ObjectMapper();%0A%09%09%0A
+try (InputStream inputStream = classLoader.getResourceAsStream(%22AQIresource/aqi-breakpoint.json%22)) %7B%0A%09
 %09%09Js
@@ -982,30 +982,26 @@
 ree(
-breakpointFile
+inputStream
 );%0A
-%09%09
 %0A%09%09
+%09
 for(
@@ -1031,16 +1031,17 @@
  root)%7B%0A
+%09
 %09%09%09Pollu
@@ -1132,16 +1132,17 @@
 de);%0A%09%09%09
+%09
 this.pol
@@ -1204,16 +1204,21 @@
 point);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09%0A%09
@@ -1457,171 +1457,7 @@
 %09%7D%0A%0A
-%09public File getBreakpointFile() %7B%0A%09%09return breakpointFile;%0A%09%7D%0A%0A%09public void setBreakpointFile(File breakpointFile) %7B%0A%09%09this.breakpointFile = breakpointFile;%0A%09%7D%0A%09%0A%09
 %0A%7D%0A

Read breakpoint file from classpath