Skip to content

Commit

Permalink
Resolve even more fields when exporing to Nahima
Browse files Browse the repository at this point in the history
  • Loading branch information
GenieTim committed Sep 28, 2021
1 parent 8711595 commit 66fcd40
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ private TextStatus checkSourceForBarcode(LuminanceSource source,
returnValue.setText(result.getText());
returnValue.setStatus(RESULT_BARCODE_SCANNED);
} catch (ReaderException e) {
log.error(e.getClass().toString() + " " + e.getMessage());
log.error(e.getClass() + " " + e.getMessage());
if (!Singleton.getSingletonInstance()
.getProperties()
.getProperties()
Expand All @@ -1076,13 +1076,13 @@ private TextStatus checkSourceForBarcode(LuminanceSource source,
returnValue.setStatus(RESULT_ERROR);
} catch (ArrayIndexOutOfBoundsException e1) {
log.error(e.getMessage());
returnValue.setText(e1.toString() + " " + e1.getMessage());
returnValue.setText(e1 + " " + e1.getMessage());
returnValue.setStatus(RESULT_ERROR);
}
}
} catch (ArrayIndexOutOfBoundsException e) {
log.error(e.getMessage());
returnValue.setText(e.toString() + " " + e.getMessage());
returnValue.setText(e + " " + e.getMessage());
returnValue.setStatus(RESULT_ERROR);
}
return returnValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,17 @@ public String getOCRText() throws OCRReadException {
// String runCommand = "convert temptiff.jpg -depth 8 temptiff.tif";
List<String> runCommand = new ArrayList<>();
runCommand.addAll(Arrays.asList(Singleton.getSingletonInstance()
.getProperties()
.getProperties()
.getProperty(ImageCaptureProperties.KEY_CONVERT_EXECUTABLE),
.getProperties()
.getProperties()
.getProperty(ImageCaptureProperties.KEY_CONVERT_EXECUTABLE),
"temptiff.jpg",
Singleton.getSingletonInstance()
.getProperties()
.getProperties()
.getProperty(ImageCaptureProperties.KEY_CONVERT_PARAMETERS),
"temptiff.tif"));
log.debug("in ConvertTesseractOCR.getOCRText() 11 runCommand is " +
runCommand.toString());
runCommand);
Runtime r = Runtime.getRuntime();
log.debug("in ConvertTesseractOCR.getOCRText() 12");
Process proc = r.exec((String[]) runCommand.toArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,11 @@ public class ImageCaptureProperties extends AbstractTableModel {
public static String KEY_NAHIMA_URL = "nahima.url";
public static String KEY_NAHIMA_PASSWORD = "nahima.password";
public static String KEY_NAHIMA_USER = "nahima.user";

private final String propertiesFilename = "imagecapture.properties";
/**
* Private properties
*/
private Properties properties = null;
private final String propertiesFilename = "imagecapture.properties";
private String propertiesFilePath = null;

public ImageCaptureProperties() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/edu/harvard/mcz/imagecapture/TesseractOCR.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ public String getOCRText() throws OCRReadException {
// Run tesseract to OCR the target file.
List<String> runCommand = new ArrayList<>();
runCommand.addAll(Arrays.asList(Singleton.getSingletonInstance()
.getProperties()
.getProperties()
.getProperty(ImageCaptureProperties.KEY_TESSERACT_EXECUTABLE),
.getProperties()
.getProperties()
.getProperty(ImageCaptureProperties.KEY_TESSERACT_EXECUTABLE),
target, tempFileBase, language));

Process proc = r.exec((String[]) runCommand.toArray());
Expand Down
100 changes: 59 additions & 41 deletions src/main/java/edu/harvard/mcz/imagecapture/data/NahimaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class NahimaManager extends AbstractRestClient {
private final String username;
private final String password;
private String token;
private Map<String, Map<String, JSONObject>> resolveCache = new HashMap<String, Map<String, JSONObject>>();
private final Map<String, Map<String, JSONObject>> resolveCache = new HashMap<String, Map<String, JSONObject>>();

public NahimaManager(String url, String username, String password) throws IOException, InterruptedException, RuntimeException {
// normalize URL
Expand Down Expand Up @@ -191,6 +191,28 @@ public JSONObject searchForString(String searchString, String objectType, boolea
return searchForString(searchString, objectType, "fulltext", ignoreCache);
}

/**
* Search in Nahima for one string, get result only if just one result though
*
* @param search the string to search for
* @param objectType the object type that is searched
* @return the object if there is only one, null if not
* @throws IOException
* @throws InterruptedException
*/
public JSONObject resolveStringSearchToOne(String search, String objectType) throws IOException, InterruptedException {
JSONObject results = this.searchForString(search, objectType);

JSONArray foundObjects = (JSONArray) results.get("objects");
if (foundObjects.length() == 1) {
return (JSONObject) foundObjects.get(0);
} else {
// TODO: create / select correct
log.info("Got != 1 " + objectType + " status. {}", results);
return null;
}
}

/**
* Find a person in Nahima
*
Expand All @@ -200,18 +222,10 @@ public JSONObject searchForString(String searchString, String objectType, boolea
* @throws InterruptedException
*/
public JSONObject resolvePerson(String personName) throws IOException, InterruptedException {
JSONObject results = this.searchForString(personName, "person");
JSONObject results = this.resolveStringSearchToOne(personName, "person");
// TODO: check compliance, does it really match well? We use "must", so let's hope so, but the resolution could still go wrong.
// also, multiple could be returned.
// or, none, in which case we might need to create one
JSONArray foundPersons = (JSONArray) results.get("objects");
if (foundPersons.length() == 1) {
return (JSONObject) foundPersons.get(0);
} else {
// TODO: create / select correct
log.info("Got != 1 person. {}", results);
return null;
}
// TODO: create / select correct
return results;
}

/**
Expand All @@ -223,29 +237,38 @@ public JSONObject resolvePerson(String personName) throws IOException, Interrupt
* @throws InterruptedException
*/
public JSONObject resolveLocation(String location) throws IOException, InterruptedException {
JSONObject results = this.searchForString(location, "gazetteer");
JSONObject results = this.resolveStringSearchToOne(location, "gazetteer");
// TODO: create / select correct
return results;
}

JSONArray foundLocations = (JSONArray) results.get("objects");
if (foundLocations.length() == 1) {
return (JSONObject) foundLocations.get(0);
} else {
// TODO: create / select correct
log.info("Got != 1 location. {}", results);
return null;
}
/**
* Find a typusstatus in Nahima
*
* @param status the search string
* @return the nahima returned object if only one
* @throws IOException
* @throws InterruptedException
*/
public JSONObject resolveTypeStatus(String status) throws IOException, InterruptedException {
JSONObject results = this.resolveStringSearchToOne(status, "typusstatus");
// TODO: create / select correct
return results;
}

/**
* Generic function to resolve unit for a certain field
*
* @param unit the unit to find in Nahima
* @param unitSubject the unit type
* @return
* @throws IOException
* @throws InterruptedException
*/
public JSONObject resolveUnitFor(String unit, String unitSubject) throws IOException, InterruptedException {
JSONObject results = this.searchForString(unit, unitSubject, "token");

JSONArray foundUnits = (JSONArray) results.get("objects");
if (foundUnits.length() == 1) {
return (JSONObject) foundUnits.get(0);
} else {
// TODO: create / select correct
log.info("Got != 1 unit. {}", results);
return null;
}
JSONObject results = this.resolveStringSearchToOne(unit, unitSubject);
// TODO: create / select correct
return results;
}

/**
Expand Down Expand Up @@ -274,6 +297,7 @@ public JSONObject resolveUnitForErrorRadius(String unit) throws IOException, Int

/**
* Find the location "Datum" in Nahima
*
* @param format the datum to search for
* @return the nahima returned object if only one
* @throws IOException
Expand All @@ -294,22 +318,16 @@ public JSONObject resolveDatumFormat(String format) throws IOException, Interrup

/**
* Find the collection method in Nahima
*
* @param method the collection method to search for
* @return the nahima returned object if only one
* @throws IOException
* @throws InterruptedException
*/
public JSONObject resolveCollectionMethod(String method) throws IOException, InterruptedException {
JSONObject results = this.searchForString(method, "sammlungsmethoden");

JSONArray foundMethods = (JSONArray) results.get("objects");
if (foundMethods.length() == 1) {
return (JSONObject) foundMethods.get(0);
} else {
// TODO: create / select correct
log.info("Got != 1 collecting method. {}", results);
return null;
}
JSONObject results = this.resolveStringSearchToOne(method, "sammlungsmethoden");
// TODO: create / select correct
return results;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void start() {
put("preferred", true);
put("_id", imageJsonObj.get("_id"));
}})));
put("barcode", specimen.getBarcode());
put("urspruelicherdateiname", imageJsonObj.get("original_filename"));
}};
JSONObject reducedImageMediaAsset = new JSONObject(reducedMediaAssetMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.json.JSONObject;

public interface ToJSONSerializerInterface {
public JSONObject serialize2JSON(Object target);
public boolean supportsSerializationOf(Object target);
JSONObject serialize2JSON(Object target);

boolean supportsSerializationOf(Object target);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package edu.harvard.mcz.imagecapture.serializer.nahima;

import edu.harvard.mcz.imagecapture.data.NahimaManager;
import edu.harvard.mcz.imagecapture.entity.Collector;
import edu.harvard.mcz.imagecapture.entity.Determination;
import edu.harvard.mcz.imagecapture.entity.LatLong;
import edu.harvard.mcz.imagecapture.entity.Specimen;
import edu.harvard.mcz.imagecapture.entity.*;
import edu.harvard.mcz.imagecapture.serializer.ToJSONSerializerInterface;
import org.apache.commons.lang3.time.DateUtils;
import org.json.JSONArray;
Expand Down Expand Up @@ -50,6 +47,16 @@ public JSONObject serialize2JSON(Object target) {
put("_pool", NahimaManager.defaultPool);
put("_id", JSONObject.NULL);
put("typusid", det.getTypeStatus());
put("typusstatustrans", det.getTypeStatus());
put("autortrans", det.getAuthorship()); // TODO: resolve
put("taxonnametrans", toSerialize.getAssociatedTaxon()); // TODO: resolve
put("familietrans", toSerialize.getFamily()); // TODO: resolve
put("genustrans", det.getGenus()); // TODO: resolve
put("subspezifischesarttrans", det.getSubspecificEpithet()); // TODO: resolve
put("bestimmertrans", det.getIdentifiedBy());
put("infraspezifischestaxontrans", det.getInfraspecificEpithet()); // TODO: resolve
put("infrapezifischerrangtrans", det.getInfraspecificRank()); // TODO: resolve
put("arttrans", det.getSpecificEpithet()); // TODO: resolve
}};

JSONObject reverseNestedDetermination = new JSONObject(reverseNestedCollectionMap);
Expand All @@ -63,12 +70,6 @@ public JSONObject serialize2JSON(Object target) {
log.error("Failed to parse bestimmungsdatum", e);
}

reverseNestedDetermination.put("autortrans", det.getAuthorship()); // TODO: resolve
reverseNestedDetermination.put("taxonnametrans", toSerialize.getAssociatedTaxon()); // TODO: resolve
reverseNestedDetermination.put("familietrans", toSerialize.getFamily()); // TODO: resolve
reverseNestedDetermination.put("genustrans", det.getGenus()); // TODO: resolve
reverseNestedDetermination.put("subspezifischesarttrans", det.getSubspecificEpithet()); // TODO: resolve
reverseNestedDetermination.put("bestimmertrans", det.getIdentifiedBy());
JSONArray identifierPersons = new JSONArray();
try {
JSONObject identifierPerson = nahimaManager.resolvePerson(det.getIdentifiedBy());
Expand All @@ -79,6 +80,13 @@ public JSONObject serialize2JSON(Object target) {
log.info("Failed to resolve person", e);
}
reverseNestedDetermination.put("_nested:bestimmung__bestimmer_person", identifierPersons);

try {
JSONObject resolvedTypeStatus = nahimaManager.resolveTypeStatus(det.getTypeStatus());
reverseNestedDetermination.put("typusstatus", resolvedTypeStatus);
} catch (IOException | InterruptedException e) {
log.info("Failed to resolve type status", e);
}
// TODO: other fields

// finally,
Expand All @@ -96,6 +104,7 @@ public JSONObject serialize2JSON(Object target) {
put("kommentare", toSerialize.getSpecimenNotes());
put("habitattrans", toSerialize.getHabitat());
put("sammelorttrans", toSerialize.getVerbatimLocality());
put("lokalitaet", toSerialize.getSpecificLocality());
put("neuhabitat", wrapInLan(toSerialize.getHabitat()));
put("mikrohabitat", wrapInLan(toSerialize.getMicrohabitat()));
put("traegerorganismustrans", toSerialize.getAssociatedTaxon());
Expand All @@ -105,6 +114,7 @@ public JSONObject serialize2JSON(Object target) {
put("fehlerradius", georef == null ? null : georef.getMaxErrorDistance());
put("hoehemin", toSerialize.getMinimum_elevation());
put("hoehemax", toSerialize.getMaximum_elevation());
put("lokalitaettrans", toSerialize.getVerbatimLocality());
put("__idx", 0);
put("_version", 1);
put("_id", JSONObject.NULL);
Expand Down Expand Up @@ -169,7 +179,7 @@ public JSONObject serialize2JSON(Object target) {
reverseNestedCollection.put("datum", this.dateToNahima(toSerialize.getDateEmerged()));
}
} catch (ParseException e) {
log.error("Failed to parse datum " + toSerialize.getDateEmerged(), e);
log.error("Failed to parse datum " + toSerialize.getDateEmerged() + " " + toSerialize.getDateNos(), e);
}
reverseNestedCollection.put("indikator_fuer_kultur_zucht", toSerialize.getDateEmergedIndicator());
reverseNestedCollection.put("sammeldatumtrans", toSerialize.getDateCollected());
Expand All @@ -183,6 +193,16 @@ public JSONObject serialize2JSON(Object target) {
reverseNestedCollections.put(reverseNestedCollection);
result.put("_reverse_nested:aufsammlung:entomologie", reverseNestedCollections);

JSONArray parts = new JSONArray();
for (SpecimenPart part : toSerialize.getSpecimenParts()) {
JSONObject partJson = new JSONObject(new HashMap<>() {{
put("anzahlpraeparatteile", part.getLotCount());
}});
// TODO: (resolve) other fields
parts.put(partJson);
}
result.put("_nested:entomologie__praeparatteile", parts);


// TODO: other fields

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class JAccordionPanel extends JPanel {

private static final Logger log =
LoggerFactory.getLogger(JAccordionPanel.class);
private final JScrollPane mainContent;
private String titleOpen = "";
private String titleClosed = "";
private final JScrollPane mainContent;
private JButton toggleButton = null;
private boolean contentVisible = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void doApplyChange() {

int numAffected = updateQuery.executeUpdate();
session.getTransaction().commit();
JOptionPane.showMessageDialog(this, "Changed " + String.valueOf(numAffected) + " values", "Success",
JOptionPane.showMessageDialog(this, "Changed " + numAffected + " values", "Success",
JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
log.error("Failed to do query update: " + e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import edu.harvard.mcz.imagecapture.utility.CastUtility;
import net.miginfocom.swing.MigLayout;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.jdatepicker.JDatePicker;
import org.jdatepicker.UtilDateModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class NahimaExportDialog extends JDialog implements ProgressListener {
private static final Logger log =
Expand Down Expand Up @@ -44,7 +43,7 @@ private void initialize() {
didRunSuccessfully = false;
getResultsTextField().setText("Error: " + job.getLastException().getMessage());
} else if (didRunSuccessfully) {
getResultsTextField().setText("Finished processing " + String.valueOf(job.getTotalCount()) + " Specimen");
getResultsTextField().setText("Finished processing " + job.getTotalCount() + " Specimen");
}
getCloseButton().setEnabled(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
Expand Down
Loading

0 comments on commit 66fcd40

Please sign in to comment.