source: trunk/grails-app/domain/dbnp/studycapturing/Event.groovy @ 900

Last change on this file since 900 was 900, checked in by keesvb, 11 years ago

fix for bug #164 (workaround for GRAILS-6754)

  • Property svn:keywords set to Author Rev Date
File size: 3.3 KB
Line 
1package 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: 900 $
10 * $Author: keesvb $
11 * $Date: 2010-09-25 22:39:18 +0000 (za, 25 sep 2010) $
12 */
13class 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        /**
45         * return the domain fields for this domain class
46         * @return List<TemplateField>
47         */
48        static List<TemplateField> giveDomainFields() { return Event.domainFields }
49
50        // To improve performance, the domain fields list is stored as a static final variable in the class.
51        static final List<TemplateField> domainFields = [
52                new TemplateField(
53                        name: 'startTime',
54                        type: TemplateFieldType.RELTIME,
55                        comment: "Please enter the start time as a relative time from study start date. "+RelTime.getHelpText(),
56                        required: true),
57                new TemplateField(
58                        name: 'endTime',
59                        type: TemplateFieldType.RELTIME,
60                        comment: "Please enter the end time as a relative time from study start date. "+RelTime.getHelpText(),
61                        required: true)
62        ]
63
64        /**
65         * Get the duration of the event as RelTime
66         * @return RelTime
67         */
68        def getDuration() {
69                return new RelTime(startTime, endTime)
70        }
71
72         /**
73          * Return the start time of the event, which should be relative to the start of the study
74          * @return String a human readable representation of the start time of the event
75         */
76        def getStartTimeString() {
77                return new RelTime(startTime).toPrettyString();
78        }
79
80        /**
81         * Get extended, human readable string representing the duration between startTime and endTime
82     *
83         * @return String
84         */
85        def getDurationString() {
86                return new RelTime(startTime, endTime).toPrettyString();
87        }
88
89        /**
90         * Get human readable string representing the duration between startTime and endTime, rounded to one unit (weeks/days/hours etc.)
91     *
92         * @return String
93         */
94        def getRoundedDuration() {
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        /**
107         * Checks whether this Event is part of one or more of the given EventGroups
108         * @param groups
109         * @return
110         */
111        def belongsToGroup(Collection<EventGroup> groups) {
112                def eventFound = false;
113                def that = this;
114                groups.each { eventgroup ->
115                        if (!eventFound) {
116                                eventFound = (that.id in eventgroup.events.id);
117                        }
118                }
119
120                return eventFound;
121        }
122
123        def String toString() {
124                return fieldExists('Description') ? getFieldValue('Description') : ""
125        }
126}
Note: See TracBrowser for help on using the repository browser.