Ignore:
Timestamp:
May 27, 2010, 1:13:50 PM (12 years ago)
Author:
tabma
Message:
  • refactored (now using configuration file where entities to be used are configured)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/dbnp/importer/ImporterController.groovy

    r450 r485  
    3030import dbnp.studycapturing.TemplateField
    3131import grails.converters.JSON
     32import org.apache.poi.hssf.usermodel.HSSFWorkbook
    3233
    3334class ImporterController {
     
    5354    * @param importfile uploaded file to import
    5455    */
    55     def upload = {
    56         def downloadedfile = request.getFile('importfile');
     56    def upload_advanced = {
     57        def wb = handleUpload('importfile')
     58       
     59        session.importer_header = ImporterService.getHeader(wb, 0)
     60        session.importer_template_id = params.template_id
     61        session.importer_workbook = wb
     62
     63        render (view:"step1_advanced", model:[header:session.importer_header, datamatrix:ImporterService.getDatamatrix(wb, 0, 5)])
     64    }
     65
     66    /**
     67    * This method will move the uploaded file to a temporary path and send the header
     68    * and the rows to the postview
     69    *
     70    * @param importfile uploaded file to import
     71    * @param entity string representation of the entity chosen
     72    */
     73    def upload_simple = {
     74        def wb = handleUpload('importfile')
     75        def entity = grailsApplication.config.gscf.domain.importableEntities.get(params.entity).entity
     76        def entityClass = Class.forName(entity, true, this.getClass().getClassLoader())
     77
     78        session.importer_header = ImporterService.getHeader(wb, 0, entityClass)
     79        session.importer_template_id = params.template_id
     80        session.importer_workbook = wb
     81
     82        //import workbook
     83        //session.importer_importeddata = ImporterService.importdata(session.importer_template_id, session.importer_workbook, 0, 1, session.importer_header)
     84
     85        //println "DAS" + session.importer_header
     86
     87        //render(view:"step2_simple", model:[datamatrix:session.importer_importeddata])
     88        def templates = Template.get(session.importer_template_id)
     89       
     90        render(view:"step2", model:[entities:entities, header:session.importer_header, templates:templates])
     91    }
     92
     93    /**
     94     * This method handles a file being uploaded and storing it in a temporary directory
     95     * and returning a workbook
     96     *
     97     * @param formfilename name used for the file field in the form
     98     * @return workbook object reference
     99     */
     100    private HSSFWorkbook handleUpload(formfilename) {
     101
     102        def downloadedfile = request.getFile(formfilename);
    57103        def tempfile = new File(System.getProperty('java.io.tmpdir') + File.separatorChar + System.currentTimeMillis() + ".nmcdsp")
    58104        downloadedfile.transferTo(tempfile)
    59        
    60         def wb = ImporterService.getWorkbook(new FileInputStream(tempfile))
    61        
    62         def header = ImporterService.getHeader(wb, 0)
    63         def datamatrix= ImporterService.getDatamatrix(wb, 0, 5)
    64 
    65         session.importer_header = header
    66         session.importer_template_id = params.template_id
    67         session.importer_workbook = wb
    68 
    69         render (view:"step1", model:[header:header, datamatrix:datamatrix])
     105
     106        return ImporterService.getWorkbook(new FileInputStream(tempfile))
    70107    }
    71108
     
    74111    * All information of the columns is stored in a session as MappingColumn object
    75112    *
    76     * @param entity list of entities and columns it has been assigned to (columnindex:entitytype format)
     113    * @param entities list of entities and columns it has been assigned to (columnindex.entitytype)
    77114    * @param templatefieldtype list of celltypes and columns it has been assigned to (columnindex:templatefieldtype format)
    78115    * @return properties page
     
    83120        def tft = null 
    84121        def identifiercolumnindex = (params.identifier!=null) ? params.identifier.toInteger() : -1
     122        def selectedentities = []
     123
     124        // loop all entities and see which column has been assigned which entitytype
     125        // and build an array containing the selected entities
     126        params.entity.index.each { columnindex, entityname ->
     127            def _entity = [name:entityname,columnindex:columnindex.toInteger()]
     128            selectedentities.add(_entity)
     129        }
    85130
    86131        params.templatefieldtype.index.each { columnindex, _templatefieldtype ->
     
    108153        }
    109154
    110         params.entity.index.each { columnindex, entitytype ->
     155        params.entity.index.each { columnindex, entityname ->
    111156            Class clazz
    112157
    113             switch (entitytype.toInteger()) {
    114                 case 0: clazz = Study
    115                         break
    116                 case 1: clazz = Subject
    117                         break
    118                 case 2: clazz = Event
    119                         break
    120                 case 3: clazz = Protocol
    121                         break
    122                 case 4: clazz = Sample
     158            switch (entityname) {
     159                case "Study"    : clazz = Study
     160                        break
     161                case "Subject"  : clazz = Subject
     162                        break
     163                case "Event"    : clazz = Event
     164                        break
     165                case "Protocol" : clazz = Protocol
     166                        break
     167                case "Sample"   : clazz = Sample
    123168                        break
    124169                default: clazz = Object
     
    136181        def templates = Template.get(session.importer_template_id)
    137182
    138         render(view:"step2", model:[entities:params.entity, header:session.importer_header, templates:templates])
     183        render(view:"step2", model:[entities:selectedentities, header:session.importer_header, templates:templates])
    139184    }
    140185
Note: See TracChangeset for help on using the changeset viewer.