Changeset 485


Ignore:
Timestamp:
May 27, 2010, 1:13:50 PM (7 years ago)
Author:
tabma
Message:
  • refactored (now using configuration file where entities to be used are configured)
Location:
trunk/grails-app
Files:
2 deleted
8 edited
3 moved

Legend:

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

    r444 r485  
    77 * Revision information:
    88 * $Rev$
    9  * $Author:$
     9 * $Author$
    1010 * $Date$
    1111 */
     
    143143                // @see ImporterController
    144144                importableEntities: [
    145                     study       : [name: 'Study', entity: 'dbnp.studycapturing.Study'],
    146                         subject : [name: 'Subject', entity: 'dbnp.studycapturing.Subject'],
    147                         sample  : [name: 'Sample', entity: 'dbnp.studycapturing.Sample']
     145                        none    : [name: 'None', entity:'', type:-1],
     146                        event   : [name: 'Event', entity:'dbnp.studycapturing.Event', type:2],
     147                        protocol: [name: 'Protocol', entity:'dbnp.studycapturing.Protocol', type:3],
     148                        sample  : [name: 'Sample', entity: 'dbnp.studycapturing.Sample', type:4],
     149                        study   : [name: 'Study', entity: 'dbnp.studycapturing.Study', type:0],
     150                        subject : [name: 'Subject', entity: 'dbnp.studycapturing.Subject', type:1]
     151
    148152                ]
    149153        ]
  • 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
  • trunk/grails-app/services/dbnp/importer/ImporterService.groovy

    r415 r485  
    4646    /**
    4747     * @param wb high level representation of the workbook
     48     * @param sheetindex sheet to use within the workbook
    4849     * @return header representation as a MappingColumn hashmap
    4950     */
    50     def getHeader(HSSFWorkbook wb, int sheetindex){
     51    def getHeader(HSSFWorkbook wb, int sheetindex, theEntity=0){
    5152
    5253        def sheet = wb.getSheetAt(sheetindex)
     
    5556        def header = [:]
    5657        def df = new DataFormatter()
    57 
     58        def property = new dbnp.studycapturing.TemplateField()
    5859
    5960        for (HSSFCell c: sheet.getRow(datamatrix_start)) {
     
    6768            switch (datamatrix_celltype) {
    6869                    case HSSFCell.CELL_TYPE_STRING:
    69                             header[index] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.STRING, index:index);
     70                            header[index] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.STRING, index:index, entity:theEntity, property:property);
    7071                            break
    7172                    case HSSFCell.CELL_TYPE_NUMERIC:                   
    7273                            if (HSSFDateUtil.isCellDateFormatted(c)) {
    73                                 header[index] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.DATE, index:index)
     74                                header[index] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.DATE, index:index, entity:theEntity, property:property)
    7475                            }
    7576                            else
    76                                 header[index] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.INTEGER,index:index);
     77                                header[index] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.INTEGER,index:index, entity:theEntity, property:property);
    7778                            break
    7879                    case HSSFCell.CELL_TYPE_BLANK:
    79                             header[index] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.STRING, index:index);
     80                            header[index] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.STRING, index:index, entity:theEntity, property:property);
    8081                            break
    8182                    default:
    82                             header[index] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.STRING,index:index);
     83                            header[index] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.STRING,index:index, entity:theEntity, property:property);
    8384                            break
    8485            }
     
    160161        def sheet = wb.getSheetAt(sheetindex)
    161162        def table = []
     163
    162164       
    163165        // walk through all rows       
    164166        (rowindex..sheet.getLastRowNum()).each { i ->
    165             table.add(createRecord(template_id, sheet.getRow(i), mcmap))           
     167            table.add(createRecord(template_id, sheet.getRow(i), mcmap))
    166168        }
    167169
     
    263265        def sample = new Sample(name:"New sample", template:template)
    264266
    265         for (HSSFCell cell: excelrow) {
     267        for (HSSFCell cell: excelrow) {     
    266268            def mc = mcmap[cell.getColumnIndex()]
    267             def value = formatValue(df.formatCellValue(cell), mc.templatefieldtype)         
     269            def value = formatValue(df.formatCellValue(cell), mc.templatefieldtype)
    268270
    269271            switch(mc.entity) {
  • trunk/grails-app/taglib/dbnp/importer/ImporterTagLib.groovy

    r415 r485  
    2222class ImporterTagLib {
    2323    static namespace = 'importer'
    24     def standardentities = [[type:-1, name:"Don't import"], [type:0, name:"Study"], [type:1, name:"Subject"], [type:2, name:"Event"],
    25                         [type:3, name:"Protocol"], [type:4, name:"Sample"]]
    2624
    2725    /**
     
    5351
    5452    /**
    55      * @param entities array
     53     * @param entities array containing selected entities
    5654     */
    5755    def properties = { attrs ->
    58         def selectedentities = []
    5956        def header = attrs['header']
     57        def entities = attrs['entities']
    6058
    61         attrs['entities'].index.each { columnindex, entitytype ->
    62             def entity = [type:entitytype,columnindex:columnindex.toInteger()]
    63             selectedentities.add(entity)
    64         }
    65 
    66         out << render (template:"common/properties", model:[selectedentities:selectedentities, standardentities:standardentities, header:header])
     59        out << render ( template:"common/properties",
     60                        model:[selectedentities:entities,
     61                        standardentities:grailsApplication.config.gscf.domain.importableEntities,
     62                        header:header])
    6763    }
    6864
     
    124120        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
    125121
    126         standardentities.each { e ->
    127             res += "<option value=\"${e.type}\""
    128             res += (e.type == sel) ? " selected" : ""
    129             res += ">${e.name}</option>"
     122        grailsApplication.config.gscf.domain.importableEntities.each { e ->
     123            res += "<option value=\"${e.value.name}\""
     124            res += (e.value.type == sel) ? " selected" : ""
     125            res += ">${e.value.name}</option>"
    130126        }
    131127
  • trunk/grails-app/views/importer/common/_properties.gsp

    r359 r485  
    1717    <table>
    1818          <g:each var="stdentity" in ="${standardentities}">
    19               <% if (selectedentities.any { it.type.toInteger() ==stdentity.type.toInteger()} && stdentity.type.toInteger()!=-1) { %>
    20               <tr><td colspan="2"><h4>${stdentity.name}</h4></td></tr>
     19              <% if (selectedentities.any { it.name == stdentity.value.name } && stdentity.value.name!="") { %>
     20              <tr><td colspan="2"><h4>${stdentity.value.name}</h4></td></tr>
    2121              <tr>               
    2222                  <td>Columnname:</td>
     
    2424              </tr>           
    2525                <g:each var="selentity" in="${selectedentities}">
    26                     <g:if test="${selentity.type.toLong()==stdentity.type}">
     26                    <g:if test="${selentity.name==stdentity.value.name}">
    2727                        <tr>                       
    2828                            <td class="header" width="200px">
  • trunk/grails-app/views/importer/index_advanced.gsp

    r417 r485  
    1414    <h1>Importer wizard (advanced)</h1>
    1515    <p>You can import your Excel data to the server by choosing a file from your local harddisk in the form below.</p>
    16         <g:form controller="importer" method="post" action="upload" enctype="multipart/form-data">
     16        <g:form controller="importer" method="post" action="upload_advanced" enctype="multipart/form-data">
    1717        <table border="0">
    1818        <tr>
  • trunk/grails-app/views/importer/index_simple.gsp

    r449 r485  
    5757    <h1>Importer wizard (simple)</h1>
    5858    <p>You can import your Excel data to the server by choosing a file from your local harddisk in the form below.</p>
    59         <g:form controller="importer" method="post" action="upload" enctype="multipart/form-data">
     59        <g:form controller="importer" method="post" action="upload_simple" enctype="multipart/form-data">
    6060        <table border="0">
    6161        <tr>
  • trunk/grails-app/views/importer/step2_simple.gsp

    r417 r485  
    2121    <meta name="layout" content="main"/>
    2222    <link rel="stylesheet" href="${resource(dir: 'css', file: 'importer.css')}"/>
    23     <title>Step 2: import wizard entities/properties</title>
     23    <title>Step 2: import wizard properties/columns</title>
    2424  </head>
    2525  <body>
    26     <h1>Step 2: import wizard entities/properties</h1>   
    27     <p>First select the study you want to add the data to. The next step is to assign properties to the columns. Below you see the entities and columns, please make your
    28     selections.</p>
     26    <h1>Step 2: import wizard properties/columns</h1>
     27    <p>The next step is to assign properties to the columns. Below you see the columns, please assign every column to
     28    a property.</p>
    2929  <importer:properties entities="${entities}" header="${header}" templates="${templates}"/>
    3030  </body>
Note: See TracChangeset for help on using the changeset viewer.