root/trunk/grails-app/domain/dbnp/studycapturing/Event.groovy @ 209

Revision 209, 3.2 KB (checked in by duh, 4 years ago)

- added events, eventDescriptions, etc

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