Management.java [src/java/crlmod/nodes] Revision: default Date:
/*
* $Id: 1.0+62 Management.java a170e1637ffa 2021-12-20 od $
*
* 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 crlmod.nodes;
import static d.crlmod.managementImport.V1_1.formatter;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
/**
*
* @author User
*/
public class Management {
public BigInteger id;
public String path;
public String name;
public Integer duration;
public double stir;
public ArrayList<Event> events;
public String lmodData;
public String wepsData;
public String weppData;
static final String WEPS_MGMT = "wepsmgmt.man";
public Management(BigInteger fk) throws SQLException {
id = fk;
this.events = new ArrayList<>();
}
public Management() {
this.events = new ArrayList<>();
}
public Management(String fullPath) {
//TODO: break fullPath apart so that you populate name and path objects.
this.path = fullPath.substring(0, fullPath.lastIndexOf("\\"));
this.name = fullPath.substring(fullPath.lastIndexOf("\\") + 1);
}
public Management(BigInteger fk, String name, String path) throws SQLException {
this.id = fk;
this.name = name;
this.path = path;
}
public Management(BigInteger fk, String name, String path, String status) throws SQLException {
id = fk;
this.name = name;
this.path = path;
}
public Management(BigInteger fk, String name, String path, String date, String owner, String password) throws SQLException {
id = fk;
this.name = name;
this.path = path;
}
public BigInteger getID() {
return id;
}
public String getFile_path() {
return path;
}
public String getFile_name() {
return name;
}
@Override
public int hashCode() {
int hash = 3;
hash = 83 * hash + Objects.hashCode(this.id);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Management other = (Management) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
return true;
}
public void getWEPSformat() {
throw new RuntimeException("get WEPS Format is not implemented for managements.");
}
public void getWEPPformat() {
throw new RuntimeException("WEPP Format Translation is not yet complete.");
/*Rotation rot = new Rotation();
rot.managements.add(this);
Translator translator = new Translator();
try
{
//weppData = translator.translateToWEPP(rot.getJson());
}
catch(Exception ex)
{
throw new ServiceException(ex);
}
*/
}
//##################Management Import Helper Functions##############
public boolean ResolveAllPointers(HashMap operations, HashMap crops, HashMap residues) throws ClassNotFoundException, SQLException {
//for every event in the rotation
for (int i = 0; i < events.size(); i++) {
Event e = events.get(i);
//find and set operation id
if (operations.containsKey(e.operation.name)) {
e.operation.id = (BigInteger) operations.get(e.operation.name);
} else {
throw new IllegalArgumentException("Failed to resolve operation pointer: ["
+ e.operation.name
+ "] in Management File: "
+ this.name);
}
//if crop exists, find and set crop/res id.
if (e.crop != null && crops.containsKey(e.crop.name)) {
e.crop.id = (BigInteger) crops.get(e.crop.name);
} else if (e.crop != null && residues.containsKey(e.crop.name)) {
e.residue = new Residue();
e.residue.name = e.crop.name;
e.residue.id = (BigInteger) residues.get(e.residue.name);
e.crop = null;
} else if (e.crop != null) {
throw new IllegalArgumentException("Failed to resolve crop pointer: ["
+ e.crop.name
+ "] in Management File: "
+ this.name);
}
}
return true;
}
public static HashMap GetOpPointers(Connection connection) throws SQLException {
HashMap operations = new HashMap();
Statement statement = connection.createStatement();
String query = "SELECT weps_op_name, op_id FROM operations LEFT JOIN weps_ops ON (operations.fk_weps_op_id = weps_op_id);";
ResultSet result = statement.executeQuery(query);
while (result.next()) {
operations.put(result.getString("weps_op_name"), result.getBigDecimal("op_id").toBigInteger());
}
result.close();
return operations;
}
public static HashMap GetCropPointers(Connection connection) throws SQLException {
HashMap crops = new HashMap();
Statement statement = connection.createStatement();
String query = "SELECT weps_crop_name, crop_id FROM crops LEFT JOIN weps_crops ON (crops.fk_weps_crop_id = weps_crop_id);";
ResultSet result = statement.executeQuery(query);
while (result.next()) {
crops.put(result.getString("weps_crop_name"), result.getBigDecimal("crop_id").toBigInteger());
}
return crops;
}
public static HashMap GetResiduePointers(Connection connection) throws SQLException {
HashMap residues = new HashMap();
Statement statement = connection.createStatement();
String query = "SELECT weps_res_name, res_id FROM residues LEFT JOIN weps_residues ON (residues.fk_weps_res_id = weps_res_id);";
ResultSet result = statement.executeQuery(query);
while (result.next()) {
residues.put(result.getString("weps_res_name"), result.getBigDecimal("res_id").toBigInteger());
}
return residues;
}
public String PerformInsert(Connection connection) throws SQLException {
BigInteger manID;
//Insert Management (name, path)
String manInsert = "INSERT INTO managements (man_path, man_name, man_duration) VALUES ('"
+ this.path.replace("'", "''") + "', '"
+ this.name.replace("'", "''").replace(".skel", "") + "', "
+ this.duration + ")";
//System.out.println(manInsert);
Statement statement = connection.createStatement();//connection.prepareStatement(manInsert, Statement.RETURN_GENERATED_KEYS);
statement.executeUpdate(manInsert, Statement.RETURN_GENERATED_KEYS);
ResultSet rs = statement.getGeneratedKeys();
if (rs.next()) {
manID = rs.getBigDecimal(1).toBigInteger();
} else {
throw new IllegalStateException("No id returned from the database for management insert. This is bad.");
}
rs.close();
statement.close();
String eventQuery = "INSERT INTO events(fk_man_id, event_index, date, fk_op_id, fk_crop_id, fk_res_id) VALUES\n";
//Insert events related to that management
for (int i = 0; i < events.size(); i++) {
Event e = events.get(i);
eventQuery += "(" + manID + ", '"
+ i + "', '"
+ formatter.format(e.date) + "', "
+ e.operation.id + ", "
+ (e.crop != null ? e.crop.id : null) + ", "
+ (e.residue != null ? e.residue.id : null) + "),\n";
}
eventQuery = eventQuery.substring(0, eventQuery.length() - 2);
eventQuery += ";";
statement = connection.createStatement();
statement.executeUpdate(eventQuery);
connection.commit();
return "";
}
}