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

Revision 197, 2.7 KB (checked in by jahn, 4 years ago)

Modified convenience function for displaying date differences to provide formatting also for dates that do not belog to the Event class.

  • 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 */
10class Event {
11
12        Subject subject
13        EventDescription eventDescription
14        Date startTime
15        Date endTime
16        Map parameterStringValues
17        Map parameterIntegerValues
18        Map parameterFloatValues
19
20        static hasMany = [
21                parameterStringValues : String, // stores both STRING and STRINGLIST items (latter should be checked against the list)
22                parameterIntegerValues : int,
23                parameterFloatValues : float
24        ]
25
26
27        // time diff between end and start date
28        // thus, do this manually as follows
29        def getDuration( date1, date2 ) {
30                def timeMillis = (date2.getTime() - date1.getTime()).abs()
31                def days = (timeMillis / (1000 * 60 * 60 * 24)).toInteger()
32                def hours = (timeMillis / (1000 * 60 * 60)).toInteger()
33                def minutes = (timeMillis / (1000 * 60)).toInteger()
34                def seconds = (timeMillis / 1000).toInteger()
35                def millis = (timeMillis % 1000).toInteger()
36
37                return new Duration(days, hours, minutes, seconds, millis)
38        }
39
40
41        def getDuration() {
42                return getDuration(startTime, endTime)
43        }
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        def getPrettyDuration( duration ) {
50             def handleNumerus = { number ,string ->
51                     return number.toString() + (number==1 ? string : string + 's' )
52             }
53             if( duration.getYears()   > 0 ) return handleNumerus( duration.getYears(), " year" )
54             if( duration.getMonths()  > 0 ) return handleNumerus( duration.getMonths(), " month" )
55             if( duration.getDays()    > 0 ) return handleNumerus( duration.getDays(), " day" )
56             if( duration.getHours()   > 0 ) return handleNumerus( duration.getHours(), " hour" )
57             if( duration.getMinutes() > 0 ) return handleNumerus( duration.getMinutes(), " minute" )
58                                             return handleNumerus( duration.getSeconds(), " second" )
59        }
60
61
62        // convenience method. gives formatted string output for a duration
63        def getPrettyDuration() {
64             getPrettyDuration( getDuration() )
65        }
66
67
68        // convenience method. gives formatted string output for a duration
69        def getPrettyDuration( date1, date2 )
70        {
71             return  getPrettyDuration( getDuration( date1, date2 ) )
72        }
73
74
75        def getDurationString() {
76                def d = getDuration()
77                return "${d.days} days, ${d.hours} hrs, ${d.minutes} min, ${d.seconds} sec."
78        }
79
80
81        def isSamplingEvent() {
82                return ( this instanceof SamplingEvent )
83        }
84
85}
Note: See TracBrowser for help on using the browser.