V1_0.java [src/java/m/dssat/utils/replacedate] 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.replacedate;
import csip.ModelDataService;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Options;
import csip.annotations.VersionInfo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.ws.rs.Path;
/**
*
* @author kush
*/
@Name("DSSAT replace dates")
@Description("DSSAT replace dates csip service")
@VersionInfo("1.0")
@Path("d/replacedate/1.0")
@Options(timeout = "P2H")
public class V1_0 extends ModelDataService {
static final String START_DATE_OLD = "start_date_old";
static final String START_DATE = "start_date"; //NEW START DATE
@Override
protected void doProcess() throws Exception {
String start_date_old = parameter().getString(START_DATE_OLD);
String start_date = parameter().getString(START_DATE);
File[] ptxFiles = this.getPTXFiles(workspace().getDir().getAbsolutePath());
for (File currPTXFile : ptxFiles) {
processDatesInPTXFile(currPTXFile, start_date.split("-")[0].substring(2, 4), start_date_old.split("-")[0].substring(2, 4));
}
results().put(ptxFiles);
}
private File[] getPTXFiles(String folderPathToPTXFile) {
File folderWithPTXFile = new File(folderPathToPTXFile).getAbsoluteFile();
return folderWithPTXFile.listFiles((File dir, String filename) -> filename.toLowerCase().endsWith(".ptx"));
}
private void processDatesInPTXFile(File ptxFile, String replaceWith, String toReplace) throws IOException {
File fromFile = ptxFile.getAbsoluteFile();
String newPTXFileName = ptxFile.getAbsolutePath() + ".old";
File toFile = new File(newPTXFileName);
processDatesInPTXFile(fromFile, toFile, replaceWith, toReplace);
}
private void processDatesInPTXFile(File ptxFile, File newPTXFile, String replaceWith, String toReplace)
throws IOException {
// debug
PrintWriter pw = new PrintWriter(new FileWriter(newPTXFile.getAbsoluteFile()), true);
BufferedReader br = new BufferedReader(new FileReader(ptxFile));
String line;
boolean inNewSection = false;
ArrayList<Integer> rows = new ArrayList();
while ((line = br.readLine()) != null) {
if (line.trim().startsWith("*")) {
inNewSection = true;
pw.println(line);
continue;
} else if (line.trim().equals("")) {
inNewSection = false;
pw.println();
rows.clear();
continue;
} else {
if (inNewSection) {
if (lineHasDateRow(line)) {
rows = getDateRows(line);
pw.println(line);
} else {
String[] lineSplit = line.split("\\b");
String newStr = "";
if (rows.size() == 0) {
newStr = line;
pw.println(newStr);
continue;
}
for (int i = 0; i < lineSplit.length; i++) {
if (rows.contains(i)) {
newStr += lineSplit[i].trim().replaceFirst(toReplace, replaceWith);
} else {
newStr += lineSplit[i];
}
}
pw.println(newStr);
}
} else {
pw.println(line);
}
}
}
br.close();
pw.close();
ptxFile.delete();
newPTXFile.renameTo(ptxFile);
}
private boolean lineHasDateRow(String line) {
if (line.trim().startsWith("@"))
;
String[] lineArr = line.split("\\s+");
for (String currString : lineArr) {
if (currString.endsWith("DAT") || currString.endsWith("DATE")) {
return true;
}
}
return false;
}
private ArrayList<Integer> getDateRows(String line) {
ArrayList<Integer> dateRows = new ArrayList();
if (line.trim().startsWith("@"))
;
String[] lineArr = line.split("\\b"); // includes spaces in split string
int currIndex = 0;
for (String currString : lineArr) {
if (currString.endsWith("DAT") || currString.endsWith("DATE")) {
dateRows.add(currIndex);
}
currIndex++;
}
return dateRows;
}
}