Changeset 290


Ignore:
Timestamp:
Jan 22, 2010, 5:29:24 PM (10 years ago)
Author:
gav
Message:

Secure and work on import asset tree functions.
Improvements to CustomTagLib.

Location:
trunk/grails-app
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/AssetDetailedController.groovy

    r286 r290  
    1414    static allowedMethods = [delete:'POST', save:'POST', update:'POST', saveCopy:'POST']
    1515
     16    def overview = {
     17    }
     18
     19    @Secured(['ROLE_Manager','ROLE_AppAdmin'])
    1620    def importAssetTree = {
    1721    }
    18     def overview = {
    19     }
    20 
     22
     23    @Secured(['ROLE_Manager','ROLE_AppAdmin'])
    2124    def importAssetTreeSave = {
    2225        def result = csvService.importAssetTree(request)
  • trunk/grails-app/i18n/messages.properties

    r287 r290  
    11asset.tree.import.success=Asset tree imported.
    2 asset.tree.import.failure=Could not create asset tree from supplied file.
     2asset.tree.import.failure=Could not create asset tree from supplied file, failed on line {0}.
    33asset.tree.import.file.over.max.size=Supplied file is greater than max size of {0} Mbytes.
    44asset.tree.import.file.not.supplied=No file supplied.
     
    2222person.personGroups.help=Groups may be assigned to tasks and \
    2323may also provide a record of persons qualified or trained in a specific area. \
    24 Use Ctrl or Shift to select multiple groups. \
    25 Groups provide no application authorisations.
     24    Groups provide no application authorisations.
    2625person.loginName=Login Name
    2726person.loginName.help=This is the id or name that the person will use to login to the application.
  • trunk/grails-app/services/AssetSubItemService.groovy

    r285 r290  
    2222
    2323    def save(params) {
    24         Asset.withTransaction { status ->
     24        AssetSubItem.withTransaction { status ->
    2525            def result = [:]
    2626
  • trunk/grails-app/services/CreateDataService.groovy

    r276 r290  
    156156        saveAndTest(authInstance)
    157157
     158        // #3
    158159        authInstance = new Authority(description:"Application User, all application users need this base role to allow login.",
    159160                                        authority:"ROLE_AppUser")
  • trunk/grails-app/services/CsvService.groovy

    r287 r290  
    1616    */
    1717    def importAssetTree(request) {
    18         def result = [:]
    19 
    20         def megaByteMultiplier = 1000 * 1000
    21         def fileMaxSize = 10 * megaByteMultiplier //Mb
    22 
    23         def multiPartFile = request.getFile('file')
    24 
    25         InputStreamReader sr = new InputStreamReader(multiPartFile.inputStream)
    26         CSVReader reader = new CSVReader(sr)
    27 
    28         def fail = { Map m ->
    29             //status.setRollbackOnly()
    30             reader.close()
    31             result.error = [ code: m.code, args: m.args ]
    32             return result
    33         }
    34 
    35         if(!multiPartFile || multiPartFile.isEmpty())
    36             return fail(code: "asset.tree.import.file.not.supplied")
    37 
    38         if (multiPartFile.getSize() > fileMaxSize)
    39             return fail(code: "asset.tree.import.file.over.max.size", args: [fileMaxSize/megaByteMultiplier])
    40 
    41         def line = reader.readNext()
    42         def lineNumber = 1
    43 
    44         def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"]
    45 
    46         if(line != header)
    47             return fail(code: "asset.tree.import.no.header")
    48 
    49         log.info "Import checks passed, start processing asset file."
    50 
    51         // Prepare the first body line.
    52         line = reader.readNext()
    53         lineNumber ++
    54 
    55         while(line) {
    56             def lineSize = line.size()
    57 //             log.info lineNumber+ "(" + lineSize + ")" + " : " + line
    58 
    59             if(line[0]) {
    60                     if( !Site.findByName(line[0]) )
    61                         new Site(name: line[0]).save()
    62             }
    63 
     18        Asset.withTransaction { status ->
     19            def result = [:]
     20
     21            def megaByteMultiplier = 1000 * 1000
     22            def fileMaxSize = 10 * megaByteMultiplier //Mb
     23
     24            def multiPartFile = request.getFile('file')
     25
     26            InputStreamReader sr = new InputStreamReader(multiPartFile.inputStream)
     27            CSVReader reader = new CSVReader(sr)
     28
     29            def fail = { Map m ->
     30                status.setRollbackOnly()
     31                reader.close()
     32                result.error = [ code: m.code, args: m.args ]
     33                return result
     34            }
     35
     36            if(!multiPartFile || multiPartFile.isEmpty())
     37                return fail(code: "asset.tree.import.file.not.supplied")
     38
     39            if (multiPartFile.getSize() > fileMaxSize)
     40                return fail(code: "asset.tree.import.file.over.max.size", args: [fileMaxSize/megaByteMultiplier])
     41
     42            def line = reader.readNext()
     43            def lineNumber = 1
     44
     45            def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"]
     46
     47            if(line != header)
     48                return fail(code: "asset.tree.import.no.header")
     49
     50            log.info "Import checks passed, start processing asset file."
     51
     52            // Prepare the first body line.
    6453            line = reader.readNext()
    6554            lineNumber ++
    66         } //while(line)
    67 
    68         // Success.
    69         reader.close()
    70         return result
    71 
     55
     56            def siteInstance
     57            def sectionInstance
     58            def assetInstance
     59
     60            while(line) {
     61                def lineSize = line.size()
     62    //             log.info lineNumber+ "(" + lineSize + ")" + " : " + line
     63
     64                if(line[0]) {
     65                        if( !Site.findByName(line[0]) )
     66                            siteInstance = new Site(name: line[0])
     67                            if(!siteInstance.save())
     68                                return fail(code: "asset.tree.import.failure", args: [lineNumber])
     69                }
     70                else continue
     71
     72                if(line[1]) {
     73                        if( !Section.findByName(line[1]) )
     74                           sectionInstance =  new Section(name: line[1],
     75                                                                                site: siteInstance)
     76                            if(!sectionInstance.save())
     77                                return fail(code: "asset.tree.import.failure", args: [lineNumber])
     78                }
     79                else continue
     80
     81                if(line[2]) {
     82                        if( !Asset.findByName(line[2]) )
     83                            assetInstance = new Asset(name: line[2],
     84                                                                        section: sectionInstance)
     85                            if(!sectionInstance.save())
     86                                return fail(code: "asset.tree.import.failure", args: [lineNumber])
     87                }
     88                else continue
     89
     90                line = reader.readNext()
     91                lineNumber ++
     92            } //while(line)
     93
     94            // Success.
     95            reader.close()
     96            return result
     97
     98        } //end withTransaction
    7299    } // end importAssetTree()
    73100
  • trunk/grails-app/taglib/CustomTagLib.groovy

    r286 r290  
    3636        def isChecked, ht, wd, style, html
    3737
     38        def displayFields = attrs.displayFields
     39
     40        def displayValue = " "
     41
    3842        // sets the style to override height and/or width if either of them
    3943        // is specified, else the default from the CSS is taken
     
    5458        from.each { obj ->
    5559
     60            displayValue = " "
     61
     62            if( displayFields?.contains("id") ) {
     63                displayValue += obj.id + " - "
     64            }
     65
     66            if(displayFields?.contains("name")) {
     67                displayValue += obj.name
     68            }
     69            else displayValue += obj
     70
    5671            // if we wanted to select the checkbox using a click anywhere on the label (also hover effect)
    5772            // but grails does not recognize index suffix in the name as an array:
     
    6176            isChecked = (value?.contains(obj."${attrs.optionKey}"))? true: false
    6277
    63             out << "<li>" << checkBox(name:cname, value:obj."${attrs.optionKey}", checked: isChecked) << " ${obj.id} - ${obj.name}" << "</li>"
     78            out << "<li>" << checkBox(name:cname, value:obj."${attrs.optionKey}", checked: isChecked) << displayValue << "</li>"
    6479        }
    6580
  • trunk/grails-app/views/assetDetailed/edit.gsp

    r286 r290  
    8888                                                                    from="${AssetSubItem.list()}"
    8989                                                                    value="${assetInstance?.assetSubItems.collect{it.id}}"
    90                                                                     optionKey="id"/>
     90                                                                    optionKey="id"
     91                                                                    displayFields="['id', 'name']"/>
    9192
    9293                                </td>
Note: See TracChangeset for help on using the changeset viewer.