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: 754 $ |
---|
10 | * $Author: keesvb $ |
---|
11 | * $Date: 2010-07-30 13:05:41 +0000 (vr, 30 jul 2010) $ |
---|
12 | */ |
---|
13 | class Event extends TemplateEntity { |
---|
14 | |
---|
15 | static belongsTo = [parent : Study] |
---|
16 | |
---|
17 | /** Start time of the event, relative to the start time of the study */ |
---|
18 | long startTime |
---|
19 | /** end time of the event, relative to the start time of the study */ |
---|
20 | long endTime |
---|
21 | |
---|
22 | /** |
---|
23 | * Constraints |
---|
24 | */ |
---|
25 | static constraints = { |
---|
26 | endTime(validator: { fields, obj, errors -> |
---|
27 | def error = false |
---|
28 | |
---|
29 | // endTime must be >= the startTime |
---|
30 | if (fields && fields.compareTo(obj.startTime) < 0) { |
---|
31 | error = true |
---|
32 | errors.rejectValue( |
---|
33 | 'endTime', |
---|
34 | 'event.endTime.greaterThanStartTime', |
---|
35 | ['endTime', fields] as Object[], |
---|
36 | 'End time should be greater than or equal to the Start Time' |
---|
37 | ) |
---|
38 | } |
---|
39 | |
---|
40 | return (!error) |
---|
41 | }) |
---|
42 | } |
---|
43 | |
---|
44 | static mapping = { |
---|
45 | |
---|
46 | // Specify that subclasses for Event should have their own database table. |
---|
47 | // This is done because otherwise we run into troubles with the SamplingEvent references from Study. |
---|
48 | tablePerHierarchy false |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * return the domain fields for this domain class |
---|
53 | * @return List<TemplateField> |
---|
54 | */ |
---|
55 | static List<TemplateField> giveDomainFields() { return Event.domainFields } |
---|
56 | |
---|
57 | // To improve performance, the domain fields list is stored as a static final variable in the class. |
---|
58 | static final List<TemplateField> domainFields = [ |
---|
59 | new TemplateField( |
---|
60 | name: 'startTime', |
---|
61 | type: TemplateFieldType.RELTIME, |
---|
62 | comment: "Please enter the start time as a relative time from study start date. "+RelTime.getHelpText()), |
---|
63 | new TemplateField( |
---|
64 | name: 'endTime', |
---|
65 | type: TemplateFieldType.RELTIME, |
---|
66 | comment: "Please enter the end time as a relative time from study start date. "+RelTime.getHelpText()) |
---|
67 | ] |
---|
68 | |
---|
69 | /** |
---|
70 | * Get the duration of the event as RelTime |
---|
71 | * @return RelTime |
---|
72 | */ |
---|
73 | def getDuration() { |
---|
74 | return new RelTime(startTime, endTime) |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Return the start time of the event, which should be relative to the start of the study |
---|
79 | * @return String a human readable representation of the start time of the event |
---|
80 | */ |
---|
81 | def getStartTimeString() { |
---|
82 | return new RelTime(startTime).toPrettyString(); |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Get extended, human readable string representing the duration between startTime and endTime |
---|
87 | * |
---|
88 | * @return String |
---|
89 | */ |
---|
90 | def getDurationString() { |
---|
91 | return new RelTime(startTime, endTime).toPrettyString(); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * Get human readable string representing the duration between startTime and endTime, rounded to one unit (weeks/days/hours etc.) |
---|
96 | * |
---|
97 | * @return String |
---|
98 | */ |
---|
99 | def getRoundedDuration() { |
---|
100 | return new RelTime(startTime, endTime).toPrettyRoundedString(); |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Return whether this is SamplingEvent |
---|
105 | * @return boolean |
---|
106 | */ |
---|
107 | def isSamplingEvent() { |
---|
108 | return (this instanceof SamplingEvent) |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Checks whether this Event is part of one or more of the given EventGroups |
---|
113 | * @param groups |
---|
114 | * @return |
---|
115 | */ |
---|
116 | def belongsToGroup(Collection<EventGroup> groups) { |
---|
117 | def eventFound = false; |
---|
118 | def that = this; |
---|
119 | groups.each { eventgroup -> |
---|
120 | if (!eventFound) { |
---|
121 | eventFound = (that.id in eventgroup.events.id); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | return eventFound; |
---|
126 | } |
---|
127 | |
---|
128 | def String toString() { |
---|
129 | return fieldExists('Description') ? getFieldValue('Description') : "" |
---|
130 | } |
---|
131 | } |
---|