Collection enum

Made an enum for the different collections. More robust than passing the
names around as strings. Contains functions for converting between the
collection name and the documents names within those collections.
This commit is contained in:
Douglas Gillespie 2023-09-29 13:43:49 +01:00
parent 773f1f542b
commit 1f8e790ae5
15 changed files with 886 additions and 219 deletions

View File

@ -97,6 +97,11 @@ public class PamArray implements Serializable, Cloneable, ManagedParameters {
*/
private String instrumentType;
/**
* Array Id. Can be anything. Compulsory for Tethys.
*/
private String instrumentId;
public String getInstrumentType() {
return instrumentType;
}
@ -113,10 +118,6 @@ public class PamArray implements Serializable, Cloneable, ManagedParameters {
this.instrumentId = instrumentId;
}
/**
* Array Id. Can be anything. Compulsory for Tethys.
*/
private String instrumentId;
// private int originInterpolation = ORIGIN_USE_LATEST;
private int originInterpolation = ORIGIN_USE_PRECEEDING;

136
src/tethys/Collection.java Normal file
View File

@ -0,0 +1,136 @@
package tethys;
/**
* Names of Tethys Collections. These are the plural names, though contain functionality
* to get the document names, which are generally the singular of the enum
* @author dg50
*
*/
public enum Collection {
Deployments, Detections, Calibrations, Localizations, SpeciesAbbreviations, Ensembles, SourceMaps, ITIS, ITIS_ranks;
/**
* A list of the main collections in the database, i.e. ones the user will
* possibly want to interract with through the GUI.
* @return list of main collections.
*/
public static Collection[] mainList() {
Collection[] cs = {Deployments, Detections, Calibrations, Localizations, SpeciesAbbreviations, Ensembles};
return cs;
}
/**
* Get the name of a document in this collection, this is generally the singular
* of the collection name.
* @return Document name, e.g. Detection for Detections
*/
public String documentName() {
switch (this) {
case Calibrations:
return "Calibration";
case Deployments:
return "Deployment";
case Detections:
return "Detections"; // this one is plural !
case Localizations:
return "Localization";
case SpeciesAbbreviations:
return "SpeciesAbbreviation";
case Ensembles:
return "Ensemble";
default:
break;
}
return null;
}
public String collectionName() {
return this.toString();
}
/**
* Find a collection for the given name. This does
* a bit more than the simple 'valueof' since it also
* allows the user to input a documentname in place, which
* is just the collection name without the plural 's' on the end
* @param name Collection name.
* @return Collection or null.
*/
public static Collection fromName(String name) {
Collection c = Collection.valueOf(name);
if (c != null) {
return c;
}
/**
* Otherwise, may need to do a longer search to see if the user has passed
* the singular document name.
*/
if (name.endsWith("s") == false) {
c = Collection.valueOf(name+"s");
if (c != null) {
return c;
}
}
return null;
}
/**
* get Tethys collection name from nilus collection objects
* @param className nilus object Class Name
* @return name of Tethys collection
*/
public static Collection fromClass(Class nilusClass) {
String className = nilusClass.getName();
switch(className) {
case "nilus.Deployment":
return Deployments;
case "nilus.Detections":
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 null;
}
}
// /**
// * get Tethys collection name from nilus collection objects
// * @param className nilus object Class Name
// * @return name of Tethys collection
// */
// public static String getCollection(Class nilusClass) {
// String className = nilusClass.getName();
// switch(className) {
// case "nilus.Deployment":
// return "Deployments";
// case "nilus.Detections":
// 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 "";
// }
// }
}

View File

@ -0,0 +1,48 @@
package tethys;
/**
* Basic information about a document that can be used to
* make document lists.
* @author dg50
*
*/
public class DocumentInfo implements Comparable<DocumentInfo> {
private Collection collection;
private String documentName;
private String documentId;
/**
* @param collection
* @param documentName
* @param documentId
*/
public DocumentInfo(Collection collection, String documentName, String documentId) {
this.collection = collection;
this.documentName = documentName;
this.documentId = documentId;
}
@Override
public int compareTo(DocumentInfo o) {
return this.documentName.compareTo(o.documentName);
}
/**
* @return the collection
*/
public Collection getCollection() {
return collection;
}
/**
* @return the documentName
*/
public String getDocumentName() {
return documentName;
}
/**
* @return the documentId
*/
public String getDocumentId() {
return documentId;
}
}

View File

@ -1,6 +1,7 @@
package tethys;
import java.awt.Desktop;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
@ -37,6 +38,7 @@ import PamguardMVC.PamDataBlock;
import metadata.MetaDataContol;
import metadata.deployment.DeploymentData;
import tethys.TethysState.StateType;
import tethys.calibration.CalibrationHandler;
import tethys.dbxml.DBXMLConnect;
import tethys.dbxml.DBXMLQueries;
import tethys.dbxml.ServerStatus;
@ -88,6 +90,7 @@ public class TethysControl extends PamControlledUnit implements PamSettings, Tet
private DeploymentHandler deploymentHandler;
private DetectionsHandler detectionsHandler;
private CalibrationHandler calibrationHandler;
private ITISFunctions itisFunctions;
@ -98,6 +101,8 @@ public class TethysControl extends PamControlledUnit implements PamSettings, Tet
dbxmlQueries = new DBXMLQueries(this, dbxmlConnect);
deploymentHandler = new DeploymentHandler(this);
detectionsHandler = new DetectionsHandler(this);
calibrationHandler = new CalibrationHandler(this);
serverCheckTimer = new Timer(10000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -127,9 +132,23 @@ public class TethysControl extends PamControlledUnit implements PamSettings, Tet
public DBXMLConnect getDbxmlConnect() {
return dbxmlConnect;
}
@Override
public JMenuItem createDetectionMenu(Frame parentFrame) {
return createTethysMenu(parentFrame);
}
@Override
public JMenuItem createFileMenu(JFrame parentFrame) {
// TODO Auto-generated method stub
return super.createFileMenu(parentFrame);
}
/**
* Make a menu. Can go either in File or Settings. TBD.
* @param parentFrame
* @return
*/
public JMenuItem createTethysMenu(Frame parentFrame) {
JMenu tethysMenu = new JMenu("Tethys");
// JMenuItem tethysExport = new JMenuItem("Export ...");
// tethysMenu.add(tethysExport);
@ -150,47 +169,19 @@ public class TethysControl extends PamControlledUnit implements PamSettings, Tet
tethysMenu.add(menuItem);
JMenuItem collections = new JMenu("Collections");
Collection[] mainCollections = Collection.mainList();
for (int i = 0; i < mainCollections.length; i++) {
Collection col = mainCollections[i];
menuItem = new JMenuItem("Open " + col.collectionName() + " collection in browser");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openTethysCollection(col);
}
});
collections.add(menuItem);
}
menuItem = new JMenuItem("Open Deployments collection in browser");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openTethysCollection("Deployments");
}
});
collections.add(menuItem);
menuItem = new JMenuItem("Open Detections collection in browser");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openTethysCollection("Detections");
}
});
collections.add(menuItem);
menuItem = new JMenuItem("Open Localizations collection in browser");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openTethysCollection("Localizations");
}
});
collections.add(menuItem);
menuItem = new JMenuItem("Open Calibrations collection in browser");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openTethysCollection("Calibrations");
}
});
collections.add(menuItem);
menuItem = new JMenuItem("Open Species Abbreviations collection in browser");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openTethysCollection("SpeciesAbbreviations");
}
});
collections.add(menuItem);
tethysMenu.add(collections);
tethysMenu.addSeparator();
JMenuItem showDeps = new JMenuItem("Show project deployments");
@ -202,6 +193,16 @@ public class TethysControl extends PamControlledUnit implements PamSettings, Tet
});
tethysMenu.add(showDeps);
JMenuItem cals = new JMenuItem("Export calibrations");
cals.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calibrationHandler.exportAllCalibrations();
}
});
tethysMenu.add(cals);
tethysMenu.addSeparator();
JMenuItem mapItem = new JMenuItem("Export species maps ...");
mapItem.setToolTipText("Export all species maps (PAMGuard codes to ITIS codes to file for import into other configurations");
@ -292,24 +293,24 @@ public class TethysControl extends PamControlledUnit implements PamSettings, Tet
// } catch (URISyntaxException e) {
// e.printStackTrace();
// }
openTethysCollection("Client");
openCollectionInBrowser("Client");
}
/**
* open client in the default web browser
*/
public void openTethysCollection(String collectionName) {
if (collectionName == null) {
public void openTethysCollection(Collection collection) {
if (collection == null) {
return;
}
if (getTethysExportParams().listDocsInPamguard && collectionName.equals("Client") == false) {
openCollectionInPAMGuard(collectionName);
if (getTethysExportParams().listDocsInPamguard) {
openCollectionInPAMGuard(collection);
}
else {
openCollectionInBrowser(collectionName);
openCollectionInBrowser(collection.collectionName());
}
}
public void openCollectionInPAMGuard(String collectionName) {
TethysDocumentsFrame.showTable(getGuiFrame(), this, collectionName);
public void openCollectionInPAMGuard(Collection collection) {
TethysDocumentsFrame.showTable(getGuiFrame(), this, collection);
}
public void openCollectionInBrowser(String collectionName) {
@ -524,19 +525,17 @@ public class TethysControl extends PamControlledUnit implements PamSettings, Tet
ArrayList<PDeployment> matchedDeployments = deploymentHandler.getMatchedDeployments();
for (DatablockSynchInfo synchInfo : dataBlockSynchInfos) {
// dataPrefixes[i] = DetectionsHandler.getDetectionsDocIdPrefix(deplData.getProject(), synchInfo.getDataBlock());
int count = 0;
int detectionCount = 0;
int documentCount = 0;
for (PDeployment pDepl : matchedDeployments) {
count += dbxmlQueries.countData(synchInfo.getDataBlock(), pDepl.deployment.getId());
detectionCount += dbxmlQueries.countData(synchInfo.getDataBlock(), pDepl.deployment.getId());
ArrayList<String> detectionsNames = getDbxmlQueries().getDetectionsDocuments(synchInfo.getDataBlock(), pDepl.deployment.getId());
if (detectionsNames != null) {
documentCount += detectionsNames.size();
}
synchInfo.setDataCount(count);
// also count the actual number of Detectoin documents
ArrayList<String> someNames = getDbxmlQueries().getDetectionsDocuments(synchInfo.getDataBlock(), null);
if (someNames == null) {
synchInfo.setDetectionDocumentCount(0);
}
else {
synchInfo.setDetectionDocumentCount(someNames.size());
}
synchInfo.setDataCount(detectionCount);
synchInfo.setDetectionDocumentCount(documentCount);
i++;
}
@ -590,6 +589,11 @@ public class TethysControl extends PamControlledUnit implements PamSettings, Tet
WarnOnce.showWarning(title, msg, WarnOnce.WARNING_MESSAGE);
}
public void displayDocument(DocumentInfo docInfo) {
String collectionName = docInfo.getCollection().collectionName();
String docId = docInfo.getDocumentId();
displayDocument(collectionName, docId);
}
/**
* Load a document from the database and display it in a popup window
* @param collection
@ -679,4 +683,12 @@ public class TethysControl extends PamControlledUnit implements PamSettings, Tet
sendStateUpdate(new TethysState(StateType.NEWPAMGUARDSELECTION));
}
/**
* @return the calibrationHandler
*/
public CalibrationHandler getCalibrationHandler() {
return calibrationHandler;
}
}

View File

@ -0,0 +1,192 @@
package tethys.calibration;
import java.util.List;
import Acquisition.AcquisitionControl;
import Acquisition.AcquisitionParameters;
import Acquisition.AcquisitionProcess;
import Array.ArrayManager;
import Array.Hydrophone;
import Array.PamArray;
import PamController.PamController;
import PamController.soundMedium.GlobalMedium;
import PamController.soundMedium.GlobalMedium.SoundMedium;
import PamController.soundMedium.GlobalMediumManager;
import nilus.Calibration;
import nilus.Calibration.FrequencyResponse;
import nilus.Calibration.QualityAssurance;
import nilus.Helper;
import nilus.MetadataInfo;
import nilus.QualityValueBasic;
import nilus.ResponsibleParty;
import tethys.TethysControl;
import tethys.TethysState;
import tethys.TethysStateObserver;
import tethys.TethysTimeFuncs;
import tethys.dbxml.DBXMLConnect;
import tethys.dbxml.TethysException;
import tethys.niluswraps.PDeployment;
import tethys.pamdata.AutoTethysProvider;
public class CalibrationHandler implements TethysStateObserver {
private TethysControl tethysControl;
/**
* @param tethysControl
*/
public CalibrationHandler(TethysControl tethysControl) {
this.tethysControl = tethysControl;
tethysControl.addStateObserver(this);
}
@Override
public void updateState(TethysState tethysState) {
// TODO Auto-generated method stub
}
public int exportAllCalibrations() {
PamArray array = ArrayManager.getArrayManager().getCurrentArray();
int nPhone = array.getHydrophoneCount();
DBXMLConnect dbxml = tethysControl.getDbxmlConnect();
int nExport = 0;
for (int i = 0; i < nPhone; i++) {
// String docName = getHydrophoneId(i);
Calibration calDoc = createCalibrationDocument(i);
boolean ok = false;
try {
ok = dbxml.postAndLog(calDoc);
} catch (TethysException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tethysControl.showException(e);
ok = false;
}
if (ok) {
nExport++;
}
}
return nExport;
}
/**
* Create a calibration document for a single hydrophone channel.
* @param pDeployment deployment, for cross referencing.
* @param channelIndex channel id. One document per channel for a multi hydrophone array.
* @return Calibration document.
*/
public Calibration createCalibrationDocument(int channelIndex) {
AcquisitionControl daqControl = (AcquisitionControl) PamController.getInstance().findControlledUnit(AcquisitionControl.unitType);
return createCalibrationDocument(daqControl, channelIndex);
}
/**
* Get an id based on the instrument identifiers and channel number.
* @param channelIndex
* @return id string - instrument type + instrument id + channel
*/
public String getHydrophoneId(int channelIndex) {
PamArray array = ArrayManager.getArrayManager().getCurrentArray();
if (array == null) {
return null;
}
String id = String.format("%s_%s_ch%02d", array.getInstrumentType(), array.getInstrumentId(), channelIndex);
id = id.replace(" ", "_");
return id;
}
/**
* Create a calibration document for a single hydrophone channel.
* @param pDeployment deployment, for cross referencing.
* @param soundAcquisition Daq information - needed to get the ADC calibration information.
* @param channelIndex channel id. One document per channel for a multi hydrophone array.
* @return Calibration document.
*/
public Calibration createCalibrationDocument(AcquisitionControl soundAcquisition, int channelIndex) {
/**
* Calibrations document id and cross referencing to Deploymnet documents:
* Identifier of instrument, preamplifier, or hydrophone.
* Corresponds to elements in Deployment:
* Deployment/Instrument/Id,
* Deployment/Sensors/Audio/HydrophoneId,
* Deployment/Sensors/Audio[i]/PreampId.
* As instruments may be calibrated multiple times, it is not an error for duplicate Id values to appear.
* It is recommended that the three different types of identifiers (instrument, hydrophone, preamp) be distinct,
* but the Type element may be used to distinguish them if they are not.
*/
/*
* very remote possibility that DAQ doesn't exist. What to do in this case ? It's also possible that some configurations may
* have to have >1 DAQ's ?
*/
PamArray array = ArrayManager.getArrayManager().getCurrentArray();
if (array == null) {
return null;
}
if (channelIndex < 0 || channelIndex >= array.getHydrophoneCount()) {
return null;
}
// ArrayManager.getArrayManager().get
// hydrophones = array.
Hydrophone hydrophone = array.getHydrophoneArray().get(channelIndex);
double hSens = hydrophone.getSensitivity();
double preampGain = hydrophone.getPreampGain();
GlobalMediumManager mediumManager = PamController.getInstance().getGlobalMediumManager();
SoundMedium currentMedium = mediumManager.getCurrentMedium();
double dbRef = GlobalMedium.getdBreference(currentMedium); // probably in Pa, so multiply by 1e6.
/**
* The calibration id can be a bit tricky, it will need to be cross referenced from the
* Deployment document, and it is likely that a deployment document will have to reference several
* calibration documents for different channels.
* Make the name from the Array name (new), the array Instrument Id (unique to the array)
* and the channel number. These will then all have to go into the Deployment document in
* the list of audio devices, cross referenced as the SensorId field.
*
*/
Calibration calibration = new Calibration();
try {
Helper.createRequiredElements(calibration);
} catch (IllegalArgumentException | IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
String id = getHydrophoneId(channelIndex);
// id = String.format("%d", channelIndex);
calibration.setId(id);
calibration.setTimeStamp(TethysTimeFuncs.xmlGregCalFromMillis(System.currentTimeMillis()));
calibration.setType(GlobalMedium.getRecieverString(currentMedium, false, false));
calibration.setIntensityReferenceUPa(AutoTethysProvider.roundSignificantFigures(dbRef*1e6,3));
String sensRef = GlobalMedium.getdBRefString(currentMedium);
// it doesn't like this since it has a unicode character. Leave it or change the micro to 'u'
// calibration.setSensitivityReference(sensRef);
calibration.setSensitivityDBV(hSens+preampGain);
if (soundAcquisition != null) {
AcquisitionProcess daqProcess = soundAcquisition.getAcquisitionProcess();
double fullScale = daqProcess.rawAmplitude2dB(1, channelIndex, false);
calibration.setSensitivityDBFS(fullScale);
}
FrequencyResponse frs = calibration.getFrequencyResponse();
List<Double> hz = frs.getHz();
List<Double> db = frs.getDB();
hz.add(Double.valueOf(0));
db.add(Double.valueOf(hSens+preampGain));
MetadataInfo metaInf = calibration.getMetadataInfo();
metaInf.setDate(TethysTimeFuncs.xmlGregCalFromMillis(System.currentTimeMillis()));
metaInf.setUpdateFrequency("as-needed");
ResponsibleParty contact = metaInf.getContact();
contact.setIndividualName("Unknown");
contact.setOrganizationName("unknown");
QualityAssurance qa = calibration.getQualityAssurance();
qa.setQuality(QualityValueBasic.VALID);
qa.setComment("Unknown calibration");
return calibration;
}
}

View File

@ -0,0 +1,5 @@
package tethys.calibration.swing;
public class CalibrationsPanel {
}

View File

@ -18,6 +18,7 @@ import dbxml.JerseyClient;
import dbxml.Queries;
import dbxml.uploader.Importer;
import nilus.MarshalXML;
import tethys.Collection;
import tethys.TethysControl;
import tethys.database.TethysActions;
import tethys.database.TethysLogger;
@ -40,7 +41,7 @@ public class DBXMLConnect {
private String currentSiteURL;
public static String[] collections = {"Deployments", "Detections", "Localizations", "Calibrations", "SpeciesAbbreviations"};
// public static String[] collections = {"Deployments", "Detections", "Localizations", "Calibrations", "SpeciesAbbreviations"};
public DBXMLConnect(TethysControl tethysControl) {
this.tethysControl = tethysControl;
@ -127,20 +128,33 @@ public class DBXMLConnect {
public boolean postAndLog(Object nilusObject) throws TethysException
{
return postAndLog(nilusObject, null);
}
/**
* I don't think this should ever be used since everything goes a bit pear
* shaped if the documentName isn't the same as the Id.
* @param nilusObject
* @param documentName
* @return
* @throws TethysException
*/
private boolean postAndLog(Object nilusObject, String documentName) throws TethysException
{
TethysException e = null;
boolean success = false;
try {
success = postToTethys(nilusObject);
success = postToTethys(nilusObject, documentName);
}
catch (TethysException ex) {
e = ex;
}
TethysLogger logger = TethysLogger.getTethysLogger(tethysControl);
Class objClass = nilusObject.getClass();
String collection = getTethysCollection(objClass.getName());
Collection collection = Collection.fromClass(objClass);
String documentId = getDocumentId(nilusObject);
logger.logAction(collection, documentId, TethysActions.ADDDOCUMENT, success, "");
logger.logAction(collection.collectionName(), documentId, TethysActions.ADDDOCUMENT, success, "");
if (e != null) {
throw (e);
}
@ -154,22 +168,24 @@ public class DBXMLConnect {
* @return error string, null string means there are no errors
* @throws TethysException
*/
private boolean postToTethys(Object nilusObject) throws TethysException
private boolean postToTethys(Object nilusObject, String documentName) throws TethysException
{
Class objClass = nilusObject.getClass();
String collection = getTethysCollection(objClass.getName());
Collection collection = Collection.fromClass(nilusObject.getClass());
TethysExportParams params = new TethysExportParams();
String importReturn = null;
String tempName = getTempFileName(nilusObject);
tempName = tempDirectory.getAbsolutePath() + File.separator + tempName + ".xml";
File tempFile = new File(tempName);
String bodgeName = tempName;//"C:\\Users\\dg50\\AppData\\Local\\Temp\\PAMGuardTethys\\Meygen2022_10a.xml";
if (documentName == null) {
documentName = getTempFileName(nilusObject);
}
documentName = tempDirectory.getAbsolutePath() + File.separator + documentName + ".xml";
File tempFile = new File(documentName);
String bodgeName = documentName;//"C:\\Users\\dg50\\AppData\\Local\\Temp\\PAMGuardTethys\\Meygen2022_10a.xml";
try {
MarshalXML marshal = new MarshalXML();
marshal.createInstance(objClass);
marshal.marshal(nilusObject, tempFile.toString());
// tempFile = stripXMLHeader(tempFile);
importReturn = Importer.ImportFiles(params.getFullServerName(), collection,
importReturn = Importer.ImportFiles(params.getFullServerName(), collection.collectionName(),
new String[] { bodgeName }, "", "", false);
@ -207,7 +223,7 @@ public class DBXMLConnect {
*/
public boolean updateDocument(Object nilusDocument) throws TethysException {
deleteDocument(nilusDocument);
return postToTethys(nilusDocument);
return postToTethys(nilusDocument, null);
}
/**
@ -221,11 +237,11 @@ public class DBXMLConnect {
public boolean deleteDocument(Object nilusDocument) throws TethysException {
Class objClass = nilusDocument.getClass();
String collection = getTethysCollection(objClass.getName());
Collection collection = Collection.fromClass(objClass);
String docId = getDocumentId(nilusDocument);
String result = null;
try {
result = jerseyClient.removeDocument(collection, docId );
result = jerseyClient.removeDocument(collection.collectionName(), docId );
/**
* Return from a sucessful delete is something like
*
@ -274,6 +290,7 @@ An error will throw an exception.
*/
public boolean removeDocument(String collection, String docId) throws TethysException {
try {
// docId = "SoundTrap_600_HF_7129_ch00";
Object result = jerseyClient.removeDocument(collection, docId );
/**
* Return from a sucessful delete is something like
@ -459,35 +476,7 @@ C:\Users\dg50\AppData\Local\Temp\PAMGuardTethys\20080311_2DSimplex_0.xmlnot: 0 b
}
/**
* get Tethys collection name from nilus collection objects
* @param className nilus object Class Name
* @return name of Tethys collection
*/
public String getTethysCollection(String className) {
switch(className) {
case "nilus.Deployment":
return "Deployments";
case "nilus.Detections":
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 synchronized boolean openConnections() {
TethysExportParams params = tethysControl.getTethysExportParams();

View File

@ -31,6 +31,8 @@ import nilus.Detections;
import nilus.GranularityEnumType;
import nilus.GranularityType;
import nilus.Helper;
import tethys.Collection;
import tethys.DocumentInfo;
import tethys.TethysControl;
import tethys.TethysTimeFuncs;
import tethys.output.TethysExportParams;
@ -139,7 +141,7 @@ public class DBXMLQueries {
// Queries queries = new Queries(jerseyClient);
queryResult = jerseyClient.queryJSON(jsonQueryString, 0);
schemaPlan = jerseyClient.queryJSON(jsonQueryString, 1);
// schemaPlan = jerseyClient.queryJSON(jsonQueryString, 1);
}
catch (Exception e) {
@ -149,75 +151,138 @@ public class DBXMLQueries {
return new DBQueryResult(System.currentTimeMillis()-t1, queryResult, schemaPlan);
}
/**
* Check whether or not to strip of the s of one of the collection names.
* This is caused by some daft thing whereby the Deployments colleciton is called Deployments
* byt the Detections collection is called Detection
* @param collection
* @return
*/
public String checkCollectionPlural(String collection) {
switch (collection) {
case "Deployments":
return "Deployment";
case "Localizations":
return "Localize";
case "Calibrations":
return "Calibration";
case "SpeciesAbbreviations":
return "SpeciesAbbreviations";
}
return collection;
}
// /**
// * Check whether or not to strip of the s of one of the collection names.
// * This is caused by some daft thing whereby the Deployments colleciton is called Deployments
// * byt the Detections collection is called Detection
// * @param collection
// * @return
// */
// public String checkCollectionPlural(String collection) {
// switch (collection) {
// case "Deployments":
// return "Deployment";
// case "Localizations":
// return "Localize";
// case "Calibrations":
// return "Calibration";
// case "SpeciesAbbreviations":
// return "SpeciesAbbreviations";
// }
// return collection;
// }
/**
* Get a list of all documents in a collection.
* @param collection
* @return list of all documents in a collection, or null if no collection.
*/
public ArrayList<String> getCollectionDocumentList(String collection) {
public ArrayList<DocumentInfo> getCollectionDocumentList(Collection collection) {
if (collection == null) {
return null;
}
collection = checkCollectionPlural(collection);
// if (collection.endsWith("s")) {
// collection = collection.substring(0, collection.length()-1);
// }
String baseQuery = "{\"return\":[\"COLLECTIONNAME/Id\"],\"select\":[],\"enclose\":1}";
baseQuery = baseQuery.replace("COLLECTIONNAME", collection);
String tagName = "Id";
if (collection.equals("SpeciesAbbreviations")) {
baseQuery = "{\"return\":[\"Abbreviations/Name\"],\"select\":[],\"enclose\":1}";
tagName = "Name";
}
/**
* xQuery string based on examples in email from MR on 27/9/2023
*/
// String baseQuery = "<documents> {\r\n"
String baseQuery = "<documents xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> {\r\n"
+ " for $doc in collection(\"COLLECTIONAME\")/DOCUMENTNAME\r\n"
+ " return\r\n"
+ " <doc> {\r\n"
+ " base-uri($doc), \r\n"
+ " $doc/Id\r\n"
+ " }\r\n"
+ " </doc>\r\n"
+ "} </documents>\r\n"
+ "";
String xQuery = baseQuery.replace("COLLECTIONAME", collection.collectionName());
xQuery = xQuery.replace("DOCUMENTNAME", collection.documentName());
DBQueryResult result;
Queries queries = dbXMLConnect.getTethysQueries();
String result = null;
try {
result = executeQuery(baseQuery);
} catch (TethysQueryException e) {
System.out.println("Error with query: " + baseQuery);
tethysControl.showException(e);
result = queries.QueryTethys(xQuery);
}
catch (Exception e) {
e.printStackTrace();
}
if (result == null) {
return null;
}
// System.out.println(result);
ArrayList<DocumentInfo> documentInfos = new ArrayList<>();
if (result == null || result.queryResult == null) {
return null;
}
Document doc = convertStringToXMLDocument(result.queryResult);
Document doc = convertStringToXMLDocument(result);
if (doc == null) {
return null;
}
NodeList returns = doc.getElementsByTagName(tagName);
ArrayList<String> docIds = new ArrayList<>();
// PamguardXMLWriter pamXMLWriter = PamguardXMLWriter.getXMLWriter();
// System.out.println(pamXMLWriter.getAsString(doc));
/**
* lots of elements along lines of
* <doc>dbxml:///Deployments/Meygen20229<Id>Meygen20229</Id></doc>
*/
NodeList returns = doc.getElementsByTagName("doc");
int n = returns.getLength();
String toStrip = "dbxml:///"+collection.collectionName()+"/";
for (int i = 0; i < n; i++) {
Node aNode = returns.item(i);
String docId = aNode.getTextContent();
docIds.add(docId);
// this is the doc name with a load of stuff in front,
// e.g. dbxml:///Deployments/1705_Array-2017-09-261705_Array-2017-09-26
String nameStr = aNode.getTextContent();
nameStr = nameStr.replaceFirst(toStrip, "");
String id = null;
if (aNode instanceof Element) {
id = getElementData((Element) aNode, "Id");
}
return docIds;
DocumentInfo docInfo = new DocumentInfo(collection, nameStr, id);
documentInfos.add(docInfo);
// System.out.println(nameStr + " : " + id);
}
return documentInfos;
// if (collection.endsWith("s")) {
// collection = collection.substring(0, collection.length()-1);
// }
// String baseQuery = "{\"return\":[\"COLLECTIONNAME/Id\"],\"select\":[],\"enclose\":1}";
// baseQuery = baseQuery.replace("COLLECTIONNAME", collection);
// String tagName = "Id";
//
// if (collection.equals("SpeciesAbbreviations")) {
// baseQuery = "{\"return\":[\"Abbreviations/Name\"],\"select\":[],\"enclose\":1}";
// tagName = "Name";
// }
//
// DBQueryResult result;
// try {
// result = executeQuery(baseQuery);
// } catch (TethysQueryException e) {
// System.out.println("Error with query: " + baseQuery);
// tethysControl.showException(e);
// return null;
// }
//
// if (result == null || result.queryResult == null) {
// return null;
// }
// Document doc = convertStringToXMLDocument(result.queryResult);
// if (doc == null) {
// return null;
// }
// NodeList returns = doc.getElementsByTagName(tagName);
// ArrayList<String> docIds = new ArrayList<>();
// int n = returns.getLength();
// for (int i = 0; i < n; i++) {
// Node aNode = returns.item(i);
// String docId = aNode.getTextContent();
// docIds.add(docId);
// }
//
// return docIds;
}
public ArrayList<String> getProjectNames() {

View File

@ -58,6 +58,7 @@ import tethys.TethysLocationFuncs;
import tethys.TethysState;
import tethys.TethysStateObserver;
import tethys.TethysTimeFuncs;
import tethys.calibration.CalibrationHandler;
import tethys.TethysState.StateType;
import tethys.dbxml.DBXMLConnect;
import tethys.dbxml.TethysException;
@ -979,6 +980,7 @@ public class DeploymentHandler implements TethysStateObserver {
private String getInstrumentType() {
return ArrayManager.getArrayManager().getCurrentArray().getInstrumentType();
}
/**
* Get a geometry type string for Tethys based on information in the array manager.
* @return
@ -1009,12 +1011,15 @@ public class DeploymentHandler implements TethysStateObserver {
ArrayList<Hydrophone> phones = array.getHydrophoneArray();
int iPhone = 0;
long timeMillis = TethysTimeFuncs.millisFromGregorianXML(deployment.getDeploymentDetails().getAudioTimeStamp());
CalibrationHandler calibrationHandler = tethysControl.getCalibrationHandler();
for (Hydrophone aPhone : phones) {
PamVector hydLocs = array.getAbsHydrophoneVector(iPhone, timeMillis);
Audio audio = new Audio();
audio.setNumber(BigInteger.valueOf(iPhone));
audio.setSensorId(String.format("Hydrophone %d", iPhone)); // shold replace with serial number if it exists.
String id = calibrationHandler.getHydrophoneId(iPhone);
// audio.setSensorId(String.format("Hydrophone %d", iPhone)); // should replace with serial number if it exists.
audio.setSensorId(id);
GeometryTypeM geom = new GeometryTypeM();
geom.setXM(hydLocs.getCoordinate(0));
geom.setYM(hydLocs.getCoordinate(1));

View File

@ -5,7 +5,7 @@ import java.util.HashMap;
import PamguardMVC.PamDataBlock;
public class GlobalSpeciesMap implements Serializable {
public class GlobalSpeciesMap implements Serializable, Cloneable {
public static final long serialVersionUID = 1L;
@ -14,7 +14,7 @@ public class GlobalSpeciesMap implements Serializable {
/**
* @return the datablockMaps
*/
private synchronized HashMap<String, DataBlockSpeciesMap> getDatablockMaps() {
public synchronized HashMap<String, DataBlockSpeciesMap> getDatablockMaps() {
if (datablockMaps == null) {
datablockMaps = new HashMap<>();
}
@ -29,4 +29,22 @@ public class GlobalSpeciesMap implements Serializable {
return getDatablockMaps().get(pamDataBlock.getLongDataName());
}
public DataBlockSpeciesMap removeBlock(PamDataBlock pamDataBlock) {
return getDatablockMaps().remove(pamDataBlock.getLongDataName());
}
@Override
public GlobalSpeciesMap clone() {
GlobalSpeciesMap clone;
try {
clone = (GlobalSpeciesMap) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
clone.datablockMaps = new HashMap<>();
clone.datablockMaps.putAll(this.getDatablockMaps());
return clone;
}
}

View File

@ -11,6 +11,9 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import javax.swing.JFileChooser;
@ -22,6 +25,7 @@ import PamController.PamSettings;
import PamUtils.PamFileFilter;
import PamView.dialog.warn.WarnOnce;
import PamguardMVC.PamDataBlock;
import tethys.species.swing.SpeciesMapIODialog;
/**
* Master manager for species maps which will eventually allow for export and import from XML
@ -154,6 +158,17 @@ public class SpeciesMapManager implements PamSettings {
* @return
*/
public boolean exportSpeciesMaps(Window parentFrame) {
// gather the species maps from the data blocks...
gatherSpeciesMaps();
GlobalSpeciesMap toExport = SpeciesMapIODialog.showDialog(parentFrame, globalSpeciesMap, true);
if (toExport == null) {
return false;
}
if (toExport.getDatablockMaps().size() == 0) {
return false;
}
JFileChooser chooser = getFileChooser();
int ans = chooser.showSaveDialog(parentFrame);
if (ans != JFileChooser.APPROVE_OPTION) {
@ -164,7 +179,7 @@ public class SpeciesMapManager implements PamSettings {
// write it.
try {
ObjectOutputStream op = new ObjectOutputStream(new FileOutputStream(opFile));
op.writeObject(getSettingsReference());
op.writeObject(toExport);
op.close();
} catch (IOException e) {
e.printStackTrace();
@ -219,34 +234,66 @@ public class SpeciesMapManager implements PamSettings {
return false;
}
return handleNewSpeciesMap(readSpeciesMap);
GlobalSpeciesMap keptMaps = SpeciesMapIODialog.showDialog(parentFrame, readSpeciesMap, false);
if (keptMaps == null) {
return false;
}
if (keptMaps.getDatablockMaps().size() == 0) {
return false;
}
return handleNewSpeciesMap(keptMaps);
}
private boolean handleNewSpeciesMap(GlobalSpeciesMap readSpeciesMap) {
if (readSpeciesMap == null) {
return false;
}
// could put in a dialog to only select parts of the map if we wanted to ?
int ans = WarnOnce.showWarning("Global Species Map",
"Do you want to overwrite ALL PAMGaurd species maps with the imported data ?",
"Do you want to overwrite PAMGaurd species maps with the imported data ?",
WarnOnce.YES_NO_OPTION);
if (ans == WarnOnce.CANCEL_OPTION) {
return false;
}
globalSpeciesMap = readSpeciesMap;
// no wupdate all datablock maps since they keep their own copies.
ArrayList<PamDataBlock> allDatablocks = PamController.getInstance().getDataBlocks();
for (PamDataBlock aBlock : allDatablocks) {
DataBlockSpeciesManager spManager = aBlock.getDatablockSpeciesManager();
if (spManager == null) {
Set<Entry<String, DataBlockSpeciesMap>> mapSet = readSpeciesMap.getDatablockMaps().entrySet();
Iterator<Entry<String, DataBlockSpeciesMap>> iter = mapSet.iterator();
while (iter.hasNext()) {
Entry<String, DataBlockSpeciesMap> entry = iter.next();
PamDataBlock dataBlock = PamController.getInstance().getDataBlockByLongName(entry.getKey());
if (dataBlock == null) {
String err = String.format("Data block %s does not exist in the current configuration", entry.getKey());
WarnOnce.showWarning("Missing data block", err, WarnOnce.WARNING_MESSAGE);
continue;
}
DataBlockSpeciesMap blockMap = globalSpeciesMap.get(aBlock);
if (blockMap != null) {
spManager.setDatablockSpeciesMap(blockMap);
globalSpeciesMap.put(dataBlock, entry.getValue());
DataBlockSpeciesManager spManager = dataBlock.getDatablockSpeciesManager();
if (spManager == null) {
String err = String.format("Data block %s does not have a species manager", entry.getKey());
WarnOnce.showWarning("Missing species manager", err, WarnOnce.WARNING_MESSAGE);
continue;
}
spManager.setDatablockSpeciesMap(entry.getValue());
}
// globalSpeciesMap = readSpeciesMap;
// // no wupdate all datablock maps since they keep their own copies.
// ArrayList<PamDataBlock> allDatablocks = PamController.getInstance().getDataBlocks();
// for (PamDataBlock aBlock : allDatablocks) {
// DataBlockSpeciesManager spManager = aBlock.getDatablockSpeciesManager();
// if (spManager == null) {
// continue;
// }
// DataBlockSpeciesMap blockMap = globalSpeciesMap.get(aBlock);
// if (blockMap != null) {
// spManager.setDatablockSpeciesMap(blockMap);
// }
// }
return true;
}
}

View File

@ -0,0 +1,144 @@
package tethys.species.swing;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import PamView.dialog.PamDialog;
import PamView.dialog.PamGridBagContraints;
import tethys.species.DataBlockSpeciesMap;
import tethys.species.GlobalSpeciesMap;
/**
* dialog to select which species maps to import / export.
* @author dg50
*
*/
public class SpeciesMapIODialog extends PamDialog {
private static SpeciesMapIODialog singleInstance = null;
private GlobalSpeciesMap speciesMap;
private JCheckBox everything;
private JCheckBox[] blockBoxes;
private JPanel boxesPanel;
/**
* @param parentFrame
* @param title
* @param hasDefault
*/
private SpeciesMapIODialog(Window parentFrame) {
super(parentFrame, "Map IO", true);
boxesPanel = new JPanel();
boxesPanel.setBorder(new TitledBorder("Select datablocks"));
setDialogComponent(boxesPanel);
}
public static GlobalSpeciesMap showDialog(Window parentFrame, GlobalSpeciesMap speciesMap, boolean export) {
if (singleInstance == null) {
singleInstance = new SpeciesMapIODialog(parentFrame);
}
if (speciesMap.getDatablockMaps().size() == 0) {
singleInstance.showWarning("No Data block species maps are defined");
return speciesMap;
}
singleInstance.setTitle(export ? "Export species maps" : "Import species maps");
singleInstance.setParams(speciesMap);
singleInstance.setVisible(true);
return singleInstance.speciesMap;
}
private void setParams(GlobalSpeciesMap speciesMap) {
this.speciesMap = speciesMap.clone();
boxesPanel.removeAll();
HashMap<String, DataBlockSpeciesMap> blockMaps = speciesMap.getDatablockMaps();
Set<Entry<String, DataBlockSpeciesMap>> mapSet = blockMaps.entrySet();
Iterator<Entry<String, DataBlockSpeciesMap>> iter = mapSet.iterator();
boxesPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new PamGridBagContraints();
boxesPanel.add(everything = new JCheckBox("Select All"), c);
everything.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
enableControls();
}
});
blockBoxes = new JCheckBox[mapSet.size()];
int iBox = 0;
while (iter.hasNext()) {
Entry<String, DataBlockSpeciesMap> item = iter.next();
c.gridy++;
blockBoxes[iBox] = new JCheckBox(item.getKey());
boxesPanel.add(blockBoxes[iBox], c);
iBox++;
}
enableControls();
}
protected void enableControls() {
if (blockBoxes == null) {
return;
}
boolean selAll = everything.isSelected();
for (int i = 0; i < blockBoxes.length; i++) {
blockBoxes[i].setEnabled(!selAll);
if (selAll) {
blockBoxes[i].setSelected(true);
}
}
}
@Override
public boolean getParams() {
if (everything.isSelected()) {
return true;
}
HashMap<String, DataBlockSpeciesMap> blockMaps = speciesMap.getDatablockMaps();
Set<Entry<String, DataBlockSpeciesMap>> mapSet = blockMaps.entrySet();
Iterator<Entry<String, DataBlockSpeciesMap>> iter = mapSet.iterator();
for(int i = 0; i < blockBoxes.length; i++) {
if (blockBoxes[i].isSelected() == false) {
String name = blockBoxes[i].getText();
blockMaps.remove(name);
}
}
// int iBox = 0;
// while (iter.hasNext()) {
// if (blockBoxes[iBox].isSelected() == false) {
// iter.remove();
// }
// iBox++;
// }
return true;
}
@Override
public void cancelButtonPressed() {
speciesMap = null;
}
@Override
public void restoreDefaultSettings() {
everything.setSelected(true);
enableControls();
}
}

View File

@ -18,6 +18,7 @@ import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.EmptyBorder;
import tethys.Collection;
import tethys.TethysControl;
import tethys.dbxml.DBXMLConnect;
@ -71,7 +72,7 @@ public class FancyClientButton extends JPanel {
dropButton.setBorder(new EmptyBorder(dInsets));
}
String[] collections = DBXMLConnect.collections;
Collection[] collections = Collection.mainList();
collectionsMenu = new JPopupMenu();
boolean isP = tethysControl.getTethysExportParams().listDocsInPamguard;
showBrowser = new JCheckBoxMenuItem("Show in Browser", isP == false);
@ -100,7 +101,7 @@ public class FancyClientButton extends JPanel {
collectionsMenu.addSeparator();
for (int i = 0; i < collections.length; i++) {
JMenuItem menuItem = new JMenuItem(collections[i]);
JMenuItem menuItem = new JMenuItem(collections[i].collectionName());
menuItem.addActionListener(new OpenCollection(collections[i]));
collectionsMenu.add(menuItem);
}
@ -128,9 +129,9 @@ public class FancyClientButton extends JPanel {
private class OpenCollection implements ActionListener {
private String collection;
private Collection collection;
public OpenCollection(String collection) {
public OpenCollection(Collection collection) {
super();
this.collection = collection;
}

View File

@ -21,6 +21,8 @@ import PamController.PamController;
import PamView.dialog.PamDialogPanel;
import PamView.dialog.warn.WarnOnce;
import PamView.tables.SwingTableColumnWidths;
import tethys.Collection;
import tethys.DocumentInfo;
import tethys.TethysControl;
import tethys.dbxml.TethysException;
@ -33,13 +35,13 @@ public class TethysDocumentTable implements PamDialogPanel {
private TethysControl tethysControl;
private String collectionName;
private Collection collection;
private JTable mainTable;
private TableModel tableModel;
private ArrayList<String> documentNames;
private ArrayList<DocumentInfo> documentInfos;
private JPanel mainPanel;
@ -49,24 +51,24 @@ public class TethysDocumentTable implements PamDialogPanel {
* @param tethysControl
* @param collectionName
*/
public TethysDocumentTable(TethysControl tethysControl, String collectionName) {
public TethysDocumentTable(TethysControl tethysControl, Collection collection) {
this.tethysControl = tethysControl;
this.collection = collection;
mainPanel = new JPanel(new BorderLayout());
tableModel = new TableModel();
mainTable = new JTable(tableModel);
scrollPane = new JScrollPane(mainTable);
mainPanel.add(BorderLayout.CENTER, scrollPane);
new SwingTableColumnWidths(tethysControl.getUnitName()+"TethysDocumentsTable", mainTable);
this.setCollectionName(collectionName);
mainTable.addMouseListener(new TableMouse());
mainTable.setRowSelectionAllowed(true);
mainTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
public void updateTableData() {
documentNames = tethysControl.getDbxmlQueries().getCollectionDocumentList(collectionName);
if (documentNames != null) {
Collections.sort(documentNames);
documentInfos = tethysControl.getDbxmlQueries().getCollectionDocumentList(collection);
if (documentInfos != null) {
Collections.sort(documentInfos);
}
tableModel.fireTableDataChanged();
}
@ -90,21 +92,21 @@ public class TethysDocumentTable implements PamDialogPanel {
}
public void showPopupMenu(MouseEvent e) {
if (documentNames == null) {
if (documentInfos == null) {
return;
}
int row = mainTable.getSelectedRow();
if (row < 0|| row >= documentNames.size()) {
if (row < 0|| row >= documentInfos.size()) {
return;
}
String docName = documentNames.get(row);
DocumentInfo docInfo = documentInfos.get(row);
JPopupMenu popMenu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Show document " + docName);
JMenuItem menuItem = new JMenuItem("Show document " + docInfo);
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showDocument(docName);
showDocument(docInfo);
}
});
popMenu.add(menuItem);
@ -113,11 +115,11 @@ public class TethysDocumentTable implements PamDialogPanel {
int[] rows = mainTable.getSelectedRows();
if (rows != null && rows.length == 1) {
// docName = documentNames.get(rows[0]);
menuItem = new JMenuItem("Delete document " + docName);
menuItem = new JMenuItem("Delete document " + docInfo);
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteDocument(docName);
deleteDocument(docInfo);
}
});
popMenu.add(menuItem);
@ -137,18 +139,18 @@ public class TethysDocumentTable implements PamDialogPanel {
popMenu.show(e.getComponent(), e.getX(), e.getY());
}
private void showDocument(String docName) {
tethysControl.displayDocument(collectionName, docName);
private void showDocument(DocumentInfo docInfo) {
tethysControl.displayDocument(docInfo);
}
private void deleteDocument(String docName) {
int ans = WarnOnce.showNamedWarning("deletedoc"+collectionName, PamController.getMainFrame(), "Delete document",
"Are you sure you want to delete the document " + docName, WarnOnce.OK_CANCEL_OPTION);
private void deleteDocument(DocumentInfo docInfo) {
int ans = WarnOnce.showNamedWarning("deletedoc "+ collection.collectionName(), PamController.getMainFrame(), "Delete document",
"Are you sure you want to delete the document " + docInfo, WarnOnce.OK_CANCEL_OPTION);
if (ans == WarnOnce.OK_OPTION) {
try {
tethysControl.getDbxmlConnect().removeDocument(collectionName, docName);
tethysControl.getDbxmlConnect().removeDocument(docInfo.getCollection().collectionName(), docInfo.getDocumentId());
} catch (TethysException e) {
System.out.println("Failed to delete " + docName);
System.out.println("Failed to delete " + docInfo);
System.out.println(e.getMessage());
}
}
@ -156,7 +158,7 @@ public class TethysDocumentTable implements PamDialogPanel {
}
private void deleteDocuments(int[] rows) {
int ans = WarnOnce.showNamedWarning("deletedoc"+collectionName, PamController.getMainFrame(), "Delete documents",
int ans = WarnOnce.showNamedWarning("deletedoc "+collection.collectionName(), PamController.getMainFrame(), "Delete documents",
"Are you sure you want to delete multiple documents ", WarnOnce.OK_CANCEL_OPTION);
if (ans != WarnOnce.OK_OPTION) {
return;
@ -165,16 +167,16 @@ public class TethysDocumentTable implements PamDialogPanel {
* make a new list before anything is deleted since the
* man list will get updated during deletion and be out of date.
*/
String[] docNames = new String[rows.length];
DocumentInfo[] docInfos = new DocumentInfo[rows.length];
for (int i = 0; i < rows.length; i++) {
docNames[i] = documentNames.get(rows[i]);
docInfos[i] = documentInfos.get(rows[i]);
}
// now it's safe to delete them.
for (int i = 0; i < docNames.length; i++) {
for (int i = 0; i < docInfos.length; i++) {
try {
tethysControl.getDbxmlConnect().removeDocument(collectionName, docNames[i]);
tethysControl.getDbxmlConnect().removeDocument(docInfos[i].getCollection().collectionName(), docInfos[i].getDocumentId());
} catch (TethysException e) {
System.out.println("Failed to delete " + docNames[i]);
System.out.println("Failed to delete " + docInfos[i]);
System.out.println(e.getMessage());
}
}
@ -183,14 +185,14 @@ public class TethysDocumentTable implements PamDialogPanel {
private class TableModel extends AbstractTableModel {
private String[] columnNames = {"", "Document Id/Name"};
private String[] columnNames = {"", "Document Name", "Document Id"};
@Override
public int getRowCount() {
if (documentNames == null) {
if (documentInfos == null) {
return 0;
}
return documentNames.size();
return documentInfos.size();
}
@Override
@ -200,14 +202,17 @@ public class TethysDocumentTable implements PamDialogPanel {
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (documentNames == null) {
if (documentInfos == null) {
return null;
}
DocumentInfo docInfo = documentInfos.get(rowIndex);
switch (columnIndex) {
case 0:
return rowIndex+1;
return rowIndex;
case 1:
return documentNames.get(rowIndex);
return docInfo.getDocumentName();
case 2:
return docInfo.getDocumentId();
}
return null;
}
@ -241,15 +246,13 @@ public class TethysDocumentTable implements PamDialogPanel {
/**
* @return the collectionName
*/
public String getCollectionName() {
return collectionName;
public Collection getCollection() {
return collection;
}
/**
* @param collectionName the collectionName to set
*/
public void setCollectionName(String collectionName) {
this.collectionName = collectionName;
public void setCollection(Collection collection) {
this.collection = collection;
updateTableData();
}
}

View File

@ -3,6 +3,7 @@ package tethys.swing.documents;
import java.awt.Window;
import PamView.dialog.PamDialog;
import tethys.Collection;
import tethys.TethysControl;
public class TethysDocumentsFrame extends PamDialog {
@ -23,12 +24,12 @@ public class TethysDocumentsFrame extends PamDialog {
getCancelButton().setText("Close");
}
public static void showTable(Window parentFrame, TethysControl tethysControl, String collectionName) {
public static void showTable(Window parentFrame, TethysControl tethysControl, Collection collection) {
if (singleInstance == null) {
singleInstance = new TethysDocumentsFrame(parentFrame, tethysControl);
}
singleInstance.documentsTable.setCollectionName(collectionName);
singleInstance.setTitle(collectionName + " Documents");
singleInstance.documentsTable.setCollection(collection);
singleInstance.setTitle(collection.collectionName() + " Documents");
singleInstance.setVisible(true);
}