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

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