source: trunk/grails-app/controllers/dbnp/studycapturing/EventController.groovy @ 123

Last change on this file since 123 was 123, checked in by jahn, 14 years ago

The Term class has been seperately created in dbnp.studycapturing.data and dbnp.studycapturing.data. Since there is no big difference, I retracted the later domain class. This requried adjustment of some imports in the controllers.

File size: 8.7 KB
Line 
1package dbnp.studycapturing
2import java.text.SimpleDateFormat
3import dbnp.data.Term
4
5class EventController {
6
7    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
8
9    def index = {
10        redirect(action: "list", params: params)
11    }
12
13    def list = {
14        params.max = Math.min(params.max ? params.int('max') : 10, 100)
15        [eventInstanceList: Event.list(params), eventInstanceTotal: Event.count()]
16    }
17
18    def create = {
19        def eventInstance = new Event()
20        eventInstance.properties = params
21        //chain( view: "createForEventDescription", params:params )
22        return [eventInstance:eventInstance]
23    }
24
25
26    def createForEventDescription = {
27
28        if( params["id"]==null)
29        {
30            def eventInstance = new Event()
31            def sDate = new Date( params["startTime"])
32            def eDate = new Date( params["endTime"])
33            def description = EventDescription.findById((params["eventDescription"])["id"])
34            return [testo:params.clone(), sDate:sDate, eDate:eDate, description:description ]
35        }
36        else
37        {
38            def eventInstance = Event.get(params.id)
39            if (!eventInstance) {
40                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'event.label', default: 'Event'), params.id])}"
41                redirect(action: "list")
42            }
43            return [testo:params.clone(), sDate:eventInstance.startTime, eDate:eventInstance.endTime, description:eventInstance.eventDescription]
44        }
45    }
46
47
48    // Convert date strings to date strings grails can deal with.
49    // Input format:  "01/20/2010 08:45 am"
50    // Output format: "01/20/2010 20:45"
51    // Note: the "am" amd "pm" suffixes are removed.
52
53    def parseDate = {  st ->
54            def subst = st.substring(0,16)
55            def ampm =  st.substring(17,19)
56            if(ampm=="pm")
57            {
58                 def hours=st.substring(11,13)
59                 hours = hours.toInteger() + 12
60                 st = st.substring(0,11) + hours + st.substring(13,16)
61            }
62            else { st = st.substring(0,16) }
63
64            def sdfh = new SimpleDateFormat("MM/DD/yyyy hh:mm")
65            return sdfh.parse(st)
66    }
67
68
69    def save = {
70        println "In EventController.save: ${params}"
71
72        params["startTime"] = parseDate(params["startTime"])     // parse the date strings
73        params["endTime"] = parseDate(params["endTime"])
74
75        def eventInstance = new Event(params)
76
77        println params.protocolInstance
78                                                                  // If a protocol instnace already exists,
79                                                                  // update this event's parameter values.
80        if(params.protocolInstance != null) {
81            params.protocolInstance.each { id, value ->
82                    println id + " " + value
83                    def parameter = ProtocolParameterInstance.get(id)
84                    parameter.value=value
85                    parameter.save()
86                }
87        }
88
89        if (eventInstance.save(flush: true)) {
90            flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), eventInstance.id])}"
91            redirect(action: "show", id: eventInstance.id)
92        }
93        else {
94            render(view: "create", model: [eventInstance: eventInstance])
95        }
96        render(view: "create", model: [eventInstance: eventInstance])
97    }
98
99
100    def show = {
101        def eventInstance = Event.get(params.id)
102        if (!eventInstance) {
103            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'event.label', default: 'Event'), params.id])}"
104            redirect(action: "list")
105        }
106        else {
107            [eventInstance: eventInstance]
108        }
109    }
110
111
112
113
114
115    def partial = {
116        println "In action: partial"
117        println params
118        def eventDescription = EventDescription.get(params.id)
119        if (!eventDescription) {
120            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'event.label', default: 'Event'), params.id])}"
121            redirect(action: "list")
122        }
123        else {
124            [eventDescription: event]
125        }
126        redirect(view: 'partial')
127    }
128
129
130
131
132
133    def edit = {
134
135        if( params["id"]==null)
136        {
137            def eventInstance = new Event()
138            def sDate = new Date( params["startTime"])
139            def eDate = new Date( params["endTime"])
140            def description = EventDescription.findById((params["eventDescription"])["id"])
141            return [testo:params.clone(), sDate:sDate, eDate:eDate, description:description ]
142        }
143
144        else
145        {
146            def eventInstance = Event.get(params.id)
147            if (!eventInstance) {
148                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'event.label', default: 'Event'), params.id])}"
149                redirect(action: "list")
150            }
151            return [testo:params.clone(), sDate:eventInstance.startTime, eDate:eventInstance.endTime, description:eventInstance.eventDescription]
152        }
153
154    }
155
156
157
158
159
160    def update = {
161        def eventInstance = Event.get(params.id)
162        if (eventInstance) {
163            if (params.version) {
164                def version = params.version.toLong()
165                if (eventInstance.version > version) {
166                   
167                    eventInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'event.label', default: 'Event')] as Object[], "Another user has updated this Event while you were editing")
168                    render(view: "edit", model: [eventInstance: eventInstance])
169                    return
170                }
171            }
172            eventInstance.properties = params
173            if (!eventInstance.hasErrors() && eventInstance.save(flush: true)) {
174                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'event.label', default: 'Event'), eventInstance.id])}"
175                redirect(action: "show", id: eventInstance.id)
176            }
177            else {
178                render(view: "edit", model: [eventInstance: eventInstance])
179            }
180        }
181        else {
182            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'event.label', default: 'Event'), params.id])}"
183            redirect(action: "list")
184        }
185    }
186
187    def delete = {
188        def eventInstance = Event.get(params.id)
189        if (eventInstance) {
190            try {
191                eventInstance.delete(flush: true)
192                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'event.label', default: 'Event'), params.id])}"
193                redirect(action: "list")
194            }
195            catch (org.springframework.dao.DataIntegrityViolationException e) {
196                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'event.label', default: 'Event'), params.id])}"
197                redirect(action: "show", id: params.id)
198            }
199        }
200        else {
201            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'event.label', default: 'Event'), params.id])}"
202            redirect(action: "list")
203        }
204    }
205
206
207
208    def createDummies = {
209
210         ["ONE","TWO"].each{ x -> println x }
211         def t = new Term()
212         t.name =  "homo politicus russicus"
213         t.ontology = "Ontology: Monkies and Typewriters"
214         t.accession = "up or down"
215         t.save()
216
217         def pp = new ProtocolParameter()
218         pp.name = "LSD reatment"
219         pp.unit = "Tt"
220         pp.description = "feed the LSD to the subject"
221         pp.reference = t
222         pp.type = "String"     // should become ProtocolParameterType at some point
223         pp.save()
224
225         def p = new Protocol()
226         p.name = "Hugo (dummy #1)"
227         p.reference = t
228         p.save()
229         p.addToParameters(pp)
230
231         def ppi = new ProtocolParameterInstance()
232         ppi.value = "1.2"
233         ppi.protocolParameter = pp
234         ppi.save()
235
236         def ppi2 = new ProtocolParameterInstance()
237         ppi2.value = "23.5"
238         ppi2.protocolParameter = pp
239         ppi2.save()
240
241         def pi = new ProtocolInstance()
242         pi.protocol = p.find("from Protocol p ")
243         pi.save()
244         pi.addToValues(ppi)
245         pi.addToValues(ppi2)
246
247         def s= new Subject()
248         s.name = "Vladimir Putin"
249         s.species = t
250         s.save()
251
252         def ed = new EventDescription()
253         ed.name = "dummmy name"
254         ed.description = "dummmy description"
255         ed.protocol = pi
256         ed.classification = Term.find("from Term t")
257         ed.save()
258
259         def sdfh = new SimpleDateFormat("dd/MM/yyyy hh:mm")
260         def eventInstance = new Event()
261         def someDate = sdfh.parse("29/11/2008 18:00")
262         eventInstance.subject = s
263         eventInstance.eventDescription= ed
264         eventInstance.startTime = someDate
265         eventInstance.endTime = someDate
266         if( eventInstance.save() )
267         { redirect( action:show, id: eventInstance.id ) }
268         else { chain( action:list ) }
269
270
271
272    }
273}
Note: See TracBrowser for help on using the repository browser.