package dk.aau.cs.qweb.airbase.database;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.io.FileUtils;

import dk.aau.cs.qweb.airbase.Config;
import dk.aau.cs.qweb.airbase.types.Quad;
import dk.aau.cs.qweb.airbase.types.TripleContainer;

public class TurtleFile extends Database {

	@Override
	public void writeToDisk(TripleContainer triples) {
		Set<Quad> all = new HashSet<Quad>();
		all.addAll(triples.getInformationTriples());
		all.addAll(triples.getMetadataTriples());
		all.addAll(triples.getProvenanceTriples());
		
		try(FileWriter fw = new FileWriter(Config.getDBLocation()+"/airbaseCube"+Config.getCurrentInputFilePath()+".ttl", true);
			    BufferedWriter bw = new BufferedWriter(fw);
			    PrintWriter out = new PrintWriter(bw))
			{
				for (Quad quad : all) {
					out.println(quad.toString());
				}
			} catch (IOException e) {
			    //exception handling left as an exercise for the reader
			}
	}

	@Override
	public void cleanWrite() throws IOException {
		File file = new File(Config.getDBLocation()+"/airbaseCube"+Config.getCurrentInputFilePath()+".ttl");
		if (file.exists()) {
			FileUtils.forceDelete(file);
		}
		
	}

}

@@ -680,16 +680,164 @@
 ());%0A%09%09%0A
+%09%09if (all.size() %3E 0) %7B%0A%09%09%09System.out.println(%22writing %22+all.size()+%22 triples to %22+Config.getDBLocation()+Config.getCurrentInputFileName()+%22.nq%22);%0A%09
 %09%09try(Fi
@@ -884,39 +884,24 @@
 BLocation()+
-%22/airbaseCube%22+
 Config.getCu
@@ -914,28 +914,27 @@
 nputFile
-Path
+Name
 ()+%22.
-ttl
+nq
 %22, true)
@@ -1032,16 +1032,17 @@
 er(bw))%0A
+%09
 %09%09%09%7B%0A%09%09%09
@@ -1042,16 +1042,17 @@
 %09%09%7B%0A%09%09%09%09
+%09
 for (Qua
@@ -1067,16 +1067,17 @@
  all) %7B%0A
+%09
 %09%09%09%09%09out
@@ -1103,16 +1103,17 @@
 ing());%0A
+%09
 %09%09%09%09%7D%0A%09%09
@@ -1113,16 +1113,17 @@
 %09%09%09%7D%0A%09%09%09
+%09
 %7D catch 
@@ -1147,69 +1147,11 @@
 %0A%09%09%09
-    //exception handling left as an exercise for the reader%0A%09
+%09%7D%0A
 %09%09%7D%0A
@@ -1262,23 +1262,8 @@
 n()+
-%22/airbaseCube%22+
 Conf
@@ -1288,20 +1288,19 @@
 File
-Path
+Name
 ()+%22.
-ttl
+nq
 %22);%0A
@@ -1318,16 +1318,43 @@
 exists()
+ && Config.isDbCleanWrite()
 ) %7B%0A%09%09%09F

change file defualt naming and added some better error handling