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/services
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.