I found a number of errors in this sample app. using Visual Java Server Faces in Netbeans 6.1. This example uses the sample travel data base that comes with the IDE. When running the app, you can select a row to delete and press the delete button, but often times the wrong row gets deleted and the results are often inconsistant; sometimes it works like it should and deletes the proper row, sometimes it doesn't. The add button sometimes adds a new row, sometimes it doesn't. I read the jave code behind the two buttons, and can't figure out what's wrong. Here it is:
public String deleteButton_action() {
// TODO: Process the action. Return value is a navigation
// case name where null will return to the same page.
Iterator rowKeys = selectedTrips.iterator();
while (rowKeys.hasNext()) {
RowKey rowKey = (RowKey) rowKeys.next();
System.out.println("rowKey: " + rowKey);
try {
tripDataProvider.removeRow(rowKey);
} catch (Exception e) {
error("Cannot delete trip with row key " + rowKey);
}
}
// do outside loop because commitChanges() invalidates rowkeys
try {
tripDataProvider.commitChanges();
} catch (Exception ex) {
error("Cannot commit deletions.");
}
return null;
}
/**
* Returns true if the trip for the current row is selected.
*/
public boolean isSelectedTrip() {
TableRowDataProvider trdp = (TableRowDataProvider) getBean("currentRow");
if (trdp == null) {
return false;
}
RowKey rowKey = trdp.getTableRow();
return selectedTrips.contains(rowKey);
}
/**
* Records whether or not the current trip should be marked as selected,
* based on the state of the checkbox.
*/
public void setSelectedTrip(boolean b) {
TableRowDataProvider trdp = (TableRowDataProvider) getBean("currentRow");
RowKey rowKey = trdp.getTableRow();
if (selectedTripCheckbox.isChecked()) {
selectedTrips.add(rowKey);
} else {
selectedTrips.remove(rowKey);
}
}
public String addButton_action() {
// TODO: Process the action. Return value is a navigation
// case name where null will return to the same page.
if ( tripDataProvider.canAppendRow() ) {
try {
RowKey rowKey = tripDataProvider.appendRow();
tripDataProvider.setCursorRow(rowKey);
tripDataProvider.setValue("TRIP.TRIPID", rowKey, nextPK());
tripDataProvider.setValue("TRIP.PERSONID", rowKey, personId.getSelected());
java.util.Date depDate = (java.util.Date) departureDate.getValue();
if ( depDate != null ) {
java.sql.Date date = new java.sql.Date( depDate.getTime() );
tripDataProvider.setValue("TRIP.DEPDATE", rowKey, date);
} else {
tripDataProvider.setValue("TRIP.DEPDATE", rowKey, null);
}
tripDataProvider.setValue("TRIP.DEPCITY", rowKey, departureCity.getValue());
tripDataProvider.setValue("TRIP.DESTCITY", rowKey, destinationCity.getValue());
tripDataProvider.setValue("TRIP.TRIPTYPEID", rowKey, tripType.getSelected());
tripDataProvider.commitChanges();
} catch (Exception e) {
error("Cannot append new trip: " + e);
}
} else {
error("Cannot append a new row");
}
//clear fields
departureDate.setValue(null);
departureCity.setText(null);
destinationCity.setText(null);
tripType.setForgetValue(true);
form1.discardSubmittedValues("saveChanges");
return null;
}