0

Verify existence -Verificar existencia DNI

asked 2014-09-02 15:06:57 +0800

nachoatthecontrols gravatar image nachoatthecontrols
3 2

updated 2014-09-02 17:19:33 +0800

Hi there. I have a file (.zul) that sends data from a form with intbox and textbox to a Controller. In this Controller, I need to perform a search for "Ndoc" to check if the client is not in the database. The intbox containing "Ndoc" called "intdoc". If the client is embedded in the database, I want to display a message reporting that. I hope you can help me. I send the code that could perform. This is what I could do:

    @Command
    @NotifyChange("clienteModel")
    public void onClick$btnAddCliente(ForwardEvent fe, @BindingParam("clie") Cliente cli) throws DAOException {
            List<Cliente> listaClientes = ClienteDAO.findall();
//          List<Cliente> listaClientes = new ArrayList<Cliente>();
//          listaClientes.add(ClienteDAO.findall());
            boolean encontrado = false;
            for (Cliente cliente: listaClientes) {  
//            if (cliente.getNdoc() == intdoc.getValue()) {  
              if (cliente.getNdoc() == cli.getNdoc()) {  
                encontrado = true;  
                break;   // dejo el loop si encuentro el DNI del cliente
              }
            }
            if (!encontrado) {
                Cliente cliente = new Cliente(); 
                cliente.setNdoc(intdoc.getValue());
                cliente.setApellido(txtapellido.getValue());
                cliente.setNombre(txtnombre.getValue());
                ClienteService.altaCliente(cliente); 
                List<Cliente> client = getClienteModel();
                setCurrentCliente(client);
                clienteModel.add(cliente);
                Executions.sendRedirect("/cliente.zul");
            } else{
                alert("Mensaje.");
            }
    }

En español: Buenos días. Tengo un archivo (.zul) que envía datos desde un formulario con intbox y textbox a un controller. En esta clase controller necesito realizar una busqueda por "Ndoc", para verificar si el cliente no esta en la base de datos. El intbox que contiene el Ndoc se llama "intdoc". Si es que el cliente está inserto en la base de datos, deseo mostrar un mensaje avisando esto. Espero me puedan ayudar. Les paso parte del código que pude armar.

delete flag offensive retag edit

3 Answers

Sort by » oldest newest most voted
0

answered 2014-09-03 11:32:13 +0800

Alecs gravatar image Alecs
103 5

I'm guessing that cliente.getNdoc() returns an Integer. So take a look at your comparison row:

if (cliente.getNdoc() == cli.getNdoc()) {

Basic Java: you are checking if two references points on seam heap area, which is wrong. Change it with

if (cliente.getNdoc().equals(cli.getNdoc())) {
link publish delete flag offensive edit
1

answered 2014-09-03 12:23:21 +0800

chillworld gravatar image chillworld flag of Belgium
5322 4 9
https://github.com/chillw...

updated 2014-09-03 12:57:08 +0800

Hi there,

I think you do to much in java in stead let your DB do the work. Update you DAO with this method :

public Client findByNdoc(int Ndoc);

If nothing found => return null.
If something found => return what you have found.

So the code will be then :

if (!ClienteDAO.findByNdoc(cliente.getNDoc()!=null) {
    Cliente cliente = new Cliente();
    cliente.setNdoc(intdoc.getValue());
    cliente.setApellido(txtapellido.getValue());
    cliente.setNombre(txtnombre.getValue());  
    ClienteService.altaCliente(cliente);   
    List<Cliente> client = getClienteModel(); 
    setCurrentCliente(client); 
    clienteModel.add(cliente);  
    Executions.sendRedirect("/cliente.zul"); 
} else { 
    alert("Mensaje.");  
}

Why:

In your approach you iterate over all your clients.
As long you don't have much clients this will go fast.
Take the client list of IKEA world wide, well on a normal server your server will die on permgen.
If he can survive the memory allocation for all those clients => you will still have to iterate over all of them so make your system slow.

Mine approach will return 1 Client or no Client from the DB.
Less code in your viewmodel.
Less memory allocation and no iteration.
If the DB is good indexed the search can be very quick(I have a table with 6M entries, a search for 1 is done in less then a second).

Greetz chill.

link publish delete flag offensive edit
0

answered 2014-09-04 15:12:39 +0800

nachoatthecontrols gravatar image nachoatthecontrols
3 2

It's solved! My mistake was in the way used to compare. Simply replace "==" by method ".equals ()". Thank you very much both!

link publish delete flag offensive edit

Comments

also consider chill's suggestion. Your DB is there for a reason :-)

Alecs ( 2014-09-04 16:31:26 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2014-09-02 15:06:57 +0800

Seen: 17 times

Last updated: Sep 04 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More