Skip to content

Commit fb03f14

Browse files
committed
- changed how start and end dates are calculated for plot based protocols - refactored code
1 parent 1beb3b2 commit fb03f14

File tree

3 files changed

+1752
-59
lines changed

3 files changed

+1752
-59
lines changed

src/main/groovy/au/org/ala/ecodata/paratoo/ParatooProtocolConfig.groovy

+47-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package au.org.ala.ecodata.paratoo
22

33
import au.org.ala.ecodata.*
4+
import au.org.ala.ecodata.converter.ISODateBindingConverter
45
import au.org.ala.ecodata.metadata.OutputMetadata
56
import au.org.ala.ecodata.metadata.PropertyAccessor
67
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
@@ -24,6 +25,7 @@ class ParatooProtocolConfig {
2425
String endDatePath = 'end_date_time'
2526
String surveyIdPath = 'survey_metadata'
2627
String plotVisitPath = 'plot_visit'
28+
String plotProtocolObservationDatePath = "date_time"
2729
String plotVisitStartDatePath = "${plotVisitPath}.start_date"
2830
String plotVisitEndDatePath = "${plotVisitPath}.end_date"
2931
String plotLayoutPath = "${plotVisitPath}.plot_layout"
@@ -53,20 +55,46 @@ class ParatooProtocolConfig {
5355
return null
5456
}
5557

56-
def date = getProperty(surveyData, startDatePath)
57-
date = getFirst(date)
58-
if (date == null) {
59-
if (usesPlotLayout) {
60-
date = getProperty(surveyData, plotVisitStartDatePath)
61-
}
62-
else {
58+
def date
59+
if (usesPlotLayout) {
60+
List dates = getDatesFromObservation(surveyData)
61+
date = dates ? DateUtil.format(dates.first()) : null
62+
return date
63+
}
64+
else {
65+
date = getProperty(surveyData, startDatePath)
66+
if (!date) {
6367
date = getPropertyFromSurvey(surveyData, startDatePath)
6468
}
6569

6670
date = getFirst(date)
71+
return removeMilliseconds(date)
72+
}
73+
}
74+
75+
/**
76+
* Get date from plotProtocolObservationDatePath and sort them.
77+
* @param surveyData - reverse lookup output which includes survey and observation data
78+
* @return
79+
*/
80+
List getDatesFromObservation(Map surveyData) {
81+
Map surveysData = surveyData.findAll { key, value ->
82+
![ getSurveyAttributeName(), ParatooService.PARATOO_DATAMODEL_PLOT_SELECTION,
83+
ParatooService.PARATOO_DATAMODEL_PLOT_VISIT, ParatooService.PARATOO_DATAMODEL_PLOT_LAYOUT].contains(key)
84+
}
85+
List result = []
86+
ISODateBindingConverter converter = new ISODateBindingConverter()
87+
surveysData.each { key, value ->
88+
def dates = getProperty(value, plotProtocolObservationDatePath)
89+
dates = dates instanceof List ? dates : [dates]
90+
91+
result.addAll(dates.collect { String date ->
92+
date ? converter.convert(date, ISODateBindingConverter.FORMAT) : null
93+
})
6794
}
6895

69-
removeMilliseconds(date)
96+
result = result.findAll { it != null }
97+
result.sort()
7098
}
7199

72100
def getPropertyFromSurvey(Map surveyData, String path) {
@@ -79,23 +107,21 @@ class ParatooProtocolConfig {
79107
return null
80108
}
81109

82-
def date = getProperty(surveyData, endDatePath)
83-
date = getFirst(date)
84-
if (date == null) {
85-
if (usesPlotLayout) {
86-
date = getProperty(surveyData, plotVisitEndDatePath)
87-
if (!date) {
88-
date = getProperty(surveyData, plotVisitStartDatePath)
89-
}
90-
}
91-
else {
110+
def date
111+
if (usesPlotLayout) {
112+
def dates = getDatesFromObservation(surveyData)
113+
date = dates ? DateUtil.format(dates.last()) : null
114+
return date
115+
}
116+
else {
117+
date = getProperty(surveyData, endDatePath)
118+
if (!date) {
92119
date = getPropertyFromSurvey(surveyData, endDatePath)
93120
}
94121

95122
date = getFirst(date)
123+
return removeMilliseconds(date)
96124
}
97-
98-
removeMilliseconds(date)
99125
}
100126

101127
Map getSurveyId(Map surveyData) {
@@ -198,7 +224,7 @@ class ParatooProtocolConfig {
198224
apiEndpoint
199225
}
200226

201-
def getProperty(Map surveyData, String path) {
227+
def getProperty(def surveyData, String path) {
202228
if (!path) {
203229
return null
204230
}

src/test/groovy/au/org/ala/ecodata/paratoo/ParatooProtocolConfigSpec.groovy

+40-37
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,24 @@ class ParatooProtocolConfigSpec extends Specification {
5050
[
5151
name : "vegetation-mapping-survey",
5252
dataType: "list",
53+
columns : [
54+
55+
]
56+
],
57+
[
58+
name : "vegetation-mapping-observation",
59+
dataType: "list",
5360
columns : [
5461
[
55-
name : "vegetation-mapping-observation",
56-
dataType: "list",
57-
columns : [
58-
[
59-
dataType: "feature",
60-
name : "position"
61-
]
62-
]
62+
dataType: "feature",
63+
name : "position"
6364
]
6465
]
6566
]
6667
],
6768
relationships: [
68-
ecodata : ["vegetation-mapping-survey": ["vegetation-mapping-observation": [:]]],
69-
apiOutput: ["vegetation-mapping-observation": ["vegetation-mapping-observation": [:]]]
69+
ecodata : [:],
70+
apiOutput: [:]
7071
]
7172
]
7273
]
@@ -133,8 +134,8 @@ class ParatooProtocolConfigSpec extends Specification {
133134
transformData(observation, activityForm, config)
134135

135136
then:
136-
config.getStartDate(observation) == "2021-08-26T11:26:54Z"
137-
config.getEndDate(observation) == "2021-08-26T13:26:54Z"
137+
config.getStartDate(observation) == "2024-04-08T01:23:28Z"
138+
config.getEndDate(observation) == "2024-04-10T01:23:28Z"
138139
config.getGeoJson(observation) == [type: "Feature", geometry: [type: "Polygon", coordinates: [[[152.880694, -27.388252], [152.880651, -27.388336], [152.880518, -27.388483], [152.880389, -27.388611], [152.88028, -27.388749], [152.880154, -27.388903], [152.880835, -27.389463], [152.880644, -27.389366], [152.880525, -27.389248], [152.88035, -27.389158], [152.880195, -27.389021], [152.880195, -27.389373], [152.880797, -27.388316], [152.881448, -27.388909], [152.881503, -27.388821], [152.881422, -27.388766], [152.881263, -27.388644], [152.881107, -27.388549], [152.880939, -27.388445], [152.881314, -27.389035], [152.88122, -27.389208], [152.881089, -27.389343], [152.880973, -27.389472], [152.880916, -27.389553], [152.880694, -27.388252]]]], properties: [name: "QDASEQ0001 - Control (100 x 100)", externalId: 1, description: "QDASEQ0001 - Control (100 x 100)", notes: "some comment"]]
139140
}
140141

@@ -157,23 +158,24 @@ class ParatooProtocolConfigSpec extends Specification {
157158
[
158159
dataType: "list",
159160
name : "basal-area-dbh-measure-survey",
161+
columns : [
162+
163+
]
164+
],
165+
[
166+
dataType: "list",
167+
name : "basal-area-dbh-measure-observation",
160168
columns : [
161169
[
162-
dataType: "list",
163-
name : "basal-area-dbh-measure-observation",
164-
columns : [
165-
[
166-
dataType: "feature",
167-
name : "location"
168-
]
169-
]
170+
dataType: "feature",
171+
name : "location"
170172
]
171173
]
172174
]
173175
],
174176
viewModel : [],
175177
relationships: [
176-
ecodata : ["basal-area-dbh-measure-survey":["basal-area-dbh-measure-observation": [:]]],
178+
ecodata : [:],
177179
apiOutput: [:]
178180
]
179181
]
@@ -183,8 +185,8 @@ class ParatooProtocolConfigSpec extends Specification {
183185
transformData(observation, activityForm, config)
184186

185187
expect:
186-
config.getStartDate(observation) == '2023-09-22T00:59:47Z'
187-
config.getEndDate(observation) == "2023-09-22T00:59:47Z"
188+
config.getStartDate(observation) == "2024-03-28T03:17:01Z"
189+
config.getEndDate(observation) == "2024-03-28T03:17:01Z"
188190
config.getGeoJson(observation) == [
189191
type : "Feature",
190192
geometry : [
@@ -226,23 +228,24 @@ class ParatooProtocolConfigSpec extends Specification {
226228
[
227229
name : "opportunistic-survey",
228230
dataType: "list",
229-
columns: [[
230-
name : "opportunistic-observation",
231-
dataType: "list",
232-
columns : [
233-
[
234-
name : "location",
235-
dataType: "feature",
236-
required: true,
237-
external: true
238-
]
239-
]
240-
]]
231+
columns: []
232+
],
233+
[
234+
name : "opportunistic-observation",
235+
dataType: "list",
236+
columns : [
237+
[
238+
name : "location",
239+
dataType: "feature",
240+
required: true,
241+
external: true
242+
]
243+
]
241244
]
242245
],
243246
relationships: [
244-
ecodata : ["opportunistic-survey": ["opportunistic-observation": [:]]],
245-
apiOutput: ["opportunistic-observation.opportunistic_survey": ["opportunistic-survey": [:]], "opportunistic-observation": ["opportunistic-observation": [:]]]
247+
ecodata : [:],
248+
apiOutput: [:]
246249
]
247250
]
248251
]

0 commit comments

Comments
 (0)