pepelara
Joined: 29 Nov 2008 Posts: 115
|
Posted: Sun May 31, 2009 2:46 pm Post subject: Visual Web JSF and CRUD problem |
|
|
Hi,
I am developing a a web application based on the tutorial Performing Inserts, Updates, and Deletes (http://www.netbeans.org/kb/docs/web/inserts-updates-deletes.html).
My web app is composed of several JSP pages. The last one is Piezas.java. This web page contains three DDL, three Buttons and a Table components.
My database has four tables CATEGORIA, SUBCATEGORIA, PRODUCTO and PIEZA.
In Piezas.java CATEGORIA, SUBCATEGORIA and PRODUCTO are dropped on the three DDLs setting up CATEGORIA and SUBCATEGORIA a Virtual Form for the nested PRODUCTO DDL.
So when a select a categoria and a subcategoria, producto is loaded with the corresponding values. It means that I can have some productos or not (in the last case PRODUCTO DDL has no values).
Once I select a producto, the database table PIEZA is loaded in the table component for the corresponding producto. Here it happens the same of the above PRODUCTO DDL case.
Since here I want to append rows, add a new data row, or delete a data row. But I can not get it. Always I append a row, the table component shows the first row in the database, and I need an empty row to provide new data for the correspondig selected producto.
I use four Virtual Forms: categoria_subcategoria on participate and submmit for the DDL PRODUCTO; saveproducto on participate for the nested values; producto on participate and submit for the table component PIEZA; and save on participate for te selected producto and on submmit for the button Add Pieza
I know I am doing it wrong, but I do not know where.
This is my code,
| Code: |
@Override
public void prerender() {
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();
piezas();
} 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();
piezas();
} 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();
piezas();
} 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();
piezas();
} catch (Exception e) {
error("Cannot switch to categoria/subcategoria " +
firstCategoriaId + "/" + firstSubcategoriaId);
log("Cannot switch to categoria/subcategoria " +
firstCategoriaId + "/" + firstSubcategoriaId, e);
}
}
}
private void piezas() {
RowKey[] rk = productoDataProviderPieza.getAllRows();
Object firstProductoId = null;
if (rk.length > 0) {
if (productosDD.getSelected() == 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 {
try {
// Synchronize data provider with current selection
firstProductoId = productosDD.getSelected();
productoDataProviderPieza.setCursorRow(
productoDataProviderPieza.findFirst(
"PRODUCTO.ID_PRODUCTO", 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 {
try {
getApplicationBean1().getPiezaRowSet().setObject(1, new Integer(-1));
piezaDataProvider.refresh();
getRequestBean1().setMensaje("No hay Productos");
} catch (Exception e) {
error("Cannot switch to producto " +
firstProductoId);
log("Cannot switch to producto " +
firstProductoId, e);
}
}
}
|
| Code: |
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");
}
}
|
| Code: |
public String add_action() {
// TODO: Process the action. Return value is a navigation
// case name where null will return to the same page.
RowKey[] rkArray = productoDataProviderPieza.getAllRows();
if (rkArray.length > 0) {
if (piezaDataProvider.canAppendRow()) {
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 {
log("Cannot append new pieza");
error("Cannot append new pieza");
}
} else {
getRequestBean1().setMensaje("No hay Productos");
}
return null;
}
public String save_action() {
// TODO: Process the action. Return value is a navigation
// case name where null will return to the same page.
try {
// Get the next key, using result of query on MaxTrip data provider
CachedRowSetDataProvider maxPieza = getApplicationBean1().getMaxPiezaDataProvider();
maxPieza.refresh();
maxPieza.cursorFirst();
int newPiezaId = 1;
if (((Integer) maxPieza.getValue("MAXPIEZA")) != null) {
newPiezaId = ((Integer) maxPieza.getValue("MAXPIEZA"));
}
// Navigate through rows with data provider
if (piezaDataProvider.getRowCount() > 0) {
piezaDataProvider.cursorFirst();
do {
if (piezaDataProvider.getValue("PIEZA.ID_PIEZA").equals(new Integer(0))) {
piezaDataProvider.setValue("PIEZA.ID_PIEZA",
new Integer(newPiezaId));
newPiezaId++;
}
} while (piezaDataProvider.cursorNext());
}
piezaDataProvider.commitChanges();
} catch (Exception ex) {
log("Error Description", ex);
error("Error :" + ex.getMessage());
}
return null;
}
|
Excuse my english. I have tried to explain it as good as I can. If you need more information just ask me and I will give you the information.
Thanks,
Jose Alvarez de Lara |
|