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

Last change on this file since 1198 was 1198, checked in by t.e.roos@…, 10 years ago

Added workarounds for known template text fields bug

  • Property svn:keywords set to Author Date Rev
File size: 3.7 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: 1198 $
10 * $Author: t.e.roos@rug.nl $
11 * $Date: 2010-11-24 16:21:28 +0000 (wo, 24 nov 2010) $
12 */
13class Event extends TemplateEntity {
14
15        // uncommented due to searchable issue
16        // @see http://jira.codehaus.org/browse/GRAILSPLUGINS-1577
17        // Enabling this causes the error: Trying to marshall a null id [id] for alias [Event] in the study create wizard when you add events
18        // static searchable = true
19
20        static belongsTo = [parent : Study]     
21
22        /** Start time of the event, relative to the start time of the study */
23        long startTime
24        /** end time of the event, relative to the start time of the study */
25        long endTime
26
27        /**
28         * Constraints
29         */
30        static constraints = {
31                endTime(validator: { fields, obj, errors ->
32                        def error = false
33
34                        // endTime must be >= the startTime
35                        if (fields && fields.compareTo(obj.startTime) < 0) {
36                                error = true
37                                errors.rejectValue(
38                                        'endTime',
39                                        'event.endTime.greaterThanStartTime',
40                                        ['endTime', fields] as Object[],
41                                        'End time should be greater than or equal to the Start Time'
42                                )
43                        }
44
45                        return (!error)
46                })
47        }
48
49        /**
50         * return the domain fields for this domain class
51         * @return List<TemplateField>
52         */
53        static List<TemplateField> giveDomainFields() { return Event.domainFields }
54
55        // To improve performance, the domain fields list is stored as a static final variable in the class.
56        static final List<TemplateField> domainFields = [
57                new TemplateField(
58                        name: 'startTime',
59                        type: TemplateFieldType.RELTIME,
60                        comment: "Please enter the start time as a relative time from study start date. "+RelTime.getHelpText(),
61                        required: true),
62                new TemplateField(
63                        name: 'endTime',
64                        type: TemplateFieldType.RELTIME,
65                        comment: "Please enter the end time as a relative time from study start date. "+RelTime.getHelpText(),
66                        required: true)
67        ]
68
69    static mapping = {
70        sort "startTime"
71
72        // Workaround for bug http://jira.codehaus.org/browse/GRAILS-6754
73        templateTextFields type: 'text'
74    }
75
76        /**
77         * Get the duration of the event as RelTime
78         * @return RelTime
79         */
80        def getDuration() {
81                return new RelTime(startTime, endTime)
82        }
83
84         /**
85          * Return the start time of the event, which should be relative to the start of the study
86          * @return String a human readable representation of the start time of the event
87         */
88        def getStartTimeString() {
89                return new RelTime(startTime).toPrettyString();
90        }
91
92        /**
93         * Get extended, human readable string representing the duration between startTime and endTime
94     *
95         * @return String
96         */
97        def getDurationString() {
98                return new RelTime(startTime, endTime).toPrettyString();
99        }
100
101        /**
102         * Get human readable string representing the duration between startTime and endTime, rounded to one unit (weeks/days/hours etc.)
103     *
104         * @return String
105         */
106        def getRoundedDuration() {
107                return new RelTime(startTime, endTime).toPrettyRoundedString();
108        }
109
110        /**
111         *    Return whether this is SamplingEvent
112         * @return    boolean
113         */
114        def isSamplingEvent() {
115                return (this instanceof SamplingEvent)
116        }
117
118        /**
119         * Checks whether this Event is part of one or more of the given EventGroups
120         * @param groups
121         * @return
122         */
123        def belongsToGroup(Collection<EventGroup> groups) {
124                def eventFound = false;
125                def that = this;
126                groups.each { eventgroup ->
127                        if (!eventFound && eventgroup.events) {
128                                eventFound = (that.id in eventgroup.events.id);
129                        }
130                }
131
132                return eventFound;
133        }
134
135        def String toString() {
136                return fieldExists('Description') ? getFieldValue('Description') : ""
137        }
138}
Note: See TracBrowser for help on using the repository browser.