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

Last change on this file since 602 was 602, checked in by jahn, 11 years ago

Several minor changes and cleaning up related to date repsentations communicated with SAM.

  • Property svn:keywords set to Date Rev Author
File size: 2.5 KB
Line 
1package dbnp.studycapturing
2
3import groovy.time.*
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: 602 $
12 * $Author: jahn $
13 * $Date: 2010-06-23 09:21:17 +0000 (wo, 23 jun 2010) $
14 */
15class Event extends TemplateEntity implements Serializable {
16        long startTime
17        long endTime
18
19        /**
20         * Constraints
21         */
22        static constraints = {
23                endTime(validator: { fields, obj, errors ->
24                        def error = false
25
26                        // endTime must be >= the startTime
27                        if (fields && fields.compareTo(obj.startTime) < 0) {
28                                error = true
29                                errors.rejectValue(
30                                        'endTime',
31                                        'event.endTime.greaterThanStartTime',
32                                        ['endTime', fields] as Object[],
33                                        'End time should be greater than or equal to the Start Time'
34                                )
35                        }
36
37                        return (!error)
38                })
39        }
40
41        /**
42         * return the domain fields for this domain class
43         * @return List
44         */
45        static List<TemplateField> giveDomainFields() { return Event.domainFields }
46
47        static final List<TemplateField> domainFields = [
48                new TemplateField(
49                        name: 'startTime',
50                        type: TemplateFieldType.RELTIME,
51                        comment: "Please enter the start time as a relative time from study start date. "+RelTime.getHelpText()),
52                new TemplateField(
53                        name: 'endTime',
54                        type: TemplateFieldType.RELTIME,
55                        comment: "Please enter the end time as a relative time from study start date. "+RelTime.getHelpText())
56        ]
57
58
59        /**
60         * Get extended, human readable string representing the duration between startTime and endTime
61     *
62         * @return String
63         */
64        def getDurationString() {
65                return new RelTime(startTime, endTime).toPrettyRoundedString();
66        }
67
68
69
70        /**
71         * Get short, human readable string representing the duration between startTime and endTime
72     *
73         * @return String
74         */
75        def getShortDuration() {
76                return new RelTime(startTime, endTime).toPrettyRoundedString();
77        }
78
79
80
81
82
83
84        /**
85         *    Return whether this is SamplingEvent
86         * @return    boolean
87         */
88        def isSamplingEvent() {
89                return (this instanceof SamplingEvent)
90        }
91
92        def belongsToGroup(Collection<EventGroup> groups) {
93                def eventFound = false;
94                def that = this;
95                groups.each { eventgroup ->
96                        if (!eventFound) {
97                                eventFound = (that.id in eventgroup.events.id);
98                        }
99                }
100
101                return eventFound;
102        }
103
104        def String toString() {
105                return fieldExists('Description') ? getFieldValue('Description') : ""
106        }
107}
Note: See TracBrowser for help on using the repository browser.