/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:set ts=4 sw=4 sts=4 et: */

package jp.oist.flint.executor;

import jp.oist.flint.phsp.PhspException;
import jp.oist.flint.sedml.SedmlException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import javax.swing.SwingWorker;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import jp.oist.flint.dao.SimulationDao;
import jp.oist.flint.filesystem.Workspace;
import jp.oist.flint.phsp.IPhspConfiguration;
import jp.oist.flint.phsp.PhspWriter;
import jp.oist.flint.phsp.entity.Model;
import jp.oist.flint.sedml.ISimulationConfigurationList;
import jp.oist.flint.sedml.SedmlWriter;

public class PhspSimulator extends SwingWorker <Boolean, Integer> {

    private final SimulatorService mService;

    private final File mWorkingDir;

    private SimulationDao mSimulationDao;

    private final ISimulationConfigurationList mSedmlConfig;

    private IPhspConfiguration mPhspConfig = null;

    private File mSedmlFile = null;

    private File mPhspFile = null;

    private File mLogFile = null;

    public PhspSimulator (SimulatorService service, 
                            ISimulationConfigurationList sedml, 
                            IPhspConfiguration phsp) 
        throws IOException, ParserConfigurationException, PhspException,
               SQLException, SedmlException, TransformerException {

        mService = service;
        mSedmlConfig = sedml;
        mPhspConfig = phsp;

        Model[] models = phsp.getModels();

        for (Model model : models)
            model.validate();


        mPhspFile = Workspace.createTempFile("phsp", ".phsp");
        mPhspFile.deleteOnExit();

        mSedmlFile = Workspace.createTempFile("xml", ".xml");
        mSedmlFile.deleteOnExit();

        try (FileOutputStream phspStream = new FileOutputStream(mPhspFile)) {
            PhspWriter phspWriter = new PhspWriter();

            phspWriter.write(mPhspConfig, phspStream, true);
        }
        try (FileOutputStream sedmlStream = new FileOutputStream(mSedmlFile)) {
            SedmlWriter sedmlWriter = new SedmlWriter(true);
            sedmlWriter.writeSimulationConfiguration(mSedmlConfig, sedmlStream);
        }
            mWorkingDir = Workspace.createTempDirectory("flint-exec");
    }

    public File getSedmlFile () {
        return mSedmlFile;
    }

    public File getPhspFile () {
        return mPhspFile;
    }

    public File getLogFile () {
        return mLogFile;
    }

    public ISimulationConfigurationList getSimulationConfigurationList () {
        return mSedmlConfig;
    }

    public IPhspConfiguration getPhspConfiguration () {
        return mPhspConfig;
    }

    public File getWorkingDirectory () {
        return mWorkingDir;
    }

    public SimulationDao getSimulationDao () {
        return mSimulationDao;
    }

    public boolean isStarted () {
        return StateValue.STARTED.equals(getState());
    }

    @Override
    protected Boolean doInBackground() throws Exception {
        mSimulationDao = new SimulationDao(mWorkingDir);
        mLogFile = new File(mWorkingDir, "flint.log");
        mLogFile.deleteOnExit();

        FlintExecJob job = new FlintExecJob(mSedmlFile, mPhspFile, mWorkingDir);
        return mService.call(job, mLogFile);
    }

}

@@ -937,16 +937,22 @@
 private 
+final 
 Simulati
@@ -1048,16 +1048,22 @@
 private 
+final 
 IPhspCon
@@ -1084,23 +1084,16 @@
 spConfig
- = null
 ;%0A%0A    p
@@ -1095,24 +1095,30 @@
     private 
+final 
 File mSedmlF
@@ -1116,31 +1116,24 @@
 e mSedmlFile
- = null
 ;%0A%0A    priva
@@ -1131,24 +1131,30 @@
     private 
+final 
 File mPhspFi
@@ -1151,31 +1151,24 @@
 le mPhspFile
- = null
 ;%0A%0A    priva
@@ -1170,16 +1170,22 @@
 private 
+final 
 File mLo
@@ -1193,15 +1193,8 @@
 File
- = null
 ;%0A%0A 
@@ -2344,20 +2344,16 @@
         
-    
 mWorking
@@ -2399,24 +2399,169 @@
 int-exec%22);%0A
+        mSimulationDao = new SimulationDao(mWorkingDir);%0A        mLogFile = new File(mWorkingDir, %22flint.log%22);%0A        mLogFile.deleteOnExit();%0A
     %7D%0A%0A    p
@@ -3288,154 +3288,8 @@
 n %7B%0A
-        mSimulationDao = new SimulationDao(mWorkingDir);%0A        mLogFile = new File(mWorkingDir, %22flint.log%22);%0A        mLogFile.deleteOnExit();%0A%0A
     

make them final