V1_0.java [src/java/m/dssat/utils/replaceyear] 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.utils.replaceyear;
import csip.ModelDataService;
import csip.api.server.PayloadParameter;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Options;
import csip.annotations.VersionInfo;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import javax.ws.rs.Path;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
/**
*
* @author sidereus
*/
@Name("DSSAT replace dates")
@Description("DSSAT replace dates csip service")
@VersionInfo("1.0")
@Path("d/replaceyear/1.0")
@Options(timeout = "P2H")
public class V1_0 extends ModelDataService {
private static final String TMP_DSSAT = "tmp.DSSAT";
private static final String FILE_NAME = "file_name";
private static final String YEAR_1 = "year_1";
private static final String YEAR_2 = "year_2";
private static final String YEAR_3 = "year_3";
private static final String YEAR_4 = "year_4";
private static final String YEAR_5 = "year_5";
String year_1;
String year_2;
String year_3;
String year_4;
String year_5;
@Override
protected synchronized void doProcess() throws Exception {
PayloadParameter pp = parameter();
String template = pp.getString(FILE_NAME);
year_1 = pp.getString(YEAR_1);
year_2 = pp.getString(YEAR_2, "NA");
year_3 = pp.getString(YEAR_3, "NA");
year_4 = pp.getString(YEAR_4, "NA");
year_5 = pp.getString(YEAR_5, "NA");
results().put(createConfig(template));
}
public String getYear_1() {
return year_1;
}
public String getYear_2() {
return year_2;
}
public String getYear_3() {
return year_3;
}
public String getYear_4() {
return year_4;
}
public String getYear_5() {
return year_5;
}
private File renameInputTemplate(String template) {
String sourcePath = workspace().getFile(template).getAbsolutePath();
String destPath = workspace().getDir().getAbsolutePath() + "/" + TMP_DSSAT;
File sourceTemplate = new File(sourcePath);
File destTemplate = new File(destPath);
try {
Files.copy(sourceTemplate.toPath(), destTemplate.toPath());
} catch (IOException ex) {
throw new RuntimeException(ex.getMessage());
}
try {
if (!Files.readAllLines(sourceTemplate.toPath()).equals(Files.readAllLines(destTemplate.toPath()))) {
throw new RuntimeException("File not fully copied");
} else {
Files.delete(sourceTemplate.toPath());
}
} catch (IOException ex) {
throw new RuntimeException(ex.getMessage());
}
return sourceTemplate;
}
private File createConfig(String template) throws IOException {
VelocityEngine velocity = new VelocityEngine();
velocityInit(velocity);
VelocityContext context = new VelocityContext();
context.put("dssat", this);
File filledTemplate = renameInputTemplate(template);
try (FileWriter w = new FileWriter(filledTemplate.getAbsolutePath())) {
Template vt = velocity.getTemplate(TMP_DSSAT, "utf-8");
vt.merge(context, w);
}
return filledTemplate;
}
public void velocityInit(VelocityEngine velocity) {
velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
workspace().getDir().getAbsolutePath());
velocity.init();
}
}