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