diff --git a/grails-app/services/au/org/ala/ecodata/MetadataService.groovy b/grails-app/services/au/org/ala/ecodata/MetadataService.groovy index b8e7781a3..02f9885cb 100644 --- a/grails-app/services/au/org/ala/ecodata/MetadataService.groovy +++ b/grails-app/services/au/org/ala/ecodata/MetadataService.groovy @@ -41,6 +41,7 @@ class MetadataService { SpeciesReMatchService speciesReMatchService HubService hubService ProjectService projectService + RecordService recordService /** * @deprecated use versioned API to retrieve activity form definitions @@ -896,17 +897,13 @@ class MetadataService { data } - Map autoPopulateSpeciesData (Map data, int limit = 10) { + Map autoPopulateSpeciesData (Map data) { String searchName = (data?.scientificName)?.trim() if (!data?.guid && (searchName)) { - def result = speciesReMatchService.searchBie(searchName, limit) - // find the name that exactly matches the search name - def bestMatch = result?.autoCompleteList?.find { - it.matchedNames?.findResult { String name -> - name.equalsIgnoreCase(searchName) - || name.equalsIgnoreCase(data.name) - || name.equalsIgnoreCase(data.commonName) - } + Map bestMatch = speciesReMatchService.searchByName(searchName) + if(!bestMatch && data.commonName) { + String commonName = data.commonName + bestMatch = speciesReMatchService.searchByName(commonName, false, true) } if (bestMatch) { @@ -917,7 +914,7 @@ class MetadataService { } if(!data?.name) { - data.name = data?.commonName ? data?.scientificName + '(' + data?.commonName + ')' : data?.scientificName + data.name = recordService.formatTaxonName(data, RecordService.SCIENTIFIC_NAME_COMMON_NAME) } data diff --git a/grails-app/services/au/org/ala/ecodata/RecordService.groovy b/grails-app/services/au/org/ala/ecodata/RecordService.groovy index c869a39c3..02e51e888 100644 --- a/grails-app/services/au/org/ala/ecodata/RecordService.groovy +++ b/grails-app/services/au/org/ala/ecodata/RecordService.groovy @@ -45,6 +45,10 @@ class RecordService { final def ignores = ["action", "controller", "associatedMedia"] private static final List EXCLUDED_RECORD_PROPERTIES = ["_id", "activityId", "dateCreated", "json", "outputId", "projectActivityId", "projectId", "status", "dataResourceUid"] + static final String COMMON_NAME = 'COMMONNAME' + static final String SCIENTIFIC_NAME = 'SCIENTIFICNAME' + static final String COMMON_NAME_SCIENTIFIC_NAME = 'COMMONNAME(SCIENTIFICNAME)' + static final String SCIENTIFIC_NAME_COMMON_NAME = 'SCIENTIFICNAME(COMMONNAME)' def getProjectActivityService() { grailsApplication.mainContext.projectActivityService @@ -1119,4 +1123,37 @@ class RecordService { commonService.updateProperties(output, props) } } + + /** format species by specific type **/ + String formatTaxonName (Map data, String displayType) { + String name = '' + switch (displayType){ + case COMMON_NAME_SCIENTIFIC_NAME: + if (data.commonName && data.scientificName) { + name = "${data.commonName} (${data.scientificName})" + } else if (data.commonName) { + name = data.commonName + } else if (data.scientificName) { + name = data.scientificName + } + break + case SCIENTIFIC_NAME_COMMON_NAME: + if (data.scientificName && data.commonName) { + name = "${data.scientificName} (${data.commonName})" + } else if (data.scientificName) { + name = data.scientificName + } else if (data.commonName) { + name = data.commonName + } + break + case COMMON_NAME: + name = data.commonName ?: data.scientificName ?: "" + break + case SCIENTIFIC_NAME: + name = data.scientificName ?: "" + break + } + + name + } } diff --git a/grails-app/services/au/org/ala/ecodata/SpeciesReMatchService.groovy b/grails-app/services/au/org/ala/ecodata/SpeciesReMatchService.groovy index d02361bc5..84f7e7bf2 100644 --- a/grails-app/services/au/org/ala/ecodata/SpeciesReMatchService.groovy +++ b/grails-app/services/au/org/ala/ecodata/SpeciesReMatchService.groovy @@ -125,7 +125,7 @@ class SpeciesReMatchService { } resp - }) + }) as Map } Map searchByVernacularNameOnNameMatchingServer (String name) { @@ -139,6 +139,6 @@ class SpeciesReMatchService { } resp - }) + }) as Map } } diff --git a/src/test/groovy/au/org/ala/ecodata/RecordServiceSpec.groovy b/src/test/groovy/au/org/ala/ecodata/RecordServiceSpec.groovy index 1366bad26..53a8b36cb 100644 --- a/src/test/groovy/au/org/ala/ecodata/RecordServiceSpec.groovy +++ b/src/test/groovy/au/org/ala/ecodata/RecordServiceSpec.groovy @@ -68,5 +68,34 @@ class RecordServiceSpec extends MongoSpec implements ServiceUnitTest