Changeset 548


Ignore:
Timestamp:
May 27, 2010, 8:47:06 PM (14 years ago)
Author:
gav
Message:

Add InventoryItemService savePicture().
Add bulk tests for inventory items.

Location:
trunk/grails-app
Files:
6 edited

Legend:

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

    r531 r548  
    213213    @Secured(['ROLE_AppAdmin'])
    214214    def createBulkTestData = {
    215         if(!createBulkDataService.create()) {
    216             flash.message = "Bulk test data could not be created."
    217             redirect(action: appAdmin)
    218             return
    219         }
    220 
    221         // Success.
    222         flash.message = "Bulk test data created."
     215        def result = createBulkDataService.createAll()
     216        if(!result.error) {
     217            flash.message = g.message(code:"default.create.success", args:["Bulk test data", ''])
     218            redirect(action: appAdmin)
     219            return
     220        }
     221
     222        flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
     223        redirect(action: appAdmin)
     224    }
     225
     226    /**
     227    * Allow admin to create bulk inventory test data.
     228    */
     229    @Secured(['ROLE_AppAdmin'])
     230    def createBulkInventoryTestData = {
     231        def result = createBulkDataService.createBulkInventoryTestData()
     232        if(!result.error) {
     233            flash.message = g.message(code:"default.create.success", args:["Bulk test data", ''])
     234            redirect(action: appAdmin)
     235            return
     236        }
     237
     238        flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
    223239        redirect(action: appAdmin)
    224240    }
  • trunk/grails-app/i18n/messages.properties

    r547 r548  
    154154task.associatedAssets.help=These assets are to be associated with this task, but costs will not be assigned.
    155155
     156inventory.item.already.has.picture=Inventory item already has a picture, please delete the old picture first.
     157inventory.item.picture.file.unrecognised=Image file [{0}]: type not recognised.
     158
    156159inventoryMovement.quantity.insufficientItemsInStock=Could not complete operation, insufficient items in stock.
    157160inventoryMovement.inventoryItem.notFound=Inventory Item not found.
     
    177180default.file.not.supplied=No file supplied.
    178181default.file.no.header=The supplied file does not have the correct header lines, please see the template file.
     182default.not.development.environment.failure=Could not complete operation, dev environment not detected.
    179183
    180184default.doesnt.match.message=Property [{0}] of class [{1}] with value [{2}] does not match the required pattern [{3}]
     
    314318inventoryItem.search.text.recently.used.none.found=No items used in the last {0} days.
    315319
    316 # Reports
    317 report.too.many.results.warning=Warning over {0} results, please run report again.
     320# Report error messages.
     321report.error.too.many.results=Error: over {0} results, please run report again.
     322report.error.no.locations.found=Error: no locations found, please run report again.
     323report.error.too.many.locations=Error: over {0} locations, please run report again.
     324report.error.no.inventory.items.found=Error: no inventory items found, please run report again.
     325report.error.too.many.inventory.items=Error: over {0} inventory items, please run report again.
     326
     327# Report help balloon messages.
    318328report.stock.take.overview=Stock Take (Overview)
    319329report.stock.take.overview.help=Use this report to manage inventory stock take. Use in conjunction with the Stock Take (By Location) report.
  • trunk/grails-app/services/CreateBulkDataService.groovy

    r418 r548  
    1414    def assignedGroupService
    1515    def assignedPersonService
     16    def inventoryItemService
    1617
    1718    def sessionFactory
     19    def grailsApplication
    1820    def propertyInstanceMap = org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP
    1921
     
    2931    * Make a run of data creation.
    3032    */
    31     def create() {
    32         if(!GrailsUtil.environment == "development") {
    33             log.error "Dev environment not detected, will NOT create bulk data."
    34             return false
    35         }
     33    def createAll() {
     34        def result = [:]
     35
     36        def fail = { Map m ->
     37            result.error = [ code: m.code, args: m.args ]
     38            return result
     39        }
     40
     41        if(GrailsUtil.environment != "development")
     42            return fail(code: 'default.not.development.environment.failure')
    3643
    3744        log.info "Creating BULK data..."
     
    4047        log.info "Creating persons..."
    4148        createBulkTestPersons()
    42 
    4349//         createBulkTestSites()
    4450//         createBulkTestDepartments()
    4551//         createBulkTestSuppliers()
    4652//         createBulkTestManufacturers()
    47 
    48         // Tasks
    49         log.info "Creating tasks..."
    50         createBulkTestTasks()
    51 
    52 //         createBulkTestEntries()
    53 //         createBulkTestAssignedGroups()
    54 //         createBulkTestAssignedPersons()
    55 //         createBulkTestTaskRecurringSchedules()
    56 
    57         // Inventory
    58 //         createBulkTestInventoryStores()  /// @todo: Perhaps a 'createQuickStartData' method?
    59 //         createBulkTestInventoryLocations()
    60 //         createBulkTestInventoryGroups() /// @todo: Perhaps a 'createQuickStartData' method?
    61 //         createBulkTestInventoryItems()
    6253
    6354        // Assets
     
    7162//         createBulkTestAssetSubItemExtenedAttributes()
    7263
     64        // Inventory
     65        log.info "Creating inventory..."
     66//         createBulkTestInventoryStores()  /// @todo: Perhaps a 'createQuickStartData' method?
     67        createBulkTestInventoryLocations()
     68//         createBulkTestInventoryGroups() /// @todo: Perhaps a 'createQuickStartData' method?
     69        createBulkTestInventoryItems()
     70
     71        // Tasks
     72        log.info "Creating tasks..."
     73        createBulkTestTasks()
     74//         createBulkTestEntries()
     75//         createBulkTestAssignedGroups()
     76//         createBulkTestAssignedPersons()
     77//         createBulkTestTaskRecurringSchedules()
     78
    7379        log.info "Creating BULK data...complete."
    74         return true
    75 
    76     }
     80        return result
     81
     82    } // create()
     83
     84    /**
     85    * Make a run of inventory data creation.
     86    */
     87    def createBulkInventoryTestData() {
     88        def result = [:]
     89
     90        def fail = { Map m ->
     91            result.error = [ code: m.code, args: m.args ]
     92            return result
     93        }
     94
     95        if(GrailsUtil.environment != "development")
     96            return fail(code: 'default.not.development.environment.failure')
     97
     98        log.info "Creating BULK data..."
     99
     100        // Inventory
     101        log.info "Creating inventory..."
     102//         createBulkTestInventoryStores()  /// @todo: Perhaps a 'createQuickStartData' method?
     103        createBulkTestInventoryLocations()
     104//         createBulkTestInventoryGroups() /// @todo: Perhaps a 'createQuickStartData' method?
     105        createBulkTestInventoryItems()
     106
     107        log.info "Creating BULK data...complete."
     108        return result
     109
     110    } // createBulkInventoryTestData()
    77111
    78112/******************
     
    177211        }
    178212
    179     }
     213    } // createBulkTestTasks()
    180214
    181215    def createBulkTestEntries() {
     
    202236
    203237    } // createBulkTestEntries()
     238
     239
     240/**************************
     241START OF INVENTORY
     242**************************/
     243
     244    def createBulkTestInventoryLocations() {
     245
     246        def inventoryLocationResult
     247        def p = [:]
     248
     249        def start = InventoryLocation.count() + 1
     250        def end = start + 50
     251
     252        def range = start..end
     253
     254
     255        def inventoryStore1 = InventoryStore.read(1)
     256
     257        def name = "Bulk test location "
     258        def btName = ''
     259
     260        startTime = System.currentTimeMillis()
     261        lastBatchStarted = startTime
     262
     263        range.each() {
     264
     265            if(it % 100 == 0) {
     266                logStatus("Creating inventory location #" + it)
     267                cleanUpGorm()
     268            }
     269
     270            btName = name + it
     271
     272            p = [inventoryStore: inventoryStore1,
     273                    name: btName]
     274
     275            inventoryLocationResult = new InventoryLocation(p).save()
     276        } // each()
     277
     278    } // createBulkTestInventoryLocations()
     279
     280    def createBulkTestInventoryItems() {
     281
     282        def inventoryItemInstance
     283        def p = [:]
     284
     285        def pictureResource = grailsApplication.mainContext.getResource('images/logo.png')
     286
     287        def start = InventoryItem.count() + 1
     288        def end = start + 250
     289
     290        def range = start..end
     291
     292        def inventoryLocation
     293        def inventoryLocationIndex = 0
     294        def inventoryLocationList = InventoryLocation.findAll()
     295        def unitOfMeasure2 = UnitOfMeasure.read(2)
     296        def inventoryType1 = InventoryType.read(1)
     297        def inventoryGroup1 = InventoryGroup.read(1)
     298
     299        def name = "Bulk test inventory item "
     300        def btName = ''
     301
     302        startTime = System.currentTimeMillis()
     303        lastBatchStarted = startTime
     304
     305        range.each() {
     306
     307            if(it % 100 == 0) {
     308                logStatus("Creating inventory item #" + it)
     309                cleanUpGorm()
     310            }
     311
     312            // Spread the inventoryItems across all available locations.
     313            if(inventoryLocationIndex < inventoryLocationList.size()) {
     314                inventoryLocation = inventoryLocationList[inventoryLocationIndex]
     315            }
     316            else {
     317                inventoryLocationIndex = 0
     318                inventoryLocation = inventoryLocationList[inventoryLocationIndex]
     319            }
     320            inventoryLocationIndex++
     321
     322            // Change the name for each inventoryItem.
     323            btName = name + it
     324
     325            p = [inventoryGroup: inventoryGroup1,
     326                    inventoryType: inventoryType1,
     327                    unitOfMeasure: unitOfMeasure2,
     328                    inventoryLocation: inventoryLocation,
     329                    name: btName,
     330                    description: "Bulk test data",
     331                    unitsInStock: 2,
     332                    reorderPoint: 0]
     333
     334            inventoryItemInstance = new InventoryItem(p)
     335            saveAndTest(inventoryItemInstance)
     336
     337            def pictureResult = inventoryItemService.savePicture(inventoryItemInstance, pictureResource)
     338
     339            if(pictureResult.error)
     340                log.error pictureResult.error
     341        } // each()
     342
     343    } // createBulkTestInventoryItems()
    204344
    205345    /**
  • trunk/grails-app/services/InventoryItemService.groovy

    r441 r548  
    226226    }
    227227
     228    /**
     229    * Save an inventory item picture.
     230    * @param pictureSource A supported source to get the picture image from.
     231    * Supported sources:
     232    * HttpServletRequest e.g: 'request' var from controller to run getFile('file') against.
     233    * ServletContextResource e.g: grailsApplication.mainContext.getResource('images/logo.png')
     234    */
     235    def savePicture(params, pictureSource) {
     236        InventoryItem.withTransaction { status ->
     237            def result = [:]
     238
     239            def kByteMultiplier = 1000
     240
     241            def fail = { Map m ->
     242                status.setRollbackOnly()
     243                if(result.inventoryItemInstance && m.field)
     244                    result.inventoryItemInstance.errors.rejectValue(m.field, m.code)
     245                result.error = [ code: m.code, args: m.args ?: ["InventoryItem", params.id] ]
     246                return result
     247            }
     248
     249            result.inventoryItemInstance = InventoryItem.get(params.id)
     250
     251            if(!result.inventoryItemInstance)
     252                return fail(code:"default.not.found")
     253
     254            // Optimistic locking check.
     255            if(params.version) {
     256                if(result.inventoryItemInstance.version > params.version.toLong())
     257                    return fail(field:"version", code:"default.optimistic.locking.failure")
     258            }
     259
     260            if(result.inventoryItemInstance.picture)
     261                return fail(field:"picture", code:"inventory.item.already.has.picture")
     262
     263            def picture = new Picture(inventoryItem: result.inventoryItemInstance)
     264            def imaging = new Imaging()
     265            def images = null
     266            def pictureFile
     267            def pictureFileName = ''
     268            def pictureInputStream
     269
     270            if(pictureSource instanceof javax.servlet.http.HttpServletRequest) {
     271                def multiPartFile = pictureSource.getFile('file')
     272                pictureFileName = multiPartFile.originalFilename
     273
     274                if(!multiPartFile || multiPartFile.isEmpty())
     275                    return fail(code: "default.file.not.supplied")
     276
     277                if (multiPartFile.getSize() > Image.MAX_SIZE)
     278                    return fail(code: "default.file.over.max.size", args: [Image.MAX_SIZE/kByteMultiplier, "kB"])
     279
     280                pictureInputStream = multiPartFile.inputStream
     281            }
     282            else if(pictureSource instanceof org.springframework.web.context.support.ServletContextResource) {
     283                pictureFile = pictureSource.getFile()
     284                pictureFileName = pictureFile.name
     285
     286                if ( !pictureFile.isFile() || (pictureFile.length() == 0) )
     287                    return fail(code:"default.file.not.supplied")
     288
     289                if (pictureFile.length() > Image.MAX_SIZE)
     290                    return fail(code:"default.file.over.max.size", args: [Image.MAX_SIZE/kByteMultiplier, "kB"])
     291
     292                pictureInputStream = pictureSource.inputStream
     293            }
     294            else {
     295                    return fail(code:"default.file.not.supplied")
     296            }
     297
     298            try {
     299                images = imaging.createAll(result.inventoryItemInstance, picture, pictureInputStream)
     300            }
     301            catch(Exception ex) {
     302                log.error("picture save", ex)
     303                return fail(code:"inventory.item.picture.file.unrecognised", args: [pictureFileName])
     304            }
     305
     306            images.each { image ->
     307                picture.addToImages(image)
     308            }
     309
     310            if(picture.hasErrors() || !picture.save())
     311                return fail(code:"default.create.failure", args: ["Picture"])
     312
     313            result.inventoryItemInstance.picture = picture
     314
     315            if(result.inventoryItemInstance.hasErrors() || !result.inventoryItemInstance.save())
     316                return fail(code:"default.create.failure")
     317
     318            // success
     319            return result
     320
     321        } //end withTransaction
     322    }
     323
    228324} // end class
  • trunk/grails-app/views/appCore/appAdmin.gsp

    r358 r548  
    1111        </div>
    1212        <div class="body">
    13             <g:if test="${flash.message}">
    14                 <div class="message">${flash.message}</div>
    15             </g:if>
     13            <g:render template="/shared/messages" />
    1614            <div class="dialog">
    1715                <table>
     
    6664                            <tr class="prop">
    6765                                <td valign="top" class="name">
    68                                     <label>Test:</label>
     66                                    <label>Bulk Test:</label>
    6967                                </td>
    7068                                <td valign="top" class="value">
    71                                     <g:link action="createBulkTestData">Bulk</g:link> - Create a large volume of test data.
     69                                    <g:link action="createBulkTestData">All Types</g:link> - Create a large volume of test data.
     70                                    <br />
     71                                    <g:link action="createBulkInventoryTestData">Inventory</g:link> - Create a large volume of inventory test data.
    7272                                </td>
    7373                            </tr>
  • trunk/grails-app/views/appCore/start.gsp

    r546 r548  
    1313        </div>
    1414        <div class="body">
    15             <g:if test="${flash.message}">
    16                 <div class="message">${flash.message}</div>
    17             </g:if>
     15            <g:render template="/shared/messages" />
    1816            <g:hasErrors bean="${appCore}">
    1917                <div class="errors">
Note: See TracChangeset for help on using the changeset viewer.