source: trunk/grails-app/domain/dbnp/studycapturing/Template.groovy @ 496

Last change on this file since 496 was 496, checked in by duh, 11 years ago
  • set svn keyword expansion
  • Property svn:keywords set to Date Rev Author
File size: 2.3 KB
Line 
1package dbnp.studycapturing
2
3/**
4 * The Template class describes a study template, which is basically an extension of the study capture entities
5 * in terms of extra fields (described by classes that extend the TemplateField class).
6 * At this moment, only extension of the study and subject entities is implemented.
7 *
8 * Revision information:
9 * $Rev: 496 $
10 * $Author: duh $
11 * $Date: 2010-05-28 12:25:13 +0000 (vr, 28 mei 2010) $
12 */
13class Template implements Serializable {
14        String name
15        String description
16        Class entity
17        //nimble.User owner
18        List fields
19        static hasMany = [fields: TemplateField,
20        sampleTemplates: Template] // only applicable when entity=SamplingEvent,
21        // for storing which Sample (template)s result from a SamplingEvent (template)
22
23        static constraints = {
24                description(nullable: true, blank: true)
25                // outcommented for now due to bug in Grails / Hibernate
26                // see http://jira.codehaus.org/browse/GRAILS-6020
27                //      name(unique:['entity'])
28        }
29
30        /**
31         * overloaded toString method
32         * @return String
33         */
34        def String toString() {
35                return this.name;
36        }
37
38        /**
39         * Look up the type of a certain template subject field
40         * @param       String  fieldName The name of the template field
41         * @return      String  The type (static member of TemplateFieldType) of the field, or null of the field does not exist
42         */
43        def TemplateFieldType getFieldType(String fieldName) {
44                def field = fields.find {
45                        it.name == fieldName   
46                }
47                field?.type
48        }
49
50        /**
51         * get all field of a particular type
52         * @param       Class   fieldType
53         * @return      Set<TemplateField>
54         */
55        def getFieldsByType(TemplateFieldType fieldType) {
56                def result = fields.findAll {
57                        it.type == fieldType
58                }
59                return result;
60        }
61
62        /**
63         * get all required fields
64         * @param       Class   fieldType
65         * @return      Set<TemplateField>
66         */
67        def getRequiredFields() {
68                def result = fields.findAll {
69                        it.required == true
70                }
71                return result;
72        }
73
74        /**
75         * overloading the findAllByEntity method to make it function as expected
76         * @param       Class           entity (for example: dbnp.studycapturing.Subject)
77         * @return      ArrayList
78         */
79        public static findAllByEntity(java.lang.Class entity) {
80                def results = []
81                // 'this' should not work in static context, so taking Template instead of this
82                Template.findAll().each() {
83                        if (entity.equals(it.entity)) {
84                                results[ results.size() ] = it
85                        }
86                }
87
88                return results
89        }
90}
Note: See TracBrowser for help on using the repository browser.