package lodVader.tupleManager;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.openrdf.model.Statement;
import org.openrdf.rio.helpers.RDFHandlerBase;

import lodVader.LODVaderProperties;
import lodVader.utils.NSUtils;
import lodVader.utils.Timer;

public class SplitAndStoreNT extends SuperTupleManager {

	final static Logger logger = LoggerFactory.getLogger(SplitAndStoreNT.class);

	BufferedWriter dumpNTFile = null;
	
	NSUtils nsUtils = new NSUtils();

	public SplitAndStoreNT(ConcurrentLinkedQueue<String> subjectQueue, ConcurrentLinkedQueue<String> objectQueue,
			String fileName, int distributionID) {
		this.objectQueue = objectQueue;
		this.subjectQueue = subjectQueue;
		this.fileName = fileName;
		this.distributionID = distributionID;

		startFiles();
	}

	@Override
	public void startFiles() {
		try {
			if (subjectQueue != null && objectQueue != null)
				dumpNTFile = new BufferedWriter(new FileWriter(fileName));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void closeFiles() {
		try {
			if (dumpNTFile != null)
				dumpNTFile.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public void saveStatement(String stSubject, String stPredicate, String stObject) {

		if (stSubject.startsWith("http")) {
			stSubject = "<" + stSubject + ">";
		}
		if (stPredicate.startsWith("http")) {
			stPredicate = "<" + stPredicate + ">";
		}
		if (stObject.startsWith("http")) {
			stObject = "<" + stObject + ">";
		}
		
		addToMap(allPredicates,  nsUtils.getNSFromString(stPredicate));

//		if (stObject.equals("<http://www.w3.org/2002/07/owl#Class>")) {
//			addToMap(owlClasses, stSubject);
//		} else if (stPredicate.equals("<http://www.w3.org/2000/01/rdf-schema#subClassOf>")) {
//			addToMap(rdfSubClassOf, stObject);
//		} else 
		
		if (stPredicate.equals("<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>")) {
			addToMap(rdfTypeObjects, nsUtils.getNSFromString(stObject));
		}

		try {

			dumpNTFile.write(stSubject + " " + stPredicate + " " + stObject + " ." +  "\n");

			if (totalTriplesRead % 1000000 == 0) {
				logger.info("Triples read: " + totalTriplesRead + ", time: " + t.stopTimer());
				t = new Timer();
				t.startTimer();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		totalTriplesRead++;

	}

	@Override
	public void handleStatement(Statement st) {
		String stSubject = st.getSubject().toString();
		String stPredicate = st.getPredicate().toString();
		String stObject = st.getObject().toString();
		saveStatement(stSubject, stPredicate, stObject);
	}

}

@@ -1395,16 +1395,235 @@
 ect) %7B%0A%0A
+%09%09addToMap(allPredicates,  nsUtils.getNSFromString(stPredicate));%0A%09%09%0A%09%09if (stPredicate.equals(%22http://www.w3.org/1999/02/22-rdf-syntax-ns#type%22)) %7B%0A%09%09%09addToMap(rdfTypeObjects, nsUtils.getNSFromString(stObject));%0A%09%09%7D%0A%09%09%0A
 %09%09if (st
@@ -1861,477 +1861,8 @@
 %09%09%7D%0A
-%09%09%0A%09%09addToMap(allPredicates,  nsUtils.getNSFromString(stPredicate));%0A%0A//%09%09if (stObject.equals(%22%3Chttp://www.w3.org/2002/07/owl#Class%3E%22)) %7B%0A//%09%09%09addToMap(owlClasses, stSubject);%0A//%09%09%7D else if (stPredicate.equals(%22%3Chttp://www.w3.org/2000/01/rdf-schema#subClassOf%3E%22)) %7B%0A//%09%09%09addToMap(rdfSubClassOf, stObject);%0A//%09%09%7D else %0A%09%09%0A%09%09if (stPredicate.equals(%22%3Chttp://www.w3.org/1999/02/22-rdf-syntax-ns#type%3E%22)) %7B%0A%09%09%09addToMap(rdfTypeObjects, nsUtils.getNSFromString(stObject));%0A%09%09%7D
 %0A%0A%09%09

Update SplitAndStoreNT.java