PageRenderTime 25ms CodeModel.GetById 9ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/grails-app/controllers/general/ProveedorController.groovy

http://github.com/jdmr/mateo
Groovy | 114 lines | 101 code | 13 blank | 0 comment | 15 complexity | 7de3b333a84f5c2d926a00ac538c9718 MD5 | raw file
  1package general
  2
  3import grails.converters.JSON
  4import grails.plugins.springsecurity.Secured
  5
  6@Secured(['ROLE_EMP'])
  7class ProveedorController {
  8
  9    def springSecurityService
 10
 11    static allowedMethods = [crea: "POST", actualiza: "POST", elimina: "POST"]
 12
 13    def index = {
 14        redirect(action: "lista", params: params)
 15    }
 16
 17	def lista = {
 18		params.max = Math.min(params.max ? params.int('max') : 10, 100)
 19        def usuario = springSecurityService.currentUser
 20		[proveedores: Proveedor.findAllByEmpresa(usuario.empresa, params), totalDeProveedores: Proveedor.countByEmpresa(usuario.empresa)]
 21	}
 22
 23    def nuevo = {
 24        def proveedor = new Proveedor()
 25        proveedor.properties = params
 26        return [proveedor: proveedor]
 27    }
 28
 29    def crea = {
 30        def proveedor = new Proveedor(params)
 31        def usuario = springSecurityService.currentUser
 32        proveedor.empresa = usuario.empresa
 33        if (proveedor.save(flush: true)) {
 34            flash.message = message(code: 'default.created.message', args: [message(code: 'proveedor.label', default: 'Proveedor'), proveedor.nombre])
 35            redirect(action: "ver", id: proveedor.id)
 36        }
 37        else {
 38            render(view: "nuevo", model: [proveedor: proveedor])
 39        }
 40    }
 41
 42    def ver = {
 43        def usuario = springSecurityService.currentUser
 44        def proveedor = Proveedor.findByEmpresaAndId(usuario.empresa, params.id)
 45        if (!proveedor) {
 46            flash.message = message(code: 'default.not.found.message', args: [message(code: 'proveedor.label', default: 'Proveedor'), params.id])
 47            redirect(action: "lista")
 48        }
 49        else {
 50            [proveedor: proveedor]
 51        }
 52    }
 53
 54    def edita = {
 55        def usuario = springSecurityService.currentUser
 56        def proveedor = Proveedor.findByEmpresaAndId(usuario.empresa, params.id)
 57        if (!proveedor) {
 58            flash.message = message(code: 'default.not.found.message', args: [message(code: 'proveedor.label', default: 'Proveedor'), params.id])
 59            redirect(action: "lista")
 60        }
 61        else {
 62            return [proveedor: proveedor]
 63        }
 64    }
 65
 66    def actualiza = {
 67        def usuario = springSecurityService.currentUser
 68        def proveedor = Proveedor.findByEmpresaAndId(usuario.empresa, params.id)
 69        if (proveedor) {
 70            if (params.version) {
 71                def version = params.version.toLong()
 72                if (proveedor.version > version) {
 73                    
 74                    proveedor.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'proveedor.label', default: 'Proveedor')] as Object[], "Another user has updated this Proveedor while you were editing")
 75                    render(view: "edita", model: [proveedor: proveedor])
 76                    return
 77                }
 78            }
 79            proveedor.properties = params
 80            proveedor.empresa = usuario.empresa
 81            if (!proveedor.hasErrors() && proveedor.save(flush: true)) {
 82                flash.message = message(code: 'default.updated.message', args: [message(code: 'proveedor.label', default: 'Proveedor'), proveedor.nombre])
 83                redirect(action: "ver", id: proveedor.id)
 84            }
 85            else {
 86                render(view: "edita", model: [proveedor: proveedor])
 87            }
 88        }
 89        else {
 90            flash.message = message(code: 'default.not.found.message', args: [message(code: 'proveedor.label', default: 'Proveedor'), params.id])
 91            redirect(action: "lista")
 92        }
 93    }
 94
 95    def elimina = {
 96        def proveedor = Proveedor.get(params.id)
 97        if (proveedor) {
 98            def nombre = proveedor.nombre
 99            try {
100                proveedor.delete(flush: true)
101                flash.message = message(code: 'default.deleted.message', args: [message(code: 'proveedor.label', default: 'Proveedor'), params.nombre])
102                redirect(action: "lista")
103            }
104            catch (org.springframework.dao.DataIntegrityViolationException e) {
105                flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'proveedor.label', default: 'Proveedor'), params.nombre])
106                redirect(action: "ver", id: params.id)
107            }
108        }
109        else {
110            flash.message = message(code: 'default.not.found.message', args: [message(code: 'proveedor.label', default: 'Proveedor'), params.id])
111            redirect(action: "lista")
112        }
113    }
114}