1 | package dbnp.studycapturing |
---|
2 | |
---|
3 | /** |
---|
4 | * The Event class describes an actual event, as it has happened to a certain subject. Often, the same event occurs |
---|
5 | * to multiple subjects at the same time. That is why the actual description of the event is factored out into a more |
---|
6 | * general EventDescription class. Events that also lead to sample(s) should be instantiated as SamplingEvents. |
---|
7 | * |
---|
8 | * Revision information: |
---|
9 | * $Rev: 1013 $ |
---|
10 | * $Author: business@keesvanbochove.nl $ |
---|
11 | * $Date: 2010-10-28 18:05:27 +0000 (do, 28 okt 2010) $ |
---|
12 | */ |
---|
13 | class Event extends TemplateEntity { |
---|
14 | |
---|
15 | static searchable = true |
---|
16 | |
---|
17 | static belongsTo = [parent : Study] |
---|
18 | |
---|
19 | /** Start time of the event, relative to the start time of the study */ |
---|
20 | long startTime |
---|
21 | /** end time of the event, relative to the start time of the study */ |
---|
22 | long endTime |
---|
23 | |
---|
24 | /** |
---|
25 | * Constraints |
---|
26 | */ |
---|
27 | static constraints = { |
---|
28 | endTime(validator: { fields, obj, errors -> |
---|
29 | def error = false |
---|
30 | |
---|
31 | // endTime must be >= the startTime |
---|
32 | if (fields && fields.compareTo(obj.startTime) < 0) { |
---|
33 | error = true |
---|
34 | errors.rejectValue( |
---|
35 | 'endTime', |
---|
36 | 'event.endTime.greaterThanStartTime', |
---|
37 | ['endTime', fields] as Object[], |
---|
38 | 'End time should be greater than or equal to the Start Time' |
---|
39 | ) |
---|
40 | } |
---|
41 | |
---|
42 | return (!error) |
---|
43 | }) |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * return the domain fields for this domain class |
---|
48 | * @return List<TemplateField> |
---|
49 | */ |
---|
50 | static List<TemplateField> giveDomainFields() { return Event.domainFields } |
---|
51 | |
---|
52 | // To improve performance, the domain fields list is stored as a static final variable in the class. |
---|
53 | static final List<TemplateField> domainFields = [ |
---|
54 | new TemplateField( |
---|
55 | name: 'startTime', |
---|
56 | type: TemplateFieldType.RELTIME, |
---|
57 | comment: "Please enter the start time as a relative time from study start date. "+RelTime.getHelpText(), |
---|
58 | required: true), |
---|
59 | new TemplateField( |
---|
60 | name: 'endTime', |
---|
61 | type: TemplateFieldType.RELTIME, |
---|
62 | comment: "Please enter the end time as a relative time from study start date. "+RelTime.getHelpText(), |
---|
63 | required: true) |
---|
64 | ] |
---|
65 | |
---|
66 | /** |
---|
67 | * Get the duration of the event as RelTime |
---|
68 | * @return RelTime |
---|
69 | */ |
---|
70 | def getDuration() { |
---|
71 | return new RelTime(startTime, endTime) |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Return the start time of the event, which should be relative to the start of the study |
---|
76 | * @return String a human readable representation of the start time of the event |
---|
77 | */ |
---|
78 | def getStartTimeString() { |
---|
79 | return new RelTime(startTime).toPrettyString(); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Get extended, human readable string representing the duration between startTime and endTime |
---|
84 | * |
---|
85 | * @return String |
---|
86 | */ |
---|
87 | def getDurationString() { |
---|
88 | return new RelTime(startTime, endTime).toPrettyString(); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Get human readable string representing the duration between startTime and endTime, rounded to one unit (weeks/days/hours etc.) |
---|
93 | * |
---|
94 | * @return String |
---|
95 | */ |
---|
96 | def getRoundedDuration() { |
---|
97 | return new RelTime(startTime, endTime).toPrettyRoundedString(); |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Return whether this is SamplingEvent |
---|
102 | * @return boolean |
---|
103 | */ |
---|
104 | def isSamplingEvent() { |
---|
105 | return (this instanceof SamplingEvent) |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Checks whether this Event is part of one or more of the given EventGroups |
---|
110 | * @param groups |
---|
111 | * @return |
---|
112 | */ |
---|
113 | def belongsToGroup(Collection<EventGroup> groups) { |
---|
114 | def eventFound = false; |
---|
115 | def that = this; |
---|
116 | groups.each { eventgroup -> |
---|
117 | if (!eventFound && eventgroup.events) { |
---|
118 | eventFound = (that.id in eventgroup.events.id); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | return eventFound; |
---|
123 | } |
---|
124 | |
---|
125 | def String toString() { |
---|
126 | return fieldExists('Description') ? getFieldValue('Description') : "" |
---|
127 | } |
---|
128 | } |
---|