V1_1.java [src/java/m/dssat/substor] 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 m.dssat.substor;

import csip.api.server.Executable;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.Resource;
import javax.ws.rs.Path;
import csip.annotations.*;
import static csip.annotations.ResourceType.EXECUTABLE;
import csip.utils.ZipFiles;
import java.io.File;

/**
 * Fahrenheit conversion.
 *
 * @author od
 */
@Name("DSSAT simulation execution")
@Description("DSSAT SUBSTOR csip service")
@VersionInfo("1.1")
@Path("m/substor/1.1")
@Options(timeout = "P2H")
@Resource(type = EXECUTABLE, file = "/DSSAT_4.7.5.8/dscsm047", id = "DSSAT")
@Resource(file = "/DSSAT_4.7.5.8/data.zip", type = ResourceType.ARCHIVE)
public class V1_1 extends ModelDataService {

    static final String KEY_FILE = "param";
    static final String KEY_LOGLEVEL = "loglevel";


    @Override
    protected void doProcess() throws Exception {
        String paramFile = parameter().getString(KEY_FILE);
        String loglevel = parameter().getString(KEY_LOGLEVEL, "INFO");

        Executable p = resources().getExe("DSSAT");
        p.addArguments(
                "A",
                paramFile
        );

        int ret = p.exec();
        if (ret != 0) {
            throw new ServiceException("Error running DSSAT 4.7.5.8: " + ret);
        }
    }


    @Override
    protected void postProcess() throws Exception {
        File zip = ZipFiles.zip(new File(workspace().getDir().getAbsolutePath()));
        results().put(zip);
    }


    public static void onContextInit() {
        System.out.println("Init simple service");
    }


    public static void onContextDestroy() {
        System.out.println("destroy simple service");
    }

}