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

Last change on this file since 784 was 784, checked in by duh, 10 years ago
  • set keyword expansion
  • Property svn:keywords set to Author Rev Date
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: 784 $
16 * $Author: duh $
17 * $Date: 2010-08-06 10:21:06 +0000 (vr, 06 aug 2010) $
18 */
19class Template extends Identity {
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
36        static hasMany = [fields: TemplateField]
37
38        // constraints
39        static constraints = {
40                owner(nullable: true, blank: true)
41                description(nullable: true, blank: true)
42
43                fields(validator: { fields, obj, errors ->
44                        // 'obj' refers to the actual Template object
45
46                        // define a boolean
47                        def error = false
48
49                        // iterate through fields
50                        fields.each { field ->
51                                // check if the field entity is the same as the template entity
52                                if (!field.entity.equals(obj.entity)) {
53                                        error = true
54                                        errors.rejectValue(
55                                                'fields',
56                                                'templateEntity.entityMismatch',
57                                                [field.name, obj.entity, field.entity] as Object[],
58                                                'Template field {0} must be of entity {1} and is currently of entity {2}'
59                                                )
60                                }
61                        }
62
63                        // got an error, or not?
64                        return (!error)
65                })
66
67                // outcommented for now due to bug in Grails / Hibernate
68                // see http://jira.codehaus.org/browse/GRAILS-6020
69                // This is to verify that the template name is unique with respect to the parent entity.
70                // TODO: this probably has to change in the case of private templates of different users,
71                // which can co-exist with the same name. See also TemplateField
72                //      name(unique:['entity'])
73        }
74
75        /**
76         * overloaded toString method
77         * @return String
78         */
79        def String toString() {
80                return this.name;
81        }
82
83        /**
84         * Look up the type of a certain template subject field
85         * @param String fieldName The name of the template field
86         * @return String       The type (static member of TemplateFieldType) of the field, or null of the field does not exist
87         */
88        def TemplateFieldType getFieldType(String fieldName) {
89                def field = fields.find {
90                        it.name == fieldName
91                }
92                field?.type
93        }
94
95        /**
96         * get all field of a particular type
97         * @param Class fieldType
98         * @return Set < TemplateField >
99         */
100        def getFieldsByType(TemplateFieldType fieldType) {
101                def result = fields.findAll {
102                        it.type == fieldType
103                }
104                return result;
105        }
106
107        /**
108         * get all required fields
109         * @param Class fieldType
110         * @return Set < TemplateField >
111         */
112        def getRequiredFields() {
113                def result = fields.findAll {
114                        it.required == true
115                }
116                return result;
117        }
118
119        /**
120         * Checks whether this template is used by any object
121         *
122         * @returns             true iff this template is used by any object, false otherwise
123         */
124        def inUse() {
125                return (numUses() > 0 );
126        }
127
128        /**
129         * The number of objects that use this template
130         *
131         * @returns             the number of objects that use this template.
132         */
133        def numUses() {
134                // This template can only be used in objects of the right entity. Find objects of that
135                // entity and see whether they use this method.
136                //
137                // Unfortunately, due to the grails way of creating classes, we can not use reflection for this
138                def elements;
139                switch( this.entity ) {
140                        case Event:
141                                elements = Event.findAllByTemplate( this ); break;
142                        case Sample:
143                                elements = Sample.findAllByTemplate( this ); break;
144                        case Study:
145                                elements = Study.findAllByTemplate( this ); break;
146                        case Subject:
147                                elements = Subject.findAllByTemplate( this ); break;
148                        default:
149                                return 0;
150                }
151
152                return elements.size();
153        }
154
155        /**
156         * overloading the findAllByEntity method to make it function as expected
157         * @param Class entity (for example: dbnp.studycapturing.Subject)
158         * @return ArrayList
159         */
160        public static findAllByEntity(java.lang.Class entity) {
161                def results = []
162                // 'this' should not work in static context, so taking Template instead of this
163                Template.findAll().each() {
164                        if (entity.equals(it.entity)) {
165                                results[results.size()] = it
166                        }
167                }
168
169                return results
170        }
171}
Note: See TracBrowser for help on using the repository browser.