/**
 * *****************************************************************************
 * Copyright 2012 University of Southern California
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * This code was developed by the Information Integration Group as part of the
 * Karma project at the Information Sciences Institute of the University of
 * Southern California. For more information, publications, and related
 * projects, please see: http://www.isi.edu/integration
 * ****************************************************************************
 */
/**
 *
 */
package edu.isi.karma.imp.json;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.isi.karma.imp.Import;
import edu.isi.karma.rep.ColumnMetadata.DataStructure;
import edu.isi.karma.rep.HNode;
import edu.isi.karma.rep.HTable;
import edu.isi.karma.rep.Node;
import edu.isi.karma.rep.RepFactory;
import edu.isi.karma.rep.Row;
import edu.isi.karma.rep.Table;
import edu.isi.karma.rep.Worksheet;
import edu.isi.karma.rep.Workspace;
import edu.isi.karma.rep.metadata.WorksheetProperties.Property;
import edu.isi.karma.rep.metadata.WorksheetProperties.SourceTypes;
import edu.isi.karma.util.FileUtil;
import edu.isi.karma.util.JSONUtil;

/**
 * @author szekely
 * @author mielvandersande
 * 
 */
public class JsonImport extends Import {

	private static Logger logger = LoggerFactory.getLogger(JsonImport.class);
	private final Object json;
	private int maxNumLines;
	private int numObjects;
	static JsonImportValues jiv;
	public JsonImport(Object json, String worksheetName, Workspace workspace,
			String encoding, int maxNumLines) {
		super(worksheetName, workspace, encoding);
		this.json = json;
		this.maxNumLines = maxNumLines;
	}

	public JsonImport(File jsonFile, String worksheetName, Workspace workspace,
			String encoding, int maxNumLines) {
		super(worksheetName, workspace, encoding);

		String fileContents = null;
		try {
			fileContents = FileUtil
					.readFileContentsToString(jsonFile, encoding);
		} catch (IOException ex) {
			logger.error("Error in reading the JSON file");
		}

		this.json = JSONUtil.createJson(fileContents);
		this.maxNumLines = maxNumLines;
	}

	public JsonImport(String jsonString, String worksheetName,
			Workspace workspace, String encoding, int maxNumLines) {
		this(JSONUtil.createJson(jsonString), worksheetName, workspace,
				encoding, maxNumLines);
	}

	public JsonImport(String jsonString, RepFactory repFactory, Worksheet wk,
			int maxNumLines) {
		this(JSONUtil.createJson(jsonString), repFactory, wk, maxNumLines);
	}

	public JsonImport(Object json, RepFactory repFactory, Worksheet wk,
			int maxNumLines) {
		super(repFactory, wk);
		this.json = json;
		this.maxNumLines = maxNumLines;
	}

	@SuppressWarnings("static-access")
	@Override
	public Worksheet generateWorksheet() throws JSONException {
		numObjects = 0;
		boolean importJson = false;
		if (json instanceof JSONArray) {
			getWorksheet().getMetadataContainer().getWorksheetProperties().setWorksheetDataStructure(DataStructure.COLLECTION);
			JSONArray a = (JSONArray) json;
			for (int i = 0; i < a.length(); i++) {
				jiv.addListElement(a.get(i), getWorksheet().getHeaders(),
						getWorksheet().getDataTable(), maxNumLines, numObjects, getFactory(), getWorksheet());
				if (maxNumLines > 0 && numObjects >= maxNumLines)
					break;
			}
			importJson = true;
		} else if (json instanceof JSONObject) {
			getWorksheet().getMetadataContainer().getWorksheetProperties().setWorksheetDataStructure(DataStructure.OBJECT);
			jiv.addKeysAndValues((JSONObject) json, getWorksheet().getHeaders(),
					getWorksheet().getDataTable(), maxNumLines, numObjects, getFactory(), getWorksheet());
			importJson = true;
		}

		if (importJson)
			writeJsonFile(json);
		Worksheet ws = getWorksheet();
		ws.getMetadataContainer().getWorksheetProperties().setPropertyValue(Property.sourceType, SourceTypes.JSON.toString());
		return ws;
	}

	private static void writeJsonFile(Object o) {
		JSONUtil.writeJsonFile(o, "lastJsonImport.json");
	}

	
}

@@ -1150,174 +1150,8 @@
 ion;
-%0Aimport java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.Collections;%0Aimport java.util.Iterator;%0Aimport java.util.LinkedList;%0Aimport java.util.List;
 %0A%0Aim
@@ -1409,176 +1409,18 @@
 rep.
-HNode;%0Aimport edu.isi.karma.rep.HTable;%0Aimport edu.isi.karma.rep.Node;%0Aimport edu.isi.karma.rep.RepFactory;%0Aimport edu.isi.karma.rep.Row;%0Aimport edu.isi.karma.rep.Table
+RepFactory
 ;%0Aim
@@ -1952,36 +1952,8 @@
 s;%0A%09
-static JsonImportValues jiv;
 %0A%09pu
@@ -3184,44 +3184,8 @@
 %09%7D%0A%0A
-%09@SuppressWarnings(%22static-access%22)%0A
 %09@Ov
@@ -3535,19 +3535,32 @@
 ) %7B%0A%09%09%09%09
-jiv
+JsonImportValues
 .addList
@@ -3957,11 +3957,24 @@
 %0A%09%09%09
-jiv
+JsonImportValues
 .add

Access JsonImportValues static methods using Class