samedi 4 juin 2016

Random function in Grails that searches in a table using WHERE clauses

Sorry, but I don't speak english very good.

I want to create a function that searches in a table using a "lot" or "draw" function and bring the results in a gsp.

This is my code:

My class

package tele

class Aspirante {
String nombre
String apellido_paterno
String apellido_materno
Integer edad
String sexo
String nacionalidad
String procedencia
String telefono
String correo
Date fecha_reg
Integer status

static constraints = {
    edad (maxSize:2)
    sexo(inList: ["Hombre", "Mujer"])
    correo (email: true, blank: false)
    apellido_materno (blank: true, nullable:true)
}

public String toString(){
    return "${nombre}"
}
}

My controller

 def sorteo(){
    groovy.sql.Sql sql = new groovy.sql.Sql(dataSource) 

    def res = ""
    def edad1 = ""
    def edad2 = ""
    def query = ""
    def query2= ""
    def queryup =""
    def fupdate =""

    edad1 = params.edad1
    edad2 = params.edad2
    res = params.res
    query="SELECT id AS id FROM aspirante WHERE edad between "+edad1+" and "+edad2+" AND status=0 ORDER BY RAND(), edad ASC, sexo ASC LIMIT "+res+""


    fupdate="UPDATE aspirante WHERE id = (SELECT * FROM ("+query+")) as id);"
    query2="UPDATE aspirante SET status =1 WHERE id IN(SELECT * FROM (SELECT id FROM aspirante WHERE edad between "+edad1+" and "+edad2+" AND status=0 ORDER BY edad ASC, sexo ASC LIMIT "+res+")as t);"


    def id = new ArrayList()
    def nombre = new ArrayList()
    def appat = new ArrayList()
    def apmat = new ArrayList()
    def eedad = new ArrayList()
    def sexo = new ArrayList()
    def telefono = new ArrayList()              

    sql.eachRow(query){ row->
        id.add(row.id)
        nombre.add(row.nombre)
        appat.add(row.appat)
        apmat.add(row.apmat)
        eedad.add(row.eedad)
        sexo.add(row.sexo)
        telefono.add(row.telefono)

        //query_ids="UPDATE aspirante SET status = 1 WHERE id = "+id+");"
        //def update = Aspirante.executeUpdate("UPDATE Aspirante SET status = :new_status WHERE id = :id_up",[new_status:1,id_up:id]);
        //Aspirante.executeUpdate("UPDATE Aspirante SET status = :new_status WHERE id = :id_up",[new_status:1,id_up:id]);
        //executeUpdate("delete Book b where b.title like ?",['Groovy In Action'])

    }
    sql.execute(fupdate)

    println  query
    println  query2
    println  queryup
    println  nombre
    println  appat
    println  apmat
    println  eedad
    println  sexo
    println  telefono
    println  id
    println "fupdate>>>>>>"+fupdate

    [edad1:edad1,edad2:edad2,telefono:telefono,nombre:nombre,eedad:eedad,sexo:sexo,id:id,appat:appat,apmat:apmat]

    }

My GSP

    <%@ page import="tele.*" %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="layout" content="main" />
    <g:set var="entityName" value="${message(code: 'aspirante.label', default: 'Aspirante')}" />
    <title><g:message code="default.list.label" args="[entityName]" /></title>
    </head>
    <body>
    <a href="#list-aspirante" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
    <div class="nav" role="navigation">
        <ul>
            <li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
            <li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>
            </ul>
        </div>
        <style>
        table.x{

        }
        table.x tr  td{
        vertical-align: bottom;
        text-align: center;
        width: 33%;
        }
      </style>
      <table class="x">
        <tr>
            <td style="text-align: left">
                <g:if test="${aspiranteInstanceTotal == 0 || aspiranteInstanceTotal > 0}">
                Total de Aspirantes: ${aspiranteInstanceTotal}
            </g:if>
            <g:else>
                Total de Aspirantes: ${Aspirante.count()}
            </g:else>

        </td>
        <td> <g:form action="busqueda">
                <fieldset class="form">
                    <g:render template="buscar"/>
                </fieldset>
            </g:form>  
        </td>
       </table>
     <table>
      <thead>
        <tr>


            <g:sortableColumn property="nombre" title="${message(code: 'aspirante.nombre.label', default: 'Nombre')}" />

            <g:sortableColumn property="apellido_paterno" title="${message(code: 'aspirante.apeliido_paterno.label', default: 'Apellido Paterno')}" />

            <g:sortableColumn property="apellido_materno" title="${message(code: 'aspirante.apellido_materno.label', default: 'Apellido Materno')}" />

            <g:sortableColumn property="Edad" title="${message(code: 'aspirante.edad.label', default: 'Edad')}" />

            <g:sortableColumn property="Sexo" title="${message(code: 'aspirante.sexo.label', default: 'Sexo')}" />

        </tr>
       </thead>
       <tbody>
        <g:each in="${aspiranteInstanceList}" status="i" var="aspiranteInstance">
            <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">

                <td><g:link action="show" id="${aspiranteInstance.id}">${fieldValue(bean: aspiranteInstance, field: "nombre")}</g:link> </td>

                <td>${fieldValue(bean: aspiranteInstance, field: "apellido_paterno")}</td>

                <td>${fieldValue(bean: aspiranteInstance, field: "apellido_materno")} </td>

                <td>${fieldValue(bean: aspiranteInstance, field: "edad")} </td>

                <td>${fieldValue(bean: aspiranteInstance, field: "sexo")} </td>

            </tr>
        </g:each>
       </tbody>
    </table>
    <div class="pagination">
    <g:paginate total="${aspiranteInstanceTotal}" />
    </div>
    </div>
    </body>
    </html>




Aucun commentaire:

Enregistrer un commentaire