package it.unibz.krdb.obda.owlapi3.bootstrapping;

import it.unibz.krdb.obda.io.ModelIOManager;
import it.unibz.krdb.obda.model.OBDAModel;

import java.io.File;
import java.net.URI;

import org.semanticweb.owlapi.model.OWLOntology;

public class DirectMappingBootstrapperCMD {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// check argument correctness
		if (args.length != 5) {
			System.out.println("Usage:");
			System.out
					.println(" DirectMappingBootstrapperCMD jdbc_url username password driver owlfile");
			System.out.println("");
			System.out.println(" jdbc_url    The jdbc url path");
			System.out.println(" username    The database username");
			System.out.println(" password    The database password");
			System.out.println(" driver      The jdbc driver class name");
			System.out.println(" owlfile  The full path to the owl output file");
			System.out.println("");
			return;
		}

		// get parameter values
		String url = args[0].trim();
		String user = args[1].trim();
		String passw = args[2].trim();
		String driver = args[3].trim();
		String owlfile = null;
		String obdafile = null;
		if (args.length == 5)
		{
			owlfile = args[4].trim();
			if (owlfile.endsWith(".owl"))
				obdafile = owlfile.substring(0, owlfile.length()-4)+".obda";
		}
		try {
			if (owlfile != null) {
				File owl = new File(owlfile);
				File obda = new File(obdafile);
				if (owl.canWrite() && obda.canWrite()){
				DirectMappingBootstrapper dm = new DirectMappingBootstrapper(owl.toURI().toString(), url, 
						user, passw, driver);
				OBDAModel model = dm.getModel();
				OWLOntology onto = dm.getOntology();
				ModelIOManager mng = new ModelIOManager(model);
				mng.save(obda);
				onto.getOWLOntologyManager().saveOntology(onto);
				}
				else
					System.out.println("Cannot write to the specified folder! " +owl.getAbsolutePath()+" "+obda.getAbsolutePath());
			} else {
				System.out.println("Output file not found!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

@@ -1444,53 +1444,8 @@
 );%0D%0A
-%09%09%09%09if (owl.canWrite() && obda.canWrite())%7B%0D%0A
 %09%09%09%09
@@ -1780,23 +1780,18 @@
 %0A%09%09%09
-%09%7D%0D%0A%09%09%09%09
+%7D 
 else
+ %7B
 %0D%0A
-%09
 %09%09%09%09
@@ -1814,196 +1814,211 @@
 ln(%22
-Cannot write to the specified folder! %22 +owl.getAbsolutePath()+%22 %22+obda.getAbsolutePath());%0D%0A%09%09%09%7D else %7B%0D%0A%09%09%09%09System.out.println(%22Output file not found!%22);%0D%0A%09%09%09%7D%0D%0A%09%09%7D catch (Exception e) %7B
+Output file not found!%22);%0D%0A%09%09%09%7D%0D%0A%09%09%7D catch (Exception e) %7B%0D%0A%09%09%09System.out.println(%22Error occured during bootstrapping: %22+e.getMessage());%0D%0A%09%09%09System.out.println(%22Debugging information for developers: %22);
 %0D%0A%09%09

fix in write permissions upon creation of the file