V2_0.java [src/java/d/crlmod/managementImport] 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.managementImport;
import crlmod.ServiceResources;
import static crlmod.ServiceResources.*;
import crlmod.nodes.*;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.*;
import java.io.File;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.ws.rs.Path;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
*
* @author Lucas Yaege
*/
@Name("management Import")
@Description("Management import service for the CRLMOD_2018 Database")
@VersionInfo("2.0")
@Path("d/managementImport/2.0")
@Resource(from = ServiceResources.class)
public class V2_0 extends ModelDataService {
private static HashMap operations;
private static HashMap crops;
private static HashMap residues;
public static final SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy");
private int rowsAffected = -1;
@Override
protected void doProcess() throws Exception {
importManagements();
}
@Override
protected void postProcess() throws Exception {
results().put("rowsAffected", rowsAffected);
}
private void importManagements() throws Exception {
File[] unZippedFiles = null;
File unzipDir = new File(workspace().getDir() + File.separator + "WEPSManagements");
if (unzipDir.canRead()) {
unZippedFiles = unzipDir.listFiles();
}
try (Connection connection = resources().getJDBC(getJDBCId())) {
connection.setAutoCommit(false);
operations = Management.GetOpPointers(connection);
crops = Management.GetCropPointers(connection);
residues = Management.GetResiduePointers(connection);
importManagementFromFiles(unZippedFiles, connection);
}
}
public void importManagementFromFiles(File[] files, Connection connection) throws Exception {
if (null != files) {
for (int i = 0; i < files.length; ++i) {
if (files[i].isDirectory() && !files[i].getName().startsWith(".")) {
importManagementFromFiles(files[i].listFiles(), connection);
} else {
if (!files[i].getName().startsWith(".") && !files[i].getName().startsWith("_")) {
importManagementFromFile(files[i], connection);
}
}
}
}
}
public void importManagementFromFile(File file, Connection connection) throws Exception {
ArrayList<Event> events = new ArrayList<Event>();
//System.out.println("Management Name: " + file.getAbsolutePath());
//read and parse xml
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
NodeList nl_duration = doc.getElementsByTagName("rotationyears");
NodeList NLops = doc.getElementsByTagName("op");
//TODO: lots of assumptions here...error check.
String str_duration = nl_duration.item(0).getTextContent();
int int_duration = Integer.parseInt(str_duration);
if (NLops.getLength() == 0) {
System.out.println("--Management " + file.getAbsolutePath() + " has no events...skipping for now");
return;
}
for (int i = 0; i < NLops.getLength(); i++) {
Event event = new Event();
Element Eop = (Element) NLops.item(i);
NodeList opName = Eop.getElementsByTagName("name");
NodeList opDate = Eop.getElementsByTagName("date");
NodeList vegs = Eop.getElementsByTagName("veg");
event.operation = new Operation();
event.operation.name = opName.item(0).getTextContent();
event.date = formatter.parse(opDate.item(0).getTextContent());
if (vegs.getLength() != 0) {
Element veg = (Element) vegs.item(0);
Element vegName = (Element) veg.getElementsByTagName("name").item(0);
event.crop = new Crop();
event.crop.name = vegName.getTextContent();
}
events.add(event);
//System.out.println(event);
}
String pathPrefix = workspace().getDir().getPath() + File.separator + "WEPSManagements" + File.separator;
Management management = new Management(file.getAbsolutePath().replace(pathPrefix, ""));
management.duration = int_duration;
management.events = events;
management.ResolveAllPointers(operations, crops, residues);
management.PerformInsert(connection);
System.out.println("Inserted: " + management.path + File.separator + management.name);
rowsAffected++;
}
protected String getJDBCId() {
return CR_LMOD_2018_ID;
}
@Override
protected Map<String, Object> getConfigInfo() {
return new LinkedHashMap<String, Object>() {
{
put(getJDBCId(), resources().getResolved(getJDBCId()));
}
};
}
}