Skip to content

Commit 8933698

Browse files
committed
Update the data set name after more information is available #942
1 parent f86a80e commit 8933698

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

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

+28-8
Original file line numberDiff line numberDiff line change
@@ -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

+10-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
182182
Map dataSet = [dataSetId:'d1', grantId:'g1', surveyId:paratooCollectionId.toMap(), activityId: "123"]
183183
dataSet.surveyId.survey_metadata.orgMintedUUID = orgMintedId
184184
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]
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, name: "aParatooForm 1 - 2023-09-01 10:00 am"]
186186
ParatooProject project = new ParatooProject(id: projectId, project: new Project(projectId: projectId, custom: [dataSets: [dataSet]]))
187187

188188
when:
@@ -1337,6 +1337,15 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
13371337
]
13381338
}
13391339

1340+
def "The data set name will be updated after the callback to Monitor core and be created from available information"() {
1341+
expect:
1342+
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"
1343+
ParatooService.buildUpdatedDataSetSummaryName("site", "2024-05-14T00:00:00Z", null, "Protocol 1", null) == "Protocol 1 (site) - 2024-05-14 10:00 am"
1344+
ParatooService.buildUpdatedDataSetSummaryName(null, "2024-05-14T00:00:00Z", null, "Protocol 1", null) == "Protocol 1 - 2024-05-14 10:00 am"
1345+
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"
1346+
1347+
}
1348+
13401349
private Map getNormalDefinition() {
13411350
def input = """
13421351
{

0 commit comments

Comments
 (0)