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: 1430 $ |
---|
12 | * $Author: work@osx.eu $ |
---|
13 | * $Date: 2011-01-21 20:05:36 +0000 (vr, 21 jan 2011) $ |
---|
14 | */ |
---|
15 | package dbnp.studycapturing |
---|
16 | |
---|
17 | import nl.grails.plugins.gdt.* |
---|
18 | |
---|
19 | class TermEditorController { |
---|
20 | /** |
---|
21 | * index closure |
---|
22 | */ |
---|
23 | def index = { |
---|
24 | // got a ontology get parameter? |
---|
25 | def ontologies = (params.ontologies) ? params.ontologies : null |
---|
26 | |
---|
27 | // enter the flow! |
---|
28 | redirect(action: 'pages', params:["ontologies":ontologies]) |
---|
29 | } |
---|
30 | |
---|
31 | /** |
---|
32 | * Webflow |
---|
33 | */ |
---|
34 | def pagesFlow = { |
---|
35 | // start the flow |
---|
36 | onStart { |
---|
37 | println ".start term / ontology editor flow" |
---|
38 | |
---|
39 | if (params.ontologies) { |
---|
40 | flow.ontologies = params.ontologies |
---|
41 | flow.ontologiesList = [] |
---|
42 | params.ontologies.split(/\,/).each() { ncboId -> |
---|
43 | // trim the id |
---|
44 | ncboId.trim() |
---|
45 | |
---|
46 | // and add to the flow scope |
---|
47 | flow.ontologiesList[ flow.ontologies.size() ] = ncboId |
---|
48 | } |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | // main term editor page |
---|
53 | terms { |
---|
54 | render(view: "terms") |
---|
55 | onRender { |
---|
56 | println ".rendering term selection popup" |
---|
57 | } |
---|
58 | on("add") { |
---|
59 | def ncboId = params.get('term-ncbo_id') |
---|
60 | def ncboVersionedId = params.get('term-ontology_id') |
---|
61 | def ontology = null |
---|
62 | |
---|
63 | try { |
---|
64 | // got the ncboId? |
---|
65 | if (ncboId && ncboId != "null") { |
---|
66 | // find ontology by ncboId |
---|
67 | ontology = Ontology.findByNcboId(ncboId as int) |
---|
68 | } else if (ncboVersionedId && ncboVersionedId != "null") { |
---|
69 | // find ontology by ncboId |
---|
70 | ontology = Ontology.findByNcboVersionedId(ncboVersionedId as int) |
---|
71 | } else { |
---|
72 | // somehow we didn't get both the ncboId as well |
---|
73 | // as the versioned id. Throw an error. |
---|
74 | throw new Exception("We did not receive the ontology with your request, please try again") |
---|
75 | } |
---|
76 | |
---|
77 | // do we have the ontology? |
---|
78 | if (!ontology) { |
---|
79 | // no, try to instantiate by using the BioPortal |
---|
80 | ontology = (ncboId && ncboId != "null") ? Ontology.getBioPortalOntology( ncboId as int ) : Ontology.getBioPortalOntologyByVersionedId( ncboVersionedId as String ) |
---|
81 | |
---|
82 | // validate and save ontology |
---|
83 | if (!(ontology.validate() && ontology.save(flush:true))) { |
---|
84 | if (ncboId && ncboId != "null") { |
---|
85 | throw new Exception("An Ontology with ncboId ${ncboId} (= Ontology ID) does not seem valid. See http://bioportal.bioontology.org/ontologies") |
---|
86 | } else { |
---|
87 | throw new Exception("An Ontology with ncboVersionedId ${ncboVersionedId} (= URL id) does not seem valid. See http://bioportal.bioontology.org/ontologies") |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | // instantiate term with parameters |
---|
93 | def term = new Term( |
---|
94 | name: params.get('term'), |
---|
95 | ontology: ontology, |
---|
96 | accession: params.get('term-concept_id') |
---|
97 | ) |
---|
98 | |
---|
99 | // validate term |
---|
100 | if (term.validate()) { |
---|
101 | // save the term to the database |
---|
102 | if (term.save(flush:true)) { |
---|
103 | flash.message = "'" + params.get('term') + "' was successfully added, either search for another term to add or close this window" |
---|
104 | success() |
---|
105 | } else { |
---|
106 | flash.errors = ["We encountered a problem while storing the selected term. Please try again."] |
---|
107 | term.errors.each() { println it } |
---|
108 | error() |
---|
109 | } |
---|
110 | } else { |
---|
111 | // term did not validate properly |
---|
112 | if (term.errors =~ 'unique') { |
---|
113 | flash.errors = ["'" + params.get('term') + "' already exists, either search for another term or close this window"] |
---|
114 | } else { |
---|
115 | flash.errors = ["We encountered a problem while storing the selected term. Please try again."] |
---|
116 | } |
---|
117 | |
---|
118 | error() |
---|
119 | } |
---|
120 | } catch (Exception e) { |
---|
121 | flash.errors = ["${e.getMessage()}"] |
---|
122 | |
---|
123 | error() |
---|
124 | } |
---|
125 | }.to "terms" |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|