webtier@glassfish.java.net

Visual Web JSF and CRUD problem

From: <webtier_at_javadesktop.org>
Date: Sat, 23 May 2009 03:19:37 PDT

Hi,

Going on with my web over NetBeans 6.5 Visual Web JSF, I had to make some changes to my data model because an initial wrong design. But I want to talk about Piezas.java.

In Piezas.java I have three DDLs (Categorias, Subcategorias and Productos), three buttons (“New Pieza”, “Save Pieza”, and “Return to Index”) and a UIComponent Table that represents the selected Piezas.

The relationships between database tables is as follow,

Categoria
PK: ID_CATEGORIA

Subcategoria
PK: ID_SUBCATEGORIA
FK: ID_CATEGORIA

Producto
PK: ID_PRODUCTO
FK: ID_CATEGORIA
FK: ID_SUBCATEGORIA

Pieza
PK: ID_PIEZA
FK: ID_PRODUCTO

So my Piezas.java try CRUD based on this relationship. The DDLs Categorias and Subcategorias are set in a Virtual Form to set the DDL Productos. This works fine.
And when I select some categoria or subcategoria that take me to a producto´s non value, the UIComponent Table Pieza looks empty: ok.

I have three Virtual Forms, “categoria_subcategoria” to nest with the DDL Productos, “saveproducto” that save the new nested values on the DDL Productos on Participate and Submit the new pieza, and “save” to load the corresponding pieza values on the UIComponent Table Pieza once selected a new producto.

What is my problem? When I try to add a new pieza, in the UIComponent Table Pieza appears the first row in the database table Pieza. I mean, there are two cases, there is no Producto and I cannot append any row, or there is a producto so when I append a new row it has to be from the entity of the selected Producto. Well, it is not. It is always from the entity of the first row of the database table Pieza.

This is my code,

@Override
    public void prerender() {
        if ((getRequestBean1().getAction()).equals("nothing")) {
            Object firstCategoriaId = null;
            Object firstSubcategoriaId = null;
            if ((categoriasDD.getSelected() == null) && (subcategoriasDD.getSelected() == null)) {
                try {
                    categoriaDataProviderPieza.cursorFirst();
                    subcategoriaDataProviderPieza.cursorFirst();
                    firstCategoriaId = categoriaDataProviderPieza.getValue("CATEGORIA.ID_CATEGORIA");
                    firstSubcategoriaId = subcategoriaDataProviderPieza.getValue("SUBCATEGORIA.ID_SUBCATEGORIA");
                    categoriasDD.setSelected(firstCategoriaId);
                    subcategoriasDD.setSelected(firstSubcategoriaId);
                    getApplicationBean1().getProductoRowSetPieza().setObject(
                            1, firstCategoriaId);
                    getApplicationBean1().getProductoRowSetPieza().setObject(
                            2, firstSubcategoriaId);
                    productoDataProviderPieza.refresh();
                } catch (Exception e) {
                    error("Cannot switch to categoria/subcategoria " +
                            firstCategoriaId + "/" + firstSubcategoriaId);
                    log("Cannot switch to categoria/subcategoria " +
                            firstCategoriaId + "/" + firstSubcategoriaId, e);
                }
            } else if ((categoriasDD.getSelected() != null) && (subcategoriasDD.getSelected() == null)) {
                firstCategoriaId = categoriasDD.getSelected();
                try {
                    subcategoriaDataProviderPieza.cursorFirst();
                    getApplicationBean1().getProductoRowSet().setObject(
                            1, firstCategoriaId);
                    firstSubcategoriaId = subcategoriaDataProviderPieza.getValue("SUBCATEGORIA.ID_SUBCATEGORIA");
                    subcategoriasDD.setSelected(firstSubcategoriaId);
                    getApplicationBean1().getProductoRowSetPieza().setObject(
                            2, firstSubcategoriaId);
                    productoDataProviderPieza.refresh();
                } catch (Exception e) {
                    error("Cannot switch to categoria/subcategoria " +
                            firstCategoriaId + "/" + firstSubcategoriaId);
                    log("Cannot switch to categoria/subcategoria " +
                            firstCategoriaId + "/" + firstSubcategoriaId, e);
                }
            } else if ((categoriasDD.getSelected() == null) && (subcategoriasDD.getSelected() != null)) {
                try {
                    categoriaDataProviderPieza.cursorFirst();
                    firstCategoriaId = categoriaDataProviderPieza.getValue("CATEGORIA.ID_CATEGORIA");
                    categoriasDD.setSelected(firstCategoriaId);
                    getApplicationBean1().getProductoRowSetPieza().setObject(
                            1, firstCategoriaId);
                    firstSubcategoriaId = subcategoriasDD.getSelected();
                    getApplicationBean1().getProductoRowSetPieza().setObject(
                            2, firstSubcategoriaId);
                    productoDataProviderPieza.refresh();
                } catch (Exception e) {
                    error("Cannot switch to categoria/subcategoria " +
                            firstCategoriaId + "/" + firstSubcategoriaId);
                    log("Cannot switch to categoria/subcategoria " +
                            firstCategoriaId + "/" + firstSubcategoriaId, e);
                }
            } else if ((categoriasDD.getSelected() != null) && (subcategoriasDD.getSelected() != null)) {
                try {
                    firstCategoriaId = categoriasDD.getSelected();
                    firstSubcategoriaId = subcategoriasDD.getSelected();
                    getApplicationBean1().getProductoRowSetPieza().setObject(
                            1, firstCategoriaId);
                    getApplicationBean1().getProductoRowSetPieza().setObject(
                            2, firstSubcategoriaId);
                    productoDataProviderPieza.refresh();
                } catch (Exception e) {
                    error("Cannot switch to categoria/subcategoria " +
                            firstCategoriaId + "/" + firstSubcategoriaId);
                    log("Cannot switch to categoria/subcategoria " +
                            firstCategoriaId + "/" + firstSubcategoriaId, e);
                }
            }
            RowKey[] rk = productoDataProviderPieza.getAllRows();
            if (rk.length > 0) {
                if (productosDD.getSelected() == null) {
                    Object firstProductoId = null;
                    try {
                        productoDataProviderPieza.cursorFirst();
                        firstProductoId = productoDataProviderPieza.getValue("PRODUCTO.ID_PRODUCTO");
                        productosDD.setSelected(firstProductoId);
                        getApplicationBean1().getPiezaRowSet().setObject(
                                1, firstProductoId);
                        piezaDataProvider.refresh();
                    } catch (Exception e) {
                        error("Cannot switch to producto " +
                                firstProductoId);
                        log("Cannot switch to producto " +
                                firstProductoId, e);
                    }
                }
            } else {
                getRequestBean1().setMensaje("No hay Productos");
            }
        }
        getRequestBean1().setAction("nothing");
    }


