@@ -12,7 +12,9 @@ import au.org.ala.biocollect.merit.UserService
12
12
import au.org.ala.biocollect.sightings.BieService
13
13
import grails.converters.JSON
14
14
import groovyx.net.http.ContentType
15
+ import org.apache.commons.io.FilenameUtils
15
16
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap
17
+ import org.springframework.web.multipart.MultipartFile
16
18
17
19
import static org.apache.http.HttpStatus.*
18
20
import org.codehaus.groovy.grails.web.json.JSONArray
@@ -96,7 +98,7 @@ class BioActivityController {
96
98
if (photoPoints && activityId) {
97
99
updatePhotoPoints(activityId, photoPoints)
98
100
}
99
-
101
+
100
102
postBody. outputs?. each {
101
103
it. data?. multimedia?. each {
102
104
String filename
@@ -134,10 +136,10 @@ class BioActivityController {
134
136
}
135
137
}
136
138
137
- render result as JSON
139
+ render result as JSON
138
140
}
139
141
140
- def getProjectActivityCount (String id ){
142
+ def getProjectActivityCount (String id ) {
141
143
def result = activityService. getProjectActivityCount(id)
142
144
render result as JSON
143
145
}
@@ -245,15 +247,15 @@ class BioActivityController {
245
247
if (! userId) {
246
248
response. status = 401
247
249
result = [status : 401 , error : " Access denied: User has not been authenticated." ]
248
- } else if (projectService. isUserAdminForProject(userId, activity?. projectId) || activityService. isUserOwnerForActivity(userId, activity?. activityId)) {
250
+ } else if (projectService. isUserAdminForProject(userId, activity?. projectId) || activityService. isUserOwnerForActivity(userId, activity?. activityId)) {
249
251
def resp = activityService. delete(id)
250
252
if (resp == SC_OK ) {
251
253
result = [status : resp, text : ' deleted' ]
252
254
} else {
253
255
response. status = resp
254
256
result = [status : resp, error : " Error deleting the survey, please try again later." ]
255
257
}
256
- } else {
258
+ } else {
257
259
response. status = 401
258
260
result = [status : 401 , error : " Access denied: User is not an admin or owner of this activity - ${ id} " ]
259
261
}
@@ -320,7 +322,7 @@ class BioActivityController {
320
322
def list () {
321
323
}
322
324
323
- def allRecords () {
325
+ def allRecords () {
324
326
render(view : ' list' , model : [view : ' allrecords' , user : userService. user])
325
327
}
326
328
@@ -341,8 +343,8 @@ class BioActivityController {
341
343
GrailsParameterMap queryParams = new GrailsParameterMap ([:], request)
342
344
Map parsed = commonService. parseParams(params)
343
345
parsed. userId = userService. getCurrentUserId()
344
- parsed. each{ key , value ->
345
- if (value != null && value){
346
+ parsed. each { key , value ->
347
+ if (value != null && value) {
346
348
queryParams. put(key, value)
347
349
}
348
350
}
@@ -358,9 +360,10 @@ class BioActivityController {
358
360
queryParams
359
361
}
360
362
361
- /*
362
- * Search project activities and records
363
- */
363
+ /*
364
+ * Search project activities and records
365
+ */
366
+
364
367
def searchProjectActivities () {
365
368
GrailsParameterMap queryParams = constructDefaultSearchParams(params)
366
369
@@ -405,12 +408,12 @@ class BioActivityController {
405
408
* map points are generated from this function. It requires some client side code to convert the output of this
406
409
* function to points.
407
410
*/
408
- def getProjectActivitiesRecordsForMapping (){
411
+ def getProjectActivitiesRecordsForMapping () {
409
412
GrailsParameterMap queryParams = new GrailsParameterMap ([:], request)
410
413
Map parsed = commonService. parseParams(params)
411
414
parsed. userId = userService. getCurrentUserId()
412
- parsed. each{ key , value ->
413
- if (value != null && value){
415
+ parsed. each { key , value ->
416
+ if (value != null && value) {
414
417
queryParams. put(key, value)
415
418
}
416
419
}
@@ -441,7 +444,7 @@ class BioActivityController {
441
444
coordinates : doc. coordinates
442
445
]
443
446
444
- if (doc. sites && doc. sites. size() > 0 ){
447
+ if (doc. sites && doc. sites. size() > 0 ) {
445
448
result. coordinates = doc. sites[0 ]?. extent?. geometry?. centre
446
449
}
447
450
@@ -451,7 +454,7 @@ class BioActivityController {
451
454
render([activities : activities, total : searchResult. hits?. total ?: 0 ] as JSON )
452
455
}
453
456
454
- def ajaxListForProject (String id ){
457
+ def ajaxListForProject (String id ) {
455
458
456
459
def model = [:]
457
460
def query = [pageSize : params. max ?: 10 ,
@@ -518,7 +521,7 @@ class BioActivityController {
518
521
model. site = model. activity?. siteId ? siteService. get(model. activity. siteId, [view : ' brief' ]) : null
519
522
model. mapFeatures = model. site ? siteService. getMapFeatures(model. site) : []
520
523
model. project = projectId ? projectService. get(model. activity. projectId) : null
521
- model. projectSite = model. project. sites?. find{ it. siteId == model. project. projectSiteId}
524
+ model. projectSite = model. project. sites?. find { it. siteId == model. project. projectSiteId }
522
525
523
526
// Add the species lists that are relevant to this activity.
524
527
model. speciesLists = new JSONArray ()
@@ -561,9 +564,8 @@ class BioActivityController {
561
564
562
565
if (data && ! data. error) {
563
566
activityService. lookupSpeciesInOutputData(params. pActivityId, params. type, params. listName, data. data)
564
- result = [status :SC_OK , data :data. data]
565
- }
566
- else {
567
+ result = [status : SC_OK , data : data. data]
568
+ } else {
567
569
result = data
568
570
}
569
571
@@ -573,10 +575,9 @@ class BioActivityController {
573
575
def resultJson = result as JSON
574
576
render resultJson. toString()
575
577
}
576
- }
577
- else {
578
+ } else {
578
579
response. status = SC_BAD_REQUEST
579
- result = [status : SC_BAD_REQUEST , error :' No file attachment found' ]
580
+ result = [status : SC_BAD_REQUEST , error : ' No file attachment found' ]
580
581
// This is returned to the browswer as a text response due to workaround the warning
581
582
// displayed by IE8/9 when JSON is returned from an iframe submit.
582
583
@@ -586,6 +587,40 @@ class BioActivityController {
586
587
}
587
588
}
588
589
590
+ def uploadFile () {
591
+ String stagingDirPath = grailsApplication. config. upload. path
592
+ Map result = [:]
593
+ if (request. respondsTo(' getFile' )) {
594
+ MultipartFile multipartFile = request. getFile(' files' )
595
+
596
+ if (multipartFile?. size) { // will only have size if a file was selected
597
+ String filename = multipartFile. getOriginalFilename(). replaceAll(' ' , ' _' )
598
+ String ext = FilenameUtils . getExtension(filename)
599
+ filename = FileUtils . nextUniqueFileName(FilenameUtils . getBaseName(filename) + ' .' + ext, stagingDirPath)
600
+
601
+ File stagingDir = new File (stagingDirPath)
602
+ stagingDir. mkdirs()
603
+ File file = new File (FileUtils . fullPath(filename, stagingDirPath))
604
+ multipartFile. transferTo(file)
605
+
606
+ Map metadata = [
607
+ name : filename,
608
+ size : multipartFile. size,
609
+ contentType : multipartFile. contentType,
610
+ url : FileUtils . encodeUrl(grailsApplication. config. grails. serverURL + " /download/file?filename=" , filename),
611
+ attribution : ' ' ,
612
+ notes : ' ' ,
613
+ status : " active"
614
+ ]
615
+ result = [files : [metadata]]
616
+ }
617
+ }
618
+
619
+ response. addHeader(' Content-Type' , ' text/plain' )
620
+ def resultJson = result as JSON
621
+ render resultJson. toString()
622
+ }
623
+
589
624
private static boolean isProjectActivityClosed (Map projectActivity ) {
590
625
projectActivity?. endDate && Date . parse(" yyyy-MM-dd" , projectActivity?. endDate)?. before(new Date ())
591
626
}
0 commit comments