Changeset 146
- Timestamp:
- Jan 28, 2010, 6:42:30 PM (13 years ago)
- Location:
- trunk/grails-app
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/conf/BootStrap.groovy
r143 r146 125 125 name: "A" + x++, 126 126 species: mouseTerm, 127 template: mouseTemplate, 127 128 templateStringFields: ["Genotype" : "C57/Bl6j", "Gender" : "Male"], 128 template NumberFields: ["Age" : 17, "Cage" : (int)(x/2)]129 templateIntegerFields: ["Age" : 17, "Cage" : (int)(x/2)] 129 130 ).with { if (!validate()) { errors.each { println it} } else save()} 130 131 -
trunk/grails-app/controllers/SandboxController.groovy
r144 r146 1 import dbnp.studycapturing. Study1 import dbnp.studycapturing.* 2 2 3 3 // The sandbox is meant for internal communication over code examples etc. … … 5 5 class SandboxController { 6 6 7 7 def index = { 8 8 9 // Get the example study in a lazy way10 def st = Study.get(1)11 def f = st.template.subjectFields9 // Get the example study in a lazy way 10 def st = Study.get(1) 11 def f = st.template.subjectFields 12 12 13 // This is a way to iterate over the fields in your controller 14 // And print them to the console 15 // Most of the time, you would just iterate over them in the view using <g:each> 16 // See also views/sandbox/index.gsp 17 f.each{ field -> 18 println field.name} 13 // This is a way to iterate over the fields in your controller 14 // And print them to the console 15 // Most of the time, you would just iterate over them in the view using <g:each> 16 // See also views/sandbox/index.gsp 17 f.each {field -> 18 println field.name + "(" + field.type + ")" 19 } 19 20 20 // Specify which variables we want to be available in the controller (implicit return statement) 21 [fields : f] 22 } 21 //Let's get a certain field for a certain subject 22 def subject = Subject.findByName('A1') 23 println st.template.getSubjectFieldType('Age') 24 println subject.getFieldValue('Age') 25 26 27 // Specify which variables we want to be available in the controller (implicit return statement) 28 [fields: f, subjects: st.subjects] 23 29 } 30 } -
trunk/grails-app/domain/dbnp/studycapturing/Subject.groovy
r145 r146 13 13 class Subject implements Serializable { 14 14 static searchable = true 15 Template template 15 16 String name 16 17 Term species … … 29 30 static constraints = { 30 31 } 32 33 /** 34 * Find a template field by its name and return its value for this subject 35 * @param fieldName The name of the template subject field 36 * @return the value of the field (class depends on the field type) 37 * @throws NoSuchFieldException If the field is not found or the field type is not supported 38 */ 39 def getFieldValue(String fieldName) { 40 TemplateFieldType fieldType = template.getSubjectFieldType(fieldName) 41 if (!fieldType) throw new NoSuchFieldException("Field name ${fieldName} not recognized") 42 switch(fieldType) { 43 case [TemplateFieldType.STRING, TemplateFieldType.STRINGLIST]: 44 return templateStringFields[fieldName] 45 case TemplateFieldType.INTEGER: 46 return templateIntegerFields[fieldName] 47 case TemplateFieldType.FLOAT: 48 return templateFloatFields[fieldName] 49 case TemplateFieldType.ONTOLOGYTERM: 50 return templateTermFields[fieldName] 51 default: 52 throw new NoSuchFieldException("Field type ${fieldType} not recognized") 53 } 54 } 31 55 } -
trunk/grails-app/domain/dbnp/studycapturing/Template.groovy
r140 r146 12 12 */ 13 13 class Template implements Serializable { 14 15 14 String name 15 //nimble.User owner 16 16 17 static hasMany = [subjectFields : TemplateSubjectField] 18 19 static constraints = { 20 name(unique:true) 21 } 17 static hasMany = [subjectFields: TemplateSubjectField] 22 18 23 def String toString() { 24 return this.name; 25 } 19 static constraints = { 20 name(unique: true) 21 } 22 23 def String toString() { 24 return this.name; 25 } 26 27 /** 28 * Look up the type of a certain template subject field 29 * @param fieldName The name of the template field 30 * @return The type (static member of TemplateFieldType) of the field, or null of the field does not exist 31 */ 32 def TemplateFieldType getSubjectFieldType(String fieldName) { 33 def field = subjectFields.find { 34 it.name == fieldName 35 } 36 field?.type 37 } 26 38 } -
trunk/grails-app/views/sandbox/index.gsp
r144 r146 9 9 <%@ page contentType="text/html;charset=UTF-8" %> 10 10 <html> 11 <head><title>Sandbox</title></head> 12 <body> 13 <h1>Sandbox</h1> 14 <h2>Template fields</h2> 15 <g:each in="${fields}"><p>${it.name}</p></g:each></body> 11 <head><title>Sandbox</title></head> 12 <body> 13 <h1>Sandbox</h1> 14 <h2>Subject Template fields demo</h2> 15 <table> 16 <tr> 17 <td>Name</td> 18 <g:each in="${fields}" var="field"> 19 <td>${field.name} (${field.type})</td> 20 </g:each> 21 </tr> 22 <g:each in="${subjects}" var="subject"> 23 <tr> 24 <td>${subject.name}</td> 25 <g:each in="${fields}" var="field"> 26 <td>${subject.getFieldValue(field.name)}</td> 27 </g:each> 28 </tr> 29 </g:each> 30 </tr> 31 </table> 32 </body> 16 33 </html>
Note: See TracChangeset
for help on using the changeset viewer.