1
1
package au.org.ala.alerts
2
2
3
- import au.org.ala.web.AlaSecured
4
3
import grails.converters.JSON
5
4
import org.apache.http.HttpStatus
6
5
@@ -22,66 +21,51 @@ class NotificationController {
22
21
}
23
22
24
23
def addMyAlert = {
25
- def user
26
- if (authService. userInRole(" ROLE_ADMIN" )) {
27
- user = userService. getUserById(params. userId)
28
- } else {
29
- user = userService. getUser()
30
- }
24
+ def user = getUser()
31
25
32
26
if (! user) {
33
27
response. status = HttpStatus . SC_NOT_FOUND
34
28
response. sendError(HttpStatus . SC_NOT_FOUND , " Unrecognised user" )
35
29
} else {
36
- log. debug(' add my alert ' + params. id)
37
- def notificationInstance = new Notification ()
38
- notificationInstance. query = Query . findById(params. id)
39
- notificationInstance. user = user
40
- // does this already exist?
41
- def exists = Notification . findByQueryAndUser(notificationInstance. query, notificationInstance. user)
42
- if (! exists) {
43
- log. info(" Adding alert for user: " + notificationInstance. user + " , query id: " + params. id)
44
- notificationInstance. save(flush : true )
45
- } else {
46
- log. info(" NOT Adding alert for user: " + notificationInstance. user + " , query id: " + params. id + " , already exists..." )
47
- }
30
+ notificationService. addAlertForUser(user, Long . valueOf(params. id))
48
31
return null
49
32
}
50
33
}
51
34
52
35
def deleteMyAlert = {
53
- def user
54
- if (authService. userInRole(" ROLE_ADMIN" )) {
55
- user = userService. getUserById(params. userId)
56
- } else {
57
- user = userService. getUser()
58
- }
36
+ def user = getUser()
59
37
60
38
if (! user) {
61
39
response. status = HttpStatus . SC_NOT_FOUND
62
40
response. sendError(HttpStatus . SC_NOT_FOUND , " Unrecognised user" )
63
41
} else {
64
- def query = Query . get(params. id)
65
- log. debug(' Deleting my alert : ' + params. id + ' for user : ' + authService. getDisplayName())
66
-
67
- def notificationInstance = Notification . findByUserAndQuery(user, query)
68
- if (notificationInstance) {
69
- log. debug(' Deleting my notification : ' + params. id)
70
- notificationInstance. each { it. delete(flush : true ) }
71
- } else {
72
- log. error(' *** Unable to find my notification - no delete : ' + params. id)
73
- }
74
- return null
42
+ notificationService. deleteAlertForUser(user, Long . valueOf(params. id))
75
43
}
76
44
}
77
45
78
- def deleteMyAlertWR = {
79
- def user
80
- if (authService. userInRole(" ROLE_ADMIN" )) {
81
- user = userService. getUserById(params. userId)
82
- } else {
83
- user = userService. getUser()
46
+ def addMyAnnotation = {
47
+ def user = getUser()
48
+ try {
49
+ notificationService. addMyAnnotation(user)
50
+ render ([success : true ] as JSON )
51
+ } catch (ignored) {
52
+ response. sendError(HttpStatus . SC_INTERNAL_SERVER_ERROR , " failed to add 'my annotation' alert for user " + user?. getUserId())
53
+ }
54
+
55
+ }
56
+
57
+ def deleteMyAnnotation = {
58
+ def user = getUser()
59
+ try {
60
+ notificationService. deleteMyAnnotation(user)
61
+ render ([success : true ] as JSON )
62
+ } catch (ignored) {
63
+ response. sendError(HttpStatus . SC_INTERNAL_SERVER_ERROR , " failed to delete 'my annotation' alert for user " + user?. getUserId())
84
64
}
65
+ }
66
+
67
+ def deleteMyAlertWR = {
68
+ def user = getUser()
85
69
86
70
// this is a hack to get around a CAS issue
87
71
if (user == null ) {
@@ -101,6 +85,14 @@ class NotificationController {
101
85
redirect(action : ' myAlerts' )
102
86
}
103
87
88
+ private User getUser () {
89
+ if (authService. userInRole(" ROLE_ADMIN" )) {
90
+ return userService. getUserById(params. userId)
91
+ } else {
92
+ return userService. getUser()
93
+ }
94
+ }
95
+
104
96
def changeFrequency = {
105
97
def user = userService. getUser()
106
98
log. debug(" Changing frequency to: " + params. frequency + " for user ${ user} " )
0 commit comments