V1_1.java [src/java/d/crlmod/cropImport] Revision: default Date:
/*
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API, and application suite.
*
* 2012-2024, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package d.crlmod.cropImport;
import crlmod.ServiceResources;
import static crlmod.ServiceResources.*;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.*;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Path;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.codehaus.jettison.json.JSONException;
/**
*
* @author User
*/
@Name("crop Import")
@Description("crlmod crop Import")
@VersionInfo("1.1")
@Path("d/cropImport/1.1")
@Resource(from = ServiceResources.class)
public class V1_1 extends ModelDataService {
private int rowsAffected = -1;
@Override
protected void doProcess() throws Exception {
String sourceModel = parameter().getString("source_model");
if (sourceModel.equalsIgnoreCase("weps")) {
importWeps();
} else if (sourceModel.equalsIgnoreCase("wepp")) {
importWepp();
} else {
throw new ServiceException("required input missing 'source_model' must be included in your json.");
}
}
@Override
protected void postProcess() throws Exception {
results().put("rowsAffected", rowsAffected);
}
private void importWeps() throws Exception {
File unzipDir = workspace().getFile("WEPSCrops");
File[] unZippedFiles = unzipDir.listFiles();
//Read into weps_op object
List<Cr> wepsCrops = getCropsFromFiles(unZippedFiles);
String query = "INSERT INTO weps_crops (weps_crop_path, weps_crop_name, weps_crop_param_set)\n"
+ "VALUES\n";
for (int i = 0; i < wepsCrops.size(); ++i) {
query += "\n('" + wepsCrops.get(i).crop_path + "', '"
+ wepsCrops.get(i).crop_name + "', '"
+ wepsCrops.get(i).crop_param_set.replaceAll("'", "''") + "')";
if (i != wepsCrops.size() - 1)
query += ",";
else
query += ";";
}
try (Connection connection = resources().getJDBC(getJDBCId())) {
try (Statement statement = connection.createStatement()) {
connection.setAutoCommit(false);
rowsAffected = statement.executeUpdate(query);
connection.commit();
statement.clearBatch();
}
}
}
private void importWepp() throws JSONException, SQLException, ServiceException, IOException {
File unzipDir = workspace().getFile("WEPPCrops");
File[] unZippedFiles = unzipDir.listFiles();
//Read into wepp_op object
List<Cr> weppCrops = getCropsFromFiles(unZippedFiles);
String query = "INSERT INTO wepp_crops (wepp_crop_path, wepp_crop_name, wepp_crop_param_set)\n"
+ "VALUES\n";
for (int i = 0; i < weppCrops.size(); ++i) {
query += "\n('" + weppCrops.get(i).crop_path + "', '"
+ weppCrops.get(i).crop_name + "', '"
+ weppCrops.get(i).crop_param_set.replaceAll("'", "''") + "')";
if (i != weppCrops.size() - 1)
query += ",";
else
query += ";";
}
try (Connection connection = resources().getJDBC(getJDBCId())) {
try (Statement statement = connection.createStatement()) {
connection.setAutoCommit(false);
rowsAffected = statement.executeUpdate(query);
connection.commit();
statement.clearBatch();
}
}
}
public List<Cr> getCropsFromFiles(File[] cropFiles) throws IOException {
List<Cr> crops = new ArrayList<>();
String basePath = workspace().getDir().getAbsolutePath();
for (int i = 0; i < cropFiles.length; ++i) {
if (cropFiles[i].isDirectory())
crops.addAll(getCropsFromFiles(cropFiles[i].listFiles()));
else {
Cr crop = new Cr();
//Get the path...so complicated.
String path = cropFiles[i].getParent().replace(basePath, "");
if (path.indexOf("\\", 2) != -1)
path = path.substring(path.indexOf("\\", 2) + 1);
else
path = "";
//End of path craziness
System.out.println("[" + path + "]");
crop.crop_path = path;
crop.crop_name = FilenameUtils.getBaseName(cropFiles[i].getName());
crop.crop_param_set = FileUtils.readFileToString(cropFiles[i], "UTF-8");
//crop.crop_param_set = Cr.crop_param_set.replaceAll("\n", "").replaceAll("\t", "");
crops.add(crop);
}
}
return crops;
}
protected String getJDBCId() {
return CR_LMOD_ID;
}
@Override
protected Map<String, Object> getConfigInfo() {
return new LinkedHashMap<String, Object>() {
{
put(getJDBCId(), resources().getResolved(getJDBCId()));
}
};
}
}