1 | package 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: 384 $ |
---|
10 | * $Author: keesvb $ |
---|
11 | * $Date: 2010-04-27 07:13:47 +0000 (di, 27 apr 2010) $ |
---|
12 | */ |
---|
13 | class Template implements Serializable { |
---|
14 | String name |
---|
15 | Class entity |
---|
16 | //nimble.User owner |
---|
17 | List fields |
---|
18 | static hasMany = [fields: TemplateField] |
---|
19 | |
---|
20 | static constraints = { |
---|
21 | |
---|
22 | // outcommented for now due to bug in Grails / Hibernate |
---|
23 | // see http://jira.codehaus.org/browse/GRAILS-6020 |
---|
24 | // name(unique:['entity']) |
---|
25 | } |
---|
26 | |
---|
27 | // As the entity is not known at the time the constructor is called, |
---|
28 | // we add the system fields for a new template object at the time the entity is set |
---|
29 | void setEntity(Class value) { |
---|
30 | this.entity = value |
---|
31 | if (value != null) { |
---|
32 | if (fields == null) { |
---|
33 | if (entity.systemFields) { |
---|
34 | println "adding default template fields for " + entity.systemFields |
---|
35 | entity.systemFields.each { |
---|
36 | this.addToFields(it) |
---|
37 | } |
---|
38 | } |
---|
39 | } |
---|
40 | println "" + value + "this template has now fields " + fields |
---|
41 | } |
---|
42 | |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * overloaded toString method |
---|
47 | * @return String |
---|
48 | */ |
---|
49 | def String toString() { |
---|
50 | return this.name; |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Look up the type of a certain template subject field |
---|
55 | * @param String fieldName The name of the template field |
---|
56 | * @return String The type (static member of TemplateFieldType) of the field, or null of the field does not exist |
---|
57 | */ |
---|
58 | def TemplateFieldType getFieldType(String fieldName) { |
---|
59 | def field = fields.find { |
---|
60 | it.name == fieldName |
---|
61 | } |
---|
62 | field?.type |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * get all field of a particular type |
---|
67 | * @param Class fieldType |
---|
68 | * @return Set<TemplateField> |
---|
69 | */ |
---|
70 | def getFieldsByType(TemplateFieldType fieldType) { |
---|
71 | def result = fields.findAll { |
---|
72 | it.type == fieldType |
---|
73 | } |
---|
74 | return result; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * get all required fields |
---|
79 | * @param Class fieldType |
---|
80 | * @return Set<TemplateField> |
---|
81 | */ |
---|
82 | def getRequiredFields() { |
---|
83 | def result = fields.findAll { |
---|
84 | it.required == true |
---|
85 | } |
---|
86 | return result; |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * overloading the findAllByEntity method to make it function as expected |
---|
91 | * @param Class entity (for example: dbnp.studycapturing.Subject) |
---|
92 | * @return ArrayList |
---|
93 | */ |
---|
94 | public static findAllByEntity(java.lang.Class entity) { |
---|
95 | def results = [] |
---|
96 | // 'this' should not work in static context, so taking Template instead of this |
---|
97 | Template.findAll().each() { |
---|
98 | if (entity.equals(it.entity)) { |
---|
99 | results[ results.size() ] = it |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | return results |
---|
104 | } |
---|
105 | } |
---|