Skip to content

Commit

Permalink
Merge branch 'main' into feature/remove-empty-objects
Browse files Browse the repository at this point in the history
  • Loading branch information
samleeflang committed Nov 25, 2024
2 parents cde73bb + ece746a commit b36d9d4
Show file tree
Hide file tree
Showing 15 changed files with 332 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
import eu.dissco.core.translator.terms.specimen.identification.taxonomy.Tribe;
import eu.dissco.core.translator.terms.specimen.identification.taxonomy.VerbatimTaxonRank;
import eu.dissco.core.translator.terms.specimen.identification.taxonomy.VernacularName;
import eu.dissco.core.translator.terms.specimen.location.AbstractMeterTerm;
import eu.dissco.core.translator.terms.specimen.location.Continent;
import eu.dissco.core.translator.terms.specimen.location.Country;
import eu.dissco.core.translator.terms.specimen.location.CountryCode;
Expand Down Expand Up @@ -279,6 +280,17 @@ public abstract class BaseDigitalObjectDirector {
private final FdoProperties fdoProperties;
private final List<String> identifierTerms;

private static Double stringToDouble(Term term, String value) {
try {
if (value != null) {
return Double.valueOf(value);
}
} catch (NumberFormatException ex) {
log.warn("Unable to parse value: {} to a double for term: {}", value, term.getTerm());
}
return null;
}

public DigitalSpecimen assembleDigitalSpecimenTerm(JsonNode data, boolean dwc)
throws OrganisationException, UnknownPhysicalSpecimenIdType {
var ds = assembleDigitalSpecimenTerms(data, dwc);
Expand Down Expand Up @@ -524,7 +536,6 @@ protected Identification createIdentification(JsonNode data, boolean dwc) {
return identification;
}


private List<Event> assembleEventTerms(JsonNode data, boolean dwc) {
var geoReference = new Georeference()
.withType("ods:Georeference")
Expand Down Expand Up @@ -600,29 +611,27 @@ private List<Event> assembleEventTerms(JsonNode data, boolean dwc) {
.withDwcStateProvince(termMapper.retrieveTerm(new StateProvince(), data, dwc))
.withDwcWaterBody(termMapper.retrieveTerm(new WaterBody(), data, dwc))
.withDwcHigherGeography(termMapper.retrieveTerm(new HigherGeography(), data, dwc))
.withDwcMaximumDepthInMeters(parseToDouble(new MaximumDepthInMeters(), data, dwc))
.withDwcMaximumDistanceAboveSurfaceInMeters(
parseToDouble(new MaximumDistanceAboveSurfaceInMeters(), data, dwc))
.withDwcMaximumElevationInMeters(parseToDouble(new MaximumElevationInMeters(), data, dwc))
.withDwcMinimumDepthInMeters(parseToDouble(new MinimumDepthInMeters(), data, dwc))
.withDwcMinimumDistanceAboveSurfaceInMeters(
parseToDouble(new MinimumDistanceAboveSurfaceInMeters(), data, dwc))
.withDwcMinimumElevationInMeters(parseToDouble(new MinimumElevationInMeters(), data, dwc))
.withDwcVerbatimDepth(termMapper.retrieveTerm(new VerbatimDepth(), data, dwc))
.withDwcVerbatimElevation(termMapper.retrieveTerm(new VerbatimElevation(), data, dwc))
.withDwcVerticalDatum(termMapper.retrieveTerm(new VerticalDatum(), data, dwc))
.withDwcLocationAccordingTo(termMapper.retrieveTerm(new LocationAccordingTo(), data, dwc))
.withDwcLocationRemarks(termMapper.retrieveTerm(new LocationRemarks(), data, dwc));
.withDwcLocationRemarks(termMapper.retrieveTerm(new LocationRemarks(), data, dwc))
.withOdsHasGeoreference(geoReference)
.withOdsHasGeologicalContext(geologicalContext);
setMinMaxMeterField(new MinimumElevationInMeters(), location, data, dwc);
setMinMaxMeterField(new MaximumElevationInMeters(), location, data, dwc);
setMinMaxMeterField(new MinimumDepthInMeters(), location, data, dwc);
setMinMaxMeterField(new MaximumDepthInMeters(), location, data, dwc);
if (!Objects.equals(geoReference, EMPTY_GEOREFERENCE)) {
location.setOdsHasGeoreference(geoReference);
}
if (!Objects.equals(geologicalContext, EMPTY_GEOLOGICAL_CONTEXT)) {
location.setOdsHasGeologicalContext(geologicalContext);
}
setMinMaxMeterField(new MinimumElevationInMeters(), location, data, dwc);
setMinMaxMeterField(new MaximumElevationInMeters(), location, data, dwc);
setMinMaxMeterField(new MinimumDepthInMeters(), location, data, dwc);
setMinMaxMeterField(new MaximumDepthInMeters(), location, data, dwc);
var assertions = new EventAssertions().gatherEventAssertions(mapper, data, dwc);
var event = new Event()
.withType("ods:Event")
Expand Down Expand Up @@ -664,6 +673,31 @@ private List<Event> assembleEventTerms(JsonNode data, boolean dwc) {
return List.of(event);
}

private void setMinMaxMeterField(AbstractMeterTerm term, Location location, JsonNode data,
boolean dwc) {
var sanitizedValue = termMapper.retrieveTerm(term, data, dwc);
var doubleElevation = stringToDouble(term, sanitizedValue);
if (doubleElevation == null && sanitizedValue != null) {
if (term instanceof MaximumElevationInMeters) {
location.setDwcVerbatimElevation(
location.getDwcVerbatimElevation() + "| unparseable value maximumElevationInMeters: "
+ sanitizedValue);
} else if (term instanceof MinimumElevationInMeters) {
location.setDwcVerbatimElevation(
location.getDwcVerbatimElevation() + "| unparseable value minimumElevationInMeters: "
+ sanitizedValue);
} else if (term instanceof MaximumDepthInMeters) {
location.setDwcVerbatimDepth(
location.getDwcVerbatimDepth() + "| unparseable value maximumDepthInMeters: "
+ sanitizedValue);
} else if (term instanceof MinimumDepthInMeters) {
location.setDwcVerbatimDepth(
location.getDwcVerbatimDepth() + "| unparseable value minimumDepthInMeters: "
+ sanitizedValue);
}
}
}

private <T extends Enum<T>> T retrieveEnum(Term term, JsonNode data, boolean dwc,
java.lang.Class<T> enumClass) {
var value = termMapper.retrieveTerm(term, data, dwc);
Expand Down Expand Up @@ -691,14 +725,7 @@ private Integer parseToInteger(Term term, JsonNode data, boolean dwc) {

private Double parseToDouble(Term term, JsonNode data, boolean dwc) {
var value = termMapper.retrieveTerm(term, data, dwc);
try {
if (value != null) {
return Double.valueOf(value);
}
} catch (NumberFormatException ex) {
log.warn("Unable to parse value: {} to a double for term: {}", value, term.getTerm());
}
return null;
return stringToDouble(term, value);
}

private Boolean parseToBoolean(Term term, JsonNode data, boolean dwc) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package eu.dissco.core.translator.terms.specimen.location;

import eu.dissco.core.translator.terms.Term;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class AbstractMeterTerm extends Term {

private static final Pattern M_PATTERN = Pattern.compile("((-\\s?)?\\d+([.,]\\d+)?)\\s*m\\.?(eter)?(tr?.?)?(\\sm.?)?");

protected String sanitizeInput(String input) {
if (input == null) {
return null;
}
input = input.trim().toLowerCase();
Matcher matcher = M_PATTERN.matcher(input);
if (matcher.matches()) {
log.debug("Parsed Number: {}", matcher.group(1));
return matcher.group(1);
} else {
log.debug("Input string does not match the expected format: {}", input);
return input;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package eu.dissco.core.translator.terms.specimen.location;

import com.fasterxml.jackson.databind.JsonNode;
import eu.dissco.core.translator.terms.Term;
import java.util.List;

public class MaximumDepthInMeters extends Term {
public class MaximumDepthInMeters extends AbstractMeterTerm {

public static final String TERM = DWC_PREFIX + "maximumDepthInMeters";

Expand All @@ -15,12 +14,14 @@ public class MaximumDepthInMeters extends Term {

@Override
public String retrieveFromDWCA(JsonNode unit) {
return super.searchJsonForTerm(unit, dwcaTerms);
var rawResult = super.searchJsonForTerm(unit, dwcaTerms);
return sanitizeInput(rawResult);
}

@Override
public String retrieveFromABCD(JsonNode unit) {
return super.searchJsonForTerm(unit, abcdTerms);
var rawResult = super.searchJsonForTerm(unit, abcdTerms);
return sanitizeInput(rawResult);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package eu.dissco.core.translator.terms.specimen.location;

import com.fasterxml.jackson.databind.JsonNode;
import eu.dissco.core.translator.terms.Term;
import java.util.List;

public class MaximumDistanceAboveSurfaceInMeters extends Term {
public class MaximumDistanceAboveSurfaceInMeters extends AbstractMeterTerm {

public static final String TERM = DWC_PREFIX + "maximumDistanceAboveSurfaceInMeters";

Expand All @@ -14,12 +13,14 @@ public class MaximumDistanceAboveSurfaceInMeters extends Term {

@Override
public String retrieveFromDWCA(JsonNode unit) {
return super.searchJsonForTerm(unit, dwcaTerms);
var rawResult = super.searchJsonForTerm(unit, dwcaTerms);
return sanitizeInput(rawResult);
}

@Override
public String retrieveFromABCD(JsonNode unit) {
return super.searchJsonForTerm(unit, abcdTerms);
var rawResult = super.searchJsonForTerm(unit, abcdTerms);
return sanitizeInput(rawResult);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package eu.dissco.core.translator.terms.specimen.location;

import com.fasterxml.jackson.databind.JsonNode;
import eu.dissco.core.translator.terms.Term;
import java.util.List;

public class MaximumElevationInMeters extends Term {
public class MaximumElevationInMeters extends AbstractMeterTerm {

public static final String TERM = DWC_PREFIX + "maximumElevationInMeters";

Expand All @@ -15,12 +14,14 @@ public class MaximumElevationInMeters extends Term {

@Override
public String retrieveFromDWCA(JsonNode unit) {
return super.searchJsonForTerm(unit, dwcaTerms);
var rawResult = super.searchJsonForTerm(unit, dwcaTerms);
return sanitizeInput(rawResult);
}

@Override
public String retrieveFromABCD(JsonNode unit) {
return super.searchJsonForTerm(unit, abcdTerms);
var rawResult = super.searchJsonForTerm(unit, abcdTerms);
return sanitizeInput(rawResult);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package eu.dissco.core.translator.terms.specimen.location;

import com.fasterxml.jackson.databind.JsonNode;
import eu.dissco.core.translator.terms.Term;
import java.util.List;

public class MinimumDepthInMeters extends Term {
public class MinimumDepthInMeters extends AbstractMeterTerm {

public static final String TERM = DWC_PREFIX + "minimumDepthInMeters";

Expand All @@ -15,12 +14,14 @@ public class MinimumDepthInMeters extends Term {

@Override
public String retrieveFromDWCA(JsonNode unit) {
return super.searchJsonForTerm(unit, dwcaTerms);
var rawResult = super.searchJsonForTerm(unit, dwcaTerms);
return sanitizeInput(rawResult);
}

@Override
public String retrieveFromABCD(JsonNode unit) {
return super.searchJsonForTerm(unit, abcdTerms);
var rawResult = super.searchJsonForTerm(unit, abcdTerms);
return sanitizeInput(rawResult);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package eu.dissco.core.translator.terms.specimen.location;

import com.fasterxml.jackson.databind.JsonNode;
import eu.dissco.core.translator.terms.Term;
import java.util.List;

public class MinimumDistanceAboveSurfaceInMeters extends Term {
public class MinimumDistanceAboveSurfaceInMeters extends AbstractMeterTerm {

public static final String TERM = DWC_PREFIX + "minimumDistanceAboveSurfaceInMeters";

Expand All @@ -14,12 +13,14 @@ public class MinimumDistanceAboveSurfaceInMeters extends Term {

@Override
public String retrieveFromDWCA(JsonNode unit) {
return super.searchJsonForTerm(unit, dwcaTerms);
var rawResult = super.searchJsonForTerm(unit, dwcaTerms);
return sanitizeInput(rawResult);
}

@Override
public String retrieveFromABCD(JsonNode unit) {
return super.searchJsonForTerm(unit, abcdTerms);
var rawResult = super.searchJsonForTerm(unit, abcdTerms);
return sanitizeInput(rawResult);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package eu.dissco.core.translator.terms.specimen.location;

import com.fasterxml.jackson.databind.JsonNode;
import eu.dissco.core.translator.terms.Term;
import java.util.List;

public class MinimumElevationInMeters extends Term {
public class MinimumElevationInMeters extends AbstractMeterTerm {

public static final String TERM = DWC_PREFIX + "minimumElevationInMeters";

Expand All @@ -15,12 +14,14 @@ public class MinimumElevationInMeters extends Term {

@Override
public String retrieveFromDWCA(JsonNode unit) {
return super.searchJsonForTerm(unit, dwcaTerms);
var rawResult = super.searchJsonForTerm(unit, dwcaTerms);
return sanitizeInput(rawResult);
}

@Override
public String retrieveFromABCD(JsonNode unit) {
return super.searchJsonForTerm(unit, abcdTerms);
var rawResult = super.searchJsonForTerm(unit, abcdTerms);
return sanitizeInput(rawResult);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package eu.dissco.core.translator.terms.utils;

import static eu.dissco.core.translator.schema.Identifier.DctermsType.*;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.ARK;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.DOI;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.HANDLE;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.PURL;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.URL;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.UUID;
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE;
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE;
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE_FDO_COMPLIANT;
Expand Down
Loading

0 comments on commit b36d9d4

Please sign in to comment.