V1_0.java [src/java/m/cfa/storetservice] Revision:   Date:
package m.cfa.storetservice;

import WaterData.WaterData;
import WaterData.WaterDataInterface;
import csip.ModelDataService;
import csip.api.server.PayloadParameter;
import csip.api.server.PayloadResults;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.VersionInfo;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.ws.rs.Path;

@Name("storetservice")
@Description("CSIP end point for STORET time series soap-based web service")
@VersionInfo("1.0")
@Path("m/cfa/storetservice/1.0")
public class V1_0 extends ModelDataService {

    File resultFile = null;
    
    @Override
    protected void doProcess() throws Exception {
        String directory = workspace().getDir().toString();
        
        //Get Inputs
        PayloadParameter inputPayload = parameter();
        String orgId = inputPayload.getString("org_id");
        String stationId = inputPayload.getString("station_id");
        String wqTest = inputPayload.getString("wq_test");
        String startDate = inputPayload.getString("begin_date", "");
        String endDate = inputPayload.getString("end_date", "");
        
        //Extract data from database
        WaterDataInterface waterLib = WaterData.getNewWaterDataInterface("STORET", "");
        ArrayList<String> rawData = new ArrayList<>();
        if(wqTest.equalsIgnoreCase("flow")){
            rawData = waterLib.extractFlowData_raw(directory, orgId, stationId, startDate, endDate);
        }else{
            rawData = waterLib.extractWaterQualityData_raw(directory, orgId, stationId, startDate, endDate, wqTest);
        }
        
        //Save results file
        String fileName = "STORET_" + orgId + "_" + stationId + "_results.txt";
        fileName = fileName.replace("/", "");
        resultFile = new File(directory, fileName);
        writeResultFile(directory, fileName, rawData);
    }

    @Override
    protected void postProcess() throws Exception {
        PayloadResults resultPayload = results();
        // files
        resultPayload.put(resultFile);
        // values
        resultPayload.put("result_file", resultFile.getName());
    }
    
    /**
     * Writes out the output file to the specified location
     * @param directory  the working directory to create the file in
     * @param fileName  the name of the result file
     * @param rawData  ArrayList to be written as each line of the text file
     * @throws IOException 
     */
    private void writeResultFile(String directory, String fileName, ArrayList<String> rawData) throws IOException{
        //Write output to file
        String path = directory + File.separator + fileName;
        FileWriter writer =  new FileWriter(path, false);
        PrintWriter print_line = new PrintWriter(writer);
        for(int i=0; i<rawData.size(); i++){
            print_line.printf("%s" + "\r\n", rawData.get(i));
        }
        print_line.close();
        writer.close();
        System.out.println("File located at: " + path);
    }
}