package org.sagebionetworks;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.apache.commons.io.FileUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.sagebionetworks.schema.EnumValue;
import org.sagebionetworks.schema.ObjectSchema;
import org.sagebionetworks.schema.adapter.JSONArrayAdapter;
import org.sagebionetworks.schema.adapter.JSONObjectAdapter;
import org.sagebionetworks.schema.adapter.JSONObjectAdapterException;
import org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl;

/**
 * Utility to convert old enumeration to the new enumerations with for all
 * schemas.
 *
 */
public class UpgradeEnumerations {
	
	public static void main(String[] args) throws IOException, JSONObjectAdapterException, JSONException {

		File dir = new File("src/main/resources");
		Iterator<?> itr = FileUtils.iterateFiles(dir, new String[] { "json" }, true);

		while (itr.hasNext()) {
			File file = (File) itr.next();
			JSONObjectAdapterImpl adapter = null;
			try {
				String json = readFileToString(file.getAbsolutePath());
				adapter = new JSONObjectAdapterImpl(json);
				ObjectSchema schema = new ObjectSchema(adapter);
			} catch (JSONObjectAdapterException e) {
				if(e.getMessage().contains("JSONArray[0] is not a JSONObject")) {
					System.out.println("Need to translate: "+file.getAbsolutePath());
					if(adapter.has(ObjectSchema.JSON_ENUM)) {
						JSONArrayAdapter array = adapter.getJSONArray(ObjectSchema.JSON_ENUM);
						JSONArrayAdapter newArray = translateOldToNew(array);
						// Replace the old array with a new array
						adapter.put(ObjectSchema.JSON_ENUM, newArray);
						JSONObject object = new JSONObject(adapter.toJSONString());
						System.out.println(object.toString(4));
					}
				}
			}
		}
	}
	
	private static JSONArrayAdapter translateOldToNew(JSONArrayAdapter array) throws JSONObjectAdapterException {
		JSONArrayAdapter newArray = array.createNewArray();
		for(int i=0; i<array.length(); i++) {
			String name = array.getString(i);
			EnumValue value = new EnumValue(name, "TODO Auto-generated description");
			JSONObjectAdapter newValueAdapter = array.createNew();
			value.writeToJSONObject(newValueAdapter);
			newArray.put(i, newValueAdapter);
		}
		return newArray;
	}

	private static String readFileToString(String filePath) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(filePath));
		try {
			String line = null;
			StringBuilder sb = new StringBuilder();
			String ls = System.getProperty("line.separator");
			while ((line = reader.readLine()) != null) {
				sb.append(line);
				sb.append(ls);
			}
			return sb.toString();
		} finally {
			reader.close();
		}
	}
	
	
}

@@ -1480,16 +1480,726 @@
 ));%0D%0A%09%09%09
+%09%09translateEnumRecursive(adapter);%0D%0A%09%09%09%09%09JSONObject object = new JSONObject(adapter.toJSONString());%0D%0A%09%09%09%09%09System.out.println(object.toString(4));%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A%09private static void translateEnumRecursive(JSONObjectAdapter adapter) throws JSONObjectAdapterException %7B%0D%0A%09%09translateLeaf(adapter);%0D%0A%09%09Iterator%3CString%3E keyIt = adapter.keys();%0D%0A%09%09while(keyIt.hasNext()) %7B%0D%0A%09%09%09String key = keyIt.next();%0D%0A%09%09%09try %7B%0D%0A%09%09%09%09JSONObjectAdapter child = adapter.getJSONObject(key);%0D%0A%09%09%09%09translateEnumRecursive(child);%0D%0A%09%09%09%7D catch (JSONObjectAdapterException e) %7B%0D%0A%09%09%09%09// not an object so ignore%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A%09private static void translateLeaf(JSONObjectAdapter adapter) throws JSONObjectAdapterException %7B%0D%0A
 %09%09if(ada
@@ -2234,27 +2234,24 @@
 NUM)) %7B%0D%0A%09%09%09
-%09
-%09%09
 JSONArrayAda
@@ -2306,27 +2306,24 @@
 SON_ENUM);%0D%0A
-%09%09%09
 %09%09%09JSONArray
@@ -2371,19 +2371,16 @@
 y);%0D%0A%09%09%09
-%09%09
-%09
 // Repla
@@ -2414,19 +2414,16 @@
  array%0D%0A
-%09%09%09
 %09%09%09adapt
@@ -2469,143 +2469,8 @@
 );%0D%0A
-%09%09%09%09%09%09JSONObject object = new JSONObject(adapter.toJSONString());%0D%0A%09%09%09%09%09%09System.out.println(object.toString(4));%0D%0A%09%09%09%09%09%7D%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A
 %09%09%7D%0D
@@ -2769,16 +2769,17 @@
 e, %22TODO
+:
  Auto-ge

correct translation