1 | package dbnp.studycapturing |
---|
2 | |
---|
3 | import dbnp.data.Term |
---|
4 | |
---|
5 | /** |
---|
6 | * TemplateEntity Domain Class |
---|
7 | * |
---|
8 | * Revision information: |
---|
9 | * $Rev: 247 $ |
---|
10 | * $Author: duh $ |
---|
11 | * $Date: 2010-03-08 17:29:14 +0000 (ma, 08 mrt 2010) $ |
---|
12 | */ |
---|
13 | class TemplateEntity implements Serializable { |
---|
14 | |
---|
15 | Template template |
---|
16 | |
---|
17 | Map templateStringFields = [:] |
---|
18 | Map templateTextFields = [:] |
---|
19 | Map templateStringListFields = [:] |
---|
20 | Map templateIntegerFields = [:] |
---|
21 | Map templateFloatFields = [:] |
---|
22 | Map templateDoubleFields = [:] |
---|
23 | Map templateDateFields = [:] |
---|
24 | Map templateTermFields = [:] |
---|
25 | |
---|
26 | static hasMany = [ |
---|
27 | templateStringFields: String, |
---|
28 | templateTextFields: String, |
---|
29 | templateStringListFields: TemplateFieldListItem, |
---|
30 | templateIntegerFields: int, |
---|
31 | templateFloatFields: float, |
---|
32 | templateDoubleFields: double, |
---|
33 | templateDateFields: Date, |
---|
34 | templateTermFields: Term |
---|
35 | ] |
---|
36 | |
---|
37 | static constraints = { |
---|
38 | template(nullable: true, blank: true) |
---|
39 | |
---|
40 | } |
---|
41 | |
---|
42 | static mapping = { |
---|
43 | tablePerHierarchy false |
---|
44 | |
---|
45 | templateTextFields type: 'text' |
---|
46 | } |
---|
47 | |
---|
48 | private Map getStore(TemplateFieldType fieldType) { |
---|
49 | switch(fieldType) { |
---|
50 | case TemplateFieldType.STRING: |
---|
51 | return templateStringFields |
---|
52 | case TemplateFieldType.TEXT: |
---|
53 | return templateTextFields |
---|
54 | case TemplateFieldType.STRINGLIST: |
---|
55 | return templateStringListFields |
---|
56 | case TemplateFieldType.INTEGER: |
---|
57 | return templateIntegerFields |
---|
58 | case TemplateFieldType.DATE: |
---|
59 | return templateDateFields |
---|
60 | case TemplateFieldType.FLOAT: |
---|
61 | return templateFloatFields |
---|
62 | case TemplateFieldType.DOUBLE: |
---|
63 | return templateDoubleFields |
---|
64 | case TemplateFieldType.ONTOLOGYTERM: |
---|
65 | return templateTermFields |
---|
66 | default: |
---|
67 | throw new NoSuchFieldException("Field type ${fieldType} not recognized") |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Find a template field by its name and return its value for this subject |
---|
73 | * @param fieldName The name of the template subject field |
---|
74 | * @return the value of the field (class depends on the field type) |
---|
75 | * @throws NoSuchFieldException If the field is not found or the field type is not supported |
---|
76 | */ |
---|
77 | def getFieldValue(String fieldName) { |
---|
78 | TemplateFieldType fieldType = template.getFieldType(fieldName) |
---|
79 | if (!fieldType) throw new NoSuchFieldException("Field name ${fieldName} not recognized") |
---|
80 | getStore(fieldType)[fieldName] |
---|
81 | } |
---|
82 | |
---|
83 | def setFieldValue(String fieldName, value) { |
---|
84 | if (this.properties.containsKey(fieldName)) { |
---|
85 | this[fieldName] = value |
---|
86 | } |
---|
87 | else |
---|
88 | if (template == null) { |
---|
89 | throw new NoSuchFieldException("Field ${fieldName} not found in class properties") |
---|
90 | } |
---|
91 | else { |
---|
92 | TemplateField field = this.template.fields.find { it.name == fieldName} |
---|
93 | if (field == null) { |
---|
94 | throw new NoSuchFieldException("Field ${fieldName} not found in class properties or template fields") |
---|
95 | } |
---|
96 | else { |
---|
97 | if (field.type == TemplateFieldType.STRINGLIST && value.class == String) { |
---|
98 | // Convenience setter: find template item by name |
---|
99 | value = field.listEntries.find { it.name == value } |
---|
100 | } |
---|
101 | // Caution: this assumes that all template...Field Maps are already initialized |
---|
102 | // Otherwise, the results are pretty much unpredictable! |
---|
103 | getStore(field.type)[fieldName] = value |
---|
104 | return this |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | def Set<TemplateField> giveFields() { |
---|
112 | return this.template.fields; |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | // See revision 237 for ideas about initializing the different templateField Maps |
---|
117 | // with tailored Maps that already contain the neccessary keys |
---|
118 | /** |
---|
119 | * Convenience method. Returns all unique templates used within a collection of TemplateEntities. |
---|
120 | */ |
---|
121 | static List<Template> giveTemplates(Set<TemplateEntity> entityCollection) { |
---|
122 | return entityCollection*.template.unique(); |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * Convenience method. Returns the template used within a collection of TemplateEntities. |
---|
127 | * @throws NoSuchFieldException when 0 or multiple templates are used in the collection |
---|
128 | * @return The template used by all members of a collection |
---|
129 | */ |
---|
130 | static Template giveSingleTemplate(Set<TemplateEntity> entityCollection) { |
---|
131 | def templates = giveTemplates(entityCollection); |
---|
132 | if (templates.size() == 0) { |
---|
133 | throw new NoSuchFieldException("No templates found in collection!") |
---|
134 | } |
---|
135 | else if (templates.size() == 1) { |
---|
136 | return templates[0]; |
---|
137 | } |
---|
138 | else { |
---|
139 | throw new NoSuchFieldException("Multiple templates found in collection!") |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | } |
---|