-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathActivityService.groovy
453 lines (381 loc) · 16.5 KB
/
ActivityService.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package au.org.ala.biocollect.merit
import au.org.ala.biocollect.DateUtils
import au.org.ala.biocollect.ProjectActivityService
import au.org.ala.biocollect.UtilService
import au.org.ala.biocollect.merit.SpeciesService
import grails.core.GrailsApplication
import org.joda.time.DateTime
import org.joda.time.Period
import org.joda.time.format.DateTimeFormat
class ActivityService {
static final BATCH_SIZE = 20
GrailsApplication grailsApplication
WebService webService
MetadataService metadataService
SpeciesService speciesService
ProjectActivityService projectActivityService
UserService userService
CacheService cacheService
OutputService outputService
UtilService utilService
public static final String PROGRESS_PLANNED = 'planned'
public static final String PROGRESS_FINISHED = 'finished'
public static final String PROGRESS_STARTED = 'started'
public static final String PROGRESS_DEFERRED = 'deferred'
public static final String PROGRESS_CANCELLED = 'cancelled'
private static def PROGRESS = ['planned', 'started', 'finished', 'cancelled', 'deferred']
public static Comparator<String> PROGRESS_COMPARATOR = {a,b -> PROGRESS.indexOf(a) <=> PROGRESS.indexOf(b)}
static dateFormat = "yyyy-MM-dd'T'hh:mm:ssZ"
def getCommonService() {
grailsApplication.mainContext.commonService
}
def constructName = { act ->
def date = commonService.simpleDateLocalTime(act.startDate) ?:
commonService.simpleDateLocalTime(act.endDate)
def dates = []
if (act.startDate) {
dates << commonService.simpleDateLocalTime(act.startDate)
}
if (act.endDate) {
dates << commonService.simpleDateLocalTime(act.endDate)
}
def dateRange = dates.join('-')
act.name = act.type + (dateRange ? ' ' + dateRange : '')
act
}
def list() {
def resp = webService.getJson(grailsApplication.config.ecodata.service.url + '/activity/')
// inject constructed name
resp.list.collect(constructName)
}
def assessments() {
def resp = webService.getJson(grailsApplication.config.ecodata.service.url + '/assessment/')
// inject constructed name
resp.list.collect(constructName)
}
def getProjectActivityCount(id){
webService.getJson(grailsApplication.config.ecodata.service.url + '/activity/countByProjectActivity/'+ id)
}
def getSitesWithDataForProjectActivity(id){
webService.getJson(grailsApplication.config.ecodata.service.url + '/activity/getDistinctSitesForProjectActivity/'+ id)
}
def getSitesWithDataForProject(id){
webService.getJson(grailsApplication.config.ecodata.service.url + '/activity/getDistinctSitesForProject/'+ id)
}
def get(id, version = null, userId = null, hideMemberOnlyFlds = false, includeSiteData = false) {
def params = '?hideMemberOnlyFlds=' + hideMemberOnlyFlds
if (version) {
params += '&version=' + version
}
if (userId) {
params += '&userId=' + userId
}
if (includeSiteData) {
params += '&view=site'
}
def activity = webService.getJson(grailsApplication.config.ecodata.service.url + '/activity/' + id + params)
activity
}
def activitiesForUser(userId, query){
def params = '?'+ query.collect { k,v -> "$k=$v" }.join('&')
webService.getJson(grailsApplication.config.ecodata.service.url + '/activity/listForUser/' + userId + params)
}
def activitiesForProject(id, query){
def params = '?'+ query.collect { k,v -> "$k=$v" }.join('&')
webService.getJson(grailsApplication.config.ecodata.service.url + '/activity/listByProject/' + id + params)
}
def create(activity) {
update('', activity)
}
def update(id, body) {
webService.doPost(grailsApplication.config.ecodata.service.url + '/activity/' + id, body)
}
def deleteByProjectActivity(id){
webService.doDelete(grailsApplication.config.ecodata.service.url + '/activity/deleteByProjectActivity/' + id)
}
def delete(id) {
webService.doDelete(grailsApplication.config.ecodata.service.url + '/activity/' + id)
}
def bulkDelete(List ids, boolean destroy = false) {
String url = grailsApplication.config.ecodata.service.url + '/activityBulkDelete'
if(destroy)
url += '?destroy=true'
webService.doPost(url, [ids: ids])
}
boolean batchedBulkDelete (List activityIds) {
boolean success = true
int offset = 0
while (offset < activityIds.size()) {
int end = offset + BATCH_SIZE
end = end <= activityIds.size() ? end : activityIds.size()
List ids = activityIds.subList(offset, end)
success &= bulkDelete(ids, true)?.resp?.details?.success
offset += BATCH_SIZE
}
success
}
def bulkEmbargo(List ids) {
if (ids) {
String params = "id=" + ids.join('&id=')
webService.doPost(grailsApplication.config.ecodata.service.url + "/activities/?${params}", [embargoed: true])
}
}
def batchedBulkEmbargo (List activityIds) {
int offset = 0
boolean success = true
while (offset < activityIds.size()) {
int end = offset + BATCH_SIZE
end = end <= activityIds.size() ? end : activityIds.size()
List ids = activityIds.subList(offset, end)
Map result = bulkEmbargo(ids)
if (result.resp) {
success &= true
} else {
success &= false
}
offset += BATCH_SIZE
}
success
}
def bulkRelease(List ids) {
if (ids) {
String params = "id=" + ids.join('&id=')
webService.doPost(grailsApplication.config.ecodata.service.url + "/activities/?${params}", [embargoed: false])
}
}
boolean batchedBulkRelease (List activityIds) {
int offset = 0
boolean success = true
while (offset < activityIds.size()) {
int end = offset + BATCH_SIZE
end = end <= activityIds.size() ? end : activityIds.size()
List ids = activityIds.subList(offset, end)
Map result = bulkRelease(ids)
if (result.resp) {
success &= true
} else {
success &= false
}
offset += BATCH_SIZE
}
success
}
def isUserOwnerForActivity(userId, id) {
webService.doGet(grailsApplication.config.ecodata.service.url + '/activity/isUserOwnerForActivity/'+id, [userId: userId])?.resp?.userIsOwner
}
/**
* Returns a detailed list of all activities associated with a project.
*
* Activities can be directly linked to a project, or more commonly, linked
* via a site that is associated with the project.
*
* Main output scores are also included. As is the meta-model for the activity.
*
* @param id of the project
*/
def activitiesForProject(String id) {
def list = webService.getJson(grailsApplication.config.ecodata.service.url + '/activitiesForProject/' + id)?.list
// inject the metadata model for each activity
list.each {
it.model = metadataService.getActivityModel(it.type)
}
list
}
def submitActivitiesForPublication(activityIds) {
updatePublicationStatus(activityIds, 'pendingApproval')
}
def approveActivitiesForPublication(activityIds) {
updatePublicationStatus(activityIds, 'published')
}
def rejectActivitiesForPublication(activityIds) {
updatePublicationStatus(activityIds, 'unpublished')
}
/**
* Updates the publicationStatus field of a set of Activities.
* @param activityIds a List of the activity ids. Identifies which activities to update.
* @param status the new value for the publicationStatus field.
*/
def updatePublicationStatus(activityIds, status) {
def ids = activityIds.collect{"id=${it}"}.join('&')
def body = ['publicationStatus':status]
webService.doPost(grailsApplication.config.ecodata.service.url + "/activities/?$ids", body)
}
def bulkUpdateActivities(activityIds, props) {
def ids = activityIds.collect{"id=${it}"}.join('&')
webService.doPost(grailsApplication.config.ecodata.service.url + "/activities/?$ids", props)
}
/** @see au.org.ala.ecodata.ActivityController for a description of the criteria required. */
def search(criteria) {
def modifiedCriteria = new HashMap(criteria?:[:])
// Convert dates to UTC format.
criteria.each { key, value ->
if (value instanceof Date) {
modifiedCriteria[key] = value.format(dateFormat, TimeZone.getTimeZone("UTC"))
}
}
webService.doPost(grailsApplication.config.ecodata.service.url+'/activity/search/', modifiedCriteria)
}
def isReport(activity) {
def model = metadataService.getActivityModel(activity.type)
return model.type == 'Report'
}
/**
* Creates a description for the supplied activity based on the activity type and dates.
*/
String defaultDescription(activity) {
def start = activity.plannedStartDate
def end = activity.plannedEndDate
DateTime startDate = DateUtils.parse(start)
DateTime endDate = DateUtils.parse(end).minusDays(1)
Period period = new Period(startDate.toLocalDate(), endDate.toLocalDate())
def description = DateTimeFormat.forPattern("MMM yyyy").print(endDate)
if (period.months > 1) {
description = DateTimeFormat.forPattern("MMM").print(startDate) + ' - ' + description
}
"${activity.type} (${description})"
}
/**
* Identifies species typed data in an output model and converts String values into species data via
* a species lookup operation. The values are changed inline in the supplied outputData Map.
*
* @param outputName the output for which the data has been recorded.
* @param listName (optional), if present identifies a list within the output model for which the data has been recorded
* @param outputData the output data to look for species data types.
* @return
*/
void lookupSpeciesInOutputData(String projectActivityId, String outputName, String listName, List outputData) {
def model = metadataService.annotatedOutputDataModel(outputName)
if (listName) {
model = model.find { it.name == listName }?.columns
}
// Do species lookup
def speciesField = model.find { it.dataType == 'species' }
Map singleSpecies = projectActivityService.getSingleSpecies(projectActivityId, outputName, speciesField.name)
if (speciesField) {
outputData.each { row ->
String name = row[speciesField.name]
if (singleSpecies.isSingle && (speciesField.validate == 'required' || row[speciesField.name])) {
row[speciesField.name] = [name: singleSpecies.name, listId: "not applicable", guid: singleSpecies.guid]
} else if (!singleSpecies.isSingle) {
Map speciesSearchResults = projectActivityService.searchSpecies(projectActivityId, name, 10, outputName, speciesField.name)
Map species = speciesService.findMatch(speciesSearchResults, name)
if (species) {
row[speciesField.name] = [name: species.name, listId: species.listId, guid: species.guid]
} else {
row[speciesField.name] = [name: name, listId: 'unmatched', guid: null]
}
}
}
}
}
/**
* Check whether sensitive species coordinates needs to be applied for the given projectId + userId
* Supports only point based coordinates.
*
* @param userId user identifier
* @param projectId project identifier.
* @param model model with activity and site object
*/
boolean applySensitiveSpeciesCoordinates(String userId, String projectId){
List projectIds = userId ? userService.getProjectsForUserId(userId)?.collect{it.project?.projectId} : []
boolean projectMember = projectIds && projectIds.find{it == projectId}
return !(userService.userIsAlaOrFcAdmin() || projectMember)
}
Map getDynamicFacets(){
webService.getJson(grailsApplication.config.ecodata.service.url+'/metadata/getIndicesForDataModels')
}
List getDefaultFacets(){
cacheService.get('default-facets-for-data', {
webService.getJson(grailsApplication.config.ecodata.service.url + '/activity/getDefaultFacets')
})
}
List getFacets(){
Map dynamicFacets = getDynamicFacets()?:[:]
List facets = dynamicFacets.collect{ [name: it.key] }
facets + getDefaultFacets()
}
/**
* Add dynamic facets to list of possible columns to choose when configuring data page columns.
* @return
*/
List getDynamicIndexNamesAsColumnConfig () {
cacheService.get("dynamic-index-names-as-column-config", {
Map facets = getDynamicFacets()
List result = []
facets?.each { name, value ->
Map config = [
type: "property",
propertyName: name,
displayName : "",
dataType: value[0]?.dataType
]
result.add(config)
}
result
})
}
/**
* Update output site when activity site changes. This is done to synchronize site used in activity and output.
* Works projects can choose a site for an activity on work schedule tab. In such a situation, this function is
* used to update output with the new site.
* @param newActivity
* @param oldActivity
*/
def updateOutputSite (Map newActivity, Map oldActivity, String mapDataType = 'geoMap') {
// Execute this logic only when activity siteId is updated on work schedule tab.
// Do not do this when activity is created or updated.
String activityId = oldActivity?.activityId
String type = oldActivity?.type
if( !newActivity.outputs && activityId && type && oldActivity?.outputs) {
if ( newActivity?.siteId && (newActivity?.siteId != oldActivity?.siteId) ) {
// get data model describing map data
Map outputModels = metadataService.getOutputNameAndDataModelForAnActivityName(type)
Map dataModel
String outputName
outputModels?.each { key, outputModel ->
if ( !dataModel ) {
outputName = key
dataModel = outputModel.dataModel?.find { it.dataType == mapDataType }
}
}
// update output with new site
if ( dataModel?.name ) {
String name = dataModel.name
Map output = oldActivity?.outputs?.find { it.name == outputName }
if (output) {
output.data[name] = newActivity.siteId
outputService.update(output.outputId, output)
}
}
}
}
}
def addAdditionalProperties (List additionalPropertyConfig, Map doc, Map result) {
additionalPropertyConfig?.each { Map config ->
def value = doc
List path = grailsApplication.config.activitypropertypath[config.propertyName] ?: [config.propertyName]
path?.each { String prop ->
if (value instanceof Map) {
value = value[prop]
} else if (value instanceof List) {
value = value?.collect {
it[prop]
}?.join(', ')
}
}
if (value != doc) {
result[config.propertyName] = utilService.getDisplayNameForValue(config.propertyName, value)
}
}
result
}
def convertExcelToOutputData(String id, String type, def file){
def result = webService.postMultipart(grailsApplication.config.ecodata.service.url + "/metadata/extractOutputDataFromActivityExcelTemplate", [pActivityId: id, type: type], file, 'data', true)
if (result.error) {
return result.details
}
else {
return result.content?.subMap('data') ?: result
}
}
}