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

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