diff --git a/grails-app/services/au/org/ala/ecodata/ParatooService.groovy b/grails-app/services/au/org/ala/ecodata/ParatooService.groovy index 0e6add911..ca8709715 100644 --- a/grails-app/services/au/org/ala/ecodata/ParatooService.groovy +++ b/grails-app/services/au/org/ala/ecodata/ParatooService.groovy @@ -196,7 +196,7 @@ class ParatooService { private static String buildName(String protocolId, String displayDate, Project project) { ActivityForm protocolForm = ActivityForm.findByExternalId(protocolId) - String dataSetName = protocolForm?.name + " - " + displayDate + " (" + project.name + ")" + String dataSetName = protocolForm?.name + " - " + displayDate dataSetName } @@ -277,8 +277,11 @@ class ParatooService { surveyDataAndObservations = recursivelyTransformData(form.sections[0].template.dataModel, surveyDataAndObservations, form.name, 1, config) // If we are unable to create a site, null will be returned - assigning a null siteId is valid. + String siteName = null if (!dataSet.siteId) { - dataSet.siteId = createSiteFromSurveyData(surveyDataAndObservations, collection, surveyId, project.project, config, form) + Map site = createSiteFromSurveyData(surveyDataAndObservations, collection, surveyId, project.project, config, form) + dataSet.siteId = site.siteId + siteName = site.name } // plot layout is of type geoMap. Therefore, expects a site id. @@ -290,6 +293,9 @@ class ParatooService { dataSet.endDate = config.getEndDate(surveyDataAndObservations) dataSet.format = DATASET_DATABASE_TABLE dataSet.sizeUnknown = true + // Update the data set name as the information supplied during /mint-identifier isn't enough + // to ensure uniqueness + dataSet.name = buildUpdatedDataSetSummaryName(siteName, dataSet.startDate, dataSet.endDate, form.name, surveyId) // Delete previously created activity so that duplicate species records are not created. // Updating existing activity will also create duplicates since it relies on outputSpeciesId to determine @@ -308,6 +314,23 @@ class ParatooService { } } + protected static String buildUpdatedDataSetSummaryName(String siteName, String startDate, String endDate, String protocolName, ParatooCollectionId surveyId) { + String name = protocolName + if (siteName) { + name += " (" + siteName + ")" + } + if (startDate && endDate && startDate != endDate) { + name += " - " + DateUtil.formatAsDisplayDateTime(startDate) + " to " + DateUtil.formatAsDisplayDateTime(endDate) + } + else if (startDate) { + name += " - " +DateUtil.formatAsDisplayDateTime(startDate) + } + else { + name += " - " + DateUtil.formatAsDisplayDateTime(surveyId.eventTime) + } + name + } + /** * Rearrange survey data to match the data model. * e.g. [a: [b: [c: 1, d: 2], d: 1], b: [c: 1, d: 2]] => [b: [c: 1, d: 2, a: [d: 1]]] @@ -555,12 +578,13 @@ class ParatooService { output } - private String createSiteFromSurveyData(Map observation, ParatooCollection collection, ParatooCollectionId surveyId, Project project, ParatooProtocolConfig config, ActivityForm form) { + private Map createSiteFromSurveyData(Map observation, ParatooCollection collection, ParatooCollectionId surveyId, Project project, ParatooProtocolConfig config, ActivityForm form) { String siteId = null // Create a site representing the location of the collection + Map siteProps = null Map geoJson = config.getGeoJson(observation, form) if (geoJson) { - Map siteProps = siteService.propertiesFromGeoJson(geoJson, 'upload') + siteProps = siteService.propertiesFromGeoJson(geoJson, 'upload') List features = geoJson?.features ?: [] geoJson.remove('features') siteProps.features = features @@ -592,7 +616,7 @@ class ParatooService { } siteId = result.siteId } - siteId + [siteId:siteId, name:siteProps?.name] } private Map syncParatooProtocols(List protocols) { @@ -774,10 +798,6 @@ class ParatooService { dataSet } - private static String buildSurveyQueryString(int start, int limit, String createdAt) { - "?populate=deep&sort=updatedAt&pagination[start]=$start&pagination[limit]=$limit&filters[createdAt][\$eq]=$createdAt" - } - Map retrieveSurveyAndObservations(ParatooCollection collection, Map authHeader = null) { String apiEndpoint = PARATOO_DATA_PATH Map payload = [ diff --git a/src/main/groovy/au/org/ala/ecodata/DateUtil.groovy b/src/main/groovy/au/org/ala/ecodata/DateUtil.groovy index 8c5831423..ee5d8a0ae 100644 --- a/src/main/groovy/au/org/ala/ecodata/DateUtil.groovy +++ b/src/main/groovy/au/org/ala/ecodata/DateUtil.groovy @@ -54,6 +54,11 @@ class DateUtil { dateTime.format(DISPLAY_DATE_TIME_FORMATTER) } + static String formatAsDisplayDateTime(String isoDateString) { + Date date = parse(isoDateString) + formatAsDisplayDateTime(date) + } + /** * Returns a formatted string representing the financial year a report or activity falls into, based on * the end date. This method won't necessarily work for start dates as it will subtract a day from the value diff --git a/src/test/groovy/au/org/ala/ecodata/ParatooServiceSpec.groovy b/src/test/groovy/au/org/ala/ecodata/ParatooServiceSpec.groovy index 180c1350f..d6a5351b3 100644 --- a/src/test/groovy/au/org/ala/ecodata/ParatooServiceSpec.groovy +++ b/src/test/groovy/au/org/ala/ecodata/ParatooServiceSpec.groovy @@ -12,6 +12,9 @@ import org.codehaus.jackson.map.ObjectMapper import org.grails.web.converters.marshaller.json.CollectionMarshaller import org.grails.web.converters.marshaller.json.MapMarshaller +import java.time.format.DateTimeTextProvider +import java.time.temporal.TemporalField + import static grails.async.Promises.waitAll /** * Tests for the ParatooService. @@ -33,6 +36,10 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest