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

Last change on this file since 227 was 227, checked in by duh, 9 years ago
  • Template::findAllByEntity(..) did not work as expected. Now you are able to select all Templates of a certain entity by -for example- doing: findAllByEntity(dbnp.studycapturing.Subject) which returns an ArrayList? of matching Templates
  • Property svn:keywords set to Rev Date Author
File size: 1.7 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: 227 $
10 * $Author: duh $
11 * $Date: 2010-03-03 11:06:00 +0000 (wo, 03 mrt 2010) $
12 */
13class Template implements Serializable {
14
15        String name
16        Class entity
17        //nimble.User owner
18
19        static hasMany = [fields: TemplateField]
20
21        static constraints = {
22                name(unique:['entity'])
23
24        }
25
26        /**
27         * overloaded toString method
28         * @return String
29         */
30        def String toString() {
31                return this.name;
32        }
33
34        /**
35         * Look up the type of a certain template subject field
36         * @param       String  fieldName The name of the template field
37         * @return      String  The type (static member of TemplateFieldType) of the field, or null of the field does not exist
38         */
39        def TemplateFieldType getFieldType(String fieldName) {
40                def field = fields.find {
41                        it.name == fieldName   
42                }
43                field?.type
44        }
45
46        /**
47         * get all field of a particular type
48         * @param       Class   fieldType
49         * @return      ArrayList
50         */
51        def getFieldsByType(TemplateFieldType fieldType) {
52                def result = fields.findAll {
53                        it.type == fieldType
54                }
55                return result;
56        }
57
58        /**
59         * overloading the findAllByEntity method to make it function as expected
60         * @param       Class           entity (for example: dbnp.studycapturing.Subject)
61         * @return      ArrayList
62         */
63        public static findAllByEntity(java.lang.Class entity) {
64                def results = []
65
66                // this should not work in static context, however it does so I'll keep
67                // this in for now :)
68                this.findAll().each() {
69                        if (entity.equals(it.entity)) {
70                                results[ results.size() ] = it
71                        }
72                }
73
74                return results
75        }
76}
Note: See TracBrowser for help on using the repository browser.