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

Last change on this file since 392 was 392, checked in by keesvb, 10 years ago

switched back from the fancy 'integrate domain fields as system fields directly into the Template object at the time of creation' method to the plain 'implement getFieldValue, setFieldValue and giveFields to work for both domain and template fields'. NB: this might affect Events, since until now startTime and endTime were moved directly into template! Switch any Event.template.fields to Event.giveFields()

  • Property svn:keywords set to Date Author Rev
File size: 3.2 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: 392 $
12 * $Author: keesvb $
13 * $Date: 2010-05-04 15:03:21 +0000 (di, 04 mei 2010) $
14 */
15class Event extends TemplateEntity implements Serializable {
16
17        Date startTime
18        Date endTime
19       
20        static constraints = {
21                endTime(validator: {val, obj ->
22           if (val && val.before(obj.startTime)) {
23                return 'endTimeshouldbegreater'
24           }
25        })
26        }
27       
28
29        List<TemplateField> giveDomainFields() {
30                [ new TemplateField( 
31                            name: 'startTime',
32                            type: TemplateFieldType.DATE),
33                        new TemplateField( 
34                            name: 'endTime',
35                            type: TemplateFieldType.DATE) ];
36        }
37
38        // time diff between end and start date
39        // thus, do this manually as follows
40
41        static def getDuration(date1, date2) {
42                def timeMillis = (date2.getTime() - date1.getTime()).abs()
43                def days = (timeMillis / (1000 * 60 * 60 * 24)).toInteger()
44                def hours = (timeMillis / (1000 * 60 * 60)).toInteger()
45                def minutes = (timeMillis / (1000 * 60)).toInteger()
46                def seconds = (timeMillis / 1000).toInteger()
47                def millis = (timeMillis % 1000).toInteger()
48
49                return new Duration(days, hours, minutes, seconds, millis)
50        }
51
52        def getDuration() {
53                return getDuration(startTime, endTime)
54        }
55
56        // return a string that prints the duration sensibly.
57        // the largest date unit (sec, min, h, day, week, month, or year)
58        // is output
59
60        static def getPrettyDuration(duration) {
61                def handleNumerus = {number, string ->
62                        return number.toString() + (number == 1 ? string : string + 's')
63                }
64                if (duration.getYears() > 0) return handleNumerus(duration.getYears(), " year")
65                if (duration.getMonths() > 0) return handleNumerus(duration.getMonths(), " month")
66                if (duration.getDays() > 0) return handleNumerus(duration.getDays(), " day")
67                if (duration.getHours() > 0) return handleNumerus(duration.getHours(), " hour")
68                if (duration.getMinutes() > 0) return handleNumerus(duration.getMinutes(), " minute")
69                return handleNumerus(duration.getSeconds(), " second")
70        }
71
72        // convenience method. gives formatted string output for a duration
73        def getPrettyDuration() {
74                getPrettyDuration(getDuration())
75        }
76
77        // convenience method. gives formatted string output for a duration
78        static def getPrettyDuration(date1, date2) {
79                return getPrettyDuration(getDuration(date1, date2))
80        }
81
82        def getDurationString() {
83                def d = getDuration()
84                return "${d.days} days, ${d.hours} hrs, ${d.minutes} min, ${d.seconds} sec."
85        }
86
87        def getShortDuration() {
88                def d = getDuration()
89                def days = d.days
90                def hours = d.hours - (24 * days)
91                def minutes = d.minutes - (24 * 60 * days) - (60 * hours)
92                return "${days}d ${hours}:${minutes}"
93        }
94
95        def isSamplingEvent() {
96                return (this instanceof SamplingEvent)
97        }
98
99        def String toString() {
100                return fieldExists( 'Description' ) ? getFieldValue( 'Description') : ""
101        }
102
103}
Note: See TracBrowser for help on using the repository browser.