1 | package dbnp.studycapturing |
---|
2 | |
---|
3 | import dbnp.data.Ontology |
---|
4 | |
---|
5 | /** |
---|
6 | * A TemplateField is a specification for either a 'domain field' of a subclass of TemplateEntity or a |
---|
7 | * 'template field' for a specific Template. See the Template class for an explanation of these terms. |
---|
8 | * The TemplateField class contains all information which is needed to specify what kind of data can be stored |
---|
9 | * in this particular field, such as the TemplateFieldType, the name, the ontologies from which terms can derive |
---|
10 | * in case of an ONTOLOGYTERM field, the list entries in case of a STRINGLIST fields, and so on. |
---|
11 | * The actual values of the template fields are stored in instances of subclasses of the TemplateEntity class. |
---|
12 | * For example, if there exists a Study template with a 'description' TemplateField as a member of Template.fields, |
---|
13 | * the actual description for each Study would be stored in the inherited templateStringFields map of that Study instance. |
---|
14 | * |
---|
15 | * One TemplateField can belong to many Templates, but they have to be the same entity as the TemplateField itself. |
---|
16 | * |
---|
17 | * Revision information: |
---|
18 | * $Rev: 754 $ |
---|
19 | * $Author: keesvb $ |
---|
20 | * $Date: 2010-07-30 13:05:41 +0000 (vr, 30 jul 2010) $ |
---|
21 | */ |
---|
22 | class TemplateField implements Serializable { |
---|
23 | |
---|
24 | /** The name of the TemplateField, by which it is represented to the user. */ |
---|
25 | String name |
---|
26 | |
---|
27 | /** The type of this TemplateField, such as STRING, ONTOLOGYTERM etc. */ |
---|
28 | TemplateFieldType type |
---|
29 | |
---|
30 | /** The entity for which this TemplateField is meant. Only Templates for this entity can contain this TemplateField */ |
---|
31 | Class entity |
---|
32 | |
---|
33 | /** The unit of the values of this TemplateField (optional) */ |
---|
34 | String unit |
---|
35 | |
---|
36 | /** The help string which is shown in the user interface to describe this template field (optional, TEXT) */ |
---|
37 | String comment |
---|
38 | |
---|
39 | /** The different list entries for a STRINGLIST TemplateField. This property is only used if type == TemplateFieldType.STRINGLIST */ |
---|
40 | List listEntries |
---|
41 | |
---|
42 | /** Indicates whether this field is required to be filled out or not */ |
---|
43 | boolean required |
---|
44 | |
---|
45 | /** Indicates whether this field is the preferred identifier for the resulting templated entity. |
---|
46 | This is for example used when importing to match entries in the database against the ones that are being imported. */ |
---|
47 | boolean preferredIdentifier |
---|
48 | |
---|
49 | static hasMany = [ |
---|
50 | listEntries: TemplateFieldListItem, // to store the entries to choose from when the type is 'item from predefined list' |
---|
51 | ontologies: Ontology // to store the ontologies to choose from when the type is 'ontology term' |
---|
52 | ] |
---|
53 | |
---|
54 | static constraints = { |
---|
55 | |
---|
56 | // outcommented for now due to bug in Grails / Hibernate |
---|
57 | // see http://jira.codehaus.org/browse/GRAILS-6020 |
---|
58 | // This is to verify that TemplateField names are unique within templates of each super entity |
---|
59 | // TODO: this probably has to change in the case of private templates of different users, |
---|
60 | // which can co-exist with the same name. See also Template |
---|
61 | // name(unique:['entity']) |
---|
62 | |
---|
63 | name(nullable: false, blank: false) |
---|
64 | type(nullable: false, blank: false) |
---|
65 | entity(nullable: false, blank: false) |
---|
66 | unit(nullable: true, blank: true) |
---|
67 | comment(nullable: true, blank: true) |
---|
68 | required(default: false) |
---|
69 | preferredIdentifier(default: false) |
---|
70 | } |
---|
71 | |
---|
72 | static mapping = { |
---|
73 | // Make sure the comments can be Strings of arbitrary length |
---|
74 | comment type: 'text' |
---|
75 | } |
---|
76 | |
---|
77 | String toString() { |
---|
78 | return name |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * return an escaped name which can be used in business logic |
---|
83 | * @return String |
---|
84 | */ |
---|
85 | def String escapedName() { |
---|
86 | return name.toLowerCase().replaceAll("([^a-z0-9])", "_") |
---|
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 | TemplateField.findAll().each() { |
---|
98 | if (entity.equals(it.entity)) { |
---|
99 | results[results.size()] = it |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | return results |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Checks whether this template field is used in a template |
---|
108 | * |
---|
109 | * @returns true iff this template field is used in a template (even if the template is never used), false otherwise |
---|
110 | */ |
---|
111 | def inUse() { |
---|
112 | return numUses() > 0; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * The number of templates that use this template |
---|
117 | * |
---|
118 | * @returns the number of templates that use this template. |
---|
119 | */ |
---|
120 | def numUses() { |
---|
121 | def templates = Template.findAll(); |
---|
122 | def elements; |
---|
123 | if( templates && templates.size() > 0 ) { |
---|
124 | elements = templates.findAll { template -> template.fields.contains( this ) }; |
---|
125 | } else { |
---|
126 | return 0; |
---|
127 | } |
---|
128 | |
---|
129 | return elements.size(); |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | } |
---|