From f15a6dafa6ca1591ddb410a447348a3f76966cfd Mon Sep 17 00:00:00 2001 From: kbolaughlin Date: Wed, 15 Mar 2023 15:05:33 -0700 Subject: [PATCH] TethysExportControl add post to tethys code in TethysExportControl --- src/tethys/dbxml/DBXMLConnect.java | 89 ++++++++++++++++++++ src/tethys/deployment/DeploymentHandler.java | 2 +- src/tethys/detection/DetectionsHandler.java | 2 +- src/tethys/output/StreamExportParams.java | 2 +- src/tethys/output/TethysExportParams.java | 9 +- src/tethys/output/TethysExporter.java | 34 +------- 6 files changed, 102 insertions(+), 36 deletions(-) diff --git a/src/tethys/dbxml/DBXMLConnect.java b/src/tethys/dbxml/DBXMLConnect.java index c1613cd9..7b7439a5 100644 --- a/src/tethys/dbxml/DBXMLConnect.java +++ b/src/tethys/dbxml/DBXMLConnect.java @@ -1,6 +1,21 @@ package tethys.dbxml; +import java.io.IOException; +import java.nio.file.Files; +import java.util.List; + +import javax.xml.bind.JAXBException; + +import java.nio.file.Files; +import java.nio.file.Path; + +import dbxml.uploader.Importer; +import nilus.Deployment; +import nilus.MarshalXML; import tethys.TethysControl; +import tethys.output.TethysExportParams; +import tethys.output.StreamExportParams; +import PamguardMVC.PamDataBlock; /** * Class containing functions for managing the database connection. Opening, closing, @@ -16,6 +31,80 @@ public class DBXMLConnect { this.tethysControl = tethysControl; } + + /** + * take list of nilus objects loaded with PamGuard data and post them to the Tethys database + * all objects must be of the same nilus object + * TethysExportParams obj used from UI inputs + * + * @param pamGuardObjs all nilus objects loaded with PamGuard data + * @return error string, null string means there are no errors + */ + public String postToTethys(List pamGuardObjs) + { + Class objClass = pamGuardObjs.get(0).getClass(); + String collection = getTethysCollection(objClass.getName()); + PamDataBlock defaultPamBlock = null; + TethysExportParams params = new TethysExportParams(); + String fileError = null; + try { + MarshalXML marshal = new MarshalXML(); + marshal.createInstance(objClass); + for (Object obj : pamGuardObjs ) + { + Path tempFile = Files.createTempFile("pamGuardToTethys", ".xml"); + marshal.marshal(obj, tempFile.toString()); + fileError = Importer.ImportFiles(params.getFullServerName(), collection, + new String[] { tempFile.toString() }, "", "", false); + + System.out.println(fileError); + + tempFile.toFile().deleteOnExit(); + } + } catch(IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return fileError; + } + + /** + * get tethys collection name from nilus collection objects + * @param className nilus object Class Name + * @return name of Tethys collection + */ + private String getTethysCollection(String className) { + switch(className) { + case "nilus.Deployment": + return "Deployments"; + case "nilus.Detection": + return "Detections"; + case "nilus.Calibration": + return "Calibrations"; + case "nilus.Ensemble": + return "Ensembles"; + case "nilus.Localization": + return "Localizations"; + case "nilus.SpeciesAbbreviation": + return "SpeciesAbbreviations"; + case "nilus.SourceMap": + return "SourceMaps"; + case "nilus.ITIS": + return "ITIS"; + case "nilus.ranks": + return "ITIS_ranks"; + default: + return ""; + } + } + public boolean openDatabase() { return true; diff --git a/src/tethys/deployment/DeploymentHandler.java b/src/tethys/deployment/DeploymentHandler.java index c5466966..955374b2 100644 --- a/src/tethys/deployment/DeploymentHandler.java +++ b/src/tethys/deployment/DeploymentHandler.java @@ -141,7 +141,7 @@ public class DeploymentHandler { } double[] ons = new double[n-1]; // ignore the last one since it may be artificially shortened which is OK double[] gaps = new double[n-1]; - for (int i = 0; i < n; i++) { + for (int i = 0; i < n-1; i++) { ons[i] = tempPeriods.get(i).getDuration(); } return null; diff --git a/src/tethys/detection/DetectionsHandler.java b/src/tethys/detection/DetectionsHandler.java index 4179d98f..e5e9934b 100644 --- a/src/tethys/detection/DetectionsHandler.java +++ b/src/tethys/detection/DetectionsHandler.java @@ -166,7 +166,7 @@ public class DetectionsHandler { * We should now have a fully populated Detections object, so write it to the database * using functions in DBXMLConnect */ - tethysControl.getDbxmlConnect(); // call whatever you need to call in here to write the Detections. + //tethysControl.getDbxmlConnect().postToTethys(detectionList); // call whatever you need to call in here to write the Detections. return true; diff --git a/src/tethys/output/StreamExportParams.java b/src/tethys/output/StreamExportParams.java index b19af599..d4842e64 100644 --- a/src/tethys/output/StreamExportParams.java +++ b/src/tethys/output/StreamExportParams.java @@ -12,7 +12,7 @@ import java.io.Serializable; */ public class StreamExportParams implements Serializable { - public static final long serialVersionUID = 1L; + public static final long serialVersionUID = 1L; public StreamExportParams(String longDataName, boolean selected) { super(); diff --git a/src/tethys/output/TethysExportParams.java b/src/tethys/output/TethysExportParams.java index bd1889af..c9d848e0 100644 --- a/src/tethys/output/TethysExportParams.java +++ b/src/tethys/output/TethysExportParams.java @@ -2,9 +2,9 @@ package tethys.output; import java.io.Serializable; import java.util.HashMap; - import PamguardMVC.PamDataBlock; + /** * Parameters for controlling export of Tethys data. * @author dg50 @@ -18,6 +18,13 @@ public class TethysExportParams implements Serializable, Cloneable{ * Need to add lots of other parameters here, such as the connection detils * for the tethys database. */ + public String serverName = "http://localhost"; + + public String port = "9779"; + + public String getFullServerName() { + return serverName + ":" + port; + } private HashMap streamParamsMap = new HashMap(); diff --git a/src/tethys/output/TethysExporter.java b/src/tethys/output/TethysExporter.java index e9924250..52554f0a 100644 --- a/src/tethys/output/TethysExporter.java +++ b/src/tethys/output/TethysExporter.java @@ -30,7 +30,6 @@ import dbxml.uploader.Importer; import metadata.MetaDataContol; import metadata.deployment.DeploymentData; import nilus.Deployment; -import nilus.MarshalXML; import tethys.TethysControl; import tethys.dbxml.DBXMLConnect; import tethys.deployment.DeploymentHandler; @@ -83,36 +82,7 @@ public class TethysExporter { */ // return false; // } - - Deployment deployment1 = new Deployment(); - deployment1.setId("1"); - - Path tempFile = null; - try { - - MarshalXML marshal = new MarshalXML(); - marshal.createInstance(Deployment.class); - tempFile = Files.createTempFile("pamGuardToTethys", ".xml"); - marshal.marshal(deployment1, tempFile.toString()); - String fileError = Importer.ImportFiles("http://localhost:9779", "Deployments", - new String[] { tempFile.toString() }, "", "", false); - - tempFile.toFile().deleteOnExit(); - - } catch(IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (JAXBException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - - SnapshotGeometry arrayGeometry = findArrayGeometrey(); - + /** * Doug populate instrument fields - may need to add a few things. Marie to * define what we mean by instrument. Instrument names probably need to be added @@ -218,7 +188,7 @@ public class TethysExporter { } - + tethysControl.getDbxmlConnect().postToTethys(deploymentDocs); /* * go through the export params and call something for every data block that's