package crisis.net;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.naming.StringRefAddr;

import org.apache.commons.lang.StringEscapeUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ToCsv {

	static final String delimate = ",";
	static final String disFile = "crisis.csv";
	static final String jsonDir = "/Users/user/crisisNet";

	public static void main(String[] args) {

		try {

			FileWriter writer = new FileWriter(disFile);
			writer.append("id,publishedAt,content,locationName,geo_long,geo_lat,tags");
			writer.append("\n");
			// read the json file
			File folder = new File(jsonDir);
			File[] listOfFiles = folder.listFiles();
			for (int f = 0; f < listOfFiles.length; f++) {
				if (listOfFiles[f].isFile() && listOfFiles[f].getAbsoluteFile().toString().contains("json")) {
					System.out.println("Processing file " + listOfFiles[f]);
					FileReader reader = new FileReader(listOfFiles[f]);
					JSONParser jsonParser = new JSONParser();
					JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
					JSONArray data = (JSONArray) jsonObject.get("data");
					// take the elements of the json array
					for (int i = 0; i < data.size(); i++) {
						JSONObject doc = (JSONObject) data.get(i);
						if (doc != null) {
							String id = (String) doc.get("id");
							if (id != null) {
								writer.append(StringEscapeUtils.escapeCsv(id) + delimate);
							} else {
								writer.append(delimate);
							}

							String publishedAt = (String) doc.get("publishedAt");
							if (publishedAt != null) {
								writer.append(StringEscapeUtils.escapeCsv(publishedAt) + delimate);
							} else {
								writer.append(delimate);
							}

							String content = (String) doc.get("content");
							if (content != null) {
								String subString = content;
								if (subString.length() > 20000) {
									 subString = content.substring(0, 20000);
								}
								writer.append(StringEscapeUtils.escapeCsv(subString.replaceAll("\n", "")) + delimate);
							} else {
								writer.append(delimate);
							}

							JSONObject geo = (JSONObject) doc.get("geo");
							if (geo != null) {
								JSONObject addressComponents = (JSONObject) geo.get("addressComponents");
								if (addressComponents != null) {
									String formattedAddress = (String) addressComponents.get("formattedAddress");
									String adminArea1 = (String) addressComponents.get("adminArea1");
									if (formattedAddress != null) {
										writer.append(StringEscapeUtils.escapeCsv(formattedAddress) + delimate);
									} else if (adminArea1 != null) {
										writer.append(StringEscapeUtils.escapeCsv(adminArea1) + delimate);
									} else {
										writer.append(delimate);
									}
								} else {
									writer.append(delimate);
								}

								JSONArray coords = (JSONArray) geo.get("coords");
								if (coords != null) {
									if (coords.get(0) != null) {
										writer.append(StringEscapeUtils.escapeCsv(coords.get(0).toString()) + delimate);
									} else {
										writer.append(delimate);
									}
									if (coords.get(1) != null) {
										writer.append(StringEscapeUtils.escapeCsv(coords.get(1).toString()) + delimate);
									} else {
										writer.append(delimate);
									}
								} else {
									writer.append(delimate + delimate);
								}
							} else {
								writer.append(delimate + delimate + delimate);
							}

							JSONArray tags = (JSONArray) doc.get("tags");
							if (tags != null) {
								StringBuilder string1 = new StringBuilder();
								for (int j = 0; j < tags.size(); j++) {
									JSONObject tag = (JSONObject) tags.get(j);
									if (tag != null) {
										String name = (String) tag.get("name");
										string1.append(name + ",");
									} else {
										writer.append(delimate);
									}
								}
								writer.append(StringEscapeUtils.escapeCsv(string1.toString()) + delimate);
							} else {
								writer.append(delimate);
							}
							writer.append("\n");
						}

					}

				}

			}
			writer.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (ParseException ex) {
			ex.printStackTrace();
		} catch (NullPointerException ex) {
			ex.printStackTrace();
		}

	}

}

@@ -459,38 +459,32 @@
  = %22,%22;%0A%09static 
-final 
 String disFile =
@@ -485,23 +485,8 @@
 File
- = %22crisis.csv%22
 ;%0A%09s
@@ -487,30 +487,24 @@
 le;%0A%09static 
-final 
 String jsonD
@@ -509,34 +509,8 @@
 nDir
- = %22/Users/user/crisisNet%22
 ;%0A%0A%09
@@ -551,16 +551,172 @@
 rgs) %7B%0A%0A
+%09%09if (args.length == 2) %7B%0A%09%09%09disFile = args%5B0%5D;%0A%09%09%09jsonDir = args%5B1%5D;%0A%09%09%7D else %7B%0A%09%09%09System.out.println(%22two args disFile jsonDir%22);%0A%09%09%09System.exit(-1);%0A%09%09%7D%0A
 %09%09try %7B%0A
@@ -2170,17 +2170,16 @@
 %09%09%09%09%09%09%09%09
- 
 subStrin

csv has args