How to validate if there is an existing record in a list before being inserted in java

0

I have a problem I hope and you can help me, what happens is that I have a list that I extract from the database, and what I want is to validate before inserting, that if a record exists in the list that do not insert it This is my insert method:

public void insertSale() {

        origin_adm = Session.getOriginAdmActiva();
        cve_usr = Session.getUserCve();

        tipoVenta.setOrigin_adm(Integer.parseInt(origin_adm));
        tipoVenta.setUsr_cve(cve_usr);

        if (tipoVenta.getCve_type_of_sale() == null) {
            tipoVenta.setCve_type_of_sale(0);
        }
        //System.out.println("Descripcion..." + tipoVenta.getDesc_type_of_sale());
        if (tipoVenta.getDesc_type_of_sale() == null || tipoVenta.getDesc_type_of_sale().equals("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: ", "Campo Requerido."));
            RequestContext.getCurrentInstance().update("alertaDistribucion");
            RequestContext.getCurrentInstance().execute("ocultaMsj(3000)");

        }

        else {
            int ultimoRegistro = dao.saveTypeSale(tipoVenta);
            if (tipoVenta.getCve_type_of_sale() == 0) {

                System.out.println("Entro insert...");
                TipoVentaDTO tipoVentaDTO = new TipoVentaDTO();
                tipoVentaDTO.setOrigin_adm(tipoVenta.getOrigin_adm());
                tipoVentaDTO.setCve_type_of_sale(ultimoRegistro);
                tipoVentaDTO.setDesc_type_of_sale(tipoVenta.getDesc_type_of_sale());
                tipoVentaDTO.setUsr_cve(tipoVenta.getUsr_cve());

                listTipoVentas.add(tipoVentaDTO);
                listTipoVentasFiltradas.add(tipoVentaDTO);

                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Exito: ", "Venta insertada exitosamente."));
                RequestContext.getCurrentInstance().update("alertaDistribucion");
                RequestContext.getCurrentInstance().execute("ocultaMsj(3000)");

            } else {
                System.out.println("Entro update...");
                for (int i = 0; i < listTipoVentas.size(); i++) {
                    if (listTipoVentas.get(i).getCve_type_of_sale() == tipoVenta.getCve_type_of_sale()) {
                        listTipoVentas.get(i).setDesc_type_of_sale(tipoVenta.getDesc_type_of_sale());
                    }

                }
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Exito: ", "venta actualizada exitosamente."));
                RequestContext.getCurrentInstance().update("alertaDistribucion");
                RequestContext.getCurrentInstance().execute("ocultaMsj(3000)");

            }
            reset();
            RequestContext.getCurrentInstance().execute("cancelar()");

        }

    }

I have no idea how to start that validation, I hope and you can help me with this problem, I remain alert, kind regards and if you need any other information, I'll gladly put it.

    
asked by cratus666jose 13.11.2018 в 17:43
source

3 answers

0

To see if the array contains a value before adding it, you can do this

if (listTipoVentas.contains(valor_a_buscar)) {
    System.out.println("Se encontro el valor, no agregar");
} else {
    System.out.println("El valor no esta en la lista");
}

You can read a little more about how it works in this Link

The problem so it never enters your else is by this line

 if (tipoVenta.getCve_type_of_sale() == null) {
            tipoVenta.setCve_type_of_sale(0);
        }

Each time you call your insertSale method, you set that value to 0 and then compare it if it's 0 here

if (tipoVenta.getCve_type_of_sale() == 0) {...

Since 0 is always going to fill the list but never the else to use the contains, I suggest you check that part of the logic.

    
answered by 13.11.2018 в 18:36
0

Well that's how my solution was:

 for (int i = 0; i < listTipoVentas.size(); i++) {
        if (listTipoVentas.get(i).getDesc_type_of_sale().equals(tipoVenta.getDesc_type_of_sale())) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error: ", "Valor insertado ya se encuentra."));
            RequestContext.getCurrentInstance().update("alertaDistribucion");
            RequestContext.getCurrentInstance().execute("ocultaMsj(3000)");
            return;
        }

    }

I hope someone works, greetings guys.

    
answered by 14.11.2018 в 18:29
0

I recommend that instead of bringing you all a query full of records, make a query with a stored procedure in which you send the value you need that is not repeated, it can be a select to the table and you return a object. if the object comes empty it means that there is no record with that value therefore the product can be registered, if the object comes with something it means that it already exists.

Or in your database you can add a unique key to the field that you need to not repeat, although I do not recommend it much since you must handle the exception in your application and apart you may be bothered by that unique key later on if you need to scale your database

    
answered by 14.11.2018 в 20:02