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

Last change on this file since 1868 was 1868, checked in by s.h.sikkema@…, 8 years ago

removed obsolete function 'isSamplingEvent' from Event

  • Property svn:keywords set to Rev Author Date
File size: 3.6 KB
Line 
1package dbnp.studycapturing
2import org.dbnp.gdt.*
3
4/**
5 * The Event class describes an actual event, as it has happened to a certain subject. Often, the same event occurs
6 * to multiple subjects at the same time. That is why the actual description of the event is factored out into a more
7 * general EventDescription class. Events that also lead to sample(s) should be instantiated as SamplingEvents.
8 *
9 * Revision information:
10 * $Rev: 1868 $
11 * $Author: s.h.sikkema@gmail.com $
12 * $Date: 2011-05-24 14:56:33 +0000 (di, 24 mei 2011) $
13 */
14class Event extends TemplateEntity {
15
16        // uncommented due to searchable issue
17        // @see http://jira.codehaus.org/browse/GRAILSPLUGINS-1577
18        // Enabling this causes the error: Trying to marshall a null id [id] for alias [Event] in the study create wizard when you add events
19        //static searchable = true
20
21        static belongsTo = [parent : Study]     
22
23        /** Start time of the event, relative to the start time of the study */
24        long startTime
25        /** end time of the event, relative to the start time of the study */
26        long endTime
27
28        /**
29         * Constraints
30         */
31        static constraints = {
32                endTime(validator: { fields, obj, errors ->
33                        def error = false
34
35                        // endTime must be >= the startTime
36                        if (fields && fields.compareTo(obj.startTime) < 0) {
37                                error = true
38                                errors.rejectValue(
39                                        'endTime',
40                                        'event.endTime.greaterThanStartTime',
41                                        ['endTime', fields] as Object[],
42                                        'End time should be greater than or equal to the Start Time'
43                                )
44                        }
45
46                        return (!error)
47                })
48        }
49
50        /**
51         * return the domain fields for this domain class
52         * @return List<TemplateField>
53         */
54        static List<TemplateField> giveDomainFields() { return Event.domainFields }
55
56        // To improve performance, the domain fields list is stored as a static final variable in the class.
57        static final List<TemplateField> domainFields = [
58                new TemplateField(
59                        name: 'startTime',
60                        type: TemplateFieldType.RELTIME,
61                        comment: "Please enter the start time as a relative time from study start date. "+RelTime.getHelpText(),
62                        required: true),
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                        required: true)
68        ]
69
70    static mapping = {
71        sort "startTime"
72
73        // Workaround for bug http://jira.codehaus.org/browse/GRAILS-6754
74        templateTextFields type: 'text'
75    }
76
77        /**
78         * Get the duration of the event as RelTime
79         * @return RelTime
80         */
81        def getDuration() {
82                return new RelTime(startTime, endTime)
83        }
84
85         /**
86          * Return the start time of the event, which should be relative to the start of the study
87          * @return String a human readable representation of the start time of the event
88         */
89        def getStartTimeString() {
90                return new RelTime(startTime).toPrettyString();
91        }
92
93        /**
94         * Get extended, human readable string representing the duration between startTime and endTime
95     *
96         * @return String
97         */
98        def getDurationString() {
99                return new RelTime(startTime, endTime).toPrettyString();
100        }
101
102        /**
103         * Get human readable string representing the duration between startTime and endTime, rounded to one unit (weeks/days/hours etc.)
104     *
105         * @return String
106         */
107        def getRoundedDuration() {
108                return new RelTime(startTime, endTime).toPrettyRoundedString();
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 && eventgroup.events) {
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}
Note: See TracBrowser for help on using the repository browser.