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

Last change on this file since 375 was 375, checked in by keesvb, 12 years ago

added giveDomainFields to TemplateEntity? entities, fixed some problems in BootStrap?

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