package org.obolibrary.robot;

import java.io.File;
import java.io.FileWriter;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.profiles.OWLProfile;
import org.semanticweb.owlapi.profiles.OWL2Profile;
import org.semanticweb.owlapi.profiles.OWLProfileReport;
import org.semanticweb.owlapi.profiles.Profiles;

public class ValidateProfileCommand implements Command  {

	private final Options options;

	public ValidateProfileCommand() {
        Options o = CommandLineHelper.getCommonOptions();
        o.addOption("o", "output", true, "save validation report to a file");
        o.addOption("p", "profile", true, "OWL profile to validate (DL, EL, RL, QL, or Full)");
        o.addOption("i", "input",     true, "validate ontology from a file");
        o.addOption("I", "input-iri", true, "validate ontology from an IRI");
        options = o;
    }

	@Override
	public String getName() {
		return "validate-profile";
	}

	@Override
	public String getDescription() {
		return "validate ontology against an OWL profile";
	}

	@Override
	public String getUsage() {
		return "robot validate-profile --profile <profile> --output <file>";
	}

	@Override
	public Options getOptions() {
		return options;
	}

	@Override
	public void main(String[] args) {
        try {
            execute(null, args);
        } catch (Exception e) {
            CommandLineHelper.handleException(getUsage(), getOptions(), e);
        }
    }

	@Override
	public CommandState execute(CommandState state, String[] args)
			throws Exception {
		CommandLine line = CommandLineHelper
				.getCommandLine(getUsage(), getOptions(), args);
		if (line == null) {
			return null;
		}
		IOHelper ioHelper = CommandLineHelper.getIOHelper(line);
        state = CommandLineHelper.updateInputOntology(ioHelper, state, line);
        OWLOntology ontology = state.getOntology();
		String profile = CommandLineHelper.getOptionalValue(line, "profile").toUpperCase();
		final OWLProfile owlProfile;
		switch(profile) {
            case "DL": owlProfile = Profiles.OWL2_DL; break;
            case "EL": owlProfile = Profiles.OWL2_EL; break;
            case "RL": owlProfile = Profiles.OWL2_RL; break;
            case "QL": owlProfile = Profiles.OWL2_QL; break;
            case "FULL": owlProfile = new OWL2Profile(); break; // #162
            default: owlProfile = null;
		}
		OWLProfileReport report = owlProfile.checkOntology(ontology);
		File outputFile = CommandLineHelper.getOutputFile(line);
		if (outputFile != null) {
	        FileWriter writer = new FileWriter(outputFile);
	        writer.write(report.toString());
	        writer.close();
	    } else {
			System.out.println(report.toString());
		}
		return state;
	}

}

@@ -1358,34 +1358,19 @@
 ) %7B%0A
-        try %7B%0A            
+%09%09try %7B%0A%09%09%09
 exec
@@ -1386,24 +1386,18 @@
  args);%0A
-        
+%09%09
 %7D catch 
@@ -1412,28 +1412,19 @@
 on e) %7B%0A
-            
+%09%09%09
 CommandL
@@ -1483,22 +1483,13 @@
 e);%0A
-        %7D%0A    
+%09%09%7D%0A%09
 %7D%0A%0A%09
@@ -1626,17 +1626,16 @@
 lper%0A%09%09%09
-%09
 .getComm
@@ -1776,24 +1776,18 @@
 (line);%0A
-        
+%09%09
 state = 
@@ -1848,24 +1848,18 @@
  line);%0A
-        
+%09%09
 OWLOntol
@@ -1938,22 +1938,26 @@
 .get
-Optional
+Required
 Value(
+%0A%09%09%09
 line
@@ -1967,17 +1967,65 @@
 profile%22
-)
+, %22A --profile is required%22);%0A%09%09profile = profile
 .toUpper
@@ -2083,28 +2083,19 @@
 file) %7B%0A
-            
+%09%09%09
 case %22DL
@@ -2131,36 +2131,27 @@
 _DL; break;%0A
-            
+%09%09%09
 case %22EL%22: o
@@ -2183,36 +2183,27 @@
 _EL; break;%0A
-            
+%09%09%09
 case %22RL%22: o
@@ -2235,36 +2235,27 @@
 _RL; break;%0A
-            
+%09%09%09
 case %22QL%22: o
@@ -2291,28 +2291,19 @@
  break;%0A
-            
+%09%09%09
 case %22FU
@@ -2358,46 +2358,70 @@
 162%0A
-            default: owlProfile = null
+%09%09%09default: throw new Exception(%22Invalid profile: %22 + profile)
 ;%0A%09%09
@@ -2574,24 +2574,18 @@
 ull) %7B%0A%09
-        
+%09%09
 FileWrit
@@ -2621,32 +2621,26 @@
 tputFile);%0A%09
-        
+%09%09
 writer.write
@@ -2661,24 +2661,18 @@
 ng());%0A%09
-        
+%09%09
 writer.c
@@ -2680,20 +2680,17 @@
 ose();%0A%09
-    
+%09
 %7D else %7B
@@ -2736,16 +2736,116 @@
 ));%0A%09%09%7D%0A
+%09%09if (!report.isInProfile()) %7B%0A%09%09%09throw new Exception(%22Ontology is not in profile %22 + profile);%0A%09%09%7D%0A
 %09%09return

Throw on profile violation, closes #163