package edu.ucsd.sbrg.escher.utilities;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.main.JsonValidator;
import de.zbit.util.ResourceManager;
import edu.ucsd.sbrg.escher.EscherConverter;
import edu.ucsd.sbrg.escher.model.EscherMap;
import org.sbgn.bindings.Sbgn;
import org.sbgn.schematron.SchematronValidator;
import org.sbml.jsbml.SBMLDocument;

import java.io.IOException;
import java.util.ResourceBundle;
import java.util.logging.Logger;

/**
 * Created by deveshkhandelwal on 27/06/16.
 */
public class Validation {

  /**
   * Localization support.
   */
  private static final transient ResourceBundle
      bundle           =
      ResourceManager.getBundle("Strings");
  /**
   * A {@link Logger} for this class.
   */
  private static final           Logger
      logger           =
      Logger.getLogger(Validation.class.getName());

  private static final           JsonValidator JSON_VALIDATOR = JsonSchemaFactory.byDefault().getValidator();
  private JsonSchema          escherSchema;
  private SchematronValidator schematronValidator;

  public Validation() throws IOException, ProcessingException {
    this(Utils.defaultEscherSchema());
  }


  public Validation(JsonNode jsonNode) throws IOException, ProcessingException {
    // TODO: Check if schema is valid.
    ProcessingReport report = Utils.jsonSchemaSchema().validate(jsonNode);

    if (report.isSuccess()) {
      // TODO: Log that schema is valid.
      escherSchema = JsonSchemaFactory.byDefault().getJsonSchema(jsonNode);
    }
    else {
      // TODO: Log about invalid schema file.
      throw new IllegalArgumentException("Invalid JSON Schema file!");
    }
  }


  public void validateEscher(EscherMap map) {
    throw new UnsupportedOperationException("Not yet Implemented!");
  }


  public void validateSbgnml(Sbgn document) {
    throw new UnsupportedOperationException("Not yet implemented!");
  }


  public void validateSbmlLE(SBMLDocument document) {
    throw new UnsupportedOperationException("Not yet implemented!");
  }
}

@@ -190,24 +190,88 @@
 gException;%0A
+import com.github.fge.jsonschema.core.report.ProcessingMessage;%0A
 import com.g
@@ -639,16 +639,50 @@
 s.Sbgn;%0A
+import org.sbgn.schematron.Issue;%0A
 import o
@@ -757,16 +757,174 @@
 cument;%0A
+import org.xml.sax.SAXException;%0A%0Aimport javax.xml.parsers.ParserConfigurationException;%0Aimport javax.xml.transform.TransformerException;%0Aimport java.io.File;
 %0Aimport 
@@ -936,32 +936,55 @@
 io.IOException;%0A
+import java.util.List;%0A
 import java.util
@@ -1641,16 +1641,40 @@
 lidator;
+%0A  private File logFile;
 %0A%0A  publ
@@ -2278,41 +2278,859 @@
 lic 
-void validateEscher(EscherMap map
+boolean validateEscher(File file) throws IOException %7B%0A    JsonNode node = JsonLoader.fromFile(file);%0A    try %7B%0A      ProcessingReport report = escherSchema.validate(node);%0A      return report.isSuccess();%0A    %7D catch (ProcessingException e) %7B%0A      // TODO: Log.%0A      e.printStackTrace();%0A    %7D%0A    return false;%0A  %7D%0A%0A%0A  public boolean validateSbgnml(File file) throws IOException %7B%0A    try %7B%0A      SchematronValidator.setSvrlDump(true);%0A      List%3CIssue%3E issues = SchematronValidator.validate(file);%0A%0A      if (issues == null %7C%7C issues.isEmpty()) %7B%0A        return true;%0A      %7D%0A    %7D catch (TransformerException e) %7B%0A      e.printStackTrace();%0A    %7D catch (SAXException e) %7B%0A      e.printStackTrace();%0A    %7D catch (ParserConfigurationException e) %7B%0A      e.printStackTrace();%0A    %7D%0A%0A    return false;%0A  %7D%0A%0A%0A  public boolean validateSbmlLE(File file
 ) %7B%0A
@@ -3182,17 +3182,17 @@
 Not yet 
-I
+i
 mplement
@@ -3222,36 +3222,23 @@
 oid 
-validateSbgnml(Sbgn document
+log(Issue issue
 ) %7B%0A
@@ -3330,44 +3330,37 @@
 oid 
-validateSbmlLE(SBMLDocument document
+log(ProcessingMessage message
 ) %7B%0A
@@ -3431,11 +3431,85 @@
 !%22);%0A  %7D
+%0A%0A  public void setLogFile(File logFile) %7B%0A    this.logFile = logFile;%0A  %7D
 %0A%7D%0A

Define validation methods. SBML not done right now.