source: trunk/grails-app/domain/dbnp/studycapturing/SamplingEvent.groovy @ 874

Last change on this file since 874 was 874, checked in by duh, 13 years ago
  • Resolved issue #66, marking required fields in wizard
  • Property svn:keywords set to Author Rev Date
File size: 3.2 KB
Line 
1package dbnp.studycapturing
2
3/**
4 * The SamplingEvent class describes a sampling event, an event that also results in one or more samples.
5 *
6 * NOTE: according to Grails documentation, super classes and subclasses share the same table.
7 *       thus, we could merge the sampling with the Event super class and include a boolean
8 *       However, using a separate class makes it more clear in the code that Event and SamplingEvent are treated differently
9 *
10 * Revision information:
11 * $Rev: 874 $
12 * $Author: duh $
13 * $Date: 2010-09-08 11:17:25 +0000 (wo, 08 sep 2010) $
14 */
15class SamplingEvent extends TemplateEntity {
16        // A SamplingEvent always belongs to one study.
17        // Although this is technically inherited from Event, we have to specify it here again.
18        // Otherwise, Grails expects the SamplingEvent to be referenced in Study.events,
19        // where it is actually referenced in Study.samplingEvents
20        static belongsTo = [parent : Study]
21        static hasMany = [samples : Sample]
22
23        /** Start time of the event, relative to the start time of the study */
24        long startTime
25
26        /** Duration of the sampling event, if it has any (default is 0) */
27        long duration
28
29        // define what template samples should have
30        Template sampleTemplate
31
32        // define domain constraints
33        static constraints = {
34                duration(default: 0L)
35                sampleTemplate(nullable: false, blank: false)
36        }
37
38        /**
39         * return the domain fields for this domain class
40         * @return List<TemplateField>
41         */
42        static List<TemplateField> giveDomainFields() { return SamplingEvent.domainFields }
43
44        // To improve performance, the domain fields list is stored as a static final variable in the class.
45        static final List<TemplateField> domainFields = [
46                new TemplateField(
47                        name: 'startTime',
48                        type: TemplateFieldType.RELTIME,
49                        comment: "Please enter the start time as a relative time from study start date."+RelTime.getHelpText(),
50                        required: true),
51                new TemplateField(
52                        name: 'duration',
53                        type: TemplateFieldType.RELTIME,
54                        comment: "Please enter the duration of the sampling action, if applicable. "+RelTime.getHelpText(),
55                        required: true),
56                new TemplateField(
57                        name: 'sampleTemplate',
58                        type: TemplateFieldType.TEMPLATE,
59                        entity: dbnp.studycapturing.Sample,
60                        comment: "Please select the template of the resulting samples",
61                        required: true)
62        ]
63
64         /**
65          * Return the start time of the event, which should be relative to the start of the study
66          * @return String a human readable representation of the start time of the event
67         */
68        def getStartTimeString() {
69                return new RelTime(startTime).toPrettyString();
70        }
71
72        /**
73         * Checks whether this Event is part of one or more of the given EventGroups
74         * @param groups
75         * @return
76         */
77        def belongsToGroup(Collection<EventGroup> groups) {
78                def eventFound = false;
79                def that = this;
80                groups.each { eventgroup ->
81                        if (!eventFound) {
82                                eventFound = (that.id in eventgroup.events.id);
83                        }
84                }
85
86                return eventFound;
87        }       
88
89        /**
90         * Get all samples that have this sampling event as a parent
91         * @return Map
92         */
93        def getSamples() {
94                def samples = Sample.findAll("from Sample as s where s.parentEvent.id = ${this.id}")
95                samples.collect { it.class == SamplingEvent.class }
96                samples.collect { it != null }
97                return samples == null ? [] : samples
98        }
99
100        def String toString() {
101                return fieldExists('Description') ? getFieldValue('Description') : ""
102        }
103       
104}
Note: See TracBrowser for help on using the repository browser.