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

Last change on this file since 1036 was 1036, checked in by robert@…, 12 years ago

Fixed the random order of templates in the study pages (#135) and created a remote logout call (#158).

  • Property svn:keywords set to Author Date Rev
File size: 3.4 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: 1036 $
10 * $Author: robert@isdat.nl $
11 * $Date: 2010-11-01 15:49:52 +0000 (ma, 01 nov 2010) $
12 */
13class 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    static mapping = {
67        sort "startTime"
68    }
69
70        /**
71         * Get the duration of the event as RelTime
72         * @return RelTime
73         */
74        def getDuration() {
75                return new RelTime(startTime, endTime)
76        }
77
78         /**
79          * Return the start time of the event, which should be relative to the start of the study
80          * @return String a human readable representation of the start time of the event
81         */
82        def getStartTimeString() {
83                return new RelTime(startTime).toPrettyString();
84        }
85
86        /**
87         * Get extended, human readable string representing the duration between startTime and endTime
88     *
89         * @return String
90         */
91        def getDurationString() {
92                return new RelTime(startTime, endTime).toPrettyString();
93        }
94
95        /**
96         * Get human readable string representing the duration between startTime and endTime, rounded to one unit (weeks/days/hours etc.)
97     *
98         * @return String
99         */
100        def getRoundedDuration() {
101                return new RelTime(startTime, endTime).toPrettyRoundedString();
102        }
103
104        /**
105         *    Return whether this is SamplingEvent
106         * @return    boolean
107         */
108        def isSamplingEvent() {
109                return (this instanceof SamplingEvent)
110        }
111
112        /**
113         * Checks whether this Event is part of one or more of the given EventGroups
114         * @param groups
115         * @return
116         */
117        def belongsToGroup(Collection<EventGroup> groups) {
118                def eventFound = false;
119                def that = this;
120                groups.each { eventgroup ->
121                        if (!eventFound && eventgroup.events) {
122                                eventFound = (that.id in eventgroup.events.id);
123                        }
124                }
125
126                return eventFound;
127        }
128
129        def String toString() {
130                return fieldExists('Description') ? getFieldValue('Description') : ""
131        }
132}
Note: See TracBrowser for help on using the repository browser.