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