import org.codehaus.groovy.grails.plugins.springsecurity.Secured

@Secured(['ROLE_AppAdmin', 'ROLE_Manager'])
class PersonGroupDetailedController extends BaseController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ personGroupInstanceList: PersonGroup.list( params ), personGroupInstanceTotal: PersonGroup.count() ]
    }

    def show = {
        def personGroupInstance = PersonGroup.get( params.id )

        if(!personGroupInstance) {
            flash.message = "PersonGroup not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ personGroupInstance : personGroupInstance ] }
    }

    def delete = {
        def personGroupInstance = PersonGroup.get( params.id )
        if(personGroupInstance) {
            try {
                personGroupInstance.delete(flush:true)
                flash.message = "PersonGroup ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "PersonGroup ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "PersonGroup not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def personGroupInstance = PersonGroup.get( params.id )

        if(!personGroupInstance) {
            flash.message = "PersonGroup not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ personGroupInstance : personGroupInstance ]
        }
    }

    def update = {
        def personGroupInstance = PersonGroup.get( params.id )
        if(personGroupInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(personGroupInstance.version > version) {
                    
                    personGroupInstance.errors.rejectValue("version", "default.optimistic.locking.failure")
                    render(view:'edit',model:[personGroupInstance:personGroupInstance])
                    return
                }
            }
            personGroupInstance.properties = params
            if(!personGroupInstance.hasErrors() && personGroupInstance.save(flush: true)) {
                flash.message = "PersonGroup ${params.id} updated"
                redirect(action:show,id:personGroupInstance.id)
            }
            else {
                render(view:'edit',model:[personGroupInstance:personGroupInstance])
            }
        }
        else {
            flash.message = "PersonGroup not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def personGroupInstance = new PersonGroup()
        personGroupInstance.properties = params
        return ['personGroupInstance':personGroupInstance]
    }

    def save = {
        def personGroupInstance = new PersonGroup(params)
        if(!personGroupInstance.hasErrors() && personGroupInstance.save(flush: true)) {
            flash.message = "PersonGroup ${personGroupInstance.id} created"
            redirect(action:show,id:personGroupInstance.id)
        }
        else {
            render(view:'create',model:[personGroupInstance:personGroupInstance])
        }
    }
}
