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