1
1
package org.mindinformatics.ann.framework.module.persistence
2
2
3
3
import grails.converters.JSON
4
+ import org.codehaus.groovy.grails.web.json.JSONObject
4
5
5
6
class AnnotatorController {
6
7
7
- static allowedMethods = [ create : ' POST ' , destroy : ' DELETE ' , update : ' PUT ' , search : ' GET ' , read : ' GET ' ]
8
+ def annotatorService
8
9
9
- def index () {}
10
+ static allowedMethods = [ create : ' POST ' , destroy : ' DELETE ' , update : ' PUT ' , search : ' GET ' , read : ' GET ' ]
10
11
12
+ /**
13
+ *
14
+ * @return
15
+ */
16
+ def index () {
17
+ println " Index " + params
18
+ def apiResponse = [
19
+ " name" : " Annotator Store API" ,
20
+ " version" : getGrailsApplication(). metadata[" app.version" ]]
21
+ render (apiResponse as JSON )
22
+ }
11
23
24
+ /**
25
+ *
26
+ * https://github.com/okfn/annotator/wiki/Authentication
27
+ * @return
28
+ */
12
29
def token () {
13
- println " Get token: " + params
14
- def jsonObject = request. JSON
15
- println " JSON " + jsonObject. toString()
16
-
17
- render(status : 200 , text : " your-token-has-been-created" )
30
+ // println "Get token: " + params
31
+ // def jsonObject = request.JSON
32
+ // println "JSON " + jsonObject.toString()
33
+ // response.status = 200
34
+ render(status : 200 , text : annotatorService. getToken())
35
+ // render(status: 200, text: "your-token-has-been-created")
18
36
// render(status: 503, text: 'Failed to create annotation' + annotation.errors)
19
37
}
20
38
21
39
def read () {
22
- println " read annotation " + params
40
+ println " Read " + params
23
41
def jsonObject = request. JSON
42
+ println " JSON " + jsonObject. toString()
43
+
44
+ def annotation = Annotation . get(params. id)
45
+ if (annotation) {
46
+ render annotation. toJSONObject() as JSON
47
+ }
48
+ else {
49
+ def message = " Unable to locate annotation with ID ${ params.id} "
50
+ render([status : 503 , text : message] as JSON )
51
+
52
+ }
24
53
25
54
}
26
55
27
56
def create () {
57
+ println " Create " + params
58
+ println " JSON = " + request. JSON
59
+ def annotation = annotatorService. create(request. JSON )
60
+
61
+ // Need to find a way to handle errors
62
+ if (! annotation. hasErrors()) {
63
+ println " Saved annotation " + annotation. toJSONObject()
64
+ // render annotation.toJSONObject() as JSON
65
+ redirect(action : " read" , id : annotation. id)
66
+ }
67
+ else {
68
+ println " Annotation has errors " + annotation. errors
69
+ render([status : 503 , text : ' Unable to create annotation' , errors : annotation. errors]as JSON )
70
+ }
71
+
72
+
73
+ /*
28
74
println "create annotation " + params
29
75
def jsonObject = request.JSON
30
76
println "JSON = " + jsonObject
@@ -39,29 +85,28 @@ class AnnotatorController {
39
85
}
40
86
else {
41
87
render([status: 503, text: 'Unable to create annotation', errors: annotation.errors]as JSON)
42
- }
88
+ }*/
43
89
}
44
90
45
91
46
-
47
92
def update () {
48
- println " update annotation " + params
93
+ println " Update " + params
49
94
def jsonObject = request. JSON
50
95
println " JSON " + jsonObject. toString()
51
96
97
+ def annotation = annotatorService. update(jsonObject)
98
+ // def annotation = Annotation.get(params.id)
99
+ // annotation.text = jsonObject.text
100
+ // annotation.quote = jsonObject.quote
101
+ // annotation.json = jsonObject.toString()
102
+ // annotation.save()
52
103
53
- def annotation = Annotation . get(params. id)
54
- annotation. text = jsonObject. text
55
- annotation. quote = jsonObject. quote
56
- annotation. json = jsonObject
57
- annotation. save()
58
-
59
- render annotation as JSON
60
-
104
+ // render annotation.toJSONObject() as JSON
105
+ redirect(action : " read" , id : annotation. id)
61
106
}
62
107
63
108
def destroy () {
64
- println " destroy annotation " + params
109
+ println " Destroy " + params
65
110
def jsonObject = request. JSON
66
111
println " JSON " + jsonObject. toString()
67
112
@@ -70,19 +115,27 @@ class AnnotatorController {
70
115
annotation. delete()
71
116
72
117
// Return the list of remaining annotations for this page
73
- def annotations = Annotation . findAllByUri(jsonObject. uri)
74
- println " annotations: " + annotations. size()
75
- render ([total : annotations. size, rows : annotations] as JSON )
118
+ // def annotations = Annotation.findAllByUri(jsonObject.uri)
119
+ // println "annotations: " + annotations.size()
120
+ // render ([total: annotations.size, rows: annotations] as JSON)
121
+ response. status = 204
122
+ }
76
123
124
+ def list () {
125
+ println " List " + params
126
+ def results = Annotation . list(). collect { it. toJSONObject() }
127
+ render (results as JSON )
77
128
}
78
129
130
+ def annotations () {
131
+ redirect(action : " list" )
132
+ }
133
+
134
+
79
135
def search () {
80
- println " search annotations " + params
136
+ println " Search annotations " + params
81
137
def jsonObject = request. JSON
82
- println " JSON = " + jsonObject. toString()
83
-
84
- def annotations = Annotation . findAllByUri(params. uri)
85
- println " annotations: " + annotations. size()
86
- render ([total : annotations. size, rows : annotations] as JSON )
138
+ def results = annotatorService. search(params. uri)
139
+ render ([total : results. size(), rows : results] as JSON )
87
140
}
88
141
}
0 commit comments