1 | package dbnp.studycapturing |
---|
2 | |
---|
3 | import org.nmcdsp.plugins.aaaa.SecUser |
---|
4 | import org.nmcdsp.plugins.aaaa.Entity |
---|
5 | |
---|
6 | /** |
---|
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. |
---|
10 | * |
---|
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 | * |
---|
17 | * Revision information: |
---|
18 | * $Rev: 936 $ |
---|
19 | * $Author: t.w.abma@umcutrecht.nl $ |
---|
20 | * $Date: 2010-10-11 20:23:13 +0000 (ma, 11 okt 2010) $ |
---|
21 | */ |
---|
22 | class Template extends Identity { |
---|
23 | |
---|
24 | /** The name of the template */ |
---|
25 | String name |
---|
26 | |
---|
27 | /** A string describing the template to other users */ |
---|
28 | String description |
---|
29 | |
---|
30 | /** The target TemplateEntity for this template */ |
---|
31 | Class entity |
---|
32 | |
---|
33 | /** The owner of the template. If the owner is not defined, it is a shared/public template */ |
---|
34 | SecUser owner |
---|
35 | |
---|
36 | /** The template fields which are the members of this template. This is a List to preserve the field order */ |
---|
37 | List fields |
---|
38 | |
---|
39 | static hasMany = [fields: TemplateField] |
---|
40 | |
---|
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 | |
---|
49 | // constraints |
---|
50 | static constraints = { |
---|
51 | owner(nullable: true, blank: true) |
---|
52 | description(nullable: true, blank: true) |
---|
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 | |
---|
78 | // outcommented for now due to bug in Grails / Hibernate |
---|
79 | // see http://jira.codehaus.org/browse/GRAILS-6020 |
---|
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 |
---|
83 | // name(unique:['entity']) |
---|
84 | |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * overloaded toString method |
---|
89 | * @return String |
---|
90 | */ |
---|
91 | def String toString() { |
---|
92 | return this.name; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Look up the type of a certain template subject field |
---|
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 |
---|
99 | */ |
---|
100 | def TemplateFieldType getFieldType(String fieldName) { |
---|
101 | def field = fields.find { |
---|
102 | it.name == fieldName |
---|
103 | } |
---|
104 | field?.type |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * get all field of a particular type |
---|
109 | * @param Class fieldType |
---|
110 | * @return Set < TemplateField > |
---|
111 | */ |
---|
112 | def getFieldsByType(TemplateFieldType fieldType) { |
---|
113 | def result = fields.findAll { |
---|
114 | it.type == fieldType |
---|
115 | } |
---|
116 | return result; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * get all required fields |
---|
121 | * @param Class fieldType |
---|
122 | * @return Set < TemplateField > |
---|
123 | */ |
---|
124 | def getRequiredFields() { |
---|
125 | def result = fields.findAll { |
---|
126 | it.required == true |
---|
127 | } |
---|
128 | return result; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
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 | /** |
---|
168 | * overloading the findAllByEntity method to make it function as expected |
---|
169 | * @param Class entity (for example: dbnp.studycapturing.Subject) |
---|
170 | * @return ArrayList |
---|
171 | */ |
---|
172 | public static findAllByEntity(java.lang.Class entity) { |
---|
173 | def results = [] |
---|
174 | // 'this' should not work in static context, so taking Template instead of this |
---|
175 | Template.findAll().each() { |
---|
176 | if (entity.equals(it.entity)) { |
---|
177 | results[results.size()] = it |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | return results |
---|
182 | } |
---|
183 | } |
---|