1 | /** |
---|
2 | * TermEditorController Controller |
---|
3 | * |
---|
4 | * Webflow driven term editor |
---|
5 | * |
---|
6 | * @author Jeroen Wesbeek |
---|
7 | * @since 20100420 |
---|
8 | * @package studycapturing |
---|
9 | * |
---|
10 | * Revision information: |
---|
11 | * $Rev: 997 $ |
---|
12 | * $Author: j.a.m.wesbeek@umail.leidenuniv.nl $ |
---|
13 | * $Date: 2010-10-26 14:33:21 +0000 (di, 26 okt 2010) $ |
---|
14 | */ |
---|
15 | package dbnp.studycapturing |
---|
16 | |
---|
17 | import dbnp.data.Term |
---|
18 | import dbnp.data.Ontology |
---|
19 | |
---|
20 | class TermEditorController { |
---|
21 | /** |
---|
22 | * index closure |
---|
23 | */ |
---|
24 | def index = { |
---|
25 | // got a ontology get parameter? |
---|
26 | def ontologies = (params.ontologies) ? params.ontologies : null |
---|
27 | |
---|
28 | // enter the flow! |
---|
29 | redirect(action: 'pages', params:["ontologies":ontologies]) |
---|
30 | } |
---|
31 | |
---|
32 | /** |
---|
33 | * Webflow |
---|
34 | */ |
---|
35 | def pagesFlow = { |
---|
36 | // start the flow |
---|
37 | onStart { |
---|
38 | println ".start term / ontology editor flow" |
---|
39 | |
---|
40 | if (params.ontologies) { |
---|
41 | flow.ontologies = params.ontologies |
---|
42 | flow.ontologiesList = [] |
---|
43 | params.ontologies.split(/\,/).each() { ncboId -> |
---|
44 | // trim the id |
---|
45 | ncboId.trim() |
---|
46 | |
---|
47 | // and add to the flow scope |
---|
48 | flow.ontologiesList[ flow.ontologies.size() ] = ncboId |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | // main term editor page |
---|
54 | terms { |
---|
55 | render(view: "terms") |
---|
56 | onRender { |
---|
57 | println ".rendering term selection popup" |
---|
58 | } |
---|
59 | on("add") { |
---|
60 | // get ontology by ncboVersionedId |
---|
61 | def ontology = Ontology.findByNcboVersionedId( params.get('term-ontology_id') as int ) |
---|
62 | def strTerm = params.get('term') |
---|
63 | |
---|
64 | // do we have an ontology? |
---|
65 | if (!ontology && params.get('term-ontology_id')) { |
---|
66 | // no, so either this is a new ontology that does not yet |
---|
67 | // exist in the database, or it is a new version of an |
---|
68 | // ontology that is already present in the database |
---|
69 | println ".ontology missing, first fetch ontology information" |
---|
70 | |
---|
71 | // use the NCBO REST service to fetch ontology information |
---|
72 | try { |
---|
73 | def url = "http://rest.bioontology.org/bioportal/ontologies/" + params.get('term-ontology_id') |
---|
74 | def xml = new URL( url ).getText() |
---|
75 | def data = new XmlParser().parseText( xml ) |
---|
76 | def bean = data.data.ontologyBean |
---|
77 | |
---|
78 | // instantiate Ontology with the proper values |
---|
79 | ontology = Ontology.getBioPortalOntologyByVersionedId( params.get('term-ontology_id') ) |
---|
80 | |
---|
81 | // check if this is a newer version of an existing ontology |
---|
82 | def checkOntology = Ontology.findByName( ontology.name ) |
---|
83 | if ( checkOntology ) { |
---|
84 | // this is a newer version of an existing Ontology, update |
---|
85 | // the ontology to a newer version. This is not the best |
---|
86 | // way to handle these updates as we don't know if terms |
---|
87 | // have been updated. However, introducing different versions |
---|
88 | // of Ontologies results into numerous difficulties as well: |
---|
89 | // - what to do with studies that rely on an older ontology |
---|
90 | // - when a new ontology is added, the existing terms of the |
---|
91 | // older version are lacking in the new version |
---|
92 | // - the webservice can only search on ontologyid, not on |
---|
93 | // versions of ncboVersioned id's |
---|
94 | // - if the name has changed between versions this check |
---|
95 | // will not work anymore |
---|
96 | // - etc :) |
---|
97 | // So for now, we will just update the existing ontology with |
---|
98 | // the new information until it becomes clear this needs a |
---|
99 | // more thorough workaround... |
---|
100 | // |
---|
101 | // Jeroen, 20101026 |
---|
102 | |
---|
103 | // update ontology values |
---|
104 | checkOntology.ncboVersionedId = ontology.ncboVersionedId |
---|
105 | checkOntology.versionNumber = ontology.versionNumber |
---|
106 | checkOntology.url = ontology.url |
---|
107 | |
---|
108 | // store the ontology |
---|
109 | if ( checkOntology.validate() ) { |
---|
110 | println ".updated ontology with new version information" |
---|
111 | checkOntology.save(flush:true) |
---|
112 | |
---|
113 | // and use this existing ontology |
---|
114 | ontology = checkOntology |
---|
115 | } |
---|
116 | } else if ( ontology.validate() ) { |
---|
117 | // store the ontology |
---|
118 | println ".adding new ontology" |
---|
119 | ontology.save(flush:true) |
---|
120 | } |
---|
121 | } catch (Exception e) { |
---|
122 | // something went wrong, probably the |
---|
123 | // ontology-id is invalid (hence, the term |
---|
124 | // is invalid) |
---|
125 | println ".oops? --> " + e.getMessage() |
---|
126 | flash.errors = ["We could not add the ontology for this term, please try again"] |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | // got an error? |
---|
131 | if (!flash.errors) { |
---|
132 | // instantiate term with parameters |
---|
133 | def term = new Term( |
---|
134 | name: strTerm, |
---|
135 | ontology: ontology, |
---|
136 | accession: params.get('term-concept_id') |
---|
137 | ) |
---|
138 | |
---|
139 | // validate term |
---|
140 | if (term.validate()) { |
---|
141 | // save the term to the database |
---|
142 | if (term.save(flush:true)) { |
---|
143 | flash.message = "'" + params.get('term') + "' was successfully added, either search for another term to add or close this window" |
---|
144 | success() |
---|
145 | } else { |
---|
146 | flash.errors = ["We encountered a problem while storing the selected term. Please try again."] |
---|
147 | term.errors.each() { println it } |
---|
148 | error() |
---|
149 | } |
---|
150 | } else { |
---|
151 | // term did not validate properly |
---|
152 | term.errors.each() { println it } |
---|
153 | if (term.errors =~ 'unique') { |
---|
154 | flash.errors = ["'" + params.get('term') + "' already exists, either search for another term or close this window"] |
---|
155 | } else { |
---|
156 | flash.errors = ["We encountered a problem while storing the selected term. Please try again."] |
---|
157 | } |
---|
158 | |
---|
159 | error() |
---|
160 | } |
---|
161 | } |
---|
162 | }.to "terms" |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|