package com.bio4j.exporter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jettison.json.JSONObject;

import com.tinkerpop.gremlin.structure.Graph;
import com.tinkerpop.gremlin.structure.Vertex;
import com.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
import com.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
import com.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;

//holds business logic behind the exporter
public class ExporterCore {
	private String outputFormat;
	private int limit;
	private int maxTime;
	private boolean stream;
	private String source;
	private String query;
	private Graph graph;

	private final String[] supportedFormats = { "gexf", "graphml", "graphson" };

	public ExporterCore() {
	}

	public String getFormat() {
		return this.outputFormat;
	}

	public void setFormat(String format) throws Exception {
		this.outputFormat = null;
		String lowercaseFormat = format.toLowerCase();
		// check whether the format is supported or not
		for (String supported : supportedFormats) {
			if (lowercaseFormat.equals(supported)) {
				this.outputFormat = lowercaseFormat;
				break;
			}
		}
		if (this.outputFormat == null) {
			throw new Exception("Format not supported: " + format);
		}
	}

	public int getLimit() {
		return this.limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public void setLimit(String limit) {
		setLimit(Integer.parseInt(limit));
	}

	public int getMaxTime() {
		return this.maxTime;
	}

	public void setMaxTime(int maxTime) {
		this.maxTime = maxTime;
	}

	public void setMaxTime(String maxTime) {
		setMaxTime(Integer.parseInt(maxTime));
	}

	public boolean isStream() {
		return this.stream;
	}

	public void setStream(boolean stream) {
		this.stream = stream;
	}

	public void setStream(String stream) throws Exception {
		if(stream == null){
			throw new Exception("Stream flag can't be null");
		}
		String lowercaseStream = stream.toLowerCase();
		if (lowercaseStream.equals("yes")) {
			this.stream = true;
		} else if (lowercaseStream.equals("no")) {
			this.stream = false;
		} else {
			throw new Exception("Stream flag requires 'yes' or 'no'");
		}
	}

	public String getSource() {
		return this.source;
	}

	public void setSource(String source) throws Exception {
		if(source == null){
			throw new Exception("Source can't be null");
		} 
		this.source = source;
	}

	public String getQuery() {
		return this.query;
	}

	public void setQuery(String query) {
		if(query == null){
			this.query = null;
			return;
		}
		query = query.replace("[", "(");
		query = query.replace("]", ")");
		this.query = query;		
	}

	public void runQuery() {
		final TinkerGraph g = TinkerFactory.createClassic();
		try {
			FileOutputStream f = new FileOutputStream("bio4j.json");
			if(this.outputFormat.equals("graphson")){
				// Export query to graphSON
				GraphSONWriter w = GraphSONWriter.create().build();
				w.writeVertices(f, g.traversal(GoTraversal.class).goTerms().out("created"));
				System.out.println("==> exported to bio4j.json");
			}
			if(this.outputFormat.equals("graphml")){
				//TODO export to graphml
			}
		} catch (Exception e) {
			e.printStackTrace();
		}			
	}

	public void shutdownGraph() {
		// TODO Auto-generated method stub
		
	}
}


@@ -216,24 +216,74 @@
 SONObject;%0A%0A
+import com.tinkerpop.gremlin.structure.Direction;%0A
 import com.t
@@ -3061,16 +3061,33 @@
 reate().
+embedTypes(true).
 build();
@@ -3165,16 +3165,32 @@
 reated%22)
+, Direction.BOTH
 );%0A%09%09%09%09S
@@ -3315,25 +3315,119 @@
  graphml%0A%09%09%09
-%7D
+%09%0A%09%09%09%7D%0A%09%09%09if(this.outputFormat.equals(%22gexf%22))%7B%0A%09%09%09%09//TODO export to graphml%0A%09%09%09%7D%0A%09%09%09f.close();
 %0A%09%09%7D catch (

Improve GraphSON exporting