public void categoriasDD_processValueChange(ValueChangeEvent event) {
        Object selectedCategoriaId = categoriasDD.getSelected();
        try {
            productosDD.resetValue();
            categoriaDataProviderPieza.setCursorRow(
                    categoriaDataProviderPieza.findFirst("CATEGORIA.ID_CATEGORIA",
                    selectedCategoriaId));
            getApplicationBean1().getProductoRowSetPieza().setObject(1, selectedCategoriaId);
            productoDataProviderPieza.refresh();
            form1.discardSubmittedValues("saveproducto");
        } catch (Exception e) {
            error("Cannot switch to categoria " + selectedCategoriaId);
            log("Cannot switch to categoria " + selectedCategoriaId, e);
        }
    }

    public void subcategoriasDD_processValueChange(ValueChangeEvent event) {
        Object selectedSubcategoriaId = subcategoriasDD.getSelected();
        try {
            productosDD.resetValue();
            subcategoriaDataProviderPieza.setCursorRow(
                    subcategoriaDataProviderPieza.findFirst("SUBCATEGORIA.ID_SUBCATEGORIA",
                    selectedSubcategoriaId));
            getApplicationBean1().getProductoRowSetPieza().setObject(2, selectedSubcategoriaId);
            productoDataProviderPieza.refresh();
            form1.discardSubmittedValues("saveproducto");
        } catch (Exception e) {
            error("Cannot switch to subcategoria " + selectedSubcategoriaId);
            log("Cannot switch to subcategoria " + selectedSubcategoriaId, e);
        }
    }

    public void productosDD_processValueChange(ValueChangeEvent event) {
        RowKey[] rk = productoDataProviderPieza.getAllRows();
        if (rk.length > 0) {
            Object selectedProductoId = productosDD.getSelected();
            try {
                productoDataProviderPieza.setCursorRow(
                        productoDataProviderPieza.findFirst("PRODUCTO.ID_PRODUCTO",
                        selectedProductoId));
                getApplicationBean1().getPiezaRowSet().setObject(1, selectedProductoId);
                piezaDataProvider.refresh();
                form1.discardSubmittedValues("save");
            } catch (Exception e) {
                error("Cannot switch to producto " + selectedProductoId);
                log("Cannot switch to producto " + selectedProductoId, e);
            }
        } else {
            getRequestBean1().setMensaje("No hay Productos");
        }
    }

public String add_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        getRequestBean1().setAction("addpieza");
        RowKey[] rkArray = productoDataProviderPieza.getAllRows();
        if (rkArray.length > 0) {
            try {
                RowKey rk = piezaDataProvider.appendRow();
                piezaDataProvider.setCursorRow(rk);

                piezaDataProvider.setValue("PIEZA.ID_PIEZA", new Integer(0));
                piezaDataProvider.setValue("PIEZA.ID_PRODUCTO", productosDD.getSelected());
            } catch (Exception ex) {
                log("Error Description", ex);
                error(ex.getMessage());
            }
        } else {
            getRequestBean1().setMensaje("No hay Productos");
        }
        return null;
    }

It was taking me two weeks and I have not any idea about the solution.
Please help.

Best regards,
Jose Alvarez de Lara
[Message sent by forum member 'josealvarezdelara' (josealvarezdelara)]

http://forums.java.net/jive/thread.jspa?messageID=347398