diff --git a/src/PamController/settings/output/xml/PamguardXMLWriter.java b/src/PamController/settings/output/xml/PamguardXMLWriter.java index af35b51e..58cf1bd7 100644 --- a/src/PamController/settings/output/xml/PamguardXMLWriter.java +++ b/src/PamController/settings/output/xml/PamguardXMLWriter.java @@ -903,6 +903,14 @@ public class PamguardXMLWriter implements PamSettings { return doc; } + /** + * Is this element a writable type ? Basically, this means + * that it's a primitive of some sort. Otherwise it's + * probably an object and may even be a list in which case + * it will need treating differently. + * @param clazz + * @return + */ public static boolean isWritableType(Class clazz) { if (clazz.isEnum()) return true; diff --git a/src/PamView/dialog/warn/WarnOnce.java b/src/PamView/dialog/warn/WarnOnce.java index 93ed4618..cd7ad08d 100644 --- a/src/PamView/dialog/warn/WarnOnce.java +++ b/src/PamView/dialog/warn/WarnOnce.java @@ -48,6 +48,7 @@ public class WarnOnce implements PamSettings { int ans = showWarning(parent, "Warning Messages", "Show all PAMGuard warning messages", WarnOnce.OK_CANCEL_OPTION); if (ans == WarnOnce.CANCEL_OPTION) return; singleInstance.warnOnceList.clearList(); + singleInstance.showThisSess.clear(); } @Override diff --git a/src/RightWhaleEdgeDetector/species/RWSpeciesManager.java b/src/RightWhaleEdgeDetector/species/RWSpeciesManager.java index 65c2afd7..e906bb5d 100644 --- a/src/RightWhaleEdgeDetector/species/RWSpeciesManager.java +++ b/src/RightWhaleEdgeDetector/species/RWSpeciesManager.java @@ -22,7 +22,7 @@ public class RWSpeciesManager extends DataBlockSpeciesManager { @Override public String getSpeciesCode(RWEDataUnit dataUnit) { - return RWSpeciesTypes.onlyType; + return RWSpeciesTypes.defaultName; } } diff --git a/src/tethys/dbxml/DBXMLConnect.java b/src/tethys/dbxml/DBXMLConnect.java index dd45b6d5..e6c603a5 100644 --- a/src/tethys/dbxml/DBXMLConnect.java +++ b/src/tethys/dbxml/DBXMLConnect.java @@ -239,6 +239,34 @@ An error will throw an exception. } return true; } + + /** + * Remove a document based on a collection name and a cdocument Id. + * @param collection + * @param docId + * @return + * @throws TethysException + */ + public boolean removeDocument(String collection, String docId) throws TethysException { + try { + Object result = jerseyClient.removeDocument(collection, docId ); + /** + * Return from a sucessful delete is something like + * + deployment = getTethysControl().getDeploymentHandler().createDeploymentDocument(freeId++, recordPeriod); + + ['ECoastNARW0'] + +An error will throw an exception. + */ + } + catch (Exception e) { +// System.out.printf("Error deleting %s %s: %s\n", collection, docId, e.getMessage()); + String msg = String.format("Error deleting %s:%s", collection, docId); + throw new TethysException(msg, e.getLocalizedMessage()); + } + return true; + } /** * check the return string from importFiles and if it's an diff --git a/src/tethys/detection/DetectionsHandler.java b/src/tethys/detection/DetectionsHandler.java index 706dd7fc..7598f524 100644 --- a/src/tethys/detection/DetectionsHandler.java +++ b/src/tethys/detection/DetectionsHandler.java @@ -491,71 +491,15 @@ public class DetectionsHandler { // dataSource.setEnsembleId(""); ToDo detections.setDataSource(dataSource); AlgorithmType algorithm = detections.getAlgorithm(); - algorithm.setMethod(getMethodString(dataBlock)); - algorithm.setSoftware(getSoftwareString(dataBlock)); - algorithm.setVersion(getVersionString(dataBlock)); TethysDataProvider dataProvider = dataBlock.getTethysDataProvider(); if (dataProvider != null) { -// Parameters parameters = dataProvider.getAlgorithmParameters(); - Parameters parameters = algorithm.getParameters(); - if (parameters == null) { - parameters = new Parameters(); - algorithm.setParameters(parameters); - } - List paramList = parameters.getAny(); -// algorithm.setParameters(parameters); - // make a really simple parameter or two to see if it works with simpler xml than PG generates. - /** - * Parameters should look something like - * - - Analyst detections - Triton - unknown - - 0.75 - 0.0 - 5000.0 - 30.0 - - - */ - // this works. Can look at the source to see how it's done. - // may have fun making this work for more complex structures. - try { - Helper helper = new Helper(); - helper.AddAnyElement(paramList, "Threshold", "3.5"); - /* - * and see Matlab code for dbStruct2DOM for more complex structures - * This looks like it may be possible to rewrite my functions for - * writing structures to XML using the helper.AddAnyElement function as - * an example and I should be able to output my complex structures. - */ - } catch (JAXBException | ParserConfigurationException e) { - e.printStackTrace(); - } - - Document doc = XMLUtils.createBlankDoc(); -// PamguardXMLWriter pamXMLWriter = PamguardXMLWriter.getXMLWriter(); - Element dummyEl = doc.createElement("SomeParam"); -//// dummyEl.setNodeValue("nothing"); - dummyEl.setTextContent("3.0"); - /* - * xsl:stylesheet version=\"1.0\" \n" - + " xmlns:xsl=http://www.w3.org/1999/XSL/Transform\n" - + " xmlns:ns0=http://mydata.com/H2H/Automation\n" - */ - dummyEl.setAttribute("xmlns:ns0", TethysControl.xmlNameSpace); -// dummyEl.set -// paramList.add(dummyEl); - -// Element mainEl = doc.createElement("CONFIG"); -// mainEl.appendChild(dummyEl); -// doc.appendChild(mainEl); -// System.out.println(pamXMLWriter.getAsString(doc)); + algorithm = dataProvider.getAlgorithm(); +// detections.setAlgorithm(algorithm); } - + algorithm.setMethod(getMethodString(dataBlock)); + algorithm.setSoftware(getSoftwareString(dataBlock)); + algorithm.setVersion(getVersionString(dataBlock)); List supSoft = algorithm.getSupportSoftware(); SupportSoftware supportSoft = new SupportSoftware(); diff --git a/src/tethys/pamdata/AutoTethysProvider.java b/src/tethys/pamdata/AutoTethysProvider.java index ebe498fa..26bf3807 100644 --- a/src/tethys/pamdata/AutoTethysProvider.java +++ b/src/tethys/pamdata/AutoTethysProvider.java @@ -31,7 +31,12 @@ import tethys.TethysControl; import tethys.TethysTimeFuncs; import tethys.output.StreamExportParams; import tethys.output.TethysExportParams; +import tethys.species.DataBlockSpeciesManager; +import tethys.species.ITISTypes; +import tethys.species.SpeciesMapItem; import whistleClassifier.WhistleContour; + +import javax.xml.bind.JAXBException; import javax.xml.transform.*; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; @@ -104,10 +109,13 @@ public class AutoTethysProvider implements TethysDataProvider { // TODO Auto-generated catch block e.printStackTrace(); } - algorithm.setMethod(this.getAlgorithmMethod()); - algorithm.setSoftware("PAMGuard"); - algorithm.setVersion(PamguardVersionInfo.version); - //algorithm.setParameters(this.getAlgorithmParameters()); +// algorithm.setMethod(this.getAlgorithmMethod()); +// algorithm.setSoftware("PAMGuard"); +// algorithm.setVersion(PamguardVersionInfo.version); + Parameters algoParameters = this.getAlgorithmParameters(); + if (algoParameters != null) { + algorithm.setParameters(algoParameters); + } return algorithm; } @@ -120,66 +128,80 @@ public class AutoTethysProvider implements TethysDataProvider { PamSettings pamSettings = (PamSettings) pamControlledUnit; Parameters parameters = new Parameters(); List paramList = parameters.getAny(); - Document doc = XMLUtils.createBlankDoc(); - PamguardXMLWriter pamXMLWriter = PamguardXMLWriter.getXMLWriter(); - Element dummyEl = doc.createElement("MODULES"); - doc.appendChild(dummyEl); - PamSettings[] settingsObjs = getSettingsObjects(); - if (settingsObjs == null) { + Object settings = pamSettings.getSettingsReference(); + TethysParameterPacker paramPacker = null; + try { + paramPacker = new TethysParameterPacker(); + } catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + List genList = paramPacker.packParameters(settings); + if (genList == null || genList.size() == 0) { return null; } -// pamXMLWriter.setStaticNameSpace(TethysControl.xmlNameSpace); - Element settingsEl = pamXMLWriter.writeUnitSettings(doc, dummyEl, pamSettings, settingsObjs); - if (settingsEl == null) { - return null; - } - -// settingsEl = addNameSpaceToElements(doc, settingsEl, TethysControl.xmlNameSpace); - - - dummyEl.appendChild(settingsEl); - NodeList childs = settingsEl.getChildNodes(); - for (int i = 0; i < childs.getLength(); i++) { - Node el = childs.item(i); - // System.out.println(el.getNodeName()); - if (el instanceof Element) { - paramList.add((Element) el); - } - } - - // Document doc = pamXMLWriter.writeOneModule((PamSettings) pamControlledUnit, System.currentTimeMillis()); - // String moduleXML = null; - if (doc != null) { - // this string should be XML of all the settings for the module controlling this - // datablock. - // moduleXML = pamXMLWriter.getAsString(doc, true); // change to false to get smaller xml - // System.out.printf("Module settings for datablock %s are:\n", moduleXML); - // System.out.println(moduleXML); - // Element pamguard = doc.get("PAMGUARD"); - // Element modules = (Element) pamguard.getElementsByTagName("MODULES"); - // doc.get - // NodeList childs = doc.getChildNodes(); - // for (int i = 0; i < childs.getLength(); i++) { - // Node el = childs.item(i); - // System.out.println(el.getNodeName()); - // if (el instanceof Element) { - // paramList.add((Element) el); - // } - // } - // String moduleXML = pamXMLWriter.getAsString(doc, true); // change to false to get smaller xml - // System.out.printf("Module settings for datablock %s are:\n%s", this.pamDataBlock.getDataName(), moduleXML); - } - - // // try the old say - // Document doc2 = pamXMLWriter.writeOneModule((PamSettings) pamControlledUnit, System.currentTimeMillis()); - // String moduleXML = null; - // if (doc2 != null) { - // // this string should be XML of all the settings for the module controlling this - // // datablock. - // moduleXML = pamXMLWriter.getAsString(doc2, true); // change to false to get smaller xml - // System.out.printf("Module settings for datablock %s are:\n%s", pamDataBlock.getDataName(),moduleXML); - // } - // + paramList.addAll(genList); + +// Document doc = XMLUtils.createBlankDoc(); +// PamguardXMLWriter pamXMLWriter = PamguardXMLWriter.getXMLWriter(); +// Element dummyEl = doc.createElement("MODULES"); +// doc.appendChild(dummyEl); +// PamSettings[] settingsObjs = getSettingsObjects(); +// if (settingsObjs == null) { +// return null; +// } +//// pamXMLWriter.setStaticNameSpace(TethysControl.xmlNameSpace); +// Element settingsEl = pamXMLWriter.writeUnitSettings(doc, dummyEl, pamSettings, settingsObjs); +// if (settingsEl == null) { +// return null; +// } +// +//// settingsEl = addNameSpaceToElements(doc, settingsEl, TethysControl.xmlNameSpace); +// +// +// dummyEl.appendChild(settingsEl); +// NodeList childs = settingsEl.getChildNodes(); +// for (int i = 0; i < childs.getLength(); i++) { +// Node el = childs.item(i); +// // System.out.println(el.getNodeName()); +// if (el instanceof Element) { +// paramList.add((Element) el); +// } +// } +// +// // Document doc = pamXMLWriter.writeOneModule((PamSettings) pamControlledUnit, System.currentTimeMillis()); +// // String moduleXML = null; +// if (doc != null) { +// // this string should be XML of all the settings for the module controlling this +// // datablock. +// // moduleXML = pamXMLWriter.getAsString(doc, true); // change to false to get smaller xml +// // System.out.printf("Module settings for datablock %s are:\n", moduleXML); +// // System.out.println(moduleXML); +// // Element pamguard = doc.get("PAMGUARD"); +// // Element modules = (Element) pamguard.getElementsByTagName("MODULES"); +// // doc.get +// // NodeList childs = doc.getChildNodes(); +// // for (int i = 0; i < childs.getLength(); i++) { +// // Node el = childs.item(i); +// // System.out.println(el.getNodeName()); +// // if (el instanceof Element) { +// // paramList.add((Element) el); +// // } +// // } +// // String moduleXML = pamXMLWriter.getAsString(doc, true); // change to false to get smaller xml +// // System.out.printf("Module settings for datablock %s are:\n%s", this.pamDataBlock.getDataName(), moduleXML); +// } +// +// // // try the old say +// // Document doc2 = pamXMLWriter.writeOneModule((PamSettings) pamControlledUnit, System.currentTimeMillis()); +// // String moduleXML = null; +// // if (doc2 != null) { +// // // this string should be XML of all the settings for the module controlling this +// // // datablock. +// // moduleXML = pamXMLWriter.getAsString(doc2, true); // change to false to get smaller xml +// // System.out.printf("Module settings for datablock %s are:\n%s", pamDataBlock.getDataName(),moduleXML); +// // } +// // return parameters; } @@ -275,7 +297,29 @@ public class AutoTethysProvider implements TethysDataProvider { Detection detection = new Detection(); detection.setStart(TethysTimeFuncs.xmlGregCalFromMillis(dataUnit.getTimeMilliseconds())); detection.setEnd(TethysTimeFuncs.xmlGregCalFromMillis(dataUnit.getEndTimeInMilliseconds())); - detection.setSpeciesId(getSpeciesIdType()); + + DataBlockSpeciesManager speciesManager = pamDataBlock.getDatablockSpeciesManager(); + SpeciesMapItem speciesItem = null; + if (speciesManager != null) { + speciesItem = speciesManager.getSpeciesItem(dataUnit); +// detection.setSpeciesId(new Species); +// detection.setSpeciesId(getSpeciesIdType()); + } + else { + } + SpeciesIDType species = new SpeciesIDType(); + List calls = detection.getCall(); + if (speciesItem != null) { + species.setValue(BigInteger.valueOf(speciesItem.getItisCode())); + if (speciesItem.getCallType() != null) { + calls.add(speciesItem.getCallType()); + } + } + else { + species.setValue(BigInteger.valueOf(ITISTypes.ANTHROPOGENIC)); + calls.add("unknown"); + } + detection.setSpeciesId(species); /* * NOTE: I use channel bitmaps throughout since detections are often made on multiple channels. */ diff --git a/src/tethys/pamdata/TethysParameterPacker.java b/src/tethys/pamdata/TethysParameterPacker.java new file mode 100644 index 00000000..a6ad0e01 --- /dev/null +++ b/src/tethys/pamdata/TethysParameterPacker.java @@ -0,0 +1,233 @@ +package tethys.pamdata; + +import java.io.File; +import java.lang.reflect.Array; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.namespace.QName; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.dom.DOMResult; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import PamController.settings.output.xml.PamguardXMLWriter; +import PamModel.parametermanager.ManagedParameters; +import PamModel.parametermanager.PamParameterData; +import PamModel.parametermanager.PamParameterSet; +import nilus.MarshalXML; + +/** + * Functions to pack up a PAMGuard parameters object into the correct format + * for Tethys. This is very similar to functions in PamguardXMLWriter.writeUnitSettings + * but seems to have enough differences that it needs totally rewriting for Tethys. + * @author dg50 + * + */ +public class TethysParameterPacker { + +// /** +// * Parameters should look something like below. however, only packing them with a long +// schema name seems to work. +// * +// +//Analyst detections +//Triton +//unknown +// +//0.75 +//0.0 +//5000.0 +//30.0 +// +// + /* + * +// // this works. Can look at the source to see how it's done. +// // may have fun making this work for more complex structures. +// try { +// Helper helper = new Helper(); +// helper.AddAnyElement(paramList, "Threshold", "3.5"); +// +// * and see Matlab code for dbStruct2DOM at C:\Users\dg50\source\repos\TethysMatlab\db +// * for more complex structures +// * This looks like it may be possible to rewrite my functions for +// * writing structures to XML using the helper.AddAnyElement function as +// * an example and I should be able to output my complex structures. +// +// } catch (JAXBException | ParserConfigurationException e) { +// e.printStackTrace(); +// } + */ + + private MarshalXML marshaller; + + /** + * @throws JAXBException + * + */ + public TethysParameterPacker() throws JAXBException { + super(); + try { + marshaller = new MarshalXML(); + } catch (JAXBException e) { + } + } + + + public List packParameters(Object data) { + List elList = new ArrayList(); + + ArrayList objectHierarchy = new ArrayList<>(); + + PamParameterSet parameterSet; + if (data instanceof ManagedParameters) { + parameterSet = ((ManagedParameters) data).getParameterSet(); + } + else { + int exclusions = 0; + // if (writerSettings.includeConstants == false) { + exclusions = Modifier.STATIC | Modifier.FINAL; + // } + parameterSet = PamParameterSet.autoGenerate(data, exclusions); + } + if (parameterSet == null) { + return null; + } +// Document document = null; +// try { +// document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); +// } catch (ParserConfigurationException e1) { +// e1.printStackTrace(); +// } + QName qname = new QName(MarshalXML.schema, "parameters", "ty"); + JAXBElement jaxel = new JAXBElement( + qname, String.class, parameterSet.getParentObject().getClass().getCanonicalName()); + Document doc = null; + try { + doc = marshaller.marshalToDOM(jaxel); + } catch (JAXBException | ParserConfigurationException e1) { + e1.printStackTrace(); + } + Element el = doc.getDocumentElement(); + + for (PamParameterData pamParam : parameterSet.getParameterCollection()) { + try { + Object paramData = pamParam.getData(); + boolean ok = createElement(doc, el, paramData, pamParam, objectHierarchy); +// if (newEl != null) { +// elList.add(newEl); +// } + } catch (IllegalArgumentException | IllegalAccessException e) { + e.printStackTrace(); + } + + } + elList.add(el); + return elList; + } + + private boolean createElement(Document document, Element parentEl, Object paramData, PamParameterData pamParam, ArrayList objectHierarchy) { + Class javaClass = paramData.getClass(); + if (PamguardXMLWriter.isWritableType(javaClass)) { + String name = pamParam.getFieldName(); + String value = paramData.toString(); + Element el = document.createElement(name); +// el.setNodeValue(value); + el.setTextContent(value); + parentEl.appendChild(el); + + +// QName qname = new QName(MarshalXML.schema, name, "ty"); +// JAXBElement jaxel = new JAXBElement( +// qname, String.class, value); +// +// +// try { +// jxbm.marshal(jaxel, dom); +// } catch (JAXBException e) { +// e.printStackTrace(); +// } +// Document doc = null; +// try { +// doc = marshaller.marshalToDOM(jaxel); +// } catch (JAXBException e) { +// e.printStackTrace(); +// } catch (ParserConfigurationException e) { +// e.printStackTrace(); +// } +// Element el = doc.getDocumentElement(); +// return el; + return true; + } + if (javaClass.isArray()) { + return writeArray(document, parentEl, paramData, pamParam, objectHierarchy); + + } + /* + * + if (javaClass.isArray()) { + return writeArray(doc, el, data, pamParam, objectHierarchy); + } + if (List.class.isAssignableFrom(javaClass)){ + return writeList(doc, el, data, pamParam, objectHierarchy); + } + if (Map.class.isAssignableFrom(javaClass)){ + return writeMap(doc, el, data, pamParam, objectHierarchy); + } + if (File.class.isAssignableFrom(javaClass)) { + return writeFile(doc, el, data, pamParam); + } + + else { + Element e = makeElement(doc, pamParam.getFieldName(), data.getClass().getName()); + el.appendChild(e); + writeObjectData(doc, e, data, objectHierarchy); + return e; + } + */ + return false; + } + + + private boolean writeArray(Document document, Element parentEl, Object paramData, PamParameterData pamParam, + ArrayList objectHierarchy) { + if (paramData.getClass().isArray() == false) { + return false; + } + String name = pamParam.getFieldName(); + Element el = document.createElement(name); + parentEl.appendChild(el); + int n = Array.getLength(paramData); + boolean ok = true; + for (int i = 0; i < n; i++) { + Object arrayEl = Array.get(paramData, i); + ok &= createElement(document, el, arrayEl, pamParam, objectHierarchy); + } + // TODO Auto-generated method stub + return ok; + } + + +// private Element writeArray(PamParameterData pamParam, ArrayList objectHierarchy) { +// QName qname = new QName(MarshalXML.schema, pamParam.getFieldName(), "ty"); +// JAXBElement jaxel = new JAXBElement( +// qname, String.class, value); +// +// Document doc = null; +// try { +// doc = marshaller.marshalToDOM(jaxel); +// } catch (JAXBException e) { +// e.printStackTrace(); +// } catch (ParserConfigurationException e) { +// e.printStackTrace(); +// } +// return null; +// } +} diff --git a/src/tethys/species/DataBlockSpeciesManager.java b/src/tethys/species/DataBlockSpeciesManager.java index bf94ffa9..6b08d0ce 100644 --- a/src/tethys/species/DataBlockSpeciesManager.java +++ b/src/tethys/species/DataBlockSpeciesManager.java @@ -72,6 +72,9 @@ abstract public class DataBlockSpeciesManager { super(); this.dataBlock = dataBlock; datablockSpeciesMap = SpeciesMapManager.getInstance().getSpeciesMap(dataBlock); + if (datablockSpeciesMap == null) { + datablockSpeciesMap = new DataBlockSpeciesMap(); + } // datablockSpeciesMap.clearMap(); clearMapNulls(); checkMapDefault(); @@ -81,6 +84,9 @@ abstract public class DataBlockSpeciesManager { * Clear up some old maps which have got a null null default. */ private void clearMapNulls() { + if (datablockSpeciesMap == null) { + return; + } SpeciesMapItem nullVal = datablockSpeciesMap.getItem(null); if (nullVal == null) { datablockSpeciesMap.removeItem(null); @@ -136,7 +142,7 @@ abstract public class DataBlockSpeciesManager { // if (defaultItem == null) { // datablockSpeciesMap.putItem(getDefaultSpeciesCode(), getDefaultDefaultSpecies()); // } - if (defaultDefaultSpecies == null) { + if (defaultDefaultSpecies == null || datablockSpeciesMap == null) { return; } SpeciesMapItem defaultItem = datablockSpeciesMap.getItem(defaultDefaultSpecies.getPamguardName()); diff --git a/src/tethys/swing/documents/TethysDocumentTable.java b/src/tethys/swing/documents/TethysDocumentTable.java index e1f8f623..dcdb6a0a 100644 --- a/src/tethys/swing/documents/TethysDocumentTable.java +++ b/src/tethys/swing/documents/TethysDocumentTable.java @@ -16,9 +16,12 @@ import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; +import PamController.PamController; import PamView.dialog.PamDialogPanel; +import PamView.dialog.warn.WarnOnce; import PamView.tables.SwingTableColumnWidths; import tethys.TethysControl; +import tethys.dbxml.TethysException; /** * Table view of a collection of Tethys documents. @@ -118,8 +121,17 @@ public class TethysDocumentTable implements PamDialogPanel { } private void deleteDocument(String docName) { - // TODO Auto-generated method stub - + int ans = WarnOnce.showNamedWarning("deletedoc"+collectionName, PamController.getMainFrame(), "Delete document", + "Are you sure you want to delete the document " + docName, WarnOnce.OK_CANCEL_OPTION); + if (ans == WarnOnce.OK_OPTION) { + try { + tethysControl.getDbxmlConnect().removeDocument(collectionName, docName); + } catch (TethysException e) { + System.out.println("Failed to delete " + docName); + System.out.println(e.getMessage()); + } + } + updateTableData(); } private class TableModel extends AbstractTableModel {