1 | package dbnp.studycapturing |
---|
2 | |
---|
3 | import 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: 389 $ |
---|
12 | * $Author: duh $ |
---|
13 | * $Date: 2010-04-28 14:28:39 +0000 (wo, 28 apr 2010) $ |
---|
14 | */ |
---|
15 | class Event extends TemplateEntity implements Serializable { |
---|
16 | |
---|
17 | static constraints = { |
---|
18 | startTime(nullable: true) |
---|
19 | endTime(nullable: true) |
---|
20 | endTime(validator: {val, obj -> |
---|
21 | if (val && val.before(obj.startTime)) { |
---|
22 | return 'endTimeshouldbegreater' |
---|
23 | } |
---|
24 | }) |
---|
25 | } |
---|
26 | |
---|
27 | Date getStartTime() { |
---|
28 | getFieldValue('Start time') |
---|
29 | } |
---|
30 | |
---|
31 | def setStartTime(Date value) { |
---|
32 | if (value != null) { |
---|
33 | setFieldValue('Start time',value) |
---|
34 | } |
---|
35 | return this |
---|
36 | } |
---|
37 | |
---|
38 | Date getEndTime() { |
---|
39 | getFieldValue('End time') |
---|
40 | } |
---|
41 | |
---|
42 | def setEndTime(Date value) { |
---|
43 | if (value != null) { |
---|
44 | setFieldValue('End time',value) |
---|
45 | } |
---|
46 | return this |
---|
47 | } |
---|
48 | |
---|
49 | Map giveDomainFields() { |
---|
50 | return ['startTime':TemplateFieldType.DATE,'endTime':TemplateFieldType.DATE] |
---|
51 | } |
---|
52 | |
---|
53 | // time diff between end and start date |
---|
54 | // thus, do this manually as follows |
---|
55 | |
---|
56 | static def getDuration(date1, date2) { |
---|
57 | def timeMillis = (date2.getTime() - date1.getTime()).abs() |
---|
58 | def days = (timeMillis / (1000 * 60 * 60 * 24)).toInteger() |
---|
59 | def hours = (timeMillis / (1000 * 60 * 60)).toInteger() |
---|
60 | def minutes = (timeMillis / (1000 * 60)).toInteger() |
---|
61 | def seconds = (timeMillis / 1000).toInteger() |
---|
62 | def millis = (timeMillis % 1000).toInteger() |
---|
63 | |
---|
64 | return new Duration(days, hours, minutes, seconds, millis) |
---|
65 | } |
---|
66 | |
---|
67 | def getDuration() { |
---|
68 | return getDuration(startTime, endTime) |
---|
69 | } |
---|
70 | |
---|
71 | // return a string that prints the duration sensibly. |
---|
72 | // the largest date unit (sec, min, h, day, week, month, or year) |
---|
73 | // is output |
---|
74 | |
---|
75 | static def getPrettyDuration(duration) { |
---|
76 | def handleNumerus = {number, string -> |
---|
77 | return number.toString() + (number == 1 ? string : string + 's') |
---|
78 | } |
---|
79 | if (duration.getYears() > 0) return handleNumerus(duration.getYears(), " year") |
---|
80 | if (duration.getMonths() > 0) return handleNumerus(duration.getMonths(), " month") |
---|
81 | if (duration.getDays() > 0) return handleNumerus(duration.getDays(), " day") |
---|
82 | if (duration.getHours() > 0) return handleNumerus(duration.getHours(), " hour") |
---|
83 | if (duration.getMinutes() > 0) return handleNumerus(duration.getMinutes(), " minute") |
---|
84 | return handleNumerus(duration.getSeconds(), " second") |
---|
85 | } |
---|
86 | |
---|
87 | // convenience method. gives formatted string output for a duration |
---|
88 | def getPrettyDuration() { |
---|
89 | getPrettyDuration(getDuration()) |
---|
90 | } |
---|
91 | |
---|
92 | // convenience method. gives formatted string output for a duration |
---|
93 | static def getPrettyDuration(date1, date2) { |
---|
94 | return getPrettyDuration(getDuration(date1, date2)) |
---|
95 | } |
---|
96 | |
---|
97 | def getDurationString() { |
---|
98 | def d = getDuration() |
---|
99 | return "${d.days} days, ${d.hours} hrs, ${d.minutes} min, ${d.seconds} sec." |
---|
100 | } |
---|
101 | |
---|
102 | def getShortDuration() { |
---|
103 | def d = getDuration() |
---|
104 | def days = d.days |
---|
105 | def hours = d.hours - (24 * days) |
---|
106 | def minutes = d.minutes - (24 * 60 * days) - (60 * hours) |
---|
107 | return "${days}d ${hours}:${minutes}" |
---|
108 | } |
---|
109 | |
---|
110 | def isSamplingEvent() { |
---|
111 | return (this instanceof SamplingEvent) |
---|
112 | } |
---|
113 | |
---|
114 | def String toString() { |
---|
115 | return fieldExists( 'Description' ) ? getFieldValue( 'Description') : "" |
---|
116 | } |
---|
117 | |
---|
118 | } |
---|