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

Last change on this file since 386 was 386, checked in by roberth, 9 years ago

Updated the template entities to be able to return domain fields, template fields and both.
Rolled back the change of Kees in the Event object, so the startTime and endTime fields returned.
Also updated the studies list with a new layout.

  • Property svn:keywords set to Date Author Rev
File size: 3.3 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: 386 $
12 * $Author: roberth $
13 * $Date: 2010-04-27 13:53:06 +0000 (di, 27 apr 2010) $
14 */
15class Event extends TemplateEntity implements Serializable {
16
17        Date startTime
18        Date endTime
19       
20        static constraints = {
21                startTime(nullable: true)
22                endTime(nullable: true)
23                endTime(validator: {val, obj ->
24           if (val && val.before(obj.startTime)) {
25                return 'endTimeshouldbegreater'
26           }
27        })
28        }
29       
30
31        List<DomainTemplateField> giveDomainFields() {
32                [ new DomainTemplateField( 
33                            name: 'startDate',
34                            type: TemplateFieldType.DATE),
35                        new DomainTemplateField( 
36                            name: 'endDate',
37                            type: TemplateFieldType.DATE) ];
38        }
39
40        // time diff between end and start date
41        // thus, do this manually as follows
42
43        static 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        static 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        static 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 fieldExists( 'Description' ) ? getFieldValue( 'Description') : ""
103        }
104
105}
Note: See TracBrowser for help on using the repository browser.