Skip to content

Commit bb8c9b6

Browse files
authored
Merge pull request #943 from AtlasOfLivingAustralia/feature/datasetname
Update the data set name after more information is available #942
2 parents f86a80e + 63d6e89 commit bb8c9b6

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

grails-app/services/au/org/ala/ecodata/ParatooService.groovy

+29-9
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class ParatooService {
196196

197197
private static String buildName(String protocolId, String displayDate, Project project) {
198198
ActivityForm protocolForm = ActivityForm.findByExternalId(protocolId)
199-
String dataSetName = protocolForm?.name + " - " + displayDate + " (" + project.name + ")"
199+
String dataSetName = protocolForm?.name + " - " + displayDate
200200
dataSetName
201201
}
202202

@@ -277,8 +277,11 @@ class ParatooService {
277277
surveyDataAndObservations = recursivelyTransformData(form.sections[0].template.dataModel, surveyDataAndObservations, form.name, 1, config)
278278
// If we are unable to create a site, null will be returned - assigning a null siteId is valid.
279279

280+
String siteName = null
280281
if (!dataSet.siteId) {
281-
dataSet.siteId = createSiteFromSurveyData(surveyDataAndObservations, collection, surveyId, project.project, config, form)
282+
Map site = createSiteFromSurveyData(surveyDataAndObservations, collection, surveyId, project.project, config, form)
283+
dataSet.siteId = site.siteId
284+
siteName = site.name
282285
}
283286

284287
// plot layout is of type geoMap. Therefore, expects a site id.
@@ -290,6 +293,9 @@ class ParatooService {
290293
dataSet.endDate = config.getEndDate(surveyDataAndObservations)
291294
dataSet.format = DATASET_DATABASE_TABLE
292295
dataSet.sizeUnknown = true
296+
// Update the data set name as the information supplied during /mint-identifier isn't enough
297+
// to ensure uniqueness
298+
dataSet.name = buildUpdatedDataSetSummaryName(siteName, dataSet.startDate, dataSet.endDate, form.name, surveyId)
293299

294300
// Delete previously created activity so that duplicate species records are not created.
295301
// Updating existing activity will also create duplicates since it relies on outputSpeciesId to determine
@@ -308,6 +314,23 @@ class ParatooService {
308314
}
309315
}
310316

317+
protected static String buildUpdatedDataSetSummaryName(String siteName, String startDate, String endDate, String protocolName, ParatooCollectionId surveyId) {
318+
String name = protocolName
319+
if (siteName) {
320+
name += " (" + siteName + ")"
321+
}
322+
if (startDate && endDate && startDate != endDate) {
323+
name += " - " + DateUtil.formatAsDisplayDateTime(startDate) + " to " + DateUtil.formatAsDisplayDateTime(endDate)
324+
}
325+
else if (startDate) {
326+
name += " - " +DateUtil.formatAsDisplayDateTime(startDate)
327+
}
328+
else {
329+
name += " - " + DateUtil.formatAsDisplayDateTime(surveyId.eventTime)
330+
}
331+
name
332+
}
333+
311334
/**
312335
* Rearrange survey data to match the data model.
313336
* 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 {
555578
output
556579
}
557580

558-
private String createSiteFromSurveyData(Map observation, ParatooCollection collection, ParatooCollectionId surveyId, Project project, ParatooProtocolConfig config, ActivityForm form) {
581+
private Map createSiteFromSurveyData(Map observation, ParatooCollection collection, ParatooCollectionId surveyId, Project project, ParatooProtocolConfig config, ActivityForm form) {
559582
String siteId = null
560583
// Create a site representing the location of the collection
584+
Map siteProps = null
561585
Map geoJson = config.getGeoJson(observation, form)
562586
if (geoJson) {
563-
Map siteProps = siteService.propertiesFromGeoJson(geoJson, 'upload')
587+
siteProps = siteService.propertiesFromGeoJson(geoJson, 'upload')
564588
List features = geoJson?.features ?: []
565589
geoJson.remove('features')
566590
siteProps.features = features
@@ -592,7 +616,7 @@ class ParatooService {
592616
}
593617
siteId = result.siteId
594618
}
595-
siteId
619+
[siteId:siteId, name:siteProps?.name]
596620
}
597621

598622
private Map syncParatooProtocols(List<Map> protocols) {
@@ -774,10 +798,6 @@ class ParatooService {
774798
dataSet
775799
}
776800

777-
private static String buildSurveyQueryString(int start, int limit, String createdAt) {
778-
"?populate=deep&sort=updatedAt&pagination[start]=$start&pagination[limit]=$limit&filters[createdAt][\$eq]=$createdAt"
779-
}
780-
781801
Map retrieveSurveyAndObservations(ParatooCollection collection, Map authHeader = null) {
782802
String apiEndpoint = PARATOO_DATA_PATH
783803
Map payload = [

src/main/groovy/au/org/ala/ecodata/DateUtil.groovy

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class DateUtil {
5454
dateTime.format(DISPLAY_DATE_TIME_FORMATTER)
5555
}
5656

57+
static String formatAsDisplayDateTime(String isoDateString) {
58+
Date date = parse(isoDateString)
59+
formatAsDisplayDateTime(date)
60+
}
61+
5762
/**
5863
* Returns a formatted string representing the financial year a report or activity falls into, based on
5964
* the end date. This method won't necessarily work for start dates as it will subtract a day from the value

src/test/groovy/au/org/ala/ecodata/ParatooServiceSpec.groovy

+19-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import org.codehaus.jackson.map.ObjectMapper
1212
import org.grails.web.converters.marshaller.json.CollectionMarshaller
1313
import org.grails.web.converters.marshaller.json.MapMarshaller
1414

15+
import java.time.format.DateTimeTextProvider
16+
import java.time.temporal.TemporalField
17+
1518
import static grails.async.Promises.waitAll
1619
/**
1720
* Tests for the ParatooService.
@@ -33,6 +36,10 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
3336
static Map DUMMY_POLYGON = [type: 'Polygon', coordinates: [[[1, 2], [2, 2], [2, 1], [1, 1], [1, 2]]]]
3437
static Map DUMMY_PLOT = ['type':'Point', coordinates: [1,2]]
3538

39+
// The am/pm in the formatted time is local dependent and this appears to be easiest way to determine the value.
40+
String am = DateUtil.formatAsDisplayDateTime("2024-05-14T00:00:00Z")[-2..-1]
41+
String pm = am == "AM" ? "PM" : "pm"
42+
3643
def setup() {
3744

3845
deleteAll()
@@ -156,7 +163,7 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
156163
assert dataSet.protocol == collectionId.protocolId
157164
assert dataSet.grantId == "g1"
158165
assert dataSet.progress == 'planned'
159-
assert dataSet.name == "aParatooForm 1 - ${DateUtil.formatAsDisplayDateTime(collectionId.eventTime)} (Project 1)"
166+
assert dataSet.name == "aParatooForm 1 - ${DateUtil.formatAsDisplayDateTime(collectionId.eventTime)}"
160167

161168
[status: 'ok']
162169
}
@@ -182,7 +189,7 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
182189
Map dataSet = [dataSetId:'d1', grantId:'g1', surveyId:paratooCollectionId.toMap(), activityId: "123"]
183190
dataSet.surveyId.survey_metadata.orgMintedUUID = orgMintedId
184191
Map expectedDataSetSync = dataSet + [progress: Activity.STARTED]
185-
Map expectedDataSetAsync = dataSet + [progress: Activity.STARTED, startDate: "2023-09-01T00:00:00Z", endDate: "2023-09-01T00:00:00Z", areSpeciesRecorded: false, activityId: '123', siteId: null, format: "Database Table", sizeUnknown: true]
192+
Map expectedDataSetAsync = dataSet + [progress: Activity.STARTED, startDate: "2023-09-01T00:00:00Z", endDate: "2023-09-01T00:00:00Z", areSpeciesRecorded: false, activityId: '123', siteId: null, format: "Database Table", sizeUnknown: true, name: "aParatooForm 1 - 2023-09-01 10:00 ${am}"]
186193
ParatooProject project = new ParatooProject(id: projectId, project: new Project(projectId: projectId, custom: [dataSets: [dataSet]]))
187194

188195
when:
@@ -1337,6 +1344,16 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
13371344
]
13381345
}
13391346

1347+
def "The data set name will be updated after the callback to Monitor core and be created from available information"() {
1348+
expect:
1349+
ParatooService.buildUpdatedDataSetSummaryName("site", "2024-05-14T00:00:00Z", "2024-05-14T10:00:00Z", "Protocol 1", null) == "Protocol 1 (site) - 2024-05-14 10:00 ${am} to 2024-05-14 8:00 ${pm}"
1350+
ParatooService.buildUpdatedDataSetSummaryName("site", "2024-05-14T00:00:00Z", null, "Protocol 1", null) == "Protocol 1 (site) - 2024-05-14 10:00 ${am}"
1351+
ParatooService.buildUpdatedDataSetSummaryName(null, "2024-05-14T00:00:00Z", null, "Protocol 1", null) == "Protocol 1 - 2024-05-14 10:00 ${am}"
1352+
ParatooService.buildUpdatedDataSetSummaryName(null, null, null, "Protocol 1", new ParatooCollectionId(eventTime:DateUtil.parse("2024-05-14T00:00:00Z"))) == "Protocol 1 - 2024-05-14 10:00 ${am}"
1353+
1354+
1355+
}
1356+
13401357
private Map getNormalDefinition() {
13411358
def input = """
13421359
{

0 commit comments

Comments
 (0)