Ignore:
Timestamp:
Mar 5, 2010, 2:20:10 PM (13 years ago)
Author:
keesvb
Message:

updated TemplateEntity? to include empty maps, also added the EventGroups? to EventGroup? collection in BootStrap?

Location:
trunk/grails-app/domain/dbnp/studycapturing
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/domain/dbnp/studycapturing/Study.groovy

    r232 r236  
    1313        nimble.User owner
    1414        String title
     15
     16        // TODO: The following 4 fields should be moved into templates
    1517        String code
    1618        String researchQuestion
    1719        String description
    1820        String ecCode
     21
    1922        Date dateCreated
    2023        Date lastUpdated
  • trunk/grails-app/domain/dbnp/studycapturing/Template.groovy

    r227 r236  
    4747         * get all field of a particular type
    4848         * @param       Class   fieldType
    49          * @return      ArrayList
     49         * @return      Set<TemplateField>
    5050         */
    5151        def getFieldsByType(TemplateFieldType fieldType) {
  • trunk/grails-app/domain/dbnp/studycapturing/TemplateEntity.groovy

    r228 r236  
    88        Template template
    99
    10         Map templateStringFields
    11         Map templateStringListFields
    12         Map templateIntegerFields
    13         Map templateFloatFields
    14         Map templateDoubleFields
    15         Map templateDateFields
    16         Map templateTermFields
     10        Map templateStringFields = [:]
     11        Map templateTextFields = [:]
     12        Map templateStringListFields = [:]
     13        Map templateIntegerFields = [:]
     14        Map templateFloatFields = [:]
     15        Map templateDoubleFields = [:]
     16        Map templateDateFields = [:]
     17        Map templateTermFields = [:]
    1718
    1819        static hasMany = [
    1920                templateStringFields: String,
     21                templateTextFields: String,
    2022                templateStringListFields: TemplateFieldListItem,
    2123                templateIntegerFields: int,
     
    2830        static constraints = {
    2931                template(nullable: true)
     32
     33        }
     34
     35        static mapping = {
     36                tablePerHierarchy false
     37
     38                templateTextFields type: 'text'
     39        }
     40
     41        private Map getStore(TemplateFieldType fieldType) {
     42                switch(fieldType) {
     43                        case TemplateFieldType.STRING:
     44                                return templateStringFields
     45                        case TemplateFieldType.TEXT:
     46                                return templateTextFields
     47                        case TemplateFieldType.STRINGLIST:
     48                                return templateStringListFields
     49                        case TemplateFieldType.INTEGER:
     50                                return templateIntegerFields
     51                        case TemplateFieldType.DATE:
     52                                return templateDateFields
     53                        case TemplateFieldType.FLOAT:
     54                                return templateFloatFields
     55                        case TemplateFieldType.DOUBLE:
     56                                return templateDoubleFields
     57                        case TemplateFieldType.ONTOLOGYTERM:
     58                                return templateTermFields
     59                        default:
     60                                throw new NoSuchFieldException("Field type ${fieldType} not recognized")
     61                }
    3062        }
    3163
     
    3971                TemplateFieldType fieldType = template.getFieldType(fieldName)
    4072                if (!fieldType) throw new NoSuchFieldException("Field name ${fieldName} not recognized")
    41                 switch(fieldType) {
    42                         case [TemplateFieldType.STRING, TemplateFieldType.STRINGLIST]:
    43                                 return templateStringFields[fieldName]
    44                         case [TemplateFieldType.STRINGLIST]:
    45                                 return templateStringListFields[fieldName]
    46                         case TemplateFieldType.INTEGER:
    47                                 return templateIntegerFields[fieldName]
    48                         case TemplateFieldType.DATE:
    49                                 return templateDateFields[fieldName]
    50                         case TemplateFieldType.FLOAT:
    51                                 return templateFloatFields[fieldName]
    52                         case TemplateFieldType.DOUBLE:
    53                                 return templateDoubleFields[fieldName]
    54                         case TemplateFieldType.ONTOLOGYTERM:
    55                                 return templateTermFields[fieldName]
    56                         default:
    57                                 throw new NoSuchFieldException("Field type ${fieldType} not recognized")
    58                 }
     73                getStore(fieldType)[fieldName]
    5974        }
    6075
     
    7388                        if (templateStringListFields.containsKey(fieldName) && value.class == TemplateFieldListItem) {
    7489                                // TODO: check if item really belongs to the list under fieldName
    75                                 this.templateStringFields[fieldName] = value
     90                                this.templateStringListFields[fieldName] = value
     91                        }
     92                        if (templateTextFields.containsKey(fieldName) && value.class == String) {
     93                                this.templateTextFields[fieldName] = value
    7694                        }
    7795                        else if (templateIntegerFields.containsKey(fieldName) && value.class == Integer) {
     
    95113                }
    96114        }
     115
     116
    97117
    98118        def Set<TemplateField> giveFields() {
     
    110130
    111131                println "Setting template " + newTemplate
    112                 /*if (template == null || template instanceof NullObject) {} else{ // negation doesn't seem to work well
    113                         def stringFields = template.getFieldsByType(TemplateFieldType.STRINGLIST)
     132                if (template != null) {
     133
     134                        // Loop over all template field types and
     135                        // That is inpossible in Java if the Maps are not yet set because the pointer is evaluated here
     136
     137                        // So this code is quite dangerous stuff:
     138
     139                        /*TemplateFieldType.list().each() { fieldType ->
     140                                Set<TemplateFieldType> fields = template.getFieldsByType(fieldType)
     141                                println fieldType
     142                                println "before: " + getStore(fieldType)
     143                                initFields(getStore(fieldType),fieldType.getDefaultValue(),fields)
     144                                println "after: " + getStore(fieldType)
     145
     146                        }
     147                        println "SF:" + templateStringListFields*/
     148
     149
     150                        /*def type = TemplateFieldType.STRING
     151                        //<T extends Annotation> T = type.getTypeClass().class
     152                        def stringFields = template.getFieldsByType(TemplateFieldType.STRING)
     153                        if (stringFields.size() > 0) {
     154                                templateStringFields = new HashMap<String,String>(stringFields.size())
     155                                stringFields.each {
     156                                        templateStringFields.put(it.name,TemplateFieldType.STRING.getDefaultValue())
     157                                }
     158                                println templateStringFields
     159                        }
     160                        stringFields = template.getFieldsByType(TemplateFieldType.INTEGER)
    114161                        println stringFields*.name
    115162                        if (stringFields.size() > 0) {
    116                                 templateStringFields = new HashMap<String,String>()
    117                                 templateStringFields.keyset.add stringFields*.name;
    118                                 println templateStringFields
    119                         }
    120                 }*/
     163                                templateIntegerFields = new HashMap<String,Integer>(stringFields.size())
     164                                stringFields.each {
     165                                        templateIntegerFields.put(it.name,TemplateFieldType.INTEGER.getDefaultValue())
     166                                }
     167                        }*/
     168                }
     169        }
     170
     171        // Private function to initialize template field collections
     172        private <T> void initFields(Map fieldCollection, T defaultValue, Set<TemplateFieldType> fields) {
     173                if (fields.size() > 0) {
     174                        fieldCollection = new HashMap<String,T>(fields.size());
     175                        fields.each {
     176                                fieldCollection.put(it.name,defaultValue);
     177                        }
     178                        println fieldCollection
     179                }
    121180        }
    122181
    123182        /**
    124          * Convenience method. Returns all unique templates within a collection of TemplateEntities.
     183         * Convenience method. Returns all unique templates used within a collection of TemplateEntities.
    125184         */
    126185        static List<Template> giveTemplates(Set<TemplateEntity> entityCollection) {
     
    128187        }
    129188
    130         static Template giveTemplate(Set<TemplateEntity> entityCollection) {
     189        /**
     190         * Convenience method. Returns the template used within a collection of TemplateEntities.
     191         * @throws NoSuchFieldException when 0 or multiple templates are used in the collection
     192         * @return The template used by all members of a collection
     193         */
     194        static Template giveSingleTemplate(Set<TemplateEntity> entityCollection) {
    131195                def templates = giveTemplates(entityCollection);
    132196                if (templates.size() == 0) {
  • trunk/grails-app/domain/dbnp/studycapturing/TemplateFieldType.groovy

    r212 r236  
    1010public enum TemplateFieldType {
    1111        STRING('String'),
     12        TEXT('Long string'),
    1213        INTEGER('Integer number'),
    1314        FLOAT('Floating-point number'),
     
    2425
    2526        static list() {
    26                 [STRING, INTEGER, FLOAT, DOUBLE, STRINGLIST, ONTOLOGYTERM, DATE]
     27                [STRING, TEXT, INTEGER, FLOAT, DOUBLE, STRINGLIST, ONTOLOGYTERM, DATE]
     28        }
     29
     30        def getDefaultValue() {
     31                switch(this) {
     32                        case [STRING, TEXT]:
     33                                return ""
     34                        case INTEGER:
     35                                return Integer.MIN_VALUE
     36                        case FLOAT:
     37                                return Float.NaN
     38                        case DOUBLE:
     39                                return Double.MIN_VALUE
     40                        case STRINGLIST:
     41                                return null
     42                        case ONTOLOGYTERM:
     43                                return null
     44                        case DATE:
     45                                return null
     46                        default:
     47                                throw new NoSuchFieldException("Field type ${fieldType} not recognized")
     48                }
    2749        }
    2850
Note: See TracChangeset for help on using the changeset viewer.