Skip to content

Commit

Permalink
Merge pull request #1054 from AtlasOfLivingAustralia/feature/issue1053
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala authored Jan 9, 2025
2 parents 962eed9 + 7354fe6 commit 4b1e7ad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
21 changes: 14 additions & 7 deletions grails-app/services/au/org/ala/ecodata/ParatooService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2097,33 +2097,40 @@ class ParatooService {
}

String regex = "([^\\[\\(]*)(?:\\[(.*)\\])?\\s*(?:\\(scientific:\\s*(.*?)\\))?"
String commonName, scientificName = name
String commonName, scientificName, taxonRank
Pattern pattern = Pattern.compile(regex)
Matcher matcher = pattern.matcher(name)
Map result = [scientificName: name, commonName: name, outputSpeciesId: UUID.randomUUID().toString()]

if (matcher.find()) {
commonName = matcher.group(1)?.trim()
scientificName = matcher.group(3)?.trim()
result.taxonRank = matcher.group(2)?.trim()
result.scientificName = scientificName
result.commonName = commonName
taxonRank = matcher.group(2)?.trim()
result.taxonRank = taxonRank
result.scientificName = scientificName == null ? result.scientificName : scientificName
result.commonName = commonName == null ? result.commonName : commonName
}

Map resp = speciesReMatchService.searchByName(scientificName)
Map resp = speciesReMatchService.searchByName(result.scientificName)
if (resp) {
result.putAll(resp)
}
// try again with common name
if ((result.guid == null) && commonName) {
if ((result.guid == null) && result.commonName) {
resp = speciesReMatchService.searchByName(commonName, false, true)
if (resp) {
result.putAll(resp)
result.commonName = commonName
}
}

result.name = result.commonName ? result.scientificName ? "${result.scientificName} (${result.commonName})" : result.commonName : result.scientificName
if (result.commonName != result.scientificName) {
result.name = result.commonName ? result.scientificName ? "${result.scientificName} (${result.commonName})" : result.commonName : result.scientificName
}
else {
result.name = result.scientificName ?: result.commonName
}

List specialCases = grailsApplication.config.getProperty("paratoo.species.specialCases", List)
// do not create record for special cases
if (specialCases.contains(name)) {
Expand Down
17 changes: 16 additions & 1 deletion src/test/groovy/au/org/ala/ecodata/ParatooServiceSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -800,13 +800,28 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
1 * speciesReMatchService.searchByName(_) >> null
1 * speciesReMatchService.searchByName(_, false, true) >> null

when: // user inputs scientific name to field
result = service.transformSpeciesName("Centipeda cunninghamii")
outputSpeciesId = result.remove("outputSpeciesId")

then:
outputSpeciesId != null
result == [name: "Centipeda cunninghamii (Common Sneezeweed)", scientificName: "Centipeda cunninghamii", guid: "https://id.biodiversity.org.au/node/apni/2916674", commonName: "Common Sneezeweed", taxonRank: "species"]
1 * speciesReMatchService.searchByName(_) >> [
scientificName: "Centipeda cunninghamii",
commonName: "Common Sneezeweed",
guid: "https://id.biodiversity.org.au/node/apni/2916674",
taxonRank: "species"
]
0 * speciesReMatchService.searchByName(_, false, true)

when: // Do not create record when value equals special cases. Therefore, removes guid.
result = service.transformSpeciesName("Other")
outputSpeciesId = result.remove("outputSpeciesId")

then:
outputSpeciesId != null
result == [name: "Other", scientificName: null, commonName: "Other", taxonRank: null]
result == [name: "Other", scientificName: "Other", commonName: "Other", taxonRank: null]
1 * speciesReMatchService.searchByName(_) >> null
1 * speciesReMatchService.searchByName(_, false, true) >> null

Expand Down

0 comments on commit 4b1e7ad

Please sign in to comment.