1 | /** |
---|
2 | * TemplateEditorController Controler |
---|
3 | * |
---|
4 | * Webflow driven template editor |
---|
5 | * |
---|
6 | * @author Jeroen Wesbeek |
---|
7 | * @since 20100415 |
---|
8 | * @package studycapturing |
---|
9 | * |
---|
10 | * Revision information: |
---|
11 | * $Rev: 1029 $ |
---|
12 | * $Author: robert@isdat.nl $ |
---|
13 | * $Date: 2010-11-01 12:17:38 +0000 (ma, 01 nov 2010) $ |
---|
14 | */ |
---|
15 | package dbnp.studycapturing |
---|
16 | import dbnp.data.* |
---|
17 | import dbnp.studycapturing.* |
---|
18 | |
---|
19 | import dbnp.authentication.AuthenticationService |
---|
20 | import grails.plugins.springsecurity.Secured |
---|
21 | |
---|
22 | import cr.co.arquetipos.crypto.Blowfish |
---|
23 | import grails.converters.* |
---|
24 | import java.lang.reflect.*; |
---|
25 | |
---|
26 | @Secured(['IS_AUTHENTICATED_REMEMBERED']) |
---|
27 | class TemplateEditorController { |
---|
28 | def entityName; |
---|
29 | def entity; |
---|
30 | def AuthenticationService |
---|
31 | |
---|
32 | /** |
---|
33 | * Fires after every action and determines the layout of the page |
---|
34 | */ |
---|
35 | def afterInterceptor = { model, modelAndView -> |
---|
36 | if ( params['standalone'] && params['standalone'] == 'true') { |
---|
37 | model.layout = 'main'; |
---|
38 | model.extraparams = [ 'standalone': 'true' ] ; |
---|
39 | } else { |
---|
40 | model.layout = 'dialog'; |
---|
41 | model.extraparams = [] ; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Study template editor page |
---|
47 | */ |
---|
48 | def study = { |
---|
49 | showEntity('dbnp.studycapturing.Study') |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Subject template editor page |
---|
54 | */ |
---|
55 | def subject = { |
---|
56 | showEntity('dbnp.studycapturing.Subject') |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Event template editor page |
---|
61 | */ |
---|
62 | def event = { |
---|
63 | showEntity('dbnp.studycapturing.Event') |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * Sampling Event template editor page |
---|
68 | */ |
---|
69 | def samplingEvent = { |
---|
70 | showEntity('dbnp.studycapturing.SamplingEvent') |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Event template editor page |
---|
75 | */ |
---|
76 | def sample = { |
---|
77 | showEntity('dbnp.studycapturing.Sample') |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Show the template editor page for a particular entity |
---|
82 | * @param targetEntity The full class name of the target entity |
---|
83 | */ |
---|
84 | private void showEntity(String targetEntity) { |
---|
85 | String resultEntity |
---|
86 | if (grailsApplication.config.crypto) { |
---|
87 | // if a shared secret is defined, encrypt using that |
---|
88 | resultEntity = Blowfish.encryptBase64( |
---|
89 | targetEntity.toString(), |
---|
90 | grailsApplication.config.crypto.shared.secret |
---|
91 | ) |
---|
92 | } |
---|
93 | else { |
---|
94 | // otherwise use standard encoding |
---|
95 | resultEntity = targetEntity.toString().bytes.encodeBase64() |
---|
96 | } |
---|
97 | |
---|
98 | // redirect to template editor page of the specified entity |
---|
99 | params.entity = resultEntity |
---|
100 | redirect(action: "index", params:params) |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * index closure |
---|
105 | */ |
---|
106 | def index = { |
---|
107 | // Check whether a right entity is given |
---|
108 | if( !_checkEntity() ) { |
---|
109 | return |
---|
110 | } |
---|
111 | |
---|
112 | // fetch all templates for this entity |
---|
113 | def templates = Template.findAllByEntity(entity) |
---|
114 | |
---|
115 | // Generate a human readable entity name |
---|
116 | def parts = entityName.tokenize( '.' ); |
---|
117 | def humanReadableEntity = parts[ parts.size() - 1 ]; |
---|
118 | |
---|
119 | return [ |
---|
120 | entity: entity, |
---|
121 | templates: templates, |
---|
122 | encryptedEntity: params.entity, |
---|
123 | humanReadableEntity: humanReadableEntity, |
---|
124 | ontologies: params.ontologies |
---|
125 | ]; |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * compare two or more templates |
---|
130 | */ |
---|
131 | def compare = { |
---|
132 | // Check whether a right entity is given |
---|
133 | if( !_checkEntity() ) { |
---|
134 | return |
---|
135 | } |
---|
136 | |
---|
137 | // fetch all templates for this entity |
---|
138 | def templates = Template.findAllByEntity(entity) |
---|
139 | |
---|
140 | // Find all available fields |
---|
141 | def allFields = TemplateField.findAllByEntity( entity ).sort { a, b -> a.name <=> b.name } |
---|
142 | |
---|
143 | // Generate a human readable entity name |
---|
144 | def parts = entityName.tokenize( '.' ); |
---|
145 | def humanReadableEntity = parts[ parts.size() - 1 ]; |
---|
146 | |
---|
147 | return [ |
---|
148 | entity: entity, |
---|
149 | templates: templates, |
---|
150 | allFields: allFields, |
---|
151 | encryptedEntity: params.entity, |
---|
152 | humanReadableEntity: humanReadableEntity, |
---|
153 | ontologies: params.ontologies, |
---|
154 | templateEntities: this.templateEntityList() |
---|
155 | ]; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Shows the editing of a template |
---|
160 | */ |
---|
161 | def template = { |
---|
162 | // Check whether a right entity is given |
---|
163 | if( !_checkEntity() ) { |
---|
164 | return |
---|
165 | } |
---|
166 | |
---|
167 | // Check whether a template is selected. If not, redirect the user to the index |
---|
168 | def selectedTemplate = params.template; |
---|
169 | def template = null; |
---|
170 | def domainFields = null; |
---|
171 | |
---|
172 | if( selectedTemplate ) { |
---|
173 | template = Template.get( selectedTemplate ); |
---|
174 | domainFields = template.entity.giveDomainFields(); |
---|
175 | } else { |
---|
176 | redirect(action:"index",params:[entity:params.entity]) |
---|
177 | return; |
---|
178 | } |
---|
179 | |
---|
180 | // fetch all templates for this entity |
---|
181 | def templates = Template.findAllByEntity(entity) |
---|
182 | |
---|
183 | // Generate a human readable entity name |
---|
184 | def parts = entityName.tokenize( '.' ); |
---|
185 | def humanReadableEntity = parts[ parts.size() - 1 ]; |
---|
186 | |
---|
187 | // Find all available fields |
---|
188 | def allFields = TemplateField.findAllByEntity( entity ).sort { a, b -> a.name <=> b.name } |
---|
189 | |
---|
190 | return [ |
---|
191 | entity: entity, |
---|
192 | templates: templates, |
---|
193 | encryptedEntity: params.entity, |
---|
194 | fieldTypes: TemplateFieldType.list(), |
---|
195 | ontologies: Ontology.list(), |
---|
196 | humanReadableEntity: humanReadableEntity, |
---|
197 | |
---|
198 | template: template, |
---|
199 | allFields: allFields, |
---|
200 | domainFields: domainFields |
---|
201 | ]; |
---|
202 | |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | /** |
---|
207 | * Shows an error page |
---|
208 | * |
---|
209 | * TODO: improve the error page |
---|
210 | */ |
---|
211 | def error = { |
---|
212 | render( 'view': 'error' ); |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * Creates a new template using a AJAX call |
---|
217 | * |
---|
218 | * @return JSON object with two entries: |
---|
219 | * id: [id of this object] |
---|
220 | * html: HTML to replace the contents of the LI-item that was updated. |
---|
221 | * On error the method gives a HTTP response status 500 and the error |
---|
222 | */ |
---|
223 | def createTemplate = { |
---|
224 | // Decode the entity |
---|
225 | if( !_checkEntity() ) { |
---|
226 | response.status = 500; |
---|
227 | render "Incorrect entity given"; |
---|
228 | return; |
---|
229 | } |
---|
230 | |
---|
231 | // set entity |
---|
232 | params.entity = entity; |
---|
233 | |
---|
234 | // Create the template fields and add it to the template |
---|
235 | def template = new Template( params ); |
---|
236 | if (template.validate() && template.save(flush: true)) { |
---|
237 | def html = g.render( template: 'elements/liTemplate', model: [template: template] ); |
---|
238 | def output = [ id: template.id, html: html ]; |
---|
239 | render output as JSON; |
---|
240 | } else { |
---|
241 | response.status = 500; |
---|
242 | render 'Template could not be created because errors occurred.'; |
---|
243 | return |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * Clones a template using a AJAX call |
---|
249 | * |
---|
250 | * @return JSON object with two entries: |
---|
251 | * id: [id of this object] |
---|
252 | * html: HTML of the contents of the LI-item that will be added. |
---|
253 | * On error the method gives a HTTP response status 500 and the error |
---|
254 | */ |
---|
255 | def cloneTemplate = { |
---|
256 | // Search for the template field |
---|
257 | def template = Template.get( params.id ); |
---|
258 | if( !template ) { |
---|
259 | response.status = 404; |
---|
260 | render 'Template not found'; |
---|
261 | return; |
---|
262 | } |
---|
263 | |
---|
264 | // Create the template fields and add it to the template |
---|
265 | def newTemplate = new Template( template, authenticationService.getLoggedInUser() ); |
---|
266 | if (newTemplate.validate() && newTemplate.save(flush: true)) { |
---|
267 | def html = g.render( template: 'elements/liTemplate', model: [template: newTemplate] ); |
---|
268 | def output = [ id: newTemplate.id, html: html ]; |
---|
269 | render output as JSON; |
---|
270 | } else { |
---|
271 | response.status = 500; |
---|
272 | render 'Template could not be cloned because errors occurred.'; |
---|
273 | return |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * Updates a selected template using a AJAX call |
---|
279 | * |
---|
280 | * @param id ID of the template to update |
---|
281 | * @return JSON object with two entries: |
---|
282 | * id: [id of this object] |
---|
283 | * html: HTML to replace the contents of the LI-item that was updated. |
---|
284 | * On error the method gives a HTTP response status 500 and the error |
---|
285 | */ |
---|
286 | def updateTemplate = { |
---|
287 | // Search for the template field |
---|
288 | def template = Template.get( params.id ); |
---|
289 | if( !template ) { |
---|
290 | response.status = 404; |
---|
291 | render 'Template not found'; |
---|
292 | return; |
---|
293 | } |
---|
294 | |
---|
295 | // Update the field if it is not updated in between |
---|
296 | if (params.version) { |
---|
297 | def version = params.version.toLong() |
---|
298 | if (template.version > version) { |
---|
299 | response.status = 500; |
---|
300 | render 'Template was updated while you were working on it. Please reload and try again.'; |
---|
301 | return |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | template.properties = params |
---|
306 | if (!template.hasErrors() && template.save(flush: true)) { |
---|
307 | def html = g.render( template: 'elements/liTemplate', model: [template: template] ); |
---|
308 | def output = [ id: template.id, html: html ]; |
---|
309 | render output as JSON; |
---|
310 | } else { |
---|
311 | response.status = 500; |
---|
312 | render 'Template was not updated because errors occurred.'; |
---|
313 | return |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | * Deletes a template using a AJAX call |
---|
319 | * |
---|
320 | * @param template ID of the template to move |
---|
321 | * @return JSON object with one entry: |
---|
322 | * id: [id of this object] |
---|
323 | * On error the method gives a HTTP response status 500 and the error |
---|
324 | */ |
---|
325 | def deleteTemplate = { |
---|
326 | // Search for the template field |
---|
327 | def template = Template.get( params.template ); |
---|
328 | if( !template ) { |
---|
329 | response.status = 404; |
---|
330 | render 'Template not found'; |
---|
331 | return; |
---|
332 | } |
---|
333 | |
---|
334 | // Delete the template field |
---|
335 | try { |
---|
336 | template.delete(flush: true) |
---|
337 | |
---|
338 | def output = [ id: template.id ]; |
---|
339 | render output as JSON; |
---|
340 | } |
---|
341 | catch (org.springframework.dao.DataIntegrityViolationException e) { |
---|
342 | response.status = 500; |
---|
343 | render 'Template could not be deleted: ' + e.getMessage(); |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | * Creates a new template field using a AJAX call |
---|
349 | * |
---|
350 | * @param template ID of the template to add a field to |
---|
351 | * @return JSON object with two entries: |
---|
352 | * id: [id of this object] |
---|
353 | * html: HTML to replace the contents of the LI-item that was updated. |
---|
354 | * On error the method gives a HTTP response status 500 and the error |
---|
355 | */ |
---|
356 | def createField = { |
---|
357 | // Search for the template |
---|
358 | def template = Template.get( params.template ); |
---|
359 | |
---|
360 | if( !template ) { |
---|
361 | response.status = 404; |
---|
362 | render 'Template not found'; |
---|
363 | return; |
---|
364 | } |
---|
365 | |
---|
366 | // Decode the entity, in order to set a good property |
---|
367 | if( !_checkEntity() ) { |
---|
368 | response.status = 500; |
---|
369 | render "Incorrect entity given"; |
---|
370 | return; |
---|
371 | } |
---|
372 | |
---|
373 | params.entity = entity; |
---|
374 | |
---|
375 | // See whether this field already exists. It is checked by name, type and unit and entity |
---|
376 | // The search is done using search by example (see http://grails.org/DomainClass+Dynamic+Methods, method find) |
---|
377 | def uniqueParams = [ name: params.name, type: params.type, unit: params.unit, entity: params.entity ]; |
---|
378 | if( TemplateField.find( new TemplateField( uniqueParams ) ) ) { |
---|
379 | response.status = 500; |
---|
380 | render "A field with this name, type and unit already exists."; |
---|
381 | return; |
---|
382 | } |
---|
383 | |
---|
384 | // See whether this exists as domain field. If it does, raise an error |
---|
385 | def domainFields = template.entity.giveDomainFields() |
---|
386 | if( domainFields.find { it.name.toLowerCase() == params.name.toLowerCase() } ) { |
---|
387 | response.status = 500; |
---|
388 | render "All templates for entity " + template.entity + " contain a domain field with name " + params.name + ". You can not create a field with this name.";; |
---|
389 | return; |
---|
390 | } |
---|
391 | |
---|
392 | // If this field is type stringlist, we have to prepare the parameters |
---|
393 | if( params.type.toString() == 'STRINGLIST' ) { |
---|
394 | def listEntries = []; |
---|
395 | params.listEntries.eachLine { |
---|
396 | // Search whether a listitem with this name already exists. |
---|
397 | // if it does, use that one to prevent pollution of the database |
---|
398 | def name = it.trim(); |
---|
399 | def listitem = TemplateFieldListItem.findByName( name );7 |
---|
400 | if( !listitem ) { |
---|
401 | listitem = new TemplateFieldListItem( name: name ) |
---|
402 | } |
---|
403 | listEntries.add( listitem ); |
---|
404 | } |
---|
405 | |
---|
406 | params.listEntries = listEntries; |
---|
407 | } else { |
---|
408 | params.remove( 'listEntries' ); |
---|
409 | } |
---|
410 | |
---|
411 | // If this field isnot a ontologyterm, we should remove the ontologies |
---|
412 | if( params.type.toString() != 'ONTOLOGYTERM' ) { |
---|
413 | params.remove( 'ontologies' ); |
---|
414 | } |
---|
415 | |
---|
416 | // Create the template field and add it to the template |
---|
417 | def templateField = new TemplateField( params ); |
---|
418 | if (templateField.save(flush: true)) { |
---|
419 | |
---|
420 | def html = g.render( template: 'elements/available', model: [templateField: templateField, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()] ); |
---|
421 | def output = [ id: templateField.id, html: html ]; |
---|
422 | render output as JSON; |
---|
423 | |
---|
424 | //render ''; |
---|
425 | } else { |
---|
426 | response.status = 500; |
---|
427 | render 'TemplateField could not be created because errors occurred.'; |
---|
428 | return |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | /** |
---|
433 | * Updates a selected template field using a AJAX call |
---|
434 | * |
---|
435 | * @param id ID of the field to update |
---|
436 | * @return JSON object with two entries: |
---|
437 | * id: [id of this object] |
---|
438 | * html: HTML to replace the contents of the LI-item that was updated. |
---|
439 | * On error the method gives a HTTP response status 500 and the error |
---|
440 | */ |
---|
441 | def updateField = { |
---|
442 | // Search for the template field |
---|
443 | def templateField = TemplateField.get( params.id ); |
---|
444 | if( !templateField ) { |
---|
445 | response.status = 404; |
---|
446 | render 'TemplateField not found'; |
---|
447 | return; |
---|
448 | } |
---|
449 | |
---|
450 | // Update the field if it is not updated in between |
---|
451 | if (params.version) { |
---|
452 | def version = params.version.toLong() |
---|
453 | if (templateField.version > version) { |
---|
454 | response.status = 500; |
---|
455 | render 'TemplateField was updated while you were working on it. Please reload and try again.'; |
---|
456 | return |
---|
457 | } |
---|
458 | } |
---|
459 | |
---|
460 | // If this field is type stringlist or ontology, we have to prepare the parameters |
---|
461 | if( params.type.toString() == 'STRINGLIST' ) { |
---|
462 | def listEntries = []; |
---|
463 | params.listEntries.eachLine { |
---|
464 | // Search whether a listitem with this name already exists. |
---|
465 | // if it does, use that one to prevent pollution of the database |
---|
466 | def name = it.trim(); |
---|
467 | def listitem = TemplateFieldListItem.findByName( name );7 |
---|
468 | if( !listitem ) { |
---|
469 | listitem = new TemplateFieldListItem( name: name ) |
---|
470 | } |
---|
471 | listEntries.add( listitem ); |
---|
472 | } |
---|
473 | |
---|
474 | params.listEntries = listEntries; |
---|
475 | } else { |
---|
476 | params.remove( 'listEntries' ); |
---|
477 | } |
---|
478 | |
---|
479 | // If this field is a ontologyterm, we add ontology objects |
---|
480 | if( params.type.toString() == 'ONTOLOGYTERM' && params.ontologies ) { |
---|
481 | params.ontologies = Ontology.getAll( params.ontologies.collect { Integer.parseInt( it ) } ); |
---|
482 | } else { |
---|
483 | params.remove( 'ontologies' ); |
---|
484 | } |
---|
485 | |
---|
486 | // Set all parameters |
---|
487 | templateField.properties = params |
---|
488 | if (!templateField.hasErrors() && templateField.save(flush: true)) { |
---|
489 | def html = g.render( template: 'elements/available', model: [templateField: templateField, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()] ); |
---|
490 | def output = [ id: templateField.id, html: html ]; |
---|
491 | render output as JSON; |
---|
492 | } else { |
---|
493 | response.status = 500; |
---|
494 | render 'TemplateField was not updated because errors occurred.'; |
---|
495 | return |
---|
496 | } |
---|
497 | } |
---|
498 | |
---|
499 | /** |
---|
500 | * Deletes a template field using a AJAX call |
---|
501 | * |
---|
502 | * @param templateField ID of the templatefield to move |
---|
503 | * @return JSON object with one entry: |
---|
504 | * id: [id of this object] |
---|
505 | * On error the method gives a HTTP response status 500 and the error |
---|
506 | */ |
---|
507 | def deleteField = { |
---|
508 | // Search for the template field |
---|
509 | def templateField = TemplateField.get( params.templateField ); |
---|
510 | if( !templateField ) { |
---|
511 | response.status = 404; |
---|
512 | render 'TemplateField not found'; |
---|
513 | return; |
---|
514 | } |
---|
515 | |
---|
516 | // Delete the template field |
---|
517 | try { |
---|
518 | templateField.delete(flush: true) |
---|
519 | |
---|
520 | def output = [ id: templateField.id ]; |
---|
521 | render output as JSON; |
---|
522 | } |
---|
523 | catch (org.springframework.dao.DataIntegrityViolationException e) { |
---|
524 | response.status = 500; |
---|
525 | render 'TemplateField could not be deleted: ' + e.getMessage(); |
---|
526 | } |
---|
527 | } |
---|
528 | |
---|
529 | /** |
---|
530 | * Adds a new template field to a template using a AJAX call |
---|
531 | * |
---|
532 | * @param template ID of the template to add a field to |
---|
533 | * @return JSON object with two entries: |
---|
534 | * id: [id of this object] |
---|
535 | * html: HTML to replace the contents of the LI-item that was updated. |
---|
536 | * On error the method gives a HTTP response status 404 or 500 and the error |
---|
537 | */ |
---|
538 | def addField = { |
---|
539 | // Search for the template |
---|
540 | def template = Template.get( params.template ); |
---|
541 | |
---|
542 | if( !template ) { |
---|
543 | response.status = 404; |
---|
544 | render 'Template not found'; |
---|
545 | return; |
---|
546 | } |
---|
547 | |
---|
548 | // Search for the template field |
---|
549 | def templateField = TemplateField.get( params.templateField ); |
---|
550 | if( !templateField ) { |
---|
551 | response.status = 404; |
---|
552 | render 'TemplateField does not exist'; |
---|
553 | return; |
---|
554 | } |
---|
555 | |
---|
556 | // The template field should exist within the template |
---|
557 | if( template.fields.contains( templateField ) ) { |
---|
558 | response.status = 500; |
---|
559 | render 'TemplateField is already found within template'; |
---|
560 | return; |
---|
561 | } |
---|
562 | |
---|
563 | // If the template is in use, only non-required fields can be added |
---|
564 | if( template.inUse() && templateField.required ) { |
---|
565 | response.status = 500; |
---|
566 | render 'Only non-required fields can be added to templates that are in use.' |
---|
567 | return; |
---|
568 | } |
---|
569 | |
---|
570 | // All field names within a template should be unique |
---|
571 | if( template.fields.find { it.name.toLowerCase() == templateField.name.toLowerCase() } ) { |
---|
572 | response.status = 500; |
---|
573 | render 'This template already contains a field with name ' + templateField.name + '. Field names should be unique within a template.' |
---|
574 | return; |
---|
575 | } |
---|
576 | |
---|
577 | if( !params.position || Integer.parseInt( params.position ) == -1) { |
---|
578 | template.fields.add( templateField ) |
---|
579 | } else { |
---|
580 | template.fields.add( Integer.parseInt( params.position ), templateField ) |
---|
581 | } |
---|
582 | |
---|
583 | if (!template.validate()) { |
---|
584 | response.status = 500; |
---|
585 | template.errors.each { render it} |
---|
586 | } |
---|
587 | template.save(flush:true); |
---|
588 | |
---|
589 | def html = g.render( template: 'elements/selected', model: [templateField: templateField, template: template, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()] ); |
---|
590 | def output = [ id: templateField.id, html: html ]; |
---|
591 | render output as JSON; |
---|
592 | } |
---|
593 | |
---|
594 | /** |
---|
595 | * Removes a selected template field from the template using a AJAX call |
---|
596 | * |
---|
597 | * @param templateField ID of the field to update |
---|
598 | * @param template ID of the template for which the field should be removed |
---|
599 | * @return JSON object with two entries: |
---|
600 | * id: [id of this object] |
---|
601 | * html: HTML to replace the contents of the LI-item that was updated. |
---|
602 | * On error the method gives a HTTP response status 404 or 500 and the error |
---|
603 | */ |
---|
604 | def removeField = { |
---|
605 | // Search for the template |
---|
606 | def template = Template.get( params.template ); |
---|
607 | |
---|
608 | if( !template ) { |
---|
609 | response.status = 404; |
---|
610 | render 'Template not found'; |
---|
611 | return; |
---|
612 | } |
---|
613 | |
---|
614 | // Search for the template field |
---|
615 | def templateField = TemplateField.get( params.templateField ); |
---|
616 | if( !templateField ) { |
---|
617 | response.status = 404; |
---|
618 | render 'TemplateField not found'; |
---|
619 | return; |
---|
620 | } |
---|
621 | |
---|
622 | // The template field should exist within the template |
---|
623 | if( !template.fields.contains( templateField ) ) { |
---|
624 | response.status = 404; |
---|
625 | render 'TemplateField not found within template'; |
---|
626 | return; |
---|
627 | } |
---|
628 | |
---|
629 | // If the template is in use, field can not be removed |
---|
630 | if( template.inUse() ) { |
---|
631 | response.status = 500; |
---|
632 | render 'No fields can be removed from templates that are in use.' |
---|
633 | return; |
---|
634 | } |
---|
635 | |
---|
636 | // Delete the field from this template |
---|
637 | def currentIndex = template.fields.indexOf( templateField ); |
---|
638 | template.fields.remove( currentIndex ); |
---|
639 | template.save(flush:true); |
---|
640 | |
---|
641 | |
---|
642 | def html = g.render( template: 'elements/available', model: [templateField: templateField, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()] ); |
---|
643 | def output = [ id: templateField.id, html: html ]; |
---|
644 | render output as JSON; |
---|
645 | } |
---|
646 | |
---|
647 | /** |
---|
648 | * Moves a template field using a AJAX call |
---|
649 | * |
---|
650 | * @param template ID of the template that contains this field |
---|
651 | * @param templateField ID of the templatefield to move |
---|
652 | * @param position New index of the templatefield in the array. The index is 0-based. |
---|
653 | * @return JSON object with two entries: |
---|
654 | * id: [id of this object] |
---|
655 | * html: HTML to replace the contents of the LI-item that was updated. |
---|
656 | * On error the method gives a HTTP response status 500 and the error |
---|
657 | */ |
---|
658 | def moveField = { |
---|
659 | // Search for the template |
---|
660 | def template = Template.get( params.template ); |
---|
661 | |
---|
662 | if( !template ) { |
---|
663 | response.status = 404; |
---|
664 | render 'Template not found'; |
---|
665 | return; |
---|
666 | } |
---|
667 | |
---|
668 | // Search for the template field |
---|
669 | def templateField = TemplateField.get( params.templateField ); |
---|
670 | if( !templateField ) { |
---|
671 | response.status = 404; |
---|
672 | render 'TemplateField not found'; |
---|
673 | return; |
---|
674 | } |
---|
675 | |
---|
676 | // The template field should exist within the template |
---|
677 | if( !template.fields.contains( templateField ) ) { |
---|
678 | response.status = 404; |
---|
679 | render 'TemplateField not found within template'; |
---|
680 | return; |
---|
681 | } |
---|
682 | |
---|
683 | // Move the item |
---|
684 | def currentIndex = template.fields.indexOf( templateField ); |
---|
685 | def moveField = template.fields.remove( currentIndex ); |
---|
686 | |
---|
687 | println( "Old: " + currentIndex + " - New: " + params.position ); |
---|
688 | |
---|
689 | template.fields.add( Integer.parseInt( params.position ), moveField ); |
---|
690 | template.save(flush:true); |
---|
691 | |
---|
692 | def html = g.render( template: 'elements/selected', model: [templateField: templateField, template: template, fieldTypes: TemplateFieldType.list()] ); |
---|
693 | def output = [ id: templateField.id, html: html ]; |
---|
694 | render output as JSON; |
---|
695 | } |
---|
696 | |
---|
697 | /** |
---|
698 | * Checks how many template use a specific template field |
---|
699 | * |
---|
700 | * @param id ID of the template field |
---|
701 | * @return int Number of uses |
---|
702 | */ |
---|
703 | def numFieldUses = { |
---|
704 | // Search for the template field |
---|
705 | def templateField = TemplateField.get( params.id ); |
---|
706 | if( !templateField ) { |
---|
707 | response.status = 404; |
---|
708 | render 'TemplateField not found'; |
---|
709 | return; |
---|
710 | } |
---|
711 | |
---|
712 | render templateField.numUses(); |
---|
713 | } |
---|
714 | |
---|
715 | /** |
---|
716 | * Adds a ontolgy based on the ID given |
---|
717 | * |
---|
718 | * @param ncboID |
---|
719 | * @return JSON Ontology object |
---|
720 | */ |
---|
721 | def addOntologyById = { |
---|
722 | def id = params.ncboID; |
---|
723 | |
---|
724 | if( !id ) { |
---|
725 | response.status = 500; |
---|
726 | render 'No ID given' |
---|
727 | return; |
---|
728 | } |
---|
729 | |
---|
730 | if( Ontology.findByNcboId( Integer.parseInt( id ) ) ) { |
---|
731 | response.status = 500; |
---|
732 | render 'This ontology was already added to the system'; |
---|
733 | return; |
---|
734 | } |
---|
735 | |
---|
736 | def ontology = null; |
---|
737 | |
---|
738 | try { |
---|
739 | ontology = dbnp.data.Ontology.getBioPortalOntology( id ); |
---|
740 | } catch( Exception e ) { |
---|
741 | response.status = 500; |
---|
742 | render e.getMessage(); |
---|
743 | return; |
---|
744 | } |
---|
745 | |
---|
746 | if( !ontology ) { |
---|
747 | response.status = 404; |
---|
748 | render 'Ontology with ID ' + id + ' not found'; |
---|
749 | return; |
---|
750 | } |
---|
751 | |
---|
752 | // Validate ontology |
---|
753 | if (!ontology.validate()) { |
---|
754 | response.status = 500; |
---|
755 | render ontology.errors.join( '; ' ); |
---|
756 | return; |
---|
757 | } |
---|
758 | |
---|
759 | // Save ontology and render the object as JSON |
---|
760 | ontology.save(flush: true) |
---|
761 | render ontology as JSON |
---|
762 | } |
---|
763 | |
---|
764 | /** |
---|
765 | * Adds a ontolgy based on the ID given |
---|
766 | * |
---|
767 | * @param ncboID |
---|
768 | * @return JSON Ontology object |
---|
769 | */ |
---|
770 | def addOntologyByTerm = { |
---|
771 | def id = params.termID; |
---|
772 | |
---|
773 | if( !id ) { |
---|
774 | response.status = 500; |
---|
775 | render 'No ID given' |
---|
776 | return; |
---|
777 | } |
---|
778 | |
---|
779 | def ontology = null; |
---|
780 | |
---|
781 | try { |
---|
782 | ontology = dbnp.data.Ontology.getBioPortalOntologyByTerm( id ); |
---|
783 | } catch( Exception e ) { |
---|
784 | response.status = 500; |
---|
785 | render e.getMessage(); |
---|
786 | return; |
---|
787 | } |
---|
788 | |
---|
789 | if( !ontology ) { |
---|
790 | response.status = 404; |
---|
791 | render 'Ontology form term ' + id + ' not found'; |
---|
792 | return; |
---|
793 | } |
---|
794 | |
---|
795 | // Validate ontology |
---|
796 | if (!ontology.validate()) { |
---|
797 | response.status = 500; |
---|
798 | render ontology.errors.join( '; ' ); |
---|
799 | return; |
---|
800 | } |
---|
801 | |
---|
802 | // Save ontology and render the object as JSON |
---|
803 | ontology.save(flush: true) |
---|
804 | render ontology as JSON |
---|
805 | } |
---|
806 | |
---|
807 | |
---|
808 | /** |
---|
809 | * Checks whether a correct entity is given |
---|
810 | * |
---|
811 | * @return boolean True if a correct entity is given. Returns false and raises an error otherwise |
---|
812 | * @see error() |
---|
813 | */ |
---|
814 | def _checkEntity = { |
---|
815 | // got a entity get parameter? |
---|
816 | entityName = _parseEntityType(); |
---|
817 | |
---|
818 | if( !entityName ) { |
---|
819 | error(); |
---|
820 | return false; |
---|
821 | } |
---|
822 | |
---|
823 | // Create an object of this type |
---|
824 | entity = _getEntity( entityName ); |
---|
825 | |
---|
826 | if( !entity ) { |
---|
827 | error(); |
---|
828 | return false |
---|
829 | } |
---|
830 | |
---|
831 | return true; |
---|
832 | } |
---|
833 | |
---|
834 | |
---|
835 | /** |
---|
836 | * Checks whether the entity type is given and can be parsed |
---|
837 | * |
---|
838 | * @return Name of the entity if parsing is succesful, false otherwise |
---|
839 | */ |
---|
840 | def _parseEntityType() { |
---|
841 | def entityName; |
---|
842 | if (params.entity) { |
---|
843 | // decode entity get parameter |
---|
844 | if (grailsApplication.config.crypto) { |
---|
845 | // generate a Blowfish encrypted and Base64 encoded string. |
---|
846 | entityName = Blowfish.decryptBase64( |
---|
847 | params.entity, |
---|
848 | grailsApplication.config.crypto.shared.secret |
---|
849 | ) |
---|
850 | } else { |
---|
851 | // base64 only; this is INSECURE! Even though it is not |
---|
852 | // very likely, it is possible to exploit this and have |
---|
853 | // Grails dynamically instantiate whatever class you like. |
---|
854 | // If that constructor does something harmfull this could |
---|
855 | // be dangerous. Hence, use encryption (above) instead... |
---|
856 | entityName = new String(params.entity.toString().decodeBase64()) |
---|
857 | } |
---|
858 | |
---|
859 | return entityName; |
---|
860 | } else { |
---|
861 | return false; |
---|
862 | } |
---|
863 | } |
---|
864 | |
---|
865 | /** |
---|
866 | * Creates an object of the given entity. |
---|
867 | * |
---|
868 | * @return False if the entity is not a subclass of TemplateEntity |
---|
869 | */ |
---|
870 | def _getEntity( entityName ) { |
---|
871 | // Find the templates |
---|
872 | def entity = Class.forName(entityName, true, this.getClass().getClassLoader()) |
---|
873 | |
---|
874 | // succes, is entity an instance of TemplateEntity? |
---|
875 | if (entity.superclass =~ /TemplateEntity$/ || entity.superclass.superclass =~ /TemplateEntity$/) { |
---|
876 | return entity; |
---|
877 | } else { |
---|
878 | return false; |
---|
879 | } |
---|
880 | |
---|
881 | } |
---|
882 | |
---|
883 | def templateEntityList = { |
---|
884 | def entities = [ |
---|
885 | [ name: 'Study', entity: 'dbnp.studycapturing.Study' ], |
---|
886 | [ name: 'Subject', entity: 'dbnp.studycapturing.Subject' ], |
---|
887 | [ name: 'Event', entity: 'dbnp.studycapturing.Event' ], |
---|
888 | [ name: 'Sampling Event', entity: 'dbnp.studycapturing.SamplingEvent' ], |
---|
889 | [ name: 'Sample', entity: 'dbnp.studycapturing.Sample' ], |
---|
890 | [ name: 'Assay', entity: 'dbnp.studycapturing.Assay' ] |
---|
891 | ] |
---|
892 | |
---|
893 | entities.each { |
---|
894 | // add the entity class name to the element |
---|
895 | // do we have crypto information available? |
---|
896 | if (grailsApplication.config.crypto) { |
---|
897 | // generate a Blowfish encrypted and Base64 encoded string. |
---|
898 | it.encoded = URLEncoder.encode( |
---|
899 | Blowfish.encryptBase64( |
---|
900 | it.entity.toString().replaceAll(/^class /, ''), |
---|
901 | grailsApplication.config.crypto.shared.secret |
---|
902 | ) |
---|
903 | ) |
---|
904 | } else { |
---|
905 | // base64 only; this is INSECURE! As this class |
---|
906 | // is instantiated elsewehere. Possibly exploitable! |
---|
907 | it.encoded = URLEncoder.encode(it.entity.toString().replaceAll(/^class /, '').bytes.encodeBase64()) |
---|
908 | } |
---|
909 | } |
---|
910 | |
---|
911 | return entities |
---|
912 | } |
---|
913 | } |
---|