V1_1.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.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 od, ly
*/
@Name("management Import")
@Description("CRLMOD Management import service for the CRLMOD Database")
@VersionInfo("1.1")
@Path("d/managementImport/1.1")
@Resource(from = ServiceResources.class)
public class V1_1 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 unzipDir = workspace().getFile("WEPSManagements");
File[] 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 static String importManagementFromFiles(File[] files, Connection connection) throws Exception {
for (int i = 0; i < files.length; ++i) {
if (files[i].isDirectory())
importManagementFromFiles(files[i].listFiles(), connection);
else
importManagementFromFile(files[i], connection);
}
return null;
}
public static 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);
}
Management management = new Management(file.getAbsolutePath().replace("C:\\CR_LMOD Data\\WEPSMans", ""));
management.duration = int_duration;
management.events = events;
management.ResolveAllPointers(operations, crops, residues);
management.PerformInsert(connection);
System.out.println("Inserted: " + management.path + "\\" + management.name);
}
protected String getJDBCId() {
return CR_LMOD_ID;
}
@Override
protected Map<String, Object> getConfigInfo() {
return new LinkedHashMap<String, Object>() {
{
put(getJDBCId(), resources().getResolved(getJDBCId()));
}
};
}
}