source: trunk/grails-app/domain/dbnp/studycapturing/TemplateField.groovy @ 556

Last change on this file since 556 was 556, checked in by roberth, 12 years ago

Improved the TemplateEditor? again: templates can be created and removed and some checks were built in to ensure that the same fields in different templates point to the same TemplateField? object.

  • Property svn:keywords set to Date Rev Author
File size: 2.5 KB
Line 
1package dbnp.studycapturing
2
3import dbnp.data.Ontology
4
5/**
6 * This is the class for template fields. These should be part of one or more templates via Template.fields
7 *
8 * Revision information:
9 * $Rev: 556 $
10 * $Author: roberth $
11 * $Date: 2010-06-10 14:45:53 +0000 (do, 10 jun 2010) $
12 */
13class TemplateField implements Serializable {
14        String name
15        TemplateFieldType type
16    Class entity
17        String unit
18        String comment // help string for the user interface
19        List listEntries
20        boolean required
21        boolean preferredIdentifier
22
23        static hasMany = [
24                listEntries: TemplateFieldListItem,     // to store the entries to choose from when the type is 'item from predefined list'
25                ontologies: Ontology                                    // to store the ontologies to choose from when the type is 'ontology term'
26        ]
27
28        static constraints = {
29                // TODO: verify that TemplateField names are unique within templates of each super entity
30                name(nullable: false, blank: false)
31                type(nullable: false, blank: false)
32                entity(nullable: false, blank: false)
33                unit(nullable: true, blank: true)
34                comment(nullable: true, blank: true)
35                required(default: false)
36                preferredIdentifier(default: false)
37        }
38
39        static mapping = {
40                // TODO: this doesn't seem to work in Postgres
41                comment type: 'text'
42        }
43
44        String toString() {
45                return name
46        }
47
48        /**
49         * return an escaped name which can be used in business logic
50         * @return String
51         */
52        def String escapedName() {
53                return name.toLowerCase().replaceAll("([^a-z0-9])", "_")
54        }
55
56        /**
57         * overloading the findAllByEntity method to make it function as expected
58         * @param Class entity (for example: dbnp.studycapturing.Subject)
59         * @return ArrayList
60         */
61        public static findAllByEntity(java.lang.Class entity) {
62                def results = []
63                // 'this' should not work in static context, so taking Template instead of this
64                TemplateField.findAll().each() {
65                        if (entity.equals(it.entity)) {
66                                results[results.size()] = it
67                        }
68                }
69
70                return results
71        }
72
73        /**
74         * Checks whether this template field is used in a template
75         *
76         * @returns             true iff this template field is used in a template (even if the template is never used), false otherwise
77         */
78        def inUse() {
79                return numUses() > 0;
80        }
81
82        /**
83         * The number of templates that use this template
84         *
85         * @returns             the number of templates that use this template.
86         */
87        def numUses() {
88                def templates = Template.findAll();
89                def elements;
90                if( templates && templates.size() > 0 ) {
91                        elements = templates.findAll { template -> template.fields.contains( this ) };
92                } else {
93                        return 0;
94                }
95
96                return elements.size();
97        }
98
99
100}
Note: See TracBrowser for help on using the repository browser.