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 String disFile;
	static String jsonDir;

	public static void main(String[] args) {

		if (args.length == 2) {
			disFile = args[0];
			jsonDir = args[1];
		} else {
			System.out.println("two args disFile jsonDir");
			System.exit(-1);
		}
		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();
		}

	}

}

@@ -455,17 +455,17 @@
 mate = %22
-,
+;
 %22;%0A%09stat
@@ -784,17 +784,17 @@
 pend(%22id
-,
+;
 publishe
@@ -800,17 +800,17 @@
 edAt
-,
+;
 content
-,
+;
 loca
@@ -821,17 +821,17 @@
 Name
-,
+;
 geo_long
 ,geo
@@ -830,21 +830,22 @@
 long
-,
+;
 geo_lat
-,
+;
 tags
+;
 %22);%0A
@@ -4151,32 +4151,101 @@
 %09%09%09%09%7D%0A%09%09%09%09%09%09%09%09%7D%0A
+%09%09%09%09%09%09%09%09string1.replace(string1.length() - 1, string1.length(), %22%22);%0A
 %09%09%09%09%09%09%09%09writer.a

delimiter is changed to semicolon