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: 654 $ |
---|
12 | * $Author: keesvb $ |
---|
13 | * $Date: 2010-07-15 12:26:48 +0000 (do, 15 jul 2010) $ |
---|
14 | */ |
---|
15 | class Event extends TemplateEntity implements Serializable { |
---|
16 | long startTime |
---|
17 | long endTime |
---|
18 | |
---|
19 | // TODO: assure the Event has a parent study in validate() |
---|
20 | |
---|
21 | /** |
---|
22 | * Constraints |
---|
23 | */ |
---|
24 | static constraints = { |
---|
25 | endTime(validator: { fields, obj, errors -> |
---|
26 | def error = false |
---|
27 | |
---|
28 | // endTime must be >= the startTime |
---|
29 | if (fields && fields.compareTo(obj.startTime) < 0) { |
---|
30 | error = true |
---|
31 | errors.rejectValue( |
---|
32 | 'endTime', |
---|
33 | 'event.endTime.greaterThanStartTime', |
---|
34 | ['endTime', fields] as Object[], |
---|
35 | 'End time should be greater than or equal to the Start Time' |
---|
36 | ) |
---|
37 | } |
---|
38 | |
---|
39 | return (!error) |
---|
40 | }) |
---|
41 | } |
---|
42 | |
---|
43 | /** |
---|
44 | * return the domain fields for this domain class |
---|
45 | * @return List |
---|
46 | */ |
---|
47 | static List<TemplateField> giveDomainFields() { return Event.domainFields } |
---|
48 | |
---|
49 | static final List<TemplateField> domainFields = [ |
---|
50 | new TemplateField( |
---|
51 | name: 'startTime', |
---|
52 | type: TemplateFieldType.RELTIME, |
---|
53 | comment: "Please enter the start time as a relative time from study start date. "+RelTime.getHelpText()), |
---|
54 | new TemplateField( |
---|
55 | name: 'endTime', |
---|
56 | type: TemplateFieldType.RELTIME, |
---|
57 | comment: "Please enter the end time as a relative time from study start date. "+RelTime.getHelpText()) |
---|
58 | ] |
---|
59 | |
---|
60 | /** |
---|
61 | * get the duration |
---|
62 | * @return RelTime |
---|
63 | */ |
---|
64 | def getDuration() { |
---|
65 | return new RelTime(startTime, endTime) |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Get extended, human readable string representing the duration between startTime and endTime |
---|
70 | * |
---|
71 | * @return String |
---|
72 | */ |
---|
73 | def getDurationString() { |
---|
74 | return new RelTime(startTime, endTime).toPrettyRoundedString(); |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * get a prettified duration |
---|
79 | * @return String |
---|
80 | */ |
---|
81 | static def getPrettyDuration(RelTime duration) { |
---|
82 | return duration.toPrettyRoundedString(); |
---|
83 | } |
---|
84 | |
---|
85 | def getPrettyDuration() { |
---|
86 | getPrettyDuration(getDuration()) |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Get short, human readable string representing the duration between startTime and endTime |
---|
91 | * |
---|
92 | * @return String |
---|
93 | */ |
---|
94 | def getShortDuration() { |
---|
95 | return new RelTime(startTime, endTime).toPrettyRoundedString(); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Return whether this is SamplingEvent |
---|
100 | * @return boolean |
---|
101 | */ |
---|
102 | def isSamplingEvent() { |
---|
103 | return (this instanceof SamplingEvent) |
---|
104 | } |
---|
105 | |
---|
106 | def belongsToGroup(Collection<EventGroup> groups) { |
---|
107 | def eventFound = false; |
---|
108 | def that = this; |
---|
109 | groups.each { eventgroup -> |
---|
110 | if (!eventFound) { |
---|
111 | eventFound = (that.id in eventgroup.events.id); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | return eventFound; |
---|
116 | } |
---|
117 | |
---|
118 | def String toString() { |
---|
119 | return fieldExists('Description') ? getFieldValue('Description') : "" |
---|
120 | } |
---|
121 | } |
---|