Document export

This commit is contained in:
Douglas Gillespie 2023-05-29 15:27:21 +01:00
parent 36da1bcbeb
commit 3df05c3ec6
10 changed files with 322 additions and 6 deletions

View File

@ -2598,7 +2598,7 @@ public class PamController implements PamControllerInterface, PamSettings {
if (dbc == null) {
return null;
}
return dbc.getDatabaseName();
return dbc.getLongDatabaseName();
}
return null;
}

View File

@ -3,6 +3,10 @@ package tethys;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
@ -10,9 +14,11 @@ import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
@ -20,8 +26,11 @@ import PamController.PamControlledUnit;
import PamController.PamControlledUnitSettings;
import PamController.PamController;
import PamController.PamControllerInterface;
import PamController.PamFolders;
import PamController.PamSettingManager;
import PamController.PamSettings;
import PamUtils.PamFileChooser;
import PamUtils.PamFileFilter;
import PamView.PamTabPanel;
import PamView.dialog.warn.WarnOnce;
import PamguardMVC.PamDataBlock;
@ -43,6 +52,7 @@ import tethys.output.TethysExporter;
import tethys.output.swing.TethysExportDialog;
import tethys.swing.ProjectDeploymentsDialog;
import tethys.swing.TethysTabPanel;
import tethys.swing.XMLStringView;
/**
* Quick play with a simple system for outputting data to Tethys. At it's start
@ -539,4 +549,70 @@ public class TethysControl extends PamControlledUnit implements PamSettings, Tet
WarnOnce.showWarning(title, msg, WarnOnce.WARNING_MESSAGE);
}
/**
* Load a document from the database and display it in a popup window
* @param collection
* @param documentId
*/
public void displayDocument(String collection, String documentId) {
String doc = getDbxmlQueries().getDocument(collection, documentId);
if (doc == null | doc.length() == 0) {
doc = String.format("Unable to retrieve document %s/%s from database\n", collection, documentId);
}
XMLStringView.showDialog(getGuiFrame(), collection, documentId, doc);
}
/**
* Load a document from the database and write to a file selected by the user
* @param collection
* @param documentId
*/
public void exportDocument(String collection, String documentId) {
String doc = getDbxmlQueries().getDocument(collection, documentId);
if (doc == null | doc.length() == 0) {
String msg = String.format("Unable to retrieve document %s/%s from database\n", collection, documentId);
WarnOnce.showWarning("Error", msg, WarnOnce.WARNING_MESSAGE);
}
PamFileFilter fileFilter = new PamFileFilter("XML documents", ".xml");
// fileFilter
JFileChooser fileChooser = new PamFileChooser();
fileChooser.setFileFilter(fileFilter);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// make a default name based on the document id and the dataset directory.
String defFolder = PamFolders.getDefaultProjectFolder();
if (defFolder != null) {
defFolder = String.format("%s%s%s.xml", defFolder,File.separator,documentId);
File defFile = new File(defFolder);
fileChooser.setSelectedFile(defFile);
fileChooser.setAcceptAllFileFilterUsed(true);
}
int state = fileChooser.showSaveDialog(getGuiFrame());
if (state != JFileChooser.APPROVE_OPTION) return;
File newFile = fileChooser.getSelectedFile();
if (newFile == null) return;
newFile = PamFileFilter.checkFileEnd(newFile, "xml", true);
if (newFile == null) {
return;
}
if (newFile.exists()) {
int ans2 = WarnOnce.showWarning(newFile.getAbsolutePath(),
"The file already exists. Do you want to overwrite it ?", WarnOnce.OK_CANCEL_OPTION);
if (ans2 == WarnOnce.CANCEL_OPTION) {
return;
}
}
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
bos.write(doc.getBytes());
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -85,6 +85,44 @@ public class DBXMLConnect {
return queries;
}
/**
* Convert a nilus Object into a file
* @param nilusObject nilus object
* @param file file (should not exist)
* @return file (will be the same as input file)
* @throws TethysException
*/
public File createXMLDocument(Object nilusObject, File file) throws TethysException {
Class objClass = nilusObject.getClass();
try {
MarshalXML marshal = new MarshalXML();
marshal.createInstance(objClass);
marshal.marshal(nilusObject, file.toString());
} catch(IllegalArgumentException e) {
throw new TethysException("IllegalArgumentException posting to Tethys: " + e.getMessage(), null);
} catch (IOException e) {
throw new TethysException("IOException posting to Tethys: " + e.getMessage(), null);
} catch (JAXBException e) {
throw new TethysException("JAXBException posting to Tethys: " + e.getMessage(), null);
}
return file;
}
/**
* Create a temporary nilus file.
* @param nilusObject
* @return
* @throws TethysException
*/
public File createTempXMLDocument(Object nilusObject) throws TethysException {
String tempName = getTempFileName(nilusObject);
tempName = tempDirectory.getAbsolutePath() + File.separator + tempName + ".xml";
File tempFile = new File(tempName);
File retFile = createXMLDocument(nilusObject, tempFile);
retFile.deleteOnExit();
return retFile;
}
/**
* take a nilus object loaded with PamGuard data and post it to the Tethys database
*
@ -162,7 +200,7 @@ public class DBXMLConnect {
String docId = getDocumentId(nilusDocument);
String result = null;
try {
result = jerseyClient.removeDocument(collection+" uio", docId );
result = jerseyClient.removeDocument(collection, docId );
/**
* Return from a sucessful delete is something like
*

View File

@ -440,6 +440,37 @@ public class DBXMLQueries {
return count;
}
public String getDocument(String collection, String documentId) {
// String queryBase = "return:(collection(\"replaceCollectionName\")/Detections[Id=\"ReplaceDocumentId\"])";
// queryBase = queryBase.replace("replaceCollectionName", collection);
// queryBase = queryBase.replace("ReplaceDocumentId", documentId);
//
// String result = null;
// try {
// Queries queries = dbXMLConnect.getTethysQueries();
// result = queries.QueryTethys(queryBase);
//// System.out.println(result);
// }
// catch (Exception e) {
// System.out.println("Error executing " + queryBase);
//// e.printStackTrace();
// return null;
// }
// return result;
Queries queries = dbXMLConnect.getTethysQueries();
String result = null;
try {
result = queries.getDocument(collection, documentId);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
// String queryBase = "{\"return\":[\"Deployment/Project\"],\"select\":[],\"enclose\":1}";
}
/**
* Count on effort detections in a Detections document
* @param docName

View File

@ -5,11 +5,20 @@ import java.util.ArrayList;
import java.util.List;
import javax.swing.SwingWorker;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import org.pamguard.x3.sud.SUDClickDetectorInfo;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import PamController.PamControlledUnit;
import PamController.PamguardVersionInfo;
import PamController.settings.output.xml.PamguardXMLWriter;
import PamModel.PamPluginInterface;
import PamUtils.PamCalendar;
import PamUtils.XMLUtils;
import PamguardMVC.PamDataBlock;
import PamguardMVC.PamDataUnit;
import PamguardMVC.PamProcess;
@ -18,6 +27,7 @@ import dataMap.OfflineDataMap;
import dataMap.OfflineDataMapPoint;
import metadata.deployment.DeploymentData;
import nilus.AlgorithmType;
import nilus.AlgorithmType.Parameters;
import nilus.AlgorithmType.SupportSoftware;
import nilus.DataSourceType;
import nilus.Deployment;
@ -484,6 +494,69 @@ public class DetectionsHandler {
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<Element> 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
*
<Algorithm>
<Method>Analyst detections</Method>
<Software>Triton</Software>
<Version>unknown</Version>
<Parameters>
<LTSA_plot_time_h>0.75</LTSA_plot_time_h>
<LTSA_low_Hz>0.0</LTSA_low_Hz>
<LTSA_high_Hz>5000.0</LTSA_high_Hz>
<LTSA_brightness>30.0</LTSA_brightness>
</Parameters>
</Algorithm>
*/
// 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));
}
List<SupportSoftware> supSoft = algorithm.getSupportSoftware();
SupportSoftware supportSoft = new SupportSoftware();
supportSoft.setSoftware(getSupportSoftware(dataBlock));

View File

@ -0,0 +1,13 @@
package tethys.niluswraps;
public enum TethysCollections {
Deployments, Detections, Localizations, Calibrations, SpeciesAbreviations;
@Override
public String toString() {
return super.toString();
}
}

View File

@ -112,7 +112,8 @@ public class AutoTethysProvider implements TethysDataProvider {
return algorithm;
}
private Parameters getAlgorithmParameters() {
@Override
public Parameters getAlgorithmParameters() {
if (pamControlledUnit instanceof PamSettings == false) {
return null;
}
@ -127,13 +128,13 @@ public class AutoTethysProvider implements TethysDataProvider {
if (settingsObjs == null) {
return null;
}
// pamXMLWriter.setStaticNameSpace(TethysControl.xmlNameSpace);
// pamXMLWriter.setStaticNameSpace(TethysControl.xmlNameSpace);
Element settingsEl = pamXMLWriter.writeUnitSettings(doc, dummyEl, pamSettings, settingsObjs);
if (settingsEl == null) {
return null;
}
settingsEl = addNameSpaceToElements(doc, settingsEl, TethysControl.xmlNameSpace);
// settingsEl = addNameSpaceToElements(doc, settingsEl, TethysControl.xmlNameSpace);
dummyEl.appendChild(settingsEl);

View File

@ -2,6 +2,7 @@ package tethys.pamdata;
import PamguardMVC.PamDataUnit;
import nilus.AlgorithmType;
import nilus.AlgorithmType.Parameters;
import nilus.Deployment;
import nilus.DescriptionType;
import nilus.Detection;
@ -62,5 +63,8 @@ public interface TethysDataProvider {
*/
public Detection createDetection(PamDataUnit dataUnit, TethysExportParams tethysExportParams,
StreamExportParams streamExportParams);
public Parameters getAlgorithmParameters();
}

View File

@ -33,6 +33,7 @@ import tethys.deployment.DeploymentHandler;
import tethys.deployment.DeploymentOverview;
import tethys.deployment.RecordingPeriod;
import tethys.niluswraps.PDeployment;
import tethys.niluswraps.TethysCollections;
/**
* Table view of PAMGuard deployments. For a really simple deployment, this may have only
@ -135,13 +136,30 @@ public class PAMGuardDeploymentsTable extends TethysGUIPanel {
JPopupMenu popMenu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Remove deployment document " + matchedDeployments.get(0));
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteDeployment(matchedDeployments.get(0));
}
});
popMenu.add(menuItem);
menuItem = new JMenuItem("Display deployment document " + matchedDeployments.get(0));
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayDeployment(matchedDeployments.get(0));
}
});
popMenu.add(menuItem);
menuItem = new JMenuItem("Export deployment document " + matchedDeployments.get(0));
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exportDeployment(matchedDeployments.get(0));
}
});
popMenu.add(menuItem);
popMenu.show(e.getComponent(), e.getX(), e.getY());
}
// if (newPeriods.size() == 0) {
@ -155,6 +173,14 @@ public class PAMGuardDeploymentsTable extends TethysGUIPanel {
}
protected void exportDeployment(PDeployment pDeployment) {
getTethysControl().exportDocument(TethysCollections.Deployments.toString(), pDeployment.deployment.getId());
}
protected void displayDeployment(PDeployment pDeployment) {
getTethysControl().displayDocument(TethysCollections.Deployments.toString(), pDeployment.deployment.getId());
}
protected void deleteDeployment(PDeployment pDeployment) {
Deployment dep = pDeployment.deployment;
if (dep == null) {

View File

@ -0,0 +1,54 @@
package tethys.swing;
import java.awt.BorderLayout;
import java.awt.Window;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import PamView.dialog.PamDialog;
public class XMLStringView extends PamDialog {
private JTextArea textArea;
private XMLStringView(Window parentFrame, String title, String xmlString) {
super(parentFrame, title, false);
JTextArea textArea = new JTextArea(50, 100);
JPanel mainPanel = new JPanel(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
mainPanel.add(scrollPane, BorderLayout.CENTER);
setDialogComponent(mainPanel);
setResizable(true);
textArea.setText(xmlString);
getCancelButton().setVisible(false);
}
public static void showDialog(Window parent, String collection, String documentId, String xmlString) {
String title = String.format("\"%s\"/\"%s\"", collection, documentId);
XMLStringView view = new XMLStringView(parent, title, xmlString);
view.setVisible(true);
}
@Override
public boolean getParams() {
// TODO Auto-generated method stub
return false;
}
@Override
public void cancelButtonPressed() {
// TODO Auto-generated method stub
}
@Override
public void restoreDefaultSettings() {
// TODO Auto-generated method stub
}
}