Changeset 146


Ignore:
Timestamp:
Jan 28, 2010, 6:42:30 PM (13 years ago)
Author:
keesvb
Message:

added helper functions to Subject and Template classes to easily get the template fields, and added a demonstration of use in the sandbox

Location:
trunk/grails-app
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/conf/BootStrap.groovy

    r143 r146  
    125125                                        name: "A" + x++,
    126126                                        species: mouseTerm,
     127                                        template: mouseTemplate,
    127128                                        templateStringFields: ["Genotype" : "C57/Bl6j", "Gender" : "Male"],
    128                                         templateNumberFields: ["Age" : 17, "Cage" : (int)(x/2)]
     129                                        templateIntegerFields: ["Age" : 17, "Cage" : (int)(x/2)]
    129130                                ).with { if (!validate()) { errors.each { println it} } else save()}
    130131
  • trunk/grails-app/controllers/SandboxController.groovy

    r144 r146  
    1 import dbnp.studycapturing.Study
     1import dbnp.studycapturing.*
    22
    33// The sandbox is meant for internal communication over code examples etc.
     
    55class SandboxController {
    66
    7     def index = {
     7        def index = {
    88
    9         // Get the example study in a lazy way
    10         def st = Study.get(1)
    11         def f = st.template.subjectFields
     9                // Get the example study in a lazy way
     10                def st = Study.get(1)
     11                def f = st.template.subjectFields
    1212
    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                }
    1920
    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]
    2329}
     30}
  • trunk/grails-app/domain/dbnp/studycapturing/Subject.groovy

    r145 r146  
    1313class Subject implements Serializable {
    1414        static searchable = true
     15        Template template
    1516        String name
    1617        Term species
     
    2930        static constraints = {
    3031        }
     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        }
    3155}
  • trunk/grails-app/domain/dbnp/studycapturing/Template.groovy

    r140 r146  
    1212 */
    1313class Template implements Serializable {
    14     String name
    15     //nimble.User owner
     14        String name
     15        //nimble.User owner
    1616
    17     static hasMany = [subjectFields : TemplateSubjectField]
    18  
    19     static constraints = {
    20         name(unique:true)
    21     }
     17        static hasMany = [subjectFields: TemplateSubjectField]
    2218
    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        }
    2638}
  • trunk/grails-app/views/sandbox/index.gsp

    r144 r146  
    99<%@ page contentType="text/html;charset=UTF-8" %>
    1010<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>
    1633</html>
Note: See TracChangeset for help on using the changeset viewer.