source: trunk/grails-app/domain/dbnp/studycapturing/Event.groovy @ 370

Last change on this file since 370 was 370, checked in by roberth, 11 years ago

Updated studies list to handle the new data model.
Updated the bootstrap to contain some studies with attached events and persons in order to test the updated studies list
Added fieldExists method to TemplateEntity? to check whether a given field exists or not

  • Property svn:keywords set to Date Author Rev
File size: 2.9 KB
Line 
1package dbnp.studycapturing
2
3import groovy.time.*
4
5/**
6 * The Event class describes an actual event, as it has happened to a certain subject. Often, the same event occurs
7 * to multiple subjects at the same time. That is why the actual description of the event is factored out into a more
8 * general EventDescription class. Events that also lead to sample(s) should be instantiated as SamplingEvents.
9 *
10 * Revision information:
11 * $Rev: 370 $
12 * $Author: roberth $
13 * $Date: 2010-04-22 07:31:50 +0000 (do, 22 apr 2010) $
14 */
15class Event extends TemplateEntity implements Serializable {
16        Date startTime
17        Date endTime
18
19        static constraints = {
20                startTime(nullable:false)
21                endTime(validator: {val, obj ->
22           if (val && val.before(obj.startTime)) {
23                return 'endTimeshouldbegreater'
24           }
25        })
26        }
27
28        // time diff between end and start date
29        // thus, do this manually as follows
30
31        def getDuration(date1, date2) {
32                def timeMillis = (date2.getTime() - date1.getTime()).abs()
33                def days = (timeMillis / (1000 * 60 * 60 * 24)).toInteger()
34                def hours = (timeMillis / (1000 * 60 * 60)).toInteger()
35                def minutes = (timeMillis / (1000 * 60)).toInteger()
36                def seconds = (timeMillis / 1000).toInteger()
37                def millis = (timeMillis % 1000).toInteger()
38
39                return new Duration(days, hours, minutes, seconds, millis)
40        }
41
42        def getDuration() {
43                return getDuration(startTime, endTime)
44        }
45
46        // return a string that prints the duration sensibly.
47        // the largest date unit (sec, min, h, day, week, month, or year)
48        // is output
49
50        def getPrettyDuration(duration) {
51                def handleNumerus = {number, string ->
52                        return number.toString() + (number == 1 ? string : string + 's')
53                }
54                if (duration.getYears() > 0) return handleNumerus(duration.getYears(), " year")
55                if (duration.getMonths() > 0) return handleNumerus(duration.getMonths(), " month")
56                if (duration.getDays() > 0) return handleNumerus(duration.getDays(), " day")
57                if (duration.getHours() > 0) return handleNumerus(duration.getHours(), " hour")
58                if (duration.getMinutes() > 0) return handleNumerus(duration.getMinutes(), " minute")
59                return handleNumerus(duration.getSeconds(), " second")
60        }
61
62        // convenience method. gives formatted string output for a duration
63        def getPrettyDuration() {
64                getPrettyDuration(getDuration())
65        }
66
67        // convenience method. gives formatted string output for a duration
68        def getPrettyDuration(date1, date2) {
69                return getPrettyDuration(getDuration(date1, date2))
70        }
71
72        def getDurationString() {
73                def d = getDuration()
74                return "${d.days} days, ${d.hours} hrs, ${d.minutes} min, ${d.seconds} sec."
75        }
76
77        def getShortDuration() {
78                def d = getDuration()
79                def days = d.days
80                def hours = d.hours - (24 * days)
81                def minutes = d.minutes - (24 * 60 * days) - (60 * hours)
82                return "${days}d ${hours}:${minutes}"
83        }
84
85        def isSamplingEvent() {
86                return (this instanceof SamplingEvent)
87        }
88
89        def String toString() {
90                return fieldExists( 'Description' ) ? getFieldValue( 'Description') : ""
91        }
92
93}
Note: See TracBrowser for help on using the repository browser.