Translator.java [src/translators] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package translators;

import csip.api.server.ServiceException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import nodes.Management;
import nodes.WEPSManagement;
import nodes.factories.ManagementFactory;
import org.apache.commons.io.FileUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.xml.sax.SAXException;
import utils.TranslatorException;
import utils.Urls;
import writers.ROTWriter;
import writers.WEPSWriter;

/**
 *
 * @author Brad
 */
public class Translator {

  public enum FORMAT {
    KEY, WEPS, WEPP;
  }

  private final Urls urls;

  public Translator(Urls urls) {
    this.urls = urls;
  }

  public WEPSManagement translateToWEPS(JSONObject rot) throws TranslatorException {
    WEPSManagement man;
    try {
      man = ( WEPSManagement ) loadLMODManagement(rot, FORMAT.WEPS);
    } catch (Exception ex) {
      Logger.getLogger(Translator.class.getName()).log(Level.SEVERE, null, ex);
      throw new TranslatorException("Translation failed. " + ex.getMessage(), ex);
    }

    return man;
  }

  public WEPSManagement translateToWEPS(JSONObject rot, File manFile) throws TranslatorException {

    WEPSManagement man = translateToWEPS(rot);

    WEPSWriter writer = new WEPSWriter(man);

    try {
      System.out.println("Attempting to write weps management to: " + manFile.getAbsolutePath());
      writer.writeManFile(manFile);      
      return man;
    } catch (Exception ex) {
      Logger.getLogger(Translator.class.getName()).log(Level.SEVERE, null, ex);
      throw new TranslatorException("Failed to write WEPS management.", ex);
    }
  }
  
  public String translateToWEPSString(JSONObject rot) throws JSONException, SQLException, XMLStreamException, ParserConfigurationException, ParserConfigurationException, SAXException, TranslatorException, TranslatorException, Exception, Exception {
    WEPSManagement man = translateToWEPS(rot);
    WEPSWriter writer = new WEPSWriter(man);
    return writer.writeManFile(new StringWriter());
    
//    String fileName = "temp.man";
//    File manFile = new File(System.getProperty("java.io.tmpdir") + File.pathSeparator + fileName);
//    translateToWEPS(rot, manFile);
//
//    return FileUtils.readFileToString(manFile, Charset.defaultCharset());
  }

  public JSONObject translateToLMOD(String manFileName) throws ParserConfigurationException, SAXException, IOException, FileNotFoundException, JSONException, TranslatorException, Exception {
    File manFile = new File(manFileName);

    Management man = loadWEPSManagement(manFile);

    ROTWriter writer = new ROTWriter();

    JSONObject rot = writer.createROTJSON(man);

    return rot;
  }

  public Management translateToWEPP(JSONObject rot, String manFileName) throws JSONException, SQLException, ParserConfigurationException, XMLStreamException, XMLStreamException, SAXException, SAXException, TranslatorException, Exception {

    Management man = loadLMODManagement(rot, FORMAT.WEPP);

    return man;
  }

  private Management loadLMODManagement(JSONObject rot, FORMAT type) throws IOException, JSONException, SQLException, XMLStreamException, ParserConfigurationException, SAXException, FileNotFoundException, TranslatorException, Exception {

    Management man = ManagementFactory.createManagement(type, urls);

    man.readJSONData(rot, type);

    man.fillLMODData(type);

    return man;
  }

  public Management loadWEPSManagement(File manFile) throws XMLStreamException, ParserConfigurationException, SAXException, IOException, FileNotFoundException, JSONException, TranslatorException, Exception {

    WEPSManagement man = new WEPSManagement(urls);

    man.readManData(manFile);

    man.convertResidues();

    man.fillLMODData(FORMAT.KEY);

    return man;
  }
}