source: trunk/grails-app/domain/dbnp/studycapturing/Subject.groovy @ 1545

Last change on this file since 1545 was 1545, checked in by work@…, 11 years ago
  • removed obsolete bgdt imports
  • Property svn:keywords set to Rev Author Date
File size: 3.2 KB
Line 
1package dbnp.studycapturing
2import org.dbnp.gdt.*
3
4/**
5 * This domain class describes the subjects in a study.
6 *
7 * Revision information:
8 * $Rev: 1545 $
9 * $Author: work@osx.eu $
10 * $Date: 2011-02-22 12:47:28 +0000 (di, 22 feb 2011) $
11 */
12class Subject extends TemplateEntity {
13        // uncommented due to searchable issue
14        // @see http://jira.codehaus.org/browse/GRAILSPLUGINS-1577
15        //static searchable = { [only: ['name']] }
16
17        // A Subject always belongs to one Study
18        static belongsTo = [parent: Study]
19
20        /** The name of the subject, which should be unique within the study  */
21        String name
22
23        /** The species of the subject. In the domainFields property, the ontologies from which this term may come are specified.  */
24        Term species
25
26        static constraints = {
27                // Ensure that the subject name is unique within the study
28                name(unique: ['parent'])
29        }
30
31        static mapping = {
32                name column: "subjectname"
33
34                // Workaround for bug http://jira.codehaus.org/browse/GRAILS-6754
35                templateTextFields type: 'text'
36        }
37
38        /**
39         * return the domain fields for this domain class
40         * @return List
41         */
42        static List<TemplateField> giveDomainFields() { return Subject.domainFields; }
43
44        // We have to specify an ontology list for the species property. However, at compile time, this ontology does of course not exist.
45        // Therefore, the ontology is added at runtime in the bootstrap, possibly downloading the ontology properties if it is not present in the database yet.
46        static List<TemplateField> domainFields = [
47                new TemplateField(
48                        name: 'name',
49                        type: TemplateFieldType.STRING,
50                        preferredIdentifier: true,
51                        comment: 'Use the local subject name or the pre-defined name',
52                        required: true),
53                new TemplateField(
54                        name: 'species',
55                        type: TemplateFieldType.ONTOLOGYTERM,
56                        comment: "The species name is based on the NCI Thesaurus / NCBI organismal classification ontology, a taxonomic classification of living organisms and associated artifacts. If a species is missing, please add it by using 'add more'",
57                        required: true)
58        ]
59
60        /**
61         * Return by default the name of the subject.
62         *
63         * @return name field
64         */
65        String toString() {
66                return name
67        }
68
69        /**
70         * Returns a human readable string of a list of subjects, with a maximum number
71         * of characters
72         *
73         * @param subjectList   List with Subject objects
74         * @param maxChars              maximum number of characters returned
75         * @return                              human readble string with at most maxChars characters, representing the subjects given.
76         */
77        public static String trimSubjectNames( ArrayList subjectList, Integer maxChars ) {
78                def simpleSubjects = subjectList.name.join( ', ' );
79                def showSubjects
80               
81                // If the subjects will fit, show them all
82                if( !maxChars || simpleSubjects.size() < maxChars ) {
83                  showSubjects = simpleSubjects;
84                } else {
85                  // Always add the first name
86                  def subjectNames = subjectList[0]?.name;
87
88                  // Continue adding names until the length is to long
89                  def id = 0;
90                  subjectList.each { subject ->
91                        if( id > 0 ) {
92                          if( subjectNames?.size() + subject.name?.size() < maxChars - 15 ) {
93                                subjectNames += ", " + subject.name;
94                          } else {
95                                return;
96                          }
97                        }
98                        id++;
99                  }
100
101                  // Add a postfix
102                  subjectNames += " and " + ( subjectList?.size() - id ) + " more";
103
104                  showSubjects = subjectNames;
105                }
106
107                return showSubjects
108        }
109}
Note: See TracBrowser for help on using the repository browser.