Created
November 2, 2015 18:28
-
-
Save utecht/0efb10ff9b75b99f99bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.uams.interview; | |
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.URL; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.HttpClientBuilder; | |
import sun.net.www.URLConnection; | |
public class Process { | |
//This is for posting the json data | |
public static void post(String url, String param ) throws Exception{ | |
String charset = "UTF-8"; | |
//Create connection | |
java.net.URLConnection connection = new URL(url).openConnection(); | |
//Set post | |
connection.setDoOutput(true); // Triggers POST. | |
//connection.setRequestProperty("Accept-Charset", charset); | |
//Set content type | |
connection.setRequestProperty("Content-Type", "application/json"); | |
//Create outputstream | |
try (OutputStream output = connection.getOutputStream()) { | |
output.write(param.getBytes(charset)); | |
} | |
InputStream response = connection.getInputStream(); | |
} | |
//This function is reading csv file | |
public static String run() | |
{ | |
String csvFile = "C:\\interview\\data.csv"; | |
BufferedReader br = null; | |
String line = ""; | |
String cvsSplitBy = ","; | |
try { | |
//Read file | |
br = new BufferedReader(new FileReader(csvFile)); | |
int id = 0; | |
String JsonData = "["; | |
//Read each line | |
while ((line = br.readLine()) != null) { | |
//Generate new id | |
id++; | |
//Split columns | |
String[] patientDAta = line.split(cvsSplitBy); | |
//This is a json object for patient | |
String oneObject = "{"; | |
//Note part | |
String arrayO = "["; | |
//Add new generated id | |
oneObject = oneObject + "'id':"+id+","; | |
String name = ""; | |
String realid = ""; | |
//Go through each column | |
for(int i=0; i<patientDAta.length;i++) | |
{ | |
patientDAta[i] = patientDAta[i].replaceAll("\"", ""); | |
if(i==0) //Skip patient name | |
{ | |
name = patientDAta[i]; // Save for cleanning note | |
continue; | |
} | |
else if(i==1) //Skip real id | |
{ | |
realid = patientDAta[i]; // Save for cleanning note | |
continue; | |
} | |
else if(i==2) // Add type to json | |
{ | |
oneObject = oneObject + "'type':"+patientDAta[i]+","; | |
} | |
else if(i==3) // Add description to json | |
{ | |
oneObject = oneObject + "'description':"+patientDAta[i]+","; | |
} | |
else if(i==4) // Add file path to json | |
{ | |
oneObject = oneObject + "'file_path':"+patientDAta[i]+","; | |
} | |
else | |
{ | |
//Combine note | |
if(arrayO.equals("[")) //If it is first item | |
{ | |
String cleanData = patientDAta[i].replaceAll(name, "NAME"); // Clean names from note | |
cleanData = patientDAta[i].replaceAll(realid, "ID"); // Clena id from note | |
arrayO = arrayO + cleanData; // Add to note array | |
} | |
else | |
{ | |
String cleanData = patientDAta[i].replaceAll(name, "NAME"); // Clean names from note | |
cleanData = patientDAta[i].replaceAll(realid, "ID");// Clena id from note | |
arrayO = arrayO +"," +cleanData; // Add to note array | |
} | |
} | |
} | |
arrayO = arrayO + "]"; | |
oneObject = oneObject + "'notes':"+arrayO; // Add all the notes to json | |
oneObject = oneObject + "}"; | |
try { | |
//System.out.println(oneObject); | |
//post("http://localhost:5000/image",oneObject); | |
} catch (Exception e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
//System.out.println(oneObject); | |
if(JsonData.equals("[")) // Add new patient json obj to array | |
{ | |
JsonData = JsonData + oneObject; | |
} | |
else | |
{ | |
JsonData = JsonData + ","+oneObject; | |
} | |
} | |
JsonData = JsonData + "]"; // Close array | |
//System.out.println(JsonData); | |
return JsonData; // return json | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
if (br != null) { | |
try { | |
br.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
return ""; | |
} | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
String data = run(); | |
try { | |
System.out.println(data); | |
post("http://localhost:5000/image",data); | |
} catch (Exception e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment