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