You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@click.apache.org by Kristian Lind <kl...@gmail.com> on 2013/08/08 03:03:45 UTC

FormTable problem when submitting.

Hi I have this table form.

[image: Inline image 1]



When I submit the form, save button I get some errors.
The rule is that you can only have one of down price percentage, down price
amount or price.
On the Entity object I have validation on the setter methods for this rule.

When submitting click tries to put in i.e value 1 at id 8 resulting in an
error cause there is already a down price amount.

public class EditProduct extends BorderedPage {
private static final Logger logger =
LoggerFactory.getLogger(EditProduct.class);

private DashboardSBBeanLocal dashboardSBBeanLocal =
SessionBeanManager.getDashboardSBBeanLocal();
private Form form = new Form("form");
private ProductEnt product;
private FormTable optionFormTable = new FormTable("optionFormTable");
private FormTable quantityFormTable = new FormTable("quantityFormTable");
private FormTable combinationFormTable = new
FormTable("combinationFormTable");
private ActionLink deleteOptionLink = new ActionLink("deleteOptionLink",
"Delete", this, "onDeleteProductOptionClick");
private ActionLink deleteQuantityLink = new
ActionLink("deleteQuantityLink", "Delete", this,
"onDeleteProductQuantityClick");
private ActionLink deleteCombinationLink = new
ActionLink("deleteCombinationLink", "Delete", this,
"onDeleteCombinationClick");
private PageLink editCombinationLink = new PageLink("edit", "Edit",
EditProductOptionCombination.class);
private PagingDataProvider<ProductOptionEnt> productOptionDataProvider =
null;
private PagingDataProvider<ProductQuantityEnt> productQuantityDataProvider
= null;
private PagingDataProvider<ProductOptionCombEnt>
productCombinationDataProvider = null;
private TextField productName = new TextField("name", "Product name ",
true);
protected HiddenField idField1 = new HiddenField("id1", Long.class);
protected HiddenField idField2 = new HiddenField("id2", Long.class);
protected HiddenField idField3 = new HiddenField("id3", Long.class);
protected HiddenField idField4 = new HiddenField("id4", Long.class);

//Bindable variable id automatically have the value set by request
parameters
public Long productId;

public EditProduct() {
try {
 // quantityFormTable
buildQuantityFormTable();

deleteQuantityLink.addStyleClass("btn_small btn-primary");
deleteQuantityLink.setTitle("Delete record");
deleteQuantityLink.setAttribute("onclick", "return window.confirm('Are you
sure you want to delete this record?', 'Delete Product Option');");

quantityFormTable.setPaginator(new TableInlinePaginator(quantityFormTable));
quantityFormTable.setPaginatorAttachment(Table.PAGINATOR_INLINE);

button = new Submit("save", "Save", this, "onSaveQuantityFormTable");
button.addStyleClass("btn btn-primary");
quantityFormTable.getForm().add(button);

quantityFormTable.getForm().add(idField3);

addControl(deleteQuantityLink);
addControl(quantityFormTable);

} catch (NumberFormatException e) {
logger.error(e.getMessage(), e);
setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
return;
}

}


@Override
public void onGet() {
try {
String value = getContext().getRequestParameter("productId");
if (productId != null) {
init(productId);
}
} catch (NumberFormatException | SystemException e) {
logger.error(e.getMessage(), e);
setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
return;
}

}

@SuppressWarnings("unchecked")
private void init(final Long productId) throws SystemException {
idField1.setValueObject(productId);
idField2.setValueObject(productId);
idField3.setValueObject(productId);
idField4.setValueObject(productId);
deleteOptionLink.setParameter("productId", "" + productId);
deleteQuantityLink.setParameter("productId", "" + productId);
product = dashboardSBBeanLocal.getProduct(productId);
if (product == null) {
getContext().setFlashAttribute("entityErrorMessage", "You tried to edit
product that does not exist!");
getContext().setFlashAttribute("link", new PageLink("pageLink", "Products",
Products.class));
setRedirect(com.farheap.jsi.dashboard.pages.errors.EntityDoesNotExist.class);
return;
} else {
//productIdField.setValueObject(product.getId());
productName.setValue(product.getName());
optionFormTable.getControlLink().setParameter("productId", product.getId());
quantityFormTable.getControlLink().setParameter("productId",
product.getId());
}

productQuantityDataProvider = new PagingDataProvider() {
@Override
public int size() {
Integer size = 0;
try {
//Long id =
Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
size =
Integer.parseInt(dashboardSBBeanLocal.getProductQuantityCount(productId).toString());
} catch (SystemException e) {
logger.error(e.getMessage(), e);
setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
}
return size;
}

@Override
public List<ProductQuantityEnt> getData() {
List<ProductQuantityEnt> productQuantities = null;
int start = quantityFormTable.getFirstRow();
int count = quantityFormTable.getPageSize();
String sortColumn = quantityFormTable.getSortedColumn();
if (StringUtils.isBlank(sortColumn)) {
sortColumn = "id";
}
boolean ascending = quantityFormTable.isSortedAscending();
try {
//Long id =
Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
productQuantities = dashboardSBBeanLocal.getProductQuantities(productId,
start, count, sortColumn, ascending);
} catch (SystemException e) {
logger.error(e.getMessage(), e);
setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
}
return productQuantities;
}
};
quantityFormTable.setDataProvider(productQuantityDataProvider);
quantityFormTable.setSorted(true);
 getContext().setRequestAttribute("productId", productId);

}

public boolean onSaveQuantityFormTable() throws SystemException {
if (quantityFormTable.getForm().isValid()) {
List<ProductQuantityEnt> productQuantities = quantityFormTable.getRowList();
dashboardSBBeanLocal.saveProductQuantities(productQuantities);
}
Long id = Long.parseLong(idField3.getValue());
HashMap<String, String> m = new HashMap<String, String>();
m.put("productId", "" + id);
setRedirect(EditProduct.class, m);
return true;
}

public boolean onDeleteProductQuantityClick() throws SystemException {
Long id = deleteQuantityLink.getValueLong();
dashboardSBBeanLocal.deleteQuantity(id);
HashMap<String, String> m = new HashMap<String, String>();
m.put("productId", deleteOptionLink.getParameter("productId"));
setRedirect(EditProduct.class, m);
return true;
}

private void buildQuantityFormTable() {
quantityFormTable.getForm().setButtonAlign(Form.ALIGN_RIGHT);
quantityFormTable.setClass(Table.CLASS_ITS);
quantityFormTable.setPageSize(Constants.MAX_PAGE_SIZE);
quantityFormTable.setSortable(true);
quantityFormTable.addStyleClass("dash_table");

quantityFormTable.addColumn(new Column("id", "Id"));
 RegexField lowQuantity = new RegexField();
lowQuantity.setPattern("[0-9]{1,9}");
Column column = new FieldColumn("lowQuantity", lowQuantity);
column.setTextAlign("right");
quantityFormTable.addColumn(column);

RegexField highQuantity = new RegexField();
highQuantity.setPattern("[0-9]{1,9}");
column = new FieldColumn("highQuantity", highQuantity);
column.setTextAlign("right");
quantityFormTable.addColumn(column);

RegexField downPricePercentage = new RegexField();
downPricePercentage.setPattern("[0-9]{1,9}");
column = new FieldColumn("downPricePercentage", downPricePercentage);
column.setTextAlign("right");
quantityFormTable.addColumn(column);

RegexField downPriceAmount = new RegexField();
downPriceAmount.setPattern("[0-9]{1,3}.[0-9][0-9]");
column = new FieldColumn("downPriceAmount", downPriceAmount);
column.setTextAlign("right");
quantityFormTable.addColumn(column);

RegexField price = new RegexField();
price.setPattern("[0-9]{1,3}.[0-9][0-9]");
column = new FieldColumn("price", price);
column.setTextAlign("right");
quantityFormTable.addColumn(column);

//table.addColumn(new TEXTColumn("basePrice", "Base Price"));
//table.addColumn(new Column("unitPrice", "Unit Price"));
AbstractLink[] links = new AbstractLink[] { deleteQuantityLink };
Column c = new Column("action", "");
c.setTextAlign("center");
c.setWidth("120");
c.setSortable(false);
c.setDecorator(new LinkDecorator(quantityFormTable, links, "id", "id"));
quantityFormTable.addColumn(c);

quantityFormTable.getControlLink().setActionListener(new ActionListener() {
public boolean onAction(final Control source) {
quantityFormTable.saveState(getContext());
return true;
}
});

quantityFormTable.restoreState(getContext());

}

}







-- 
Best regards

Kristian Lind