All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prerna.engine.impl.json.FlatXML Maven / Gradle / Ivy

There is a newer version: 4.2.2
Show newest version
package prerna.engine.impl.json;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import net.minidev.json.JSONArray;

import com.github.wnameless.json.flattener.JsonFlattener;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.JsonPath;

public class FlatXML {
	
	// Things I need to get stuff done
	
	// INPUT
	// Input Sequence - if not present, it is just the input URL
	// INPUT_Type - can be a file
	// INPUT_URL - the main URL to hit to get the data from
	// METHOD - Get / Post
	// Parameters - Names of parameters separated by ;
	// Parameter_Default - default value to use for this parameter
	
	// Output
	// OUTPUT_TYPE Table Vs. Graph vs. Hashtable - For now I will only handle table and Hashtable
	// Path_Pattern = All the path patterns separated by ;
	// If not specified the path is treated as a single value - if it it not it is stringified
	// later, we can also add in where the path pattern can have previous zooms. We will come to that in a bit
	// = alias header to use

	// the output is finally cobbled into a R datatable through a c()
	
	public static final String input_type = "input_type";
	public static final String output_type = "output_type";
	public static final String path_patterns = "path_patterns";
	public static final String concat = "concat";
	public static final String delim = "delim";

	
	Properties prop = null;
	Object document = null;
	
	
	public static void main(String [] args) throws Exception
	{
		// get the XML
		// convert to json
		// flatten json
		FlatXML client = new FlatXML();
		//String file = "c:\\users\\pkapaleeswaran\\workspacej3\\datasets\\interaction.json";
		String file = "c:\\users\\pkapaleeswaran\\workspacej3\\datasets\\repos.json";
		String propFile = "c:\\users\\pkapaleeswaran\\workspacej3\\datasets\\reposProp.prop";
		//client.getURLData("https://api.fda.gov/drug/event.json?search=patient.drug.medicinalproduct:%22Robitussen%22+patient.reaction.reactionoutcome:%225%22");
		//client.loadProp(propFile);
		//client.getData(file);
		//client.flattenJson(null);
		client.flattenJsonFromFile(file);
	}
	
	
	
	
	public void loadProp(String propFile)
	{
		try {
			this.prop = new Properties();
			prop.load(new FileInputStream(propFile));
			if(prop.containsKey("input_type") && ((String)prop.get("input_type")).equalsIgnoreCase("file"))
				document = Configuration.defaultConfiguration().jsonProvider().parse(new FileInputStream(prop.getProperty("input_url")), "utf-8");

			execQuery(prop.getProperty("path_patterns"));
				
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void getURLData(String url)
	{
		String retString = null;
		
		try {
			
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpGet httpget = new HttpGet(url);
			
			ResponseHandler handler = new BasicResponseHandler();
			CloseableHttpResponse response = httpclient.execute(httpget);
			
			retString = handler.handleResponse(response);
			
			System.out.println(">> " + retString);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		//return retString;

	}
	
	public Object execQuery(String pathPatterns)
	{
		// the exec query has to get a couple of different things
		// parameters for the input
		// selection for the output
		// output metadata - may not be needed
		// need to use a better delimiter
		String [] pathParts = pathPatterns.split("@@@");
		String [] inputParams = null;
		String [] jsonPaths = null;
		String [] types = null; 
		if(pathParts.length == 2)
		{
			inputParams = pathParts[0].split(";");		
			jsonPaths = pathParts[1].split(";");
			
			// process input here
		}
		else
		{
			jsonPaths = pathParts[0].split(";");			
		}
				
		JSONArray [] data = new JSONArray[jsonPaths.length];
		types = new String[jsonPaths.length];
		int numRows = 0;
		StringBuffer headers = null;
		
		for(int pathIndex = 0;pathIndex < jsonPaths.length;pathIndex++)
		{
			data[pathIndex] = JsonPath.read(document, jsonPaths[pathIndex]);
			
			if(headers == null)
				headers = new StringBuffer(prop.getProperty(jsonPaths[pathIndex]));
			else
				headers.append(",").append(prop.getProperty(jsonPaths[pathIndex]));
						
			if(numRows == 0 || numRows > data[pathIndex].size())
				numRows = data[pathIndex].size();
			
			System.out.println(" >> " + data[pathIndex].toString());
			System.out.println("Length >> " + data[pathIndex].size());
			
			Object firstOne = data[pathIndex].get(0);
			if(firstOne instanceof Integer)
				types[pathIndex] = "int";
			if(firstOne instanceof JSONArray)
				types[pathIndex] = "String";
			if(firstOne instanceof Double)
				types[pathIndex] = "Double";
		}
		
		System.out.println("Output..  " + data);
		
		Hashtable retHash = new Hashtable();
		retHash.put("TYPES", types);
		retHash.put("HEADERS", headers);
		retHash.put("DATA", data);
		retHash.put("COUNT", numRows);
		
		writeCSV(data, "random.csv", numRows, headers.toString());

		return retHash;
	}
	
	public void writeCSV(JSONArray [] data, String fileName, int numRows, String headers)
	{
		// I need to see the number of elements and based on that process through the stuff
		String [] cols = new String [data.length];
		int numItems = data.length;
		
		
		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
			bw.write(headers);
			bw.write("\n");
			
			for(int rowIndex = 0;rowIndex < numRows;rowIndex++)
			{
				// for each row get hte column and print
			
				StringBuffer composer = null;
				
				for(int colIndex = 0;colIndex < cols.length;colIndex++)
				{
					JSONArray thisArray = data[colIndex];
					
					String thisData = thisArray.get(rowIndex).toString().replace(",","_");
					
					if(composer == null)
						composer = new StringBuffer(thisData);
					else
						composer = composer.append(",").append(thisData);
				}
				
				bw.write(composer.toString());
				bw.write("\n");
			}
			
			bw.flush();
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void getData(String file)
	{
		try {
			InputStream inputStream = new FileInputStream(file);
			Reader reader = new InputStreamReader(inputStream);
			BufferedReader breader = new BufferedReader(reader);
			
			String jsonString = breader.readLine();
			System.out.println("String is .. " + jsonString);

			Object document = Configuration.defaultConfiguration().jsonProvider().parse(jsonString);

			// getting a particular element
			//Object rxcuis = JsonPath.read(document, "$..[?(@.rxcui == \"75207\")]");
			
			// getting from an array
			//Object rxcuis = JsonPath.read(document, "$..[?(\"656659\" in @.['rxcuis'])]");
			
			Object rxcuis = JsonPath.read(document, "$..minConcept[*].rxcui");
			
			
//			Object severity = JsonPath.read(document, "$..name");

			// get all interaction pairs where there is a severity of high
			JSONArray severity = JsonPath.read(document, "$..interactionPair[?(@.severity == \"high\")])");
			
			String severityStr = severity.toJSONString();
			Object document2 = Configuration.defaultConfiguration().jsonProvider().parse(severityStr);

			// get the name from min concept
			Object name = JsonPath.read(document2, "$..minConceptItem['name']");
			

			System.out.println("Value..  " + rxcuis + " \n" + severity);

			System.out.println("Value..  " + rxcuis + " \n" + name);

		} catch (InvalidJsonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Map  flattenJson(String json)
	{
		if(json == null)
			json = "{ \"a\" : { \"b\" : 1, \"c\": null, \"d\": [false, true] }, \"e\": \"f\", \"g\":2.3 }";
		Map flattenJson = JsonFlattener.flattenAsMap(json);
		
		String output = JsonFlattener.flatten(json);
		
		System.out.println(flattenJson);
		
		System.out.println(output);
		
		return flattenJson;
	}

	public Map  flattenJsonFromFile(String file) throws Exception
	{
		InputStream inputStream = new FileInputStream(file);
		Reader reader = new InputStreamReader(inputStream);
		BufferedReader breader = new BufferedReader(reader);
		
		//String jsonString = breader.readLine();
		//System.out.println(jsonString);

		//flattenJson(jsonString);
		// Support Reader as input 
		JsonFlattener jf = new JsonFlattener(reader);
		
		Map flattenJson = jf.flattenAsMap();
		
		//System.out.println(">>> " + jf.flatten());
		
		//System.out.println(flattenJson);
		
		//System.out.println(output);
		
		return flattenJson;
	}

	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy