Skip to content

Commit

Permalink
Resolve even more objects
Browse files Browse the repository at this point in the history
  • Loading branch information
GenieTim committed Sep 30, 2021
1 parent 7a060a6 commit 3cd052e
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 53 deletions.
139 changes: 119 additions & 20 deletions src/main/java/edu/harvard/mcz/imagecapture/data/NahimaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,13 @@ public JSONObject createObjectInNahima(JSONObject object, String objType) throws
protected JSONObject loadFromCache(String id, String objectType) {
Map<String, JSONObject> typeCache = resolveCache.get(objectType);
if (typeCache != null) {
JSONObject cacheLoaded = typeCache.get(id);
if (cacheLoaded != null) {
return cacheLoaded;
}
return typeCache.get(id);
}
return null;
}

protected void storeInCache(String id, String objectType, JSONObject result) {
Map<String, JSONObject> typeCache = resolveCache.get(objectType);
if (typeCache == null) {
typeCache = new HashMap<>();
resolveCache.put(objectType, typeCache);
}
Map<String, JSONObject> typeCache = resolveCache.computeIfAbsent(objectType, k -> new HashMap<>());
typeCache.put(id, result);
}

Expand All @@ -174,6 +167,10 @@ protected void storeInCache(String id, String objectType, JSONObject result) {
* @return Nahima's response
*/
public JSONObject searchForString(String searchString, String objectType, String searchMode, boolean ignoreCache) throws IOException, InterruptedException {
if (searchString == null || searchString.trim().equals("")) {
return null;
}

// check cache for results
if (!ignoreCache) {
JSONObject fromCache = loadFromCache(searchString, objectType);
Expand Down Expand Up @@ -268,11 +265,19 @@ public JSONObject resolveStringSearchToOne(String search, String objectType) thr
* @return the matching or new object
*/
public JSONObject resolveOrCreate(String name, String objectType, String mask, JSONObject inner) throws IOException, InterruptedException {
return resolveOrCreate(name, objectType, mask, inner, false);
}

public JSONObject resolveOrCreate(String name, String objectType, String mask, JSONObject inner, boolean omitPool) throws IOException, InterruptedException {
if (name == null || name.equals("")) {
return null;
}

JSONObject results = this.resolveStringSearchToOne(name, objectType);
// TODO: select correct
if (results == null) {
// create
JSONObject toCreate = wrapForCreation(inner, objectType, mask);
JSONObject toCreate = wrapForCreation(inner, objectType, mask, omitPool);
this.createObjectInNahima(toCreate, objectType);
// TODO: it would be simpler to store the created in cache. But well... current format does not allow
return this.resolveStringSearchToOne(name, objectType, true);
Expand All @@ -292,7 +297,7 @@ public JSONObject resolveOrCreate(String name, String objectType, String mask, J
public JSONObject resolveNameBemerkungObject(String name, String objectType, String mask) throws IOException, InterruptedException {
return this.resolveOrCreate(name, objectType, mask, new JSONObject(new HashMap<>() {{
put("name", name);
put("bemerkung", "Created by " + ImageCaptureApp.APP_NAME + " " + ImageCaptureApp.getAppVersion());
put("bemerkung", getCreatedByThisSoftwareIndication());
}}));
}

Expand Down Expand Up @@ -403,17 +408,86 @@ public JSONObject resolveUnitForErrorRadius(String unit) throws IOException, Int
/**
* Find a family in Nahima
*
* @param family
* @return
* @throws IOException
* @throws InterruptedException
* @param family the family to search for resp. create
*/
public JSONObject resolveFamily(String family) throws IOException, InterruptedException {
return resolveOrCreate(family, "familien", "familien_all_fields", new JSONObject(new HashMap<>() {{
put("familielat", family);
}}));
}

/**
* Find a subfamily in Nahima
*
* @param family the subfamily's name to resolve
*/
public JSONObject resolveSubFamily(String family) throws IOException, InterruptedException {
return resolveOrCreate(family, "unterfamilien", "unterfamilien_all_fields", new JSONObject(new HashMap<>() {{
put("unterfamilielat", family);
}}));
}

/**
* Find a specific epithet ("art") in Nahima
*
* @param specificEpithet the subfamily's name to resolve
*/
public JSONObject resolveSpecificEpithet(String specificEpithet) throws IOException, InterruptedException {
return resolveOrCreate(specificEpithet, "art", "art_all_fields", new JSONObject(new HashMap<>() {{
put("artlat", specificEpithet);
put("bemerkung", getCreatedByThisSoftwareIndication());
}}));
}

/**
* Find a subspecific epithet in Nahima
*
* @param subspecificEpithet the subfamily's name to resolve
*/
public JSONObject resolveSubSpecificEpithet(String subspecificEpithet) throws IOException, InterruptedException {
return resolveOrCreate(subspecificEpithet, "subspezifischeart", "subspezifischeart_all_fields", new JSONObject(new HashMap<>() {{
put("subspezifischeartlat", subspecificEpithet);
put("bemerkung", getCreatedByThisSoftwareIndication());
}}));
}

/**
* Find an author in Nahima
*
* @param authorship the name to search for
* @return the existing or new author
*/
public JSONObject resolveAuthorship(String authorship) throws IOException, InterruptedException {
return resolveOrCreate(authorship, "authors", "author__all_fields", new JSONObject(new HashMap<>() {{
put("autor", authorship);
put("_nested:authors__personen", new JSONArray());
}}), true);
}

/**
* Find an infraspecific epithet in Nahima
*
* @param infraspecificEpithet the subfamily's name to resolve
*/
public JSONObject resolveInfraspecificEpithet(String infraspecificEpithet) throws IOException, InterruptedException {
return resolveOrCreate(infraspecificEpithet, "infraspezifischetaxon", "infraspezifischetaxon_all_fields", new JSONObject(new HashMap<>() {{
put("infraspezifischetaxon", infraspecificEpithet);
put("abkuerzung", JSONObject.NULL);
put("_nested:infraspezifischetaxon__trivialnamen", new JSONArray());
put("_nested:infraspezifischetaxon__referenzenfuerintraspezifischestaxon", new JSONArray());
// put("beschreibung", getCreatedByThisSoftwareIndication());
}}));
}

/**
* Find an infraspecific rank in Nahima
*
* @param infraspecificRank the subfamily's name to resolve
*/
public JSONObject resolveInfraspecificRank(String infraspecificRank) throws IOException, InterruptedException {
return resolveNameBemerkungObject(infraspecificRank, "infraspezifischerrang", "id_typen_all_fields_1");
}

/**
* Find the location "Datum" in Nahima
*
Expand Down Expand Up @@ -471,11 +545,8 @@ public JSONObject reduceAssociateForAssociation(JSONObject associate) {
* @param mask the mask specifying the fields to use
* @return the wrapped object, ready to be created
*/
public JSONObject wrapForCreation(JSONObject inner, String objectType, String mask) {
inner.put("_pool", defaultPool);
inner.put("_id", JSONObject.NULL);
inner.put("_version", 1);

private JSONObject wrapForCreation(JSONObject inner, String objectType, String mask, boolean omitPool) {
inner = this.addDefaultValuesForCreation(inner, omitPool);
JSONObject result = new JSONObject(new HashMap<>() {{
put("_mask", mask);
put("_objecttype", objectType);
Expand All @@ -485,7 +556,35 @@ public JSONObject wrapForCreation(JSONObject inner, String objectType, String ma
return result;
}

public JSONObject wrapForCreation(JSONObject inner, String objectType, String mask) {
return wrapForCreation(inner, objectType, mask, false);
}

public JSONObject wrapForCreation(JSONObject inner, String objectType) {
return wrapForCreation(inner, objectType, objectType + "_all_fields");
}

/**
* Add fields with values that are required when creating a new entry in Nahima
*
* @param inner the object to add the fields to
* @return the object with the adjusted/added fields
*/
public JSONObject addDefaultValuesForCreation(JSONObject inner) {
return addDefaultValuesForCreation(inner, false);
}

public JSONObject addDefaultValuesForCreation(JSONObject inner, boolean omitPool) {
if (!omitPool) {
inner.put("_pool", defaultPool);
}
inner.put("_id", JSONObject.NULL);
inner.put("_version", 1);
return inner;

}

public String getCreatedByThisSoftwareIndication() {
return "Created by " + ImageCaptureApp.APP_NAME + " " + ImageCaptureApp.getAppVersion();
}
}
33 changes: 33 additions & 0 deletions src/main/java/edu/harvard/mcz/imagecapture/entity/Specimen.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.harvard.mcz.imagecapture.entity;

import edu.harvard.mcz.imagecapture.ImageCaptureApp;
import edu.harvard.mcz.imagecapture.ImageCaptureProperties;
import edu.harvard.mcz.imagecapture.Singleton;
import edu.harvard.mcz.imagecapture.data.MetadataRetriever;
Expand Down Expand Up @@ -1053,6 +1054,38 @@ public void setDeterminations(Set<Determination> determinations) {
this.determinations = determinations;
}

/**
* Get the determinations of this Specimen (see #getDeterminations) but including the one safed on the specimen too.
*
* @return the joined Determinations plus the Determination stored on this entity
*/
public Set<Determination> getAllDeterminations() {
// copy
Set<Determination> allDets = new HashSet<>(this.determinations);
// create new one
Determination thisDet = new Determination();
thisDet.setGenus(this.getGenus());
thisDet.setSpecificEpithet(this.getSpecificEpithet());
thisDet.setSubspecificEpithet(this.getSubspecificEpithet());
thisDet.setInfraspecificEpithet(this.getInfraspecificEpithet());
thisDet.setInfraspecificRank(this.getInfraspecificRank());
thisDet.setAuthorship(this.getAuthorship());
thisDet.setUnNamedForm(this.getUnNamedForm());
thisDet.setIdentifiedBy(this.getIdentifiedBy());
thisDet.setTypeStatus(this.getTypeStatus());
thisDet.setNatureOfId(this.getNatureOfId());
thisDet.setDateIdentified(this.getDateIdentified());
thisDet.setVerbatimText("Main determination created by "
+ ImageCaptureApp.APP_NAME + " " + ImageCaptureApp.getAppVersion()
+ "\n" + this.getVerbatimUnclassifiedText());
thisDet.setRemarks(this.getIdentificationRemarks());
// thisDet.setSpeciesNumber(this.getSpecimenNotes());
// add new one
allDets.add(thisDet);
// return
return allDets;
}

public Set<Tracking> getTrackings() {
return this.trackings;
}
Expand Down
Loading

0 comments on commit 3cd052e

Please sign in to comment.