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

Last change on this file since 754 was 754, checked in by keesvb, 12 years ago

big refactoring / change of the data model: implemented belongsTo everywhere where it should be, added comments to the domain classes, changed BootStrap? accordingly

  • Property svn:keywords set to Date Rev Author
File size: 4.8 KB
Line 
1package dbnp.studycapturing
2
3/**
4 * The Template class describes a TemplateEntity template, which is basically an extension of the study capture entities
5 * in terms of extra fields (which are described by classes that extend the TemplateField class).
6 * Study, Subject, Sample and Event are all TemplateEntities.
7 *
8 * Within a Template, we have two different types of fields: 'domain fields' and 'template fields'.
9 * The domain fields are TemplateFields which are given by the TemplateEntity itself and therefore always present
10 * in any instance of that TemplateEntity. They are specified by implementing TemplateEntity.giveDomainFields()
11 * The template fields are TemplateFields which are added specifically by the Template. They are specified
12 * in the fields property of the Template object which is referenced by the TemplateEntity.template property.
13 *
14 * Revision information:
15 * $Rev: 754 $
16 * $Author: keesvb $
17 * $Date: 2010-07-30 13:05:41 +0000 (vr, 30 jul 2010) $
18 */
19class Template implements Serializable {
20
21        /** The name of the template */
22        String name
23
24        /** A string describing the template to other users */
25        String description
26
27        /** The target TemplateEntity for this template */
28        Class entity
29
30        /** The owner of the template. If the owner is not defined, it is a shared/public template */
31        nimble.User owner
32
33        /** The template fields which are the members of this template. This is a List to preserve the field order */
34        List fields
35        static hasMany = [fields: TemplateField]
36
37        static constraints = {
38
39                owner(nullable: true, blank: true)
40                description(nullable: true, blank: true)
41
42                fields(validator: { fields, obj, errors ->
43                        // 'obj' refers to the actual Template object
44
45                        // define a boolean
46                        def error = false
47
48                        // iterate through fields
49                        fields.each { field ->
50                                // check if the field entity is the same as the template entity
51                                if (!field.entity.equals(obj.entity)) {
52                                        error = true
53                                        errors.rejectValue(
54                                                'fields',
55                                                'templateEntity.entityMismatch',
56                                                [field.name, obj.entity, field.entity] as Object[],
57                                                'Template field {0} must be of entity {1} and is currently of entity {2}'
58                                                )
59                                }
60                        }
61
62                        // got an error, or not?
63                        return (!error)
64                })
65
66                // outcommented for now due to bug in Grails / Hibernate
67                // see http://jira.codehaus.org/browse/GRAILS-6020
68                // This is to verify that the template name is unique with respect to the parent entity.
69                // TODO: this probably has to change in the case of private templates of different users,
70                // which can co-exist with the same name. See also TemplateField
71                //      name(unique:['entity'])
72        }
73
74        /**
75         * overloaded toString method
76         * @return String
77         */
78        def String toString() {
79                return this.name;
80        }
81
82        /**
83         * Look up the type of a certain template subject field
84         * @param String fieldName The name of the template field
85         * @return String       The type (static member of TemplateFieldType) of the field, or null of the field does not exist
86         */
87        def TemplateFieldType getFieldType(String fieldName) {
88                def field = fields.find {
89                        it.name == fieldName
90                }
91                field?.type
92        }
93
94        /**
95         * get all field of a particular type
96         * @param Class fieldType
97         * @return Set < TemplateField >
98         */
99        def getFieldsByType(TemplateFieldType fieldType) {
100                def result = fields.findAll {
101                        it.type == fieldType
102                }
103                return result;
104        }
105
106        /**
107         * get all required fields
108         * @param Class fieldType
109         * @return Set < TemplateField >
110         */
111        def getRequiredFields() {
112                def result = fields.findAll {
113                        it.required == true
114                }
115                return result;
116        }
117
118        /**
119         * Checks whether this template is used by any object
120         *
121         * @returns             true iff this template is used by any object, false otherwise
122         */
123        def inUse() {
124                return (numUses() > 0 );
125        }
126
127        /**
128         * The number of objects that use this template
129         *
130         * @returns             the number of objects that use this template.
131         */
132        def numUses() {
133                // This template can only be used in objects of the right entity. Find objects of that
134                // entity and see whether they use this method.
135                //
136                // Unfortunately, due to the grails way of creating classes, we can not use reflection for this
137                def elements;
138                switch( this.entity ) {
139                        case Event:
140                                elements = Event.findAllByTemplate( this ); break;
141                        case Sample:
142                                elements = Sample.findAllByTemplate( this ); break;
143                        case Study:
144                                elements = Study.findAllByTemplate( this ); break;
145                        case Subject:
146                                elements = Subject.findAllByTemplate( this ); break;
147                        default:
148                                return 0;
149                }
150
151                return elements.size();
152        }
153
154        /**
155         * overloading the findAllByEntity method to make it function as expected
156         * @param Class entity (for example: dbnp.studycapturing.Subject)
157         * @return ArrayList
158         */
159        public static findAllByEntity(java.lang.Class entity) {
160                def results = []
161                // 'this' should not work in static context, so taking Template instead of this
162                Template.findAll().each() {
163                        if (entity.equals(it.entity)) {
164                                results[results.size()] = it
165                        }
166                }
167
168                return results
169        }
170}
Note: See TracBrowser for help on using the repository browser.