Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sanitize logic to location meter field #74

Merged
merged 6 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,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 @@ -252,6 +253,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 @@ -455,7 +467,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 @@ -531,21 +542,21 @@ 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))
.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);
var assertions = new EventAssertions().gatherEventAssertions(mapper, data, dwc);
var event = new Event()
.withType("ods:Event")
Expand Down Expand Up @@ -585,6 +596,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: "
samleeflang marked this conversation as resolved.
Show resolved Hide resolved
+ 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 @@ -612,14 +648,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));
southeo marked this conversation as resolved.
Show resolved Hide resolved
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
Loading