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/06 20:15:12 UTC

Request Parameter

Hi, I have this page, it contains a product that has a list of options.

The product has a name that can be changed, and you can change the options.

My Problem is when submitting the table form save button, I do not have the
product id anymore.
So in the OnInit method I can not get the data.


[image: Inline image 1]


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

private static final String PAGE_TITLE_CREATE = "Add Product";
private static final String PAGE_TITLE_EDIT = "Edit Product";
private static final int MIN_NAME_LENGTH = 3;
private static final int VALIDATE_NAME_LENGTH = 50;

private DashboardSBBeanLocal dashboardSBBeanLocal =
SessionBeanManager.getDashboardSBBeanLocal();
private EditProductForm form = null;
private ProductEnt product;
private FormTable table = new FormTable("table");
private ActionLink deleteLink = new ActionLink("delete", this,
"onDeleteClick");
private PagingDataProvider<ProductOptionEnt> dataProvider = null;
 @Bindable
private Long productId;
 public EditProduct() {
form = new EditProductForm("form");
form.setButtonAlign("right");
Button button = new Submit("save", "Save", this, "onSave");
button.addStyleClass("btn btn-primary");
form.add(button);

PageSubmit pageSubmit = new PageSubmit("cancel", "Cancel", Products.class);
pageSubmit.addStyleClass("btn btn-primary");
form.add(pageSubmit);

// TableForm
button = new Submit("save", "Save", this, "onSaveTableForm");
button.addStyleClass("btn btn-primary");
table.getForm().add(button);

button = new Submit("cancel", "Cancel", this, "onCancelTableForm");
button.addStyleClass("btn btn-primary");
table.getForm().add(button);
 addControl(form);
}

@Override
public final String getTitle() {
if (product == null) {
return PAGE_TITLE_CREATE;
} else {
return PAGE_TITLE_EDIT;
}
}

public final boolean onSave() throws SystemException {
if (form.isValid()) {
Long id = form.getProductId();
if (id == null) {
ProductEnt productEnt = new ProductEnt();
PrintProviderEnt printProviderEnt =
dashboardSBBeanLocal.getPrintProviderByName(getContext().getRequest().getUserPrincipal().getName());
productEnt.setPrintProviderEnt(printProviderEnt);
form.copyTo(productEnt);
dashboardSBBeanLocal.saveProduct(productEnt);
} else {
ProductEnt productEnt = dashboardSBBeanLocal.getProduct(id);
form.copyTo(productEnt);
dashboardSBBeanLocal.saveProduct(productEnt);
}
form.clearValues();
HashMap<String, String> m = new HashMap<String, String>();
m.put("productId", "" + id);
setRedirect(EditProduct.class, m);
}
return true;
}

private class EditProductForm extends Form {
private TextField productName = new TextField("name", "Product name ",
true);
private Field productIdField = new HiddenField("id", Long.class);

public EditProductForm(final String name) {
super(name);
add(productIdField);
productName.setMinLength(MIN_NAME_LENGTH);
productName.addStyleClass("dash_input");
add(productName);
deleteLink.addStyleClass("btn_small btn-primary");
deleteLink.setTitle("Delete record");
deleteLink.setAttribute("onclick", "return window.confirm('Are you sure you
want to delete this record?', 'Delete Product Option');");
addControl(deleteLink);

AbstractLink[] links = new AbstractLink[] { deleteLink };
table.getForm().setButtonAlign(Form.ALIGN_RIGHT);
table.setClass(Table.CLASS_ITS);
table.setPageSize(Constants.MAX_PAGE_SIZE);
table.setSortable(true);
table.addStyleClass("dash_table");

table.addColumn(new Column("id", "id"));
table.addColumn(new Column("optionType", "Options"));

Select select = new Select();
Column column = new FieldColumn("value", select);
table.addColumn(column);

Column c = new Column("action", "");
c.setTextAlign("center");
c.setWidth("120");
c.setSortable(false);
c.setDecorator(new LinkDecorator(table, links, "id", "id"));
table.addColumn(c);

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

table.restoreState(getContext());

addControl(table);

PageLink addLink = new PageLink("add", " New Option ", EditProduct.class);
addLink.setId("new_option");
addLink.addStyleClass("btn btn-primary");
addControl(addLink);

}

public Long getProductId() {
return (Long) productIdField.getValueObject();
}

public TextField getProductName() {
return productName;
}

@Override
public void onInit() {
super.onInit();
try {
Long productId = null;
String value = getContext().getRequestParameter("productId");
getContext().setRequestAttribute("productId", value);
if (!StringUtils.isBlank(value)) {
productId = Long.parseLong(value);
} else {
value = table.getControlLink().getParameter("productId");
if (!StringUtils.isBlank(value)) {
productId = Long.parseLong(value);
}
}
if (productId != null) {
deleteLink.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());
table.getControlLink().setParameter("productId", product.getId());
}
dataProvider = new PagingDataProvider() {
@Override
public int size() {
Integer size = 0;
try {
Long id = Long.parseLong(table.getControlLink().getParameter("productId"));
size =
Integer.parseInt(dashboardSBBeanLocal.getProductOptionCount(id).toString());
} catch (SystemException e) {
logger.error(e.getMessage(), e);
setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
}
return size;
}

@Override
public List<ProductOptionEnt> getData() {
List<ProductOptionEnt> productOptions = null;
int start = table.getFirstRow();
int count = table.getPageSize();
String sortColumn = table.getSortedColumn();
boolean ascending = table.isSortedAscending();
Long id = Long.parseLong(table.getControlLink().getParameter("productId"));
try {
productOptions = dashboardSBBeanLocal.getProductOptions(id, start, count,
sortColumn, ascending);
} catch (SystemException e) {
logger.error(e.getMessage(), e);
setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
}
return productOptions;
}
};
table.setDataProvider(dataProvider);

List<ProductOptionEnt> rowList = table.getRowList();
for (ProductOptionEnt productOptionEnt : rowList) {
com.farheap.jsi.utils.Constants.Option optionType =
productOptionEnt.getOptionType();
List<EnumI> enums = optionType.getEnums();
Select select = new Select();
for (EnumI enumI : enums) {
select.add(enumI.name());
}
FieldColumn column = (FieldColumn) table.getColumn("value");
column.setField(select);

}
}

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

}

private void validateSize(final Field field, final int size) {
if (field.getValue().length() > size) {
field.setError("Maximum " + field.getName() + " size exceeded (" + size +
").");
}
}

@Override
public void validate() {
super.validate();
validateSize(productName, VALIDATE_NAME_LENGTH);
}

}

public boolean onSaveTableForm() throws SystemException {
if (table.getForm().isValid()) {
List<ProductOptionEnt> productOptions = table.getRowList();
dashboardSBBeanLocal.saveProductOptions(productOptions);
}
HashMap<String, String> m = new HashMap<String, String>();
*m.put("productId", // NEED THE PRODUCT ID HERE...  );*
setRedirect(EditProduct.class, m);
return true;
}

public boolean onCancelTableForm() throws SystemException {
table.setDataProvider(dataProvider);
// As form was cancelled, don't render the user submitted values
table.setRenderSubmittedValues(false);
return true;
}

@Override
public String getHelp() {
return getContext().getRequest().getContextPath() +
"/provider/edit-product-help.htm";
}

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

}

Re: Request Parameter

Posted by Kristian Lind <kl...@gmail.com>.
Any chance you can provide with a code example that takes base on what I
submitted...


On Wed, Aug 7, 2013 at 1:19 PM, Gilberto <gi...@gmail.com> wrote:

> When you view the page on the first place, you are using the get method
> and consequently the public page parameter. After that, on submit, you are
> using the post method and consequently the hidden field defined on your
> form.
> You set the hidden field of your form with the public parameter on the get
> method and get it back (the hidden field) on the submit action.
>
>
> 2013/8/7 Kristian Lind <kl...@gmail.com>
>
>> How can this help me.. ?
>> The first method called when the save button is pressed if the OnInit()
>> method.. and the productId is null.
>>
>> Don't quit understand how I can use the post method...
>>
>>
>>
>> On Wed, Aug 7, 2013 at 11:54 AM, Gilberto <gi...@gmail.com> wrote:
>>
>>>
>>>
>>> You can use the browser's View Page Source to check the hidden[1] fields
>>> before submitting the page. And increase the trace as well [2] .
>>>
>>> Regards,
>>>
>>> Gilberto
>>>
>>> [1]
>>> http://click.apache.org/docs/click-api/org/apache/click/control/HiddenField.html
>>> [2]
>>> http://click.apache.org/docs/user-guide/htmlsingle/click-book.html#application-mode
>>>
>>
>>
>>
>> --
>> Best regards
>>
>> Kristian Lind
>>
>
>


-- 
Best regards

Kristian Lind

Re: Request Parameter

Posted by Gilberto <gi...@gmail.com>.
When you view the page on the first place, you are using the get method and
consequently the public page parameter. After that, on submit, you are
using the post method and consequently the hidden field defined on your
form.
You set the hidden field of your form with the public parameter on the get
method and get it back (the hidden field) on the submit action.


2013/8/7 Kristian Lind <kl...@gmail.com>

> How can this help me.. ?
> The first method called when the save button is pressed if the OnInit()
> method.. and the productId is null.
>
> Don't quit understand how I can use the post method...
>
>
>
> On Wed, Aug 7, 2013 at 11:54 AM, Gilberto <gi...@gmail.com> wrote:
>
>>
>>
>> You can use the browser's View Page Source to check the hidden[1] fields
>> before submitting the page. And increase the trace as well [2] .
>>
>> Regards,
>>
>> Gilberto
>>
>> [1]
>> http://click.apache.org/docs/click-api/org/apache/click/control/HiddenField.html
>> [2]
>> http://click.apache.org/docs/user-guide/htmlsingle/click-book.html#application-mode
>>
>
>
>
> --
> Best regards
>
> Kristian Lind
>

Re: Request Parameter

Posted by Kristian Lind <kl...@gmail.com>.
How can this help me.. ?
The first method called when the save button is pressed if the OnInit()
method.. and the productId is null.

Don't quit understand how I can use the post method...



On Wed, Aug 7, 2013 at 11:54 AM, Gilberto <gi...@gmail.com> wrote:

>
>
> You can use the browser's View Page Source to check the hidden[1] fields
> before submitting the page. And increase the trace as well [2] .
>
> Regards,
>
> Gilberto
>
> [1]
> http://click.apache.org/docs/click-api/org/apache/click/control/HiddenField.html
> [2]
> http://click.apache.org/docs/user-guide/htmlsingle/click-book.html#application-mode
>



-- 
Best regards

Kristian Lind

Re: Request Parameter

Posted by Gilberto <gi...@gmail.com>.
You can use the browser's View Page Source to check the hidden[1] fields
before submitting the page. And increase the trace as well [2] .

Regards,

Gilberto

[1]
http://click.apache.org/docs/click-api/org/apache/click/control/HiddenField.html
[2]
http://click.apache.org/docs/user-guide/htmlsingle/click-book.html#application-mode

Re: Request Parameter

Posted by Kristian Lind <kl...@gmail.com>.
I have one form, and actually 3 FormTables.
I have a submit button for each of the 4 forms.

When submitting one of the table forms, I managed to stay on the same page
and keep the productId, so the redirected the product is loaded again.
But I has to add a HiddenField to every form.

I had a cancel button for each of the FormTables, but I could not get that
to work. The idea was to have the cancel button not submit anything, but
only refresh the page with data from the server.

Here is a part of the code that relates to the quantityFormTable


protected HiddenField idField3 = new HiddenField("id3", Long.class);
private FormTable quantityFormTable = new FormTable("quantityFormTable");

quantityFormTable.getForm().add(idField3);

private void init(final Long productId) throws SystemException {
        ...
idField3.setValueObject(productId);
        ...
}

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

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;
}



[image: Inline image 1]


On Thu, Aug 8, 2013 at 11:41 AM, Bob Schellink <sa...@gmail.com> wrote:

>  I never used FormTable but from the screenshot it seems you have a Form
> and a FormTable on the page. Are you submitting the Form and expecting the
> FormTable to be submitted as well? That won't work since HTML only sends
> the form data you are submitting, not all forms on the page.
>
> regards
>
> Bob
>
>
> On 2013/08/07 18:52, Kristian Lind wrote:
>
> When I press the save button on options, OnInit() is the first method to
> be called, but the productId is null.
> *How can I make sure it gets the productId ??? *
>
>  The next method that is called is the
>  public boolean onSaveOptionFormTable() throws SystemException {
> if (optionFormTable.getForm().isValid()) {
>  List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
>  dashboardSBBeanLocal.saveProductOptions(productOptions);
> }
> Long id = Long.parseLong(idField1.getValue());
> HashMap<String, String> m = new HashMap<String, String>();
> m.put("productId", "" + id);
> setRedirect(EditProduct.class, m);
> return true;
> }
>
>  but here the productOptions list is empty... cause of the null productId
> from the OnInit() method.
>
>  The next method is then again the OnInit() method, and now there is a
> productId.. cause the
> m.put("productId", "" + id);
>  in onSaveOptionFormTable.
>
>
>
>
>
>
>
> On Tue, Aug 6, 2013 at 5:13 PM, Kristian Lind <kl...@gmail.com> wrote:
>
>> I have been looking at those examples. I cant find any that have the
>> needs I have.
>> I am displaying one product, so the page need the id of the product.
>> Which it get from the previous page...
>>  But I wanna stay on the page after saving... the examples return to
>> another page.
>>
>>  I am trying to do some like this..
>>
>> http://click.avoka.com/click-examples/source-viewer.htm?filename=WEB-INF/classes/org/apache/click/examples/page/table/FormTablePage.java
>>  Here all the customers are fetched...
>>
>> *public* DataProvider createDataProvider() {
>>         DataProvider dp = *new* DataProvider() {
>>             *public* List<Customer> getData() {
>>                 *return* customerService.getCustomersSortedByName(NUM_ROWS);
>>             }
>>         };
>>    *return* dp;
>> }
>>
>> Whereas I need to get the options from a specific product.
>>
>>
>>
>>  I now managed to be able to stay on the page, so the productId is used
>> in the save methods..
>>
>>  Long id = Long.parseLong(idField1.getValue());
>>  HashMap<String, String> m = new HashMap<String, String>();
>>  m.put("productId", "" + id);
>> setRedirect(EditProduct.class, m);
>>
>>
>>  The problem now is that in the in the method onSaveOptionFormTable()
>> the
>>  List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
>>  is empty.
>>
>>  So no options are updated...
>>
>>  and the onInit() method has no productId.
>>
>>
>>  /**
>>  */
>> public class EditProduct extends BorderedPage {
>>  private static final Logger logger =
>> LoggerFactory.getLogger(EditProduct.class);
>>
>>  private static final String PAGE_TITLE_CREATE = "Add Product";
>>  private static final String PAGE_TITLE_EDIT = "Edit Product";
>>  private static final int MIN_NAME_LENGTH = 3;
>>  private static final int VALIDATE_NAME_LENGTH = 50;
>>
>>  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 ActionLink deleteOptionLink = new
>> ActionLink("deleteOptionLink", "Delete", this,
>> "onDeleteProductOptionClick");
>>  private ActionLink deleteQuantityLink = new
>> ActionLink("deleteQuantityLink", "Delete", this,
>> "onDeleteProductQuantityClick");
>>  private PagingDataProvider<ProductOptionEnt> productOptionDataProvider
>> = null;
>>  private PagingDataProvider<ProductOptionEnt>
>> productQuantityDataProvider = null;
>>  private TextField productName = new TextField("name", "Product name ",
>> true);
>>  protected HiddenField idField = new HiddenField("id", Long.class);
>>  protected HiddenField idField1 = new HiddenField("id", Long.class);
>>  protected HiddenField idField2 = new HiddenField("id", Long.class);
>>
>>  /**
>>  * Bindable variable id automatically have the value set by request
>>  * parameters
>>  */
>>   public Long productId;
>>
>>  public EditProduct() {
>>  try {
>>  form.add(idField);
>>  productName.setMinLength(MIN_NAME_LENGTH);
>>  productName.addStyleClass("dash_input");
>>  form.add(productName);
>>
>>  deleteOptionLink.addStyleClass("btn_small btn-primary");
>>  deleteOptionLink.setTitle("Delete record");
>>  deleteOptionLink.setAttribute("onclick", "return window.confirm('Are
>> you sure you want to delete this record?', 'Delete Product Option');");
>>  addControl(deleteOptionLink);
>>
>>  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');");
>>  addControl(deleteQuantityLink);
>>
>>  setOptionFormTable();
>>  addControl(optionFormTable);
>>  setQuantityFormTable();
>>  addControl(quantityFormTable);
>>
>>  PageLink addLink = new PageLink("add", " New Option ",
>> EditProduct.class);
>>  addLink.setId("new_option");
>>  addLink.addStyleClass("btn btn-primary");
>>  addControl(addLink);
>>
>>  form.setButtonAlign("right");
>>  Button button = new Submit("save", "Save", this, "onSave");
>>  button.addStyleClass("btn btn-primary");
>>  form.add(button);
>>
>>  PageSubmit pageSubmit = new PageSubmit("cancel", "Cancel",
>> Products.class);
>>  pageSubmit.addStyleClass("btn btn-primary");
>>  form.add(pageSubmit);
>>
>>  // optionFormTable
>>  button = new Submit("save", "Save", this, "onSaveOptionFormTable");
>>  button.addStyleClass("btn btn-primary");
>>  optionFormTable.getForm().add(button);
>>
>>  button = new Submit("cancel", "Cancel", this,
>> "onCancelOptionFormTable");
>>  button.addStyleClass("btn btn-primary");
>>  optionFormTable.getForm().add(button);
>>
>>  // quantityFormTable
>>  button = new Submit("save", "Save", this, "onSaveQuantityFormTable");
>>  button.addStyleClass("btn btn-primary");
>>  quantityFormTable.getForm().add(button);
>>
>>  button = new Submit("cancel", "Cancel", this,
>> "onCancelQuantityFormTable");
>>  button.addStyleClass("btn btn-primary");
>>  quantityFormTable.getForm().add(button);
>>
>>   optionFormTable.getForm().add(idField1);
>>  quantityFormTable.getForm().add(idField2);
>>  addControl(form);
>>
>>  } catch (NumberFormatException e) {
>>  logger.error(e.getMessage(), e);
>>  setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>  return;
>>  }
>>
>>  }
>>
>>  @SuppressWarnings("unchecked")
>>  @Override
>>  public void onGet() {
>>  try {
>>  if (productId != null) {
>>  idField.setValueObject(productId);
>>  idField1.setValueObject(productId);
>>  idField2.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());
>>  }
>>
>>  productOptionDataProvider = new PagingDataProvider() {
>>  @Override
>>  public int size() {
>>  Integer size = 0;
>>  try {
>>  Long id =
>> Long.parseLong(optionFormTable.getControlLink().getParameter("productId"));
>>  size =
>> Integer.parseInt(dashboardSBBeanLocal.getProductOptionCount(id).toString());
>>  } catch (SystemException e) {
>>  logger.error(e.getMessage(), e);
>>  setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>  }
>>  return size;
>>  }
>>
>>   @Override
>>  public List<ProductOptionEnt> getData() {
>>  List<ProductOptionEnt> productOptions = null;
>>  int start = optionFormTable.getFirstRow();
>>  int count = optionFormTable.getPageSize();
>>  String sortColumn = optionFormTable.getSortedColumn();
>>  boolean ascending = optionFormTable.isSortedAscending();
>>  Long id =
>> Long.parseLong(optionFormTable.getControlLink().getParameter("productId"));
>>  try {
>>  productOptions = dashboardSBBeanLocal.getProductOptions(id, start,
>> count, sortColumn, ascending);
>>  } catch (SystemException e) {
>>  logger.error(e.getMessage(), e);
>>  setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>  }
>>  return productOptions;
>>  }
>>  };
>>  optionFormTable.setDataProvider(productOptionDataProvider);
>>
>>  List<ProductOptionEnt> rowList = optionFormTable.getRowList();
>>  for (ProductOptionEnt productOptionEnt : rowList) {
>>  com.farheap.jsi.utils.Constants.Option optionType =
>> productOptionEnt.getOptionType();
>>  List<EnumI> enums = optionType.getEnums();
>>  Select select = new Select();
>>  for (EnumI enumI : enums) {
>>  select.add(enumI.name());
>>  }
>>  FieldColumn column = (FieldColumn) optionFormTable.getColumn("value");
>>  column.setField(select);
>>
>>  }
>>
>>  productQuantityDataProvider = new PagingDataProvider() {
>>  @Override
>>  public int size() {
>>  Integer size = 0;
>>  try {
>>  Long id =
>> Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
>>  size =
>> Integer.parseInt(dashboardSBBeanLocal.getProductQuantityCount(id).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();
>>  boolean ascending = quantityFormTable.isSortedAscending();
>>  Long id =
>> Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
>>  try {
>>  productQuantities = dashboardSBBeanLocal.getProductQuantities(id,
>> 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);
>>  }
>>  } catch (NumberFormatException | SystemException e) {
>>  logger.error(e.getMessage(), e);
>>  setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>  return;
>>  }
>>
>>  }
>>
>>    @Override
>>  public void onInit() {
>>  super.onInit();
>>   String value = getContext().getRequestParameter("productId");
>>  System.out.println(value);
>>  System.out.println(productId);
>>  }
>>
>>  @Override
>>  public final String getTitle() {
>>  if (product == null) {
>>  return PAGE_TITLE_CREATE;
>>  } else {
>>  return PAGE_TITLE_EDIT;
>>  }
>>  }
>>
>>  public final boolean onSave() throws SystemException {
>>  if (form.isValid()) {
>>   Long id = Long.parseLong(idField.getValue());
>>  if (id == null) {
>>  ProductEnt productEnt = new ProductEnt();
>>  PrintProviderEnt printProviderEnt =
>> dashboardSBBeanLocal.getPrintProviderByName(getContext().getRequest().getUserPrincipal().getName());
>>  productEnt.setPrintProviderEnt(printProviderEnt);
>>  form.copyTo(productEnt);
>>  dashboardSBBeanLocal.saveProduct(productEnt);
>>  } else {
>>  ProductEnt productEnt = dashboardSBBeanLocal.getProduct(id);
>>  form.copyTo(productEnt);
>>  dashboardSBBeanLocal.saveProduct(productEnt);
>>  }
>>  form.clearValues();
>>   HashMap<String, String> m = new HashMap<String, String>();
>>   m.put("productId", "" + id);
>>  setRedirect(EditProduct.class, m);
>>  }
>>  return true;
>>  }
>>
>>   public boolean onSaveOptionFormTable() throws SystemException {
>>  if (optionFormTable.getForm().isValid()) {
>>  List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
>>  dashboardSBBeanLocal.saveProductOptions(productOptions);
>>  }
>>  Long id = Long.parseLong(idField1.getValue());
>>  HashMap<String, String> m = new HashMap<String, String>();
>>   m.put("productId", "" + id);
>>  setRedirect(EditProduct.class, m);
>>  return true;
>>  }
>>
>>   public boolean onCancelOptionFormTable() throws SystemException {
>>  optionFormTable.setDataProvider(productOptionDataProvider);
>>  // As form was cancelled, don't render the user submitted values
>>  optionFormTable.setRenderSubmittedValues(false);
>>  return true;
>>  }
>>
>>  public boolean onSaveQuantityFormTable() throws SystemException {
>>  if (quantityFormTable.getForm().isValid()) {
>>  List<ProductQuantityEnt> productQuantities =
>> quantityFormTable.getRowList();
>>  dashboardSBBeanLocal.saveProductQuantities(productQuantities);
>>  }
>>  Long id = Long.parseLong(idField2.getValue());
>>  HashMap<String, String> m = new HashMap<String, String>();
>>   m.put("productId", "" + id);
>>  setRedirect(EditProduct.class, m);
>>  return true;
>>  }
>>
>>   public boolean onCancelQuantityFormTable() throws SystemException {
>>  quantityFormTable.setDataProvider(productQuantityDataProvider);
>>  // As form was cancelled, don't render the user submitted values
>>  quantityFormTable.setRenderSubmittedValues(false);
>>  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;
>>  }
>>
>>  public boolean onDeleteProductOptionClick() throws SystemException {
>>  Long id = deleteOptionLink.getValueLong();
>>  dashboardSBBeanLocal.deleteProductOption(id);
>>   HashMap<String, String> m = new HashMap<String, String>();
>>   m.put("productId", deleteOptionLink.getParameter("productId"));
>>  setRedirect(EditProduct.class, m);
>>  return true;
>>  }
>>
>>  @Override
>>  public String getHelp() {
>>  return getContext().getRequest().getContextPath() +
>> "/provider/edit-product-help.htm";
>>  }
>>
>>  private void setQuantityFormTable() {
>>                   ...
>>
>>  }
>>
>>   private void setOptionFormTable() {
>>  ...
>>  }
>>  public TextField getProductName() {
>>  return productName;
>>  }
>> }
>>
>>
>>
>>
>>
>> On Tue, Aug 6, 2013 at 4:24 PM, Gilberto <gi...@gmail.com> wrote:
>>
>>>
>>> Kristian, I would like to suggest another alternative until you get used
>>> to the click framework. The view->edit[1] and view->insert pattern is a
>>> good point to start and after you have more expirience with the framework
>>> you can do more experiments.
>>>  That was what I did [2] and after that I could understand how the
>>> framework is simple.
>>>
>>>
>>> [1] http://click.avoka.com/click-examples/table/edit-table.htm
>>> [2]
>>> https://code.google.com/p/construtor/source/browse/trunk/park-samples/#
>>>
>>> 2013/8/6 Kristian Lind <kl...@gmail.com>
>>>
>>>>  The problem is I have no where to get the Id from in the onSave
>>>> method.. the idField is empty and is not set the in the OnGet() method
>>>> cause the request parameter is null.
>>>>
>>>>  I wanna do
>>>>
>>>>   HashMap<String, String> m = new HashMap<String, String>();
>>>> m.put("productId", idField.getValue());
>>>>  setRedirect(EditProduct.class, m);
>>>>
>>>>  if I test with a hardcoded id
>>>>  HashMap<String, String> m = new HashMap<String, String>();
>>>>  m.put("productId", "1");
>>>> setRedirect(EditProduct.class, m);
>>>>
>>>>  it works... so I don't think I have to use the alternative redirect
>>>> methods.
>>>>
>>>>
>>>>     --
>>>>
>>>>>    Best regards
>>>>>>
>>>>>> Kristian Lind
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>>  --
>>>> Best regards
>>>>
>>>> Kristian Lind
>>>>
>>>
>>>
>>
>>
>>  --
>> Best regards
>>
>> Kristian Lind
>>
>
>
>
>  --
> Best regards
>
> Kristian Lind
>
>
>


-- 
Best regards

Kristian Lind

Re: Request Parameter

Posted by Bob Schellink <sa...@gmail.com>.
I never used FormTable but from the screenshot it seems you have a Form and a FormTable on the page. 
Are you submitting the Form and expecting the FormTable to be submitted as well? That won't work 
since HTML only sends the form data you are submitting, not all forms on the page.

regards

Bob

On 2013/08/07 18:52, Kristian Lind wrote:
> When I press the save button on options, OnInit() is the first method to be called, but 
> the productId is null.
> *How can I make sure it gets the productId ??? *
>
> The next method that is called is the
> public boolean onSaveOptionFormTable() throws SystemException {
> if (optionFormTable.getForm().isValid()) {
> List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
> dashboardSBBeanLocal.saveProductOptions(productOptions);
> }
> Long id = Long.parseLong(idField1.getValue());
> HashMap<String, String> m = new HashMap<String, String>();
> m.put("productId", "" + id);
> setRedirect(EditProduct.class, m);
> return true;
> }
>
> but here the productOptions list is empty... cause of the null productId from the OnInit() method.
>
> The next method is then again the OnInit() method, and now there is a productId.. cause the
> m.put("productId", "" + id);
> in onSaveOptionFormTable.
>
>
>
>
>
>
>
> On Tue, Aug 6, 2013 at 5:13 PM, Kristian Lind <klindp@gmail.com <ma...@gmail.com>> wrote:
>
>     I have been looking at those examples. I cant find any that have the needs I have.
>     I am displaying one product, so the page need the id of the product. Which it get from the
>     previous page...
>     But I wanna stay on the page after saving... the examples return to another page.
>
>     I am trying to do some like this..
>     http://click.avoka.com/click-examples/source-viewer.htm?filename=WEB-INF/classes/org/apache/click/examples/page/table/FormTablePage.java
>     Here all the customers are fetched...
>
>     *public*  DataProvider createDataProvider() {
>              DataProvider dp =*new*  DataProvider() {
>                  *public*  List<Customer> getData() {
>                      *return*  customerService.getCustomersSortedByName(NUM_ROWS);
>                  }
>              };
>         *return*  dp;
>     }
>
>     Whereas I need to get the options from a specific product.  
>
>
>
>     I now managed to be able to stay on the page, so the productId is used in the save methods..
>
>     Long id = Long.parseLong(idField1.getValue());
>     HashMap<String, String> m = new HashMap<String, String>();
>     m.put("productId", "" + id);
>     setRedirect(EditProduct.class, m);
>
>
>     The problem now is that in the in the method onSaveOptionFormTable()
>     the
>     List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
>     is empty.
>
>     So no options are updated...
>
>     and the onInit() method has no productId.
>
>
>     /**
>      */
>     public class EditProduct extends BorderedPage {
>     private static final Logger logger = LoggerFactory.getLogger(EditProduct.class);
>
>     private static final String PAGE_TITLE_CREATE = "Add Product";
>     private static final String PAGE_TITLE_EDIT = "Edit Product";
>     private static final int MIN_NAME_LENGTH = 3;
>     private static final int VALIDATE_NAME_LENGTH = 50;
>
>     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 ActionLink deleteOptionLink = new ActionLink("deleteOptionLink", "Delete", this,
>     "onDeleteProductOptionClick");
>     private ActionLink deleteQuantityLink = new ActionLink("deleteQuantityLink", "Delete", this,
>     "onDeleteProductQuantityClick");
>     private PagingDataProvider<ProductOptionEnt> productOptionDataProvider = null;
>     private PagingDataProvider<ProductOptionEnt> productQuantityDataProvider = null;
>     private TextField productName = new TextField("name", "Product name ", true);
>     protected HiddenField idField = new HiddenField("id", Long.class);
>     protected HiddenField idField1 = new HiddenField("id", Long.class);
>     protected HiddenField idField2 = new HiddenField("id", Long.class);
>
>     /**
>     * Bindable variable id automatically have the value set by request
>     * parameters
>     */
>     public Long productId;
>
>     public EditProduct() {
>     try {
>     form.add(idField);
>     productName.setMinLength(MIN_NAME_LENGTH);
>     productName.addStyleClass("dash_input");
>     form.add(productName);
>
>     deleteOptionLink.addStyleClass("btn_small btn-primary");
>     deleteOptionLink.setTitle("Delete record");
>     deleteOptionLink.setAttribute("onclick", "return window.confirm('Are you sure you want to
>     delete this record?', 'Delete Product Option');");
>     addControl(deleteOptionLink);
>
>     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');");
>     addControl(deleteQuantityLink);
>
>     setOptionFormTable();
>     addControl(optionFormTable);
>     setQuantityFormTable();
>     addControl(quantityFormTable);
>
>     PageLink addLink = new PageLink("add", " New Option ", EditProduct.class);
>     addLink.setId("new_option");
>     addLink.addStyleClass("btn btn-primary");
>     addControl(addLink);
>
>     form.setButtonAlign("right");
>     Button button = new Submit("save", "Save", this, "onSave");
>     button.addStyleClass("btn btn-primary");
>     form.add(button);
>
>     PageSubmit pageSubmit = new PageSubmit("cancel", "Cancel", Products.class);
>     pageSubmit.addStyleClass("btn btn-primary");
>     form.add(pageSubmit);
>
>     // optionFormTable
>     button = new Submit("save", "Save", this, "onSaveOptionFormTable");
>     button.addStyleClass("btn btn-primary");
>     optionFormTable.getForm().add(button);
>
>     button = new Submit("cancel", "Cancel", this, "onCancelOptionFormTable");
>     button.addStyleClass("btn btn-primary");
>     optionFormTable.getForm().add(button);
>
>     // quantityFormTable
>     button = new Submit("save", "Save", this, "onSaveQuantityFormTable");
>     button.addStyleClass("btn btn-primary");
>     quantityFormTable.getForm().add(button);
>
>     button = new Submit("cancel", "Cancel", this, "onCancelQuantityFormTable");
>     button.addStyleClass("btn btn-primary");
>     quantityFormTable.getForm().add(button);
>
>     optionFormTable.getForm().add(idField1);
>     quantityFormTable.getForm().add(idField2);
>     addControl(form);
>
>     } catch (NumberFormatException e) {
>     logger.error(e.getMessage(), e);
>     setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>     return;
>     }
>
>     }
>
>     @SuppressWarnings("unchecked")
>     @Override
>     public void onGet() {
>     try {
>     if (productId != null) {
>     idField.setValueObject(productId);
>     idField1.setValueObject(productId);
>     idField2.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());
>     }
>
>     productOptionDataProvider = new PagingDataProvider() {
>     @Override
>     public int size() {
>     Integer size = 0;
>     try {
>     Long id = Long.parseLong(optionFormTable.getControlLink().getParameter("productId"));
>     size = Integer.parseInt(dashboardSBBeanLocal.getProductOptionCount(id).toString());
>     } catch (SystemException e) {
>     logger.error(e.getMessage(), e);
>     setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>     }
>     return size;
>     }
>
>     @Override
>     public List<ProductOptionEnt> getData() {
>     List<ProductOptionEnt> productOptions = null;
>     int start = optionFormTable.getFirstRow();
>     int count = optionFormTable.getPageSize();
>     String sortColumn = optionFormTable.getSortedColumn();
>     boolean ascending = optionFormTable.isSortedAscending();
>     Long id = Long.parseLong(optionFormTable.getControlLink().getParameter("productId"));
>     try {
>     productOptions = dashboardSBBeanLocal.getProductOptions(id, start, count, sortColumn, ascending);
>     } catch (SystemException e) {
>     logger.error(e.getMessage(), e);
>     setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>     }
>     return productOptions;
>     }
>     };
>     optionFormTable.setDataProvider(productOptionDataProvider);
>
>     List<ProductOptionEnt> rowList = optionFormTable.getRowList();
>     for (ProductOptionEnt productOptionEnt : rowList) {
>     com.farheap.jsi.utils.Constants.Option optionType = productOptionEnt.getOptionType();
>     List<EnumI> enums = optionType.getEnums();
>     Select select = new Select();
>     for (EnumI enumI : enums) {
>     select.add(enumI.name());
>     }
>     FieldColumn column = (FieldColumn) optionFormTable.getColumn("value");
>     column.setField(select);
>
>     }
>
>     productQuantityDataProvider = new PagingDataProvider() {
>     @Override
>     public int size() {
>     Integer size = 0;
>     try {
>     Long id = Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
>     size = Integer.parseInt(dashboardSBBeanLocal.getProductQuantityCount(id).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();
>     boolean ascending = quantityFormTable.isSortedAscending();
>     Long id = Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
>     try {
>     productQuantities = dashboardSBBeanLocal.getProductQuantities(id, 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);
>     }
>     } catch (NumberFormatException | SystemException e) {
>     logger.error(e.getMessage(), e);
>     setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>     return;
>     }
>
>     }
>
>     @Override
>     public void onInit() {
>     super.onInit();
>     String value = getContext().getRequestParameter("productId");
>     System.out.println(value);
>     System.out.println(productId);
>     }
>
>     @Override
>     public final String getTitle() {
>     if (product == null) {
>     return PAGE_TITLE_CREATE;
>     } else {
>     return PAGE_TITLE_EDIT;
>     }
>     }
>
>     public final boolean onSave() throws SystemException {
>     if (form.isValid()) {
>     Long id = Long.parseLong(idField.getValue());
>     if (id == null) {
>     ProductEnt productEnt = new ProductEnt();
>     PrintProviderEnt printProviderEnt =
>     dashboardSBBeanLocal.getPrintProviderByName(getContext().getRequest().getUserPrincipal().getName());
>     productEnt.setPrintProviderEnt(printProviderEnt);
>     form.copyTo(productEnt);
>     dashboardSBBeanLocal.saveProduct(productEnt);
>     } else {
>     ProductEnt productEnt = dashboardSBBeanLocal.getProduct(id);
>     form.copyTo(productEnt);
>     dashboardSBBeanLocal.saveProduct(productEnt);
>     }
>     form.clearValues();
>     HashMap<String, String> m = new HashMap<String, String>();
>     m.put("productId", "" + id);
>     setRedirect(EditProduct.class, m);
>     }
>     return true;
>     }
>
>     public boolean onSaveOptionFormTable() throws SystemException {
>     if (optionFormTable.getForm().isValid()) {
>     List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
>     dashboardSBBeanLocal.saveProductOptions(productOptions);
>     }
>     Long id = Long.parseLong(idField1.getValue());
>     HashMap<String, String> m = new HashMap<String, String>();
>     m.put("productId", "" + id);
>     setRedirect(EditProduct.class, m);
>     return true;
>     }
>
>     public boolean onCancelOptionFormTable() throws SystemException {
>     optionFormTable.setDataProvider(productOptionDataProvider);
>     // As form was cancelled, don't render the user submitted values
>     optionFormTable.setRenderSubmittedValues(false);
>     return true;
>     }
>
>     public boolean onSaveQuantityFormTable() throws SystemException {
>     if (quantityFormTable.getForm().isValid()) {
>     List<ProductQuantityEnt> productQuantities = quantityFormTable.getRowList();
>     dashboardSBBeanLocal.saveProductQuantities(productQuantities);
>     }
>     Long id = Long.parseLong(idField2.getValue());
>     HashMap<String, String> m = new HashMap<String, String>();
>     m.put("productId", "" + id);
>     setRedirect(EditProduct.class, m);
>     return true;
>     }
>
>     public boolean onCancelQuantityFormTable() throws SystemException {
>     quantityFormTable.setDataProvider(productQuantityDataProvider);
>     // As form was cancelled, don't render the user submitted values
>     quantityFormTable.setRenderSubmittedValues(false);
>     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;
>     }
>
>     public boolean onDeleteProductOptionClick() throws SystemException {
>     Long id = deleteOptionLink.getValueLong();
>     dashboardSBBeanLocal.deleteProductOption(id);
>     HashMap<String, String> m = new HashMap<String, String>();
>     m.put("productId", deleteOptionLink.getParameter("productId"));
>     setRedirect(EditProduct.class, m);
>     return true;
>     }
>
>     @Override
>     public String getHelp() {
>     return getContext().getRequest().getContextPath() + "/provider/edit-product-help.htm";
>     }
>
>     private void setQuantityFormTable() {
>                       ...
>
>     }
>
>     private void setOptionFormTable() {
>     ...
>     }
>     public TextField getProductName() {
>     return productName;
>     }
>     }
>
>
>
>
>
>     On Tue, Aug 6, 2013 at 4:24 PM, Gilberto <gilbertoca@gmail.com <ma...@gmail.com>>
>     wrote:
>
>
>         Kristian, I would like to suggest another alternative until you get used to the click
>         framework. The view->edit[1] and view->insert pattern is a good point to start and after
>         you have more expirience with the framework you can do more experiments.
>         That was what I did [2] and after that I could understand how the framework is simple.
>
>
>         [1] http://click.avoka.com/click-examples/table/edit-table.htm
>         [2] https://code.google.com/p/construtor/source/browse/trunk/park-samples/#
>
>         2013/8/6 Kristian Lind <klindp@gmail.com <ma...@gmail.com>>
>
>             The problem is I have no where to get the Id from in the onSave method.. the idField
>             is empty and is not set the in the OnGet() method cause the request parameter is null.
>
>             I wanna do
>
>             HashMap<String, String> m = new HashMap<String, String>();
>             m.put("productId", idField.getValue());
>             setRedirect(EditProduct.class, m);
>
>             if I test with a hardcoded id
>             HashMap<String, String> m = new HashMap<String, String>();
>             m.put("productId", "1");
>             setRedirect(EditProduct.class, m);
>
>             it works... so I don't think I have to use the alternative redirect methods.
>
>
>             -- 
>
>                     Best regards
>
>                     Kristian Lind
>
>
>
>
>
>             -- 
>             Best regards
>
>             Kristian Lind
>
>
>
>
>
>     -- 
>     Best regards
>
>     Kristian Lind
>
>
>
>
> -- 
> Best regards
>
> Kristian Lind


Re: Request Parameter

Posted by Kristian Lind <kl...@gmail.com>.
When I press the save button on options, OnInit() is the first method to be
called, but the productId is null.
*How can I make sure it gets the productId ??? *

The next method that is called is the
public boolean onSaveOptionFormTable() throws SystemException {
if (optionFormTable.getForm().isValid()) {
List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
dashboardSBBeanLocal.saveProductOptions(productOptions);
}
Long id = Long.parseLong(idField1.getValue());
HashMap<String, String> m = new HashMap<String, String>();
m.put("productId", "" + id);
setRedirect(EditProduct.class, m);
return true;
}

but here the productOptions list is empty... cause of the null productId
from the OnInit() method.

The next method is then again the OnInit() method, and now there is a
productId.. cause the
m.put("productId", "" + id);
in onSaveOptionFormTable.







On Tue, Aug 6, 2013 at 5:13 PM, Kristian Lind <kl...@gmail.com> wrote:

> I have been looking at those examples. I cant find any that have the needs
> I have.
> I am displaying one product, so the page need the id of the product. Which
> it get from the previous page...
> But I wanna stay on the page after saving... the examples return to
> another page.
>
> I am trying to do some like this..
>
> http://click.avoka.com/click-examples/source-viewer.htm?filename=WEB-INF/classes/org/apache/click/examples/page/table/FormTablePage.java
> Here all the customers are fetched...
>
> *public* DataProvider createDataProvider() {
>         DataProvider dp = *new* DataProvider() {
>             *public* List<Customer> getData() {
>                 *return* customerService.getCustomersSortedByName(NUM_ROWS);
>             }
>         };
>    *return* dp;
> }
>
> Whereas I need to get the options from a specific product.
>
>
>
> I now managed to be able to stay on the page, so the productId is used in
> the save methods..
>
> Long id = Long.parseLong(idField1.getValue());
> HashMap<String, String> m = new HashMap<String, String>();
> m.put("productId", "" + id);
> setRedirect(EditProduct.class, m);
>
>
> The problem now is that in the in the method onSaveOptionFormTable()
> the
> List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
> is empty.
>
> So no options are updated...
>
> and the onInit() method has no productId.
>
>
> /**
>  */
> public class EditProduct extends BorderedPage {
> private static final Logger logger =
> LoggerFactory.getLogger(EditProduct.class);
>
> private static final String PAGE_TITLE_CREATE = "Add Product";
> private static final String PAGE_TITLE_EDIT = "Edit Product";
>  private static final int MIN_NAME_LENGTH = 3;
> private static final int VALIDATE_NAME_LENGTH = 50;
>
>  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 ActionLink deleteOptionLink = new ActionLink("deleteOptionLink",
> "Delete", this, "onDeleteProductOptionClick");
>  private ActionLink deleteQuantityLink = new
> ActionLink("deleteQuantityLink", "Delete", this,
> "onDeleteProductQuantityClick");
> private PagingDataProvider<ProductOptionEnt> productOptionDataProvider =
> null;
>  private PagingDataProvider<ProductOptionEnt> productQuantityDataProvider
> = null;
> private TextField productName = new TextField("name", "Product name ",
> true);
>  protected HiddenField idField = new HiddenField("id", Long.class);
> protected HiddenField idField1 = new HiddenField("id", Long.class);
>  protected HiddenField idField2 = new HiddenField("id", Long.class);
>
> /**
>  * Bindable variable id automatically have the value set by request
>  * parameters
>  */
> public Long productId;
>
>  public EditProduct() {
> try {
> form.add(idField);
>  productName.setMinLength(MIN_NAME_LENGTH);
> productName.addStyleClass("dash_input");
> form.add(productName);
>
> deleteOptionLink.addStyleClass("btn_small btn-primary");
> deleteOptionLink.setTitle("Delete record");
>  deleteOptionLink.setAttribute("onclick", "return window.confirm('Are you
> sure you want to delete this record?', 'Delete Product Option');");
>  addControl(deleteOptionLink);
>
> 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');");
>  addControl(deleteQuantityLink);
>
> setOptionFormTable();
> addControl(optionFormTable);
>  setQuantityFormTable();
> addControl(quantityFormTable);
>
> PageLink addLink = new PageLink("add", " New Option ", EditProduct.class);
>  addLink.setId("new_option");
> addLink.addStyleClass("btn btn-primary");
> addControl(addLink);
>
> form.setButtonAlign("right");
> Button button = new Submit("save", "Save", this, "onSave");
>  button.addStyleClass("btn btn-primary");
> form.add(button);
>
> PageSubmit pageSubmit = new PageSubmit("cancel", "Cancel", Products.class);
>  pageSubmit.addStyleClass("btn btn-primary");
> form.add(pageSubmit);
>
> // optionFormTable
>  button = new Submit("save", "Save", this, "onSaveOptionFormTable");
> button.addStyleClass("btn btn-primary");
>  optionFormTable.getForm().add(button);
>
> button = new Submit("cancel", "Cancel", this, "onCancelOptionFormTable");
>  button.addStyleClass("btn btn-primary");
> optionFormTable.getForm().add(button);
>
>  // quantityFormTable
> button = new Submit("save", "Save", this, "onSaveQuantityFormTable");
>  button.addStyleClass("btn btn-primary");
> quantityFormTable.getForm().add(button);
>
>  button = new Submit("cancel", "Cancel", this,
> "onCancelQuantityFormTable");
> button.addStyleClass("btn btn-primary");
>  quantityFormTable.getForm().add(button);
>
> optionFormTable.getForm().add(idField1);
> quantityFormTable.getForm().add(idField2);
>  addControl(form);
>
> } catch (NumberFormatException e) {
> logger.error(e.getMessage(), e);
>  setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
> return;
> }
>
> }
>
> @SuppressWarnings("unchecked")
> @Override
>  public void onGet() {
> try {
>  if (productId != null) {
>  idField.setValueObject(productId);
> idField1.setValueObject(productId);
> idField2.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());
> }
>
>  productOptionDataProvider = new PagingDataProvider() {
> @Override
> public int size() {
>  Integer size = 0;
> try {
> Long id =
> Long.parseLong(optionFormTable.getControlLink().getParameter("productId"));
>  size =
> Integer.parseInt(dashboardSBBeanLocal.getProductOptionCount(id).toString());
> } catch (SystemException e) {
>  logger.error(e.getMessage(), e);
> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>  }
> return size;
> }
>
>  @Override
> public List<ProductOptionEnt> getData() {
> List<ProductOptionEnt> productOptions = null;
>  int start = optionFormTable.getFirstRow();
> int count = optionFormTable.getPageSize();
> String sortColumn = optionFormTable.getSortedColumn();
>  boolean ascending = optionFormTable.isSortedAscending();
> Long id =
> Long.parseLong(optionFormTable.getControlLink().getParameter("productId"));
>  try {
> productOptions = dashboardSBBeanLocal.getProductOptions(id, start, count,
> sortColumn, ascending);
>  } catch (SystemException e) {
> logger.error(e.getMessage(), e);
> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>  }
> return productOptions;
> }
> };
>  optionFormTable.setDataProvider(productOptionDataProvider);
>
> List<ProductOptionEnt> rowList = optionFormTable.getRowList();
>  for (ProductOptionEnt productOptionEnt : rowList) {
> com.farheap.jsi.utils.Constants.Option optionType =
> productOptionEnt.getOptionType();
>  List<EnumI> enums = optionType.getEnums();
> Select select = new Select();
> for (EnumI enumI : enums) {
>  select.add(enumI.name());
> }
> FieldColumn column = (FieldColumn) optionFormTable.getColumn("value");
>  column.setField(select);
>
> }
>
> productQuantityDataProvider = new PagingDataProvider() {
>  @Override
> public int size() {
> Integer size = 0;
>  try {
> Long id =
> Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
>  size =
> Integer.parseInt(dashboardSBBeanLocal.getProductQuantityCount(id).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();
>  boolean ascending = quantityFormTable.isSortedAscending();
> Long id =
> Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
>  try {
> productQuantities = dashboardSBBeanLocal.getProductQuantities(id, 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);
> }
> } catch (NumberFormatException | SystemException e) {
>  logger.error(e.getMessage(), e);
> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>  return;
> }
>
> }
>
> @Override
>  public void onInit() {
> super.onInit();
> String value = getContext().getRequestParameter("productId");
>  System.out.println(value);
> System.out.println(productId);
> }
>
> @Override
> public final String getTitle() {
> if (product == null) {
>  return PAGE_TITLE_CREATE;
> } else {
> return PAGE_TITLE_EDIT;
>  }
> }
>
> public final boolean onSave() throws SystemException {
>  if (form.isValid()) {
> Long id = Long.parseLong(idField.getValue());
> if (id == null) {
>  ProductEnt productEnt = new ProductEnt();
> PrintProviderEnt printProviderEnt =
> dashboardSBBeanLocal.getPrintProviderByName(getContext().getRequest().getUserPrincipal().getName());
>  productEnt.setPrintProviderEnt(printProviderEnt);
> form.copyTo(productEnt);
> dashboardSBBeanLocal.saveProduct(productEnt);
>  } else {
> ProductEnt productEnt = dashboardSBBeanLocal.getProduct(id);
> form.copyTo(productEnt);
>  dashboardSBBeanLocal.saveProduct(productEnt);
> }
> form.clearValues();
>  HashMap<String, String> m = new HashMap<String, String>();
> m.put("productId", "" + id);
>  setRedirect(EditProduct.class, m);
> }
> return true;
>  }
>
> public boolean onSaveOptionFormTable() throws SystemException {
> if (optionFormTable.getForm().isValid()) {
>  List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
> dashboardSBBeanLocal.saveProductOptions(productOptions);
>  }
> Long id = Long.parseLong(idField1.getValue());
> HashMap<String, String> m = new HashMap<String, String>();
>  m.put("productId", "" + id);
> setRedirect(EditProduct.class, m);
> return true;
>  }
>
> public boolean onCancelOptionFormTable() throws SystemException {
> optionFormTable.setDataProvider(productOptionDataProvider);
>  // As form was cancelled, don't render the user submitted values
> optionFormTable.setRenderSubmittedValues(false);
>  return true;
> }
>
> public boolean onSaveQuantityFormTable() throws SystemException {
>  if (quantityFormTable.getForm().isValid()) {
> List<ProductQuantityEnt> productQuantities =
> quantityFormTable.getRowList();
>  dashboardSBBeanLocal.saveProductQuantities(productQuantities);
> }
> Long id = Long.parseLong(idField2.getValue());
>  HashMap<String, String> m = new HashMap<String, String>();
> m.put("productId", "" + id);
>  setRedirect(EditProduct.class, m);
> return true;
> }
>
> public boolean onCancelQuantityFormTable() throws SystemException {
> quantityFormTable.setDataProvider(productQuantityDataProvider);
>  // As form was cancelled, don't render the user submitted values
> quantityFormTable.setRenderSubmittedValues(false);
>  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;
> }
>
> public boolean onDeleteProductOptionClick() throws SystemException {
>  Long id = deleteOptionLink.getValueLong();
> dashboardSBBeanLocal.deleteProductOption(id);
> HashMap<String, String> m = new HashMap<String, String>();
>  m.put("productId", deleteOptionLink.getParameter("productId"));
> setRedirect(EditProduct.class, m);
>  return true;
> }
>
> @Override
> public String getHelp() {
>  return getContext().getRequest().getContextPath() +
> "/provider/edit-product-help.htm";
> }
>
>  private void setQuantityFormTable() {
>                   ...
>
> }
>
>  private void setOptionFormTable() {
> ...
> }
>  public TextField getProductName() {
> return productName;
> }
> }
>
>
>
>
>
> On Tue, Aug 6, 2013 at 4:24 PM, Gilberto <gi...@gmail.com> wrote:
>
>>
>> Kristian, I would like to suggest another alternative until you get used
>> to the click framework. The view->edit[1] and view->insert pattern is a
>> good point to start and after you have more expirience with the framework
>> you can do more experiments.
>> That was what I did [2] and after that I could understand how the
>> framework is simple.
>>
>>
>> [1] http://click.avoka.com/click-examples/table/edit-table.htm
>> [2]
>> https://code.google.com/p/construtor/source/browse/trunk/park-samples/#
>>
>> 2013/8/6 Kristian Lind <kl...@gmail.com>
>>
>>> The problem is I have no where to get the Id from in the onSave method..
>>> the idField is empty and is not set the in the OnGet() method cause the
>>> request parameter is null.
>>>
>>> I wanna do
>>>
>>> HashMap<String, String> m = new HashMap<String, String>();
>>> m.put("productId", idField.getValue());
>>> setRedirect(EditProduct.class, m);
>>>
>>> if I test with a hardcoded id
>>> HashMap<String, String> m = new HashMap<String, String>();
>>> m.put("productId", "1");
>>> setRedirect(EditProduct.class, m);
>>>
>>> it works... so I don't think I have to use the alternative redirect
>>> methods.
>>>
>>>
>>> --
>>>
>>>> Best regards
>>>>>
>>>>> Kristian Lind
>>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> Best regards
>>>
>>> Kristian Lind
>>>
>>
>>
>
>
> --
> Best regards
>
> Kristian Lind
>



-- 
Best regards

Kristian Lind

Re: Request Parameter

Posted by Kristian Lind <kl...@gmail.com>.
I have been looking at those examples. I cant find any that have the needs
I have.
I am displaying one product, so the page need the id of the product. Which
it get from the previous page...
But I wanna stay on the page after saving... the examples return to another
page.

I am trying to do some like this..
http://click.avoka.com/click-examples/source-viewer.htm?filename=WEB-INF/classes/org/apache/click/examples/page/table/FormTablePage.java
Here all the customers are fetched...

*public* DataProvider createDataProvider() {
        DataProvider dp = *new* DataProvider() {
            *public* List<Customer> getData() {
                *return* customerService.getCustomersSortedByName(NUM_ROWS);
            }
        };
   *return* dp;
}

Whereas I need to get the options from a specific product.



I now managed to be able to stay on the page, so the productId is used in
the save methods..

Long id = Long.parseLong(idField1.getValue());
HashMap<String, String> m = new HashMap<String, String>();
m.put("productId", "" + id);
setRedirect(EditProduct.class, m);


The problem now is that in the in the method onSaveOptionFormTable()
the
List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
is empty.

So no options are updated...

and the onInit() method has no productId.


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

private static final String PAGE_TITLE_CREATE = "Add Product";
private static final String PAGE_TITLE_EDIT = "Edit Product";
private static final int MIN_NAME_LENGTH = 3;
private static final int VALIDATE_NAME_LENGTH = 50;

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 ActionLink deleteOptionLink = new ActionLink("deleteOptionLink",
"Delete", this, "onDeleteProductOptionClick");
private ActionLink deleteQuantityLink = new
ActionLink("deleteQuantityLink", "Delete", this,
"onDeleteProductQuantityClick");
private PagingDataProvider<ProductOptionEnt> productOptionDataProvider =
null;
private PagingDataProvider<ProductOptionEnt> productQuantityDataProvider =
null;
private TextField productName = new TextField("name", "Product name ",
true);
protected HiddenField idField = new HiddenField("id", Long.class);
protected HiddenField idField1 = new HiddenField("id", Long.class);
protected HiddenField idField2 = new HiddenField("id", Long.class);

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

public EditProduct() {
try {
form.add(idField);
productName.setMinLength(MIN_NAME_LENGTH);
productName.addStyleClass("dash_input");
form.add(productName);

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

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');");
addControl(deleteQuantityLink);

setOptionFormTable();
addControl(optionFormTable);
setQuantityFormTable();
addControl(quantityFormTable);

PageLink addLink = new PageLink("add", " New Option ", EditProduct.class);
addLink.setId("new_option");
addLink.addStyleClass("btn btn-primary");
addControl(addLink);

form.setButtonAlign("right");
Button button = new Submit("save", "Save", this, "onSave");
button.addStyleClass("btn btn-primary");
form.add(button);

PageSubmit pageSubmit = new PageSubmit("cancel", "Cancel", Products.class);
pageSubmit.addStyleClass("btn btn-primary");
form.add(pageSubmit);

// optionFormTable
button = new Submit("save", "Save", this, "onSaveOptionFormTable");
button.addStyleClass("btn btn-primary");
optionFormTable.getForm().add(button);

button = new Submit("cancel", "Cancel", this, "onCancelOptionFormTable");
button.addStyleClass("btn btn-primary");
optionFormTable.getForm().add(button);

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

button = new Submit("cancel", "Cancel", this, "onCancelQuantityFormTable");
button.addStyleClass("btn btn-primary");
quantityFormTable.getForm().add(button);

optionFormTable.getForm().add(idField1);
quantityFormTable.getForm().add(idField2);
addControl(form);

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

}

@SuppressWarnings("unchecked")
@Override
public void onGet() {
try {
 if (productId != null) {
idField.setValueObject(productId);
idField1.setValueObject(productId);
idField2.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());
}

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

@Override
public List<ProductOptionEnt> getData() {
List<ProductOptionEnt> productOptions = null;
int start = optionFormTable.getFirstRow();
int count = optionFormTable.getPageSize();
String sortColumn = optionFormTable.getSortedColumn();
boolean ascending = optionFormTable.isSortedAscending();
Long id =
Long.parseLong(optionFormTable.getControlLink().getParameter("productId"));
try {
productOptions = dashboardSBBeanLocal.getProductOptions(id, start, count,
sortColumn, ascending);
} catch (SystemException e) {
logger.error(e.getMessage(), e);
setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
}
return productOptions;
}
};
optionFormTable.setDataProvider(productOptionDataProvider);

List<ProductOptionEnt> rowList = optionFormTable.getRowList();
for (ProductOptionEnt productOptionEnt : rowList) {
com.farheap.jsi.utils.Constants.Option optionType =
productOptionEnt.getOptionType();
List<EnumI> enums = optionType.getEnums();
Select select = new Select();
for (EnumI enumI : enums) {
select.add(enumI.name());
}
FieldColumn column = (FieldColumn) optionFormTable.getColumn("value");
column.setField(select);

}

productQuantityDataProvider = new PagingDataProvider() {
@Override
public int size() {
Integer size = 0;
try {
Long id =
Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
size =
Integer.parseInt(dashboardSBBeanLocal.getProductQuantityCount(id).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();
boolean ascending = quantityFormTable.isSortedAscending();
Long id =
Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
try {
productQuantities = dashboardSBBeanLocal.getProductQuantities(id, 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);
}
} catch (NumberFormatException | SystemException e) {
logger.error(e.getMessage(), e);
setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
return;
}

}

@Override
public void onInit() {
super.onInit();
String value = getContext().getRequestParameter("productId");
System.out.println(value);
System.out.println(productId);
}

@Override
public final String getTitle() {
if (product == null) {
return PAGE_TITLE_CREATE;
} else {
return PAGE_TITLE_EDIT;
}
}

public final boolean onSave() throws SystemException {
if (form.isValid()) {
Long id = Long.parseLong(idField.getValue());
if (id == null) {
ProductEnt productEnt = new ProductEnt();
PrintProviderEnt printProviderEnt =
dashboardSBBeanLocal.getPrintProviderByName(getContext().getRequest().getUserPrincipal().getName());
productEnt.setPrintProviderEnt(printProviderEnt);
form.copyTo(productEnt);
dashboardSBBeanLocal.saveProduct(productEnt);
} else {
ProductEnt productEnt = dashboardSBBeanLocal.getProduct(id);
form.copyTo(productEnt);
dashboardSBBeanLocal.saveProduct(productEnt);
}
form.clearValues();
HashMap<String, String> m = new HashMap<String, String>();
m.put("productId", "" + id);
setRedirect(EditProduct.class, m);
}
return true;
}

public boolean onSaveOptionFormTable() throws SystemException {
if (optionFormTable.getForm().isValid()) {
List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
dashboardSBBeanLocal.saveProductOptions(productOptions);
}
Long id = Long.parseLong(idField1.getValue());
HashMap<String, String> m = new HashMap<String, String>();
m.put("productId", "" + id);
setRedirect(EditProduct.class, m);
return true;
}

public boolean onCancelOptionFormTable() throws SystemException {
optionFormTable.setDataProvider(productOptionDataProvider);
// As form was cancelled, don't render the user submitted values
optionFormTable.setRenderSubmittedValues(false);
return true;
}

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

public boolean onCancelQuantityFormTable() throws SystemException {
quantityFormTable.setDataProvider(productQuantityDataProvider);
// As form was cancelled, don't render the user submitted values
quantityFormTable.setRenderSubmittedValues(false);
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;
}

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

@Override
public String getHelp() {
return getContext().getRequest().getContextPath() +
"/provider/edit-product-help.htm";
}

private void setQuantityFormTable() {
                  ...

}

private void setOptionFormTable() {
...
}
public TextField getProductName() {
return productName;
}
}





On Tue, Aug 6, 2013 at 4:24 PM, Gilberto <gi...@gmail.com> wrote:

>
> Kristian, I would like to suggest another alternative until you get used
> to the click framework. The view->edit[1] and view->insert pattern is a
> good point to start and after you have more expirience with the framework
> you can do more experiments.
> That was what I did [2] and after that I could understand how the
> framework is simple.
>
>
> [1] http://click.avoka.com/click-examples/table/edit-table.htm
> [2]
> https://code.google.com/p/construtor/source/browse/trunk/park-samples/#
>
> 2013/8/6 Kristian Lind <kl...@gmail.com>
>
>> The problem is I have no where to get the Id from in the onSave method..
>> the idField is empty and is not set the in the OnGet() method cause the
>> request parameter is null.
>>
>> I wanna do
>>
>> HashMap<String, String> m = new HashMap<String, String>();
>> m.put("productId", idField.getValue());
>> setRedirect(EditProduct.class, m);
>>
>> if I test with a hardcoded id
>> HashMap<String, String> m = new HashMap<String, String>();
>> m.put("productId", "1");
>> setRedirect(EditProduct.class, m);
>>
>> it works... so I don't think I have to use the alternative redirect
>> methods.
>>
>>
>> --
>>
>>> Best regards
>>>>
>>>> Kristian Lind
>>>>
>>>
>>>
>>
>>
>> --
>> Best regards
>>
>> Kristian Lind
>>
>
>


-- 
Best regards

Kristian Lind

Re: Request Parameter

Posted by Gilberto <gi...@gmail.com>.
Kristian, I would like to suggest another alternative until you get used to
the click framework. The view->edit[1] and view->insert pattern is a good
point to start and after you have more expirience with the framework you
can do more experiments.
That was what I did [2] and after that I could understand how the framework
is simple.


[1] http://click.avoka.com/click-examples/table/edit-table.htm
[2] https://code.google.com/p/construtor/source/browse/trunk/park-samples/#

2013/8/6 Kristian Lind <kl...@gmail.com>

> The problem is I have no where to get the Id from in the onSave method..
> the idField is empty and is not set the in the OnGet() method cause the
> request parameter is null.
>
> I wanna do
>
> HashMap<String, String> m = new HashMap<String, String>();
> m.put("productId", idField.getValue());
> setRedirect(EditProduct.class, m);
>
> if I test with a hardcoded id
> HashMap<String, String> m = new HashMap<String, String>();
> m.put("productId", "1");
> setRedirect(EditProduct.class, m);
>
> it works... so I don't think I have to use the alternative redirect
> methods.
>
>
> --
>
>> Best regards
>>>
>>> Kristian Lind
>>>
>>
>>
>
>
> --
> Best regards
>
> Kristian Lind
>

Re: Request Parameter

Posted by Kristian Lind <kl...@gmail.com>.
The problem is I have no where to get the Id from in the onSave method..
the idField is empty and is not set the in the OnGet() method cause the
request parameter is null.

I wanna do

HashMap<String, String> m = new HashMap<String, String>();
m.put("productId", idField.getValue());
setRedirect(EditProduct.class, m);

if I test with a hardcoded id
HashMap<String, String> m = new HashMap<String, String>();
m.put("productId", "1");
setRedirect(EditProduct.class, m);

it works... so I don't think I have to use the alternative redirect
methods.




On Tue, Aug 6, 2013 at 2:28 PM, Gilberto <gi...@gmail.com> wrote:

> You need use the alternative redirect utility method[1].
>
> Regards
>
> [1]
> http://click.apache.org/docs/click-api/org/apache/click/Page.html#setRedirect%28java.lang.String,%20java.util.Map%29
>
>
>
> 2013/8/6 Kristian Lind <kl...@gmail.com>
>
>> Hi, I have been looking at the links, and I did some changes to my code.
>> But I can not get it to work.
>>
>> When I press the save button the OnGet() method is called with no ID, and
>> after that the onSave() method is called, but now the idField is empty...
>>
>> I want to stay on the same page after save is pressed... also when the
>> save buttons for the form tables are pressed.
>>
>> [image: Inline image 1]
>>
>>
>>
>> /**
>>  */
>> public class EditProduct extends BorderedPage {
>> private static final Logger logger =
>> LoggerFactory.getLogger(EditProduct.class);
>>
>> private static final String PAGE_TITLE_CREATE = "Add Product";
>> private static final String PAGE_TITLE_EDIT = "Edit Product";
>>  private static final int MIN_NAME_LENGTH = 3;
>> private static final int VALIDATE_NAME_LENGTH = 50;
>>
>>  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 ActionLink deleteOptionLink = new ActionLink("deleteOptionLink",
>> "Delete", this, "onDeleteProductOptionClick");
>>  private ActionLink deleteQuantityLink = new
>> ActionLink("deleteQuantityLink", "Delete", this,
>> "onDeleteProductQuantityClick");
>> private PagingDataProvider<ProductOptionEnt> productOptionDataProvider =
>> null;
>>  private PagingDataProvider<ProductOptionEnt>
>> productQuantityDataProvider = null;
>> private TextField productName = new TextField("name", "Product name ",
>> true);
>>  protected HiddenField idField = new HiddenField("id", Long.class);
>>
>> public Long productId;
>>
>> public EditProduct() {
>> try {
>> form.add(idField);
>>  productName.setMinLength(MIN_NAME_LENGTH);
>> productName.addStyleClass("dash_input");
>> form.add(productName);
>>
>> deleteOptionLink.addStyleClass("btn_small btn-primary");
>> deleteOptionLink.setTitle("Delete record");
>>  deleteOptionLink.setAttribute("onclick", "return window.confirm('Are
>> you sure you want to delete this record?', 'Delete Product Option');");
>>  addControl(deleteOptionLink);
>>
>> 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');");
>>  addControl(deleteQuantityLink);
>>
>> setOptionFormTable();
>> addControl(optionFormTable);
>>  setQuantityFormTable();
>> addControl(quantityFormTable);
>>
>> PageLink addLink = new PageLink("add", " New Option ", EditProduct.class);
>>  addLink.setId("new_option");
>> addLink.addStyleClass("btn btn-primary");
>> addControl(addLink);
>>
>> form.setButtonAlign("right");
>> Button button = new Submit("save", "Save", this, "onSave");
>>  button.addStyleClass("btn btn-primary");
>> form.add(button);
>>
>> PageSubmit pageSubmit = new PageSubmit("cancel", "Cancel",
>> Products.class);
>>  pageSubmit.addStyleClass("btn btn-primary");
>> form.add(pageSubmit);
>>
>> // optionFormTable
>>  button = new Submit("save", "Save", this, "onSaveOptionFormTable");
>> button.addStyleClass("btn btn-primary");
>>  optionFormTable.getForm().add(button);
>>
>> button = new Submit("cancel", "Cancel", this, "onCancelOptionFormTable");
>>  button.addStyleClass("btn btn-primary");
>> optionFormTable.getForm().add(button);
>>
>>  // quantityFormTable
>> button = new Submit("save", "Save", this, "onSaveQuantityFormTable");
>>  button.addStyleClass("btn btn-primary");
>> quantityFormTable.getForm().add(button);
>>
>>  button = new Submit("cancel", "Cancel", this,
>> "onCancelQuantityFormTable");
>> button.addStyleClass("btn btn-primary");
>>  quantityFormTable.getForm().add(button);
>>
>> addControl(form);
>>
>> } catch (NumberFormatException e) {
>>  logger.error(e.getMessage(), e);
>> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>  return;
>> }
>>
>> }
>>
>> @SuppressWarnings("unchecked")
>>  @Override
>> public void onGet() {
>> try {
>> if (productId != null) {
>>  idField.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());
>> }
>>
>>  productOptionDataProvider = new PagingDataProvider() {
>> @Override
>> public int size() {
>>  Integer size = 0;
>> try {
>> Long id =
>> Long.parseLong(optionFormTable.getControlLink().getParameter("productId"));
>>  size =
>> Integer.parseInt(dashboardSBBeanLocal.getProductOptionCount(id).toString());
>> } catch (SystemException e) {
>>  logger.error(e.getMessage(), e);
>> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>  }
>> return size;
>> }
>>
>>  @Override
>> public List<ProductOptionEnt> getData() {
>> List<ProductOptionEnt> productOptions = null;
>>  int start = optionFormTable.getFirstRow();
>> int count = optionFormTable.getPageSize();
>> String sortColumn = optionFormTable.getSortedColumn();
>>  boolean ascending = optionFormTable.isSortedAscending();
>> Long id =
>> Long.parseLong(optionFormTable.getControlLink().getParameter("productId"));
>>  try {
>> productOptions = dashboardSBBeanLocal.getProductOptions(id, start, count,
>> sortColumn, ascending);
>>  } catch (SystemException e) {
>> logger.error(e.getMessage(), e);
>> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>  }
>> return productOptions;
>> }
>> };
>>  optionFormTable.setDataProvider(productOptionDataProvider);
>>
>> List<ProductOptionEnt> rowList = optionFormTable.getRowList();
>>  for (ProductOptionEnt productOptionEnt : rowList) {
>> com.farheap.jsi.utils.Constants.Option optionType =
>> productOptionEnt.getOptionType();
>>  List<EnumI> enums = optionType.getEnums();
>> Select select = new Select();
>> for (EnumI enumI : enums) {
>>  select.add(enumI.name());
>> }
>> FieldColumn column = (FieldColumn) optionFormTable.getColumn("value");
>>  column.setField(select);
>>
>> }
>>
>> productQuantityDataProvider = new PagingDataProvider() {
>>  @Override
>> public int size() {
>> Integer size = 0;
>>  try {
>> Long id =
>> Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
>>  size =
>> Integer.parseInt(dashboardSBBeanLocal.getProductQuantityCount(id).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();
>>  boolean ascending = quantityFormTable.isSortedAscending();
>> Long id =
>> Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
>>  try {
>> productQuantities = dashboardSBBeanLocal.getProductQuantities(id, 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);
>> }
>> } catch (NumberFormatException | SystemException e) {
>>  logger.error(e.getMessage(), e);
>> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>  return;
>> }
>>
>> }
>>
>>  public final boolean onSave() throws SystemException {
>>  if (form.isValid()) {
>> Long id = Long.parseLong(idField.getValue());
>> if (id == null) {
>>  ProductEnt productEnt = new ProductEnt();
>> PrintProviderEnt printProviderEnt =
>> dashboardSBBeanLocal.getPrintProviderByName(getContext().getRequest().getUserPrincipal().getName());
>>  productEnt.setPrintProviderEnt(printProviderEnt);
>> form.copyTo(productEnt);
>> dashboardSBBeanLocal.saveProduct(productEnt);
>>  } else {
>> ProductEnt productEnt = dashboardSBBeanLocal.getProduct(id);
>> form.copyTo(productEnt);
>>  dashboardSBBeanLocal.saveProduct(productEnt);
>> }
>> form.clearValues();
>>  HashMap<String, String> m = new HashMap<String, String>();
>> m.put("productId", idField.getValue());
>>  setRedirect(EditProduct.class);
>> }
>> return true;
>>  }
>>
>> public boolean onSaveOptionFormTable() throws SystemException {
>> if (optionFormTable.getForm().isValid()) {
>>  List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
>> dashboardSBBeanLocal.saveProductOptions(productOptions);
>>  }
>> System.out.println(productId);
>> // HashMap<String, String> m = new HashMap<String, String>();
>> // m.put("productId", "1");
>> setRedirect(EditProduct.class);
>> return true;
>>  }
>>
>> public boolean onCancelOptionFormTable() throws SystemException {
>> optionFormTable.setDataProvider(productOptionDataProvider);
>>  // As form was cancelled, don't render the user submitted values
>> optionFormTable.setRenderSubmittedValues(false);
>>  return true;
>> }
>>
>> public boolean onSaveQuantityFormTable() throws SystemException {
>>  if (quantityFormTable.getForm().isValid()) {
>> List<ProductQuantityEnt> productQuantities =
>> quantityFormTable.getRowList();
>>  dashboardSBBeanLocal.saveProductQuantities(productQuantities);
>> }
>> System.out.println(productId);
>> // HashMap<String, String> m = new HashMap<String, String>();
>> // m.put("productId", "1");
>>  setRedirect(EditProduct.class);
>> return true;
>> }
>>
>> public boolean onCancelQuantityFormTable() throws SystemException {
>> quantityFormTable.setDataProvider(productQuantityDataProvider);
>>  // As form was cancelled, don't render the user submitted values
>> quantityFormTable.setRenderSubmittedValues(false);
>>  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;
>> }
>>
>> public boolean onDeleteProductOptionClick() throws SystemException {
>>  Long id = deleteOptionLink.getValueLong();
>> dashboardSBBeanLocal.deleteProductOption(id);
>> HashMap<String, String> m = new HashMap<String, String>();
>>  m.put("productId", deleteOptionLink.getParameter("productId"));
>> setRedirect(EditProduct.class, m);
>>  return true;
>> }
>>
>> @Override
>>  public String getHelp() {
>>  return getContext().getRequest().getContextPath() +
>> "/provider/edit-product-help.htm";
>> }
>>
>>  private void setQuantityFormTable() {
>>                 .....
>> }
>>
>> private void setOptionFormTable() {
>>                ......
>>
>> }
>>
>> }
>>
>>
>>
>>
>>
>>
>> On Tue, Aug 6, 2013 at 11:51 AM, Gilberto <gi...@gmail.com> wrote:
>>
>>> I think you misunderstood the page flow[1] and onInit method usage[2].
>>> Take a look at this example [3], there you will see some work division.
>>> It shows some click + cayenne/jpa integration code, just my learning
>>> repository.
>>>
>>> Regards,
>>>
>>> Gilberto
>>>
>>> [1]
>>> http://click.apache.org/docs/user-guide/htmlsingle/click-book.html#execution
>>> [2]
>>> http://click.apache.org/docs/click-api/org/apache/click/Page.html#onInit%28%29
>>> [3]
>>> https://code.google.com/p/construtor/source/browse/trunk/park-samples/park-cayenne/src/main/java/park/web/page/EditVehicle.java
>>>
>>>
>>> 2013/8/6 Kristian Lind <kl...@gmail.com>
>>>
>>>> Hi, I have this page, it contains a product that has a list of options.
>>>>
>>>> The product has a name that can be changed, and you can change the
>>>> options.
>>>>
>>>> My Problem is when submitting the table form save button, I do not have
>>>> the product id anymore.
>>>> So in the OnInit method I can not get the data.
>>>>
>>>>
>>>> [image: Inline image 1]
>>>>
>>>>
>>>> /**
>>>>  */
>>>> public class EditProduct extends BorderedPage {
>>>> private static final Logger logger =
>>>> LoggerFactory.getLogger(EditProduct.class);
>>>>
>>>> private static final String PAGE_TITLE_CREATE = "Add Product";
>>>> private static final String PAGE_TITLE_EDIT = "Edit Product";
>>>>  private static final int MIN_NAME_LENGTH = 3;
>>>> private static final int VALIDATE_NAME_LENGTH = 50;
>>>>
>>>>  private DashboardSBBeanLocal dashboardSBBeanLocal =
>>>> SessionBeanManager.getDashboardSBBeanLocal();
>>>> private EditProductForm form = null;
>>>>  private ProductEnt product;
>>>> private FormTable table = new FormTable("table");
>>>> private ActionLink deleteLink = new ActionLink("delete", this,
>>>> "onDeleteClick");
>>>>  private PagingDataProvider<ProductOptionEnt> dataProvider = null;
>>>>  @Bindable
>>>>  private Long productId;
>>>>  public EditProduct() {
>>>>  form = new EditProductForm("form");
>>>> form.setButtonAlign("right");
>>>> Button button = new Submit("save", "Save", this, "onSave");
>>>>  button.addStyleClass("btn btn-primary");
>>>> form.add(button);
>>>>
>>>> PageSubmit pageSubmit = new PageSubmit("cancel", "Cancel",
>>>> Products.class);
>>>>  pageSubmit.addStyleClass("btn btn-primary");
>>>> form.add(pageSubmit);
>>>>
>>>> // TableForm
>>>>  button = new Submit("save", "Save", this, "onSaveTableForm");
>>>> button.addStyleClass("btn btn-primary");
>>>>  table.getForm().add(button);
>>>>
>>>> button = new Submit("cancel", "Cancel", this, "onCancelTableForm");
>>>>  button.addStyleClass("btn btn-primary");
>>>> table.getForm().add(button);
>>>>  addControl(form);
>>>> }
>>>>
>>>> @Override
>>>>  public final String getTitle() {
>>>> if (product == null) {
>>>> return PAGE_TITLE_CREATE;
>>>>  } else {
>>>> return PAGE_TITLE_EDIT;
>>>> }
>>>> }
>>>>
>>>> public final boolean onSave() throws SystemException {
>>>> if (form.isValid()) {
>>>> Long id = form.getProductId();
>>>>  if (id == null) {
>>>> ProductEnt productEnt = new ProductEnt();
>>>> PrintProviderEnt printProviderEnt =
>>>> dashboardSBBeanLocal.getPrintProviderByName(getContext().getRequest().getUserPrincipal().getName());
>>>>  productEnt.setPrintProviderEnt(printProviderEnt);
>>>> form.copyTo(productEnt);
>>>> dashboardSBBeanLocal.saveProduct(productEnt);
>>>>  } else {
>>>> ProductEnt productEnt = dashboardSBBeanLocal.getProduct(id);
>>>> form.copyTo(productEnt);
>>>>  dashboardSBBeanLocal.saveProduct(productEnt);
>>>> }
>>>> form.clearValues();
>>>>  HashMap<String, String> m = new HashMap<String, String>();
>>>> m.put("productId", "" + id);
>>>>  setRedirect(EditProduct.class, m);
>>>> }
>>>> return true;
>>>>  }
>>>>
>>>> private class EditProductForm extends Form {
>>>> private TextField productName = new TextField("name", "Product name ",
>>>> true);
>>>>  private Field productIdField = new HiddenField("id", Long.class);
>>>>
>>>> public EditProductForm(final String name) {
>>>>  super(name);
>>>> add(productIdField);
>>>> productName.setMinLength(MIN_NAME_LENGTH);
>>>>  productName.addStyleClass("dash_input");
>>>> add(productName);
>>>> deleteLink.addStyleClass("btn_small btn-primary");
>>>>  deleteLink.setTitle("Delete record");
>>>> deleteLink.setAttribute("onclick", "return window.confirm('Are you sure
>>>> you want to delete this record?', 'Delete Product Option');");
>>>>  addControl(deleteLink);
>>>>
>>>> AbstractLink[] links = new AbstractLink[] { deleteLink };
>>>> table.getForm().setButtonAlign(Form.ALIGN_RIGHT);
>>>>  table.setClass(Table.CLASS_ITS);
>>>> table.setPageSize(Constants.MAX_PAGE_SIZE);
>>>> table.setSortable(true);
>>>>  table.addStyleClass("dash_table");
>>>>
>>>> table.addColumn(new Column("id", "id"));
>>>>  table.addColumn(new Column("optionType", "Options"));
>>>>
>>>> Select select = new Select();
>>>>  Column column = new FieldColumn("value", select);
>>>> table.addColumn(column);
>>>>
>>>>  Column c = new Column("action", "");
>>>> c.setTextAlign("center");
>>>> c.setWidth("120");
>>>>  c.setSortable(false);
>>>> c.setDecorator(new LinkDecorator(table, links, "id", "id"));
>>>>  table.addColumn(c);
>>>>
>>>> table.getControlLink().setActionListener(new ActionListener() {
>>>>  public boolean onAction(final Control source) {
>>>> table.saveState(getContext());
>>>> return true;
>>>>  }
>>>> });
>>>>
>>>> table.restoreState(getContext());
>>>>
>>>> addControl(table);
>>>>
>>>> PageLink addLink = new PageLink("add", " New Option ",
>>>> EditProduct.class);
>>>>  addLink.setId("new_option");
>>>> addLink.addStyleClass("btn btn-primary");
>>>> addControl(addLink);
>>>>
>>>> }
>>>>
>>>> public Long getProductId() {
>>>> return (Long) productIdField.getValueObject();
>>>>  }
>>>>
>>>> public TextField getProductName() {
>>>> return productName;
>>>>  }
>>>>
>>>> @Override
>>>> public void onInit() {
>>>>  super.onInit();
>>>> try {
>>>> Long productId = null;
>>>>  String value = getContext().getRequestParameter("productId");
>>>> getContext().setRequestAttribute("productId", value);
>>>>  if (!StringUtils.isBlank(value)) {
>>>> productId = Long.parseLong(value);
>>>> } else {
>>>>  value = table.getControlLink().getParameter("productId");
>>>> if (!StringUtils.isBlank(value)) {
>>>>  productId = Long.parseLong(value);
>>>> }
>>>> }
>>>>  if (productId != null) {
>>>> deleteLink.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());
>>>> table.getControlLink().setParameter("productId", product.getId());
>>>>  }
>>>> dataProvider = new PagingDataProvider() {
>>>> @Override
>>>>  public int size() {
>>>> Integer size = 0;
>>>> try {
>>>>  Long id =
>>>> Long.parseLong(table.getControlLink().getParameter("productId"));
>>>> size =
>>>> Integer.parseInt(dashboardSBBeanLocal.getProductOptionCount(id).toString());
>>>>  } catch (SystemException e) {
>>>> logger.error(e.getMessage(), e);
>>>> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>>>  }
>>>> return size;
>>>> }
>>>>
>>>>  @Override
>>>> public List<ProductOptionEnt> getData() {
>>>> List<ProductOptionEnt> productOptions = null;
>>>>  int start = table.getFirstRow();
>>>> int count = table.getPageSize();
>>>> String sortColumn = table.getSortedColumn();
>>>>  boolean ascending = table.isSortedAscending();
>>>> Long id =
>>>> Long.parseLong(table.getControlLink().getParameter("productId"));
>>>>  try {
>>>> productOptions = dashboardSBBeanLocal.getProductOptions(id, start,
>>>> count, sortColumn, ascending);
>>>>  } catch (SystemException e) {
>>>> logger.error(e.getMessage(), e);
>>>> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>>>  }
>>>> return productOptions;
>>>> }
>>>> };
>>>>  table.setDataProvider(dataProvider);
>>>>
>>>> List<ProductOptionEnt> rowList = table.getRowList();
>>>>  for (ProductOptionEnt productOptionEnt : rowList) {
>>>> com.farheap.jsi.utils.Constants.Option optionType =
>>>> productOptionEnt.getOptionType();
>>>>  List<EnumI> enums = optionType.getEnums();
>>>> Select select = new Select();
>>>> for (EnumI enumI : enums) {
>>>>  select.add(enumI.name());
>>>> }
>>>> FieldColumn column = (FieldColumn) table.getColumn("value");
>>>>  column.setField(select);
>>>>
>>>> }
>>>> }
>>>>
>>>> } catch (SystemException | NumberFormatException e) {
>>>> logger.error(e.getMessage(), e);
>>>>  setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>>> return;
>>>> }
>>>>
>>>> }
>>>>
>>>> private void validateSize(final Field field, final int size) {
>>>> if (field.getValue().length() > size) {
>>>>  field.setError("Maximum " + field.getName() + " size exceeded (" +
>>>> size + ").");
>>>> }
>>>>  }
>>>>
>>>> @Override
>>>> public void validate() {
>>>>  super.validate();
>>>> validateSize(productName, VALIDATE_NAME_LENGTH);
>>>> }
>>>>
>>>> }
>>>>
>>>> public boolean onSaveTableForm() throws SystemException {
>>>> if (table.getForm().isValid()) {
>>>>  List<ProductOptionEnt> productOptions = table.getRowList();
>>>> dashboardSBBeanLocal.saveProductOptions(productOptions);
>>>>  }
>>>> HashMap<String, String> m = new HashMap<String, String>();
>>>> *m.put("productId", // NEED THE PRODUCT ID HERE...  );*
>>>>  setRedirect(EditProduct.class, m);
>>>> return true;
>>>> }
>>>>
>>>> public boolean onCancelTableForm() throws SystemException {
>>>> table.setDataProvider(dataProvider);
>>>>  // As form was cancelled, don't render the user submitted values
>>>> table.setRenderSubmittedValues(false);
>>>>  return true;
>>>> }
>>>>
>>>> @Override
>>>> public String getHelp() {
>>>>  return getContext().getRequest().getContextPath() +
>>>> "/provider/edit-product-help.htm";
>>>> }
>>>>
>>>>  public boolean onDeleteClick() throws SystemException {
>>>> Long id = deleteLink.getValueLong();
>>>> dashboardSBBeanLocal.deleteProductOption(id);
>>>>  HashMap<String, String> m = new HashMap<String, String>();
>>>> m.put("productId", deleteLink.getParameter("productId"));
>>>>  setRedirect(EditProduct.class, m);
>>>> return true;
>>>> }
>>>>
>>>> }
>>>>
>>>
>>>
>>
>>
>> --
>> Best regards
>>
>> Kristian Lind
>>
>
>


-- 
Best regards

Kristian Lind

Re: Request Parameter

Posted by Gilberto <gi...@gmail.com>.
You need use the alternative redirect utility method[1].

Regards

[1]
http://click.apache.org/docs/click-api/org/apache/click/Page.html#setRedirect%28java.lang.String,%20java.util.Map%29



2013/8/6 Kristian Lind <kl...@gmail.com>

> Hi, I have been looking at the links, and I did some changes to my code.
> But I can not get it to work.
>
> When I press the save button the OnGet() method is called with no ID, and
> after that the onSave() method is called, but now the idField is empty...
>
> I want to stay on the same page after save is pressed... also when the
> save buttons for the form tables are pressed.
>
> [image: Inline image 1]
>
>
>
> /**
>  */
> public class EditProduct extends BorderedPage {
> private static final Logger logger =
> LoggerFactory.getLogger(EditProduct.class);
>
> private static final String PAGE_TITLE_CREATE = "Add Product";
> private static final String PAGE_TITLE_EDIT = "Edit Product";
>  private static final int MIN_NAME_LENGTH = 3;
> private static final int VALIDATE_NAME_LENGTH = 50;
>
>  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 ActionLink deleteOptionLink = new ActionLink("deleteOptionLink",
> "Delete", this, "onDeleteProductOptionClick");
>  private ActionLink deleteQuantityLink = new
> ActionLink("deleteQuantityLink", "Delete", this,
> "onDeleteProductQuantityClick");
> private PagingDataProvider<ProductOptionEnt> productOptionDataProvider =
> null;
>  private PagingDataProvider<ProductOptionEnt> productQuantityDataProvider
> = null;
> private TextField productName = new TextField("name", "Product name ",
> true);
>  protected HiddenField idField = new HiddenField("id", Long.class);
>
> public Long productId;
>
> public EditProduct() {
> try {
> form.add(idField);
>  productName.setMinLength(MIN_NAME_LENGTH);
> productName.addStyleClass("dash_input");
> form.add(productName);
>
> deleteOptionLink.addStyleClass("btn_small btn-primary");
> deleteOptionLink.setTitle("Delete record");
>  deleteOptionLink.setAttribute("onclick", "return window.confirm('Are you
> sure you want to delete this record?', 'Delete Product Option');");
>  addControl(deleteOptionLink);
>
> 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');");
>  addControl(deleteQuantityLink);
>
> setOptionFormTable();
> addControl(optionFormTable);
>  setQuantityFormTable();
> addControl(quantityFormTable);
>
> PageLink addLink = new PageLink("add", " New Option ", EditProduct.class);
>  addLink.setId("new_option");
> addLink.addStyleClass("btn btn-primary");
> addControl(addLink);
>
> form.setButtonAlign("right");
> Button button = new Submit("save", "Save", this, "onSave");
>  button.addStyleClass("btn btn-primary");
> form.add(button);
>
> PageSubmit pageSubmit = new PageSubmit("cancel", "Cancel", Products.class);
>  pageSubmit.addStyleClass("btn btn-primary");
> form.add(pageSubmit);
>
> // optionFormTable
>  button = new Submit("save", "Save", this, "onSaveOptionFormTable");
> button.addStyleClass("btn btn-primary");
>  optionFormTable.getForm().add(button);
>
> button = new Submit("cancel", "Cancel", this, "onCancelOptionFormTable");
>  button.addStyleClass("btn btn-primary");
> optionFormTable.getForm().add(button);
>
>  // quantityFormTable
> button = new Submit("save", "Save", this, "onSaveQuantityFormTable");
>  button.addStyleClass("btn btn-primary");
> quantityFormTable.getForm().add(button);
>
>  button = new Submit("cancel", "Cancel", this,
> "onCancelQuantityFormTable");
> button.addStyleClass("btn btn-primary");
>  quantityFormTable.getForm().add(button);
>
> addControl(form);
>
> } catch (NumberFormatException e) {
>  logger.error(e.getMessage(), e);
> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>  return;
> }
>
> }
>
> @SuppressWarnings("unchecked")
>  @Override
> public void onGet() {
> try {
> if (productId != null) {
>  idField.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());
> }
>
>  productOptionDataProvider = new PagingDataProvider() {
> @Override
> public int size() {
>  Integer size = 0;
> try {
> Long id =
> Long.parseLong(optionFormTable.getControlLink().getParameter("productId"));
>  size =
> Integer.parseInt(dashboardSBBeanLocal.getProductOptionCount(id).toString());
> } catch (SystemException e) {
>  logger.error(e.getMessage(), e);
> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>  }
> return size;
> }
>
>  @Override
> public List<ProductOptionEnt> getData() {
> List<ProductOptionEnt> productOptions = null;
>  int start = optionFormTable.getFirstRow();
> int count = optionFormTable.getPageSize();
> String sortColumn = optionFormTable.getSortedColumn();
>  boolean ascending = optionFormTable.isSortedAscending();
> Long id =
> Long.parseLong(optionFormTable.getControlLink().getParameter("productId"));
>  try {
> productOptions = dashboardSBBeanLocal.getProductOptions(id, start, count,
> sortColumn, ascending);
>  } catch (SystemException e) {
> logger.error(e.getMessage(), e);
> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>  }
> return productOptions;
> }
> };
>  optionFormTable.setDataProvider(productOptionDataProvider);
>
> List<ProductOptionEnt> rowList = optionFormTable.getRowList();
>  for (ProductOptionEnt productOptionEnt : rowList) {
> com.farheap.jsi.utils.Constants.Option optionType =
> productOptionEnt.getOptionType();
>  List<EnumI> enums = optionType.getEnums();
> Select select = new Select();
> for (EnumI enumI : enums) {
>  select.add(enumI.name());
> }
> FieldColumn column = (FieldColumn) optionFormTable.getColumn("value");
>  column.setField(select);
>
> }
>
> productQuantityDataProvider = new PagingDataProvider() {
>  @Override
> public int size() {
> Integer size = 0;
>  try {
> Long id =
> Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
>  size =
> Integer.parseInt(dashboardSBBeanLocal.getProductQuantityCount(id).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();
>  boolean ascending = quantityFormTable.isSortedAscending();
> Long id =
> Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
>  try {
> productQuantities = dashboardSBBeanLocal.getProductQuantities(id, 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);
> }
> } catch (NumberFormatException | SystemException e) {
>  logger.error(e.getMessage(), e);
> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>  return;
> }
>
> }
>
> public final boolean onSave() throws SystemException {
>  if (form.isValid()) {
> Long id = Long.parseLong(idField.getValue());
> if (id == null) {
>  ProductEnt productEnt = new ProductEnt();
> PrintProviderEnt printProviderEnt =
> dashboardSBBeanLocal.getPrintProviderByName(getContext().getRequest().getUserPrincipal().getName());
>  productEnt.setPrintProviderEnt(printProviderEnt);
> form.copyTo(productEnt);
> dashboardSBBeanLocal.saveProduct(productEnt);
>  } else {
> ProductEnt productEnt = dashboardSBBeanLocal.getProduct(id);
> form.copyTo(productEnt);
>  dashboardSBBeanLocal.saveProduct(productEnt);
> }
> form.clearValues();
>  HashMap<String, String> m = new HashMap<String, String>();
> m.put("productId", idField.getValue());
>  setRedirect(EditProduct.class);
> }
> return true;
>  }
>
> public boolean onSaveOptionFormTable() throws SystemException {
> if (optionFormTable.getForm().isValid()) {
>  List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
> dashboardSBBeanLocal.saveProductOptions(productOptions);
>  }
> System.out.println(productId);
> // HashMap<String, String> m = new HashMap<String, String>();
> // m.put("productId", "1");
> setRedirect(EditProduct.class);
> return true;
>  }
>
> public boolean onCancelOptionFormTable() throws SystemException {
> optionFormTable.setDataProvider(productOptionDataProvider);
>  // As form was cancelled, don't render the user submitted values
> optionFormTable.setRenderSubmittedValues(false);
>  return true;
> }
>
> public boolean onSaveQuantityFormTable() throws SystemException {
>  if (quantityFormTable.getForm().isValid()) {
> List<ProductQuantityEnt> productQuantities =
> quantityFormTable.getRowList();
>  dashboardSBBeanLocal.saveProductQuantities(productQuantities);
> }
> System.out.println(productId);
> // HashMap<String, String> m = new HashMap<String, String>();
> // m.put("productId", "1");
>  setRedirect(EditProduct.class);
> return true;
> }
>
> public boolean onCancelQuantityFormTable() throws SystemException {
> quantityFormTable.setDataProvider(productQuantityDataProvider);
>  // As form was cancelled, don't render the user submitted values
> quantityFormTable.setRenderSubmittedValues(false);
>  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;
> }
>
> public boolean onDeleteProductOptionClick() throws SystemException {
>  Long id = deleteOptionLink.getValueLong();
> dashboardSBBeanLocal.deleteProductOption(id);
> HashMap<String, String> m = new HashMap<String, String>();
>  m.put("productId", deleteOptionLink.getParameter("productId"));
> setRedirect(EditProduct.class, m);
>  return true;
> }
>
> @Override
> public String getHelp() {
>  return getContext().getRequest().getContextPath() +
> "/provider/edit-product-help.htm";
> }
>
>  private void setQuantityFormTable() {
>                 .....
> }
>
> private void setOptionFormTable() {
>                ......
>
> }
>
> }
>
>
>
>
>
>
> On Tue, Aug 6, 2013 at 11:51 AM, Gilberto <gi...@gmail.com> wrote:
>
>> I think you misunderstood the page flow[1] and onInit method usage[2].
>> Take a look at this example [3], there you will see some work division.
>> It shows some click + cayenne/jpa integration code, just my learning
>> repository.
>>
>> Regards,
>>
>> Gilberto
>>
>> [1]
>> http://click.apache.org/docs/user-guide/htmlsingle/click-book.html#execution
>> [2]
>> http://click.apache.org/docs/click-api/org/apache/click/Page.html#onInit%28%29
>> [3]
>> https://code.google.com/p/construtor/source/browse/trunk/park-samples/park-cayenne/src/main/java/park/web/page/EditVehicle.java
>>
>>
>> 2013/8/6 Kristian Lind <kl...@gmail.com>
>>
>>> Hi, I have this page, it contains a product that has a list of options.
>>>
>>> The product has a name that can be changed, and you can change the
>>> options.
>>>
>>> My Problem is when submitting the table form save button, I do not have
>>> the product id anymore.
>>> So in the OnInit method I can not get the data.
>>>
>>>
>>> [image: Inline image 1]
>>>
>>>
>>> /**
>>>  */
>>> public class EditProduct extends BorderedPage {
>>> private static final Logger logger =
>>> LoggerFactory.getLogger(EditProduct.class);
>>>
>>> private static final String PAGE_TITLE_CREATE = "Add Product";
>>> private static final String PAGE_TITLE_EDIT = "Edit Product";
>>>  private static final int MIN_NAME_LENGTH = 3;
>>> private static final int VALIDATE_NAME_LENGTH = 50;
>>>
>>>  private DashboardSBBeanLocal dashboardSBBeanLocal =
>>> SessionBeanManager.getDashboardSBBeanLocal();
>>> private EditProductForm form = null;
>>>  private ProductEnt product;
>>> private FormTable table = new FormTable("table");
>>> private ActionLink deleteLink = new ActionLink("delete", this,
>>> "onDeleteClick");
>>>  private PagingDataProvider<ProductOptionEnt> dataProvider = null;
>>>  @Bindable
>>>  private Long productId;
>>>  public EditProduct() {
>>>  form = new EditProductForm("form");
>>> form.setButtonAlign("right");
>>> Button button = new Submit("save", "Save", this, "onSave");
>>>  button.addStyleClass("btn btn-primary");
>>> form.add(button);
>>>
>>> PageSubmit pageSubmit = new PageSubmit("cancel", "Cancel",
>>> Products.class);
>>>  pageSubmit.addStyleClass("btn btn-primary");
>>> form.add(pageSubmit);
>>>
>>> // TableForm
>>>  button = new Submit("save", "Save", this, "onSaveTableForm");
>>> button.addStyleClass("btn btn-primary");
>>>  table.getForm().add(button);
>>>
>>> button = new Submit("cancel", "Cancel", this, "onCancelTableForm");
>>>  button.addStyleClass("btn btn-primary");
>>> table.getForm().add(button);
>>>  addControl(form);
>>> }
>>>
>>> @Override
>>>  public final String getTitle() {
>>> if (product == null) {
>>> return PAGE_TITLE_CREATE;
>>>  } else {
>>> return PAGE_TITLE_EDIT;
>>> }
>>> }
>>>
>>> public final boolean onSave() throws SystemException {
>>> if (form.isValid()) {
>>> Long id = form.getProductId();
>>>  if (id == null) {
>>> ProductEnt productEnt = new ProductEnt();
>>> PrintProviderEnt printProviderEnt =
>>> dashboardSBBeanLocal.getPrintProviderByName(getContext().getRequest().getUserPrincipal().getName());
>>>  productEnt.setPrintProviderEnt(printProviderEnt);
>>> form.copyTo(productEnt);
>>> dashboardSBBeanLocal.saveProduct(productEnt);
>>>  } else {
>>> ProductEnt productEnt = dashboardSBBeanLocal.getProduct(id);
>>> form.copyTo(productEnt);
>>>  dashboardSBBeanLocal.saveProduct(productEnt);
>>> }
>>> form.clearValues();
>>>  HashMap<String, String> m = new HashMap<String, String>();
>>> m.put("productId", "" + id);
>>>  setRedirect(EditProduct.class, m);
>>> }
>>> return true;
>>>  }
>>>
>>> private class EditProductForm extends Form {
>>> private TextField productName = new TextField("name", "Product name ",
>>> true);
>>>  private Field productIdField = new HiddenField("id", Long.class);
>>>
>>> public EditProductForm(final String name) {
>>>  super(name);
>>> add(productIdField);
>>> productName.setMinLength(MIN_NAME_LENGTH);
>>>  productName.addStyleClass("dash_input");
>>> add(productName);
>>> deleteLink.addStyleClass("btn_small btn-primary");
>>>  deleteLink.setTitle("Delete record");
>>> deleteLink.setAttribute("onclick", "return window.confirm('Are you sure
>>> you want to delete this record?', 'Delete Product Option');");
>>>  addControl(deleteLink);
>>>
>>> AbstractLink[] links = new AbstractLink[] { deleteLink };
>>> table.getForm().setButtonAlign(Form.ALIGN_RIGHT);
>>>  table.setClass(Table.CLASS_ITS);
>>> table.setPageSize(Constants.MAX_PAGE_SIZE);
>>> table.setSortable(true);
>>>  table.addStyleClass("dash_table");
>>>
>>> table.addColumn(new Column("id", "id"));
>>>  table.addColumn(new Column("optionType", "Options"));
>>>
>>> Select select = new Select();
>>>  Column column = new FieldColumn("value", select);
>>> table.addColumn(column);
>>>
>>>  Column c = new Column("action", "");
>>> c.setTextAlign("center");
>>> c.setWidth("120");
>>>  c.setSortable(false);
>>> c.setDecorator(new LinkDecorator(table, links, "id", "id"));
>>>  table.addColumn(c);
>>>
>>> table.getControlLink().setActionListener(new ActionListener() {
>>>  public boolean onAction(final Control source) {
>>> table.saveState(getContext());
>>> return true;
>>>  }
>>> });
>>>
>>> table.restoreState(getContext());
>>>
>>> addControl(table);
>>>
>>> PageLink addLink = new PageLink("add", " New Option ",
>>> EditProduct.class);
>>>  addLink.setId("new_option");
>>> addLink.addStyleClass("btn btn-primary");
>>> addControl(addLink);
>>>
>>> }
>>>
>>> public Long getProductId() {
>>> return (Long) productIdField.getValueObject();
>>>  }
>>>
>>> public TextField getProductName() {
>>> return productName;
>>>  }
>>>
>>> @Override
>>> public void onInit() {
>>>  super.onInit();
>>> try {
>>> Long productId = null;
>>>  String value = getContext().getRequestParameter("productId");
>>> getContext().setRequestAttribute("productId", value);
>>>  if (!StringUtils.isBlank(value)) {
>>> productId = Long.parseLong(value);
>>> } else {
>>>  value = table.getControlLink().getParameter("productId");
>>> if (!StringUtils.isBlank(value)) {
>>>  productId = Long.parseLong(value);
>>> }
>>> }
>>>  if (productId != null) {
>>> deleteLink.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());
>>> table.getControlLink().setParameter("productId", product.getId());
>>>  }
>>> dataProvider = new PagingDataProvider() {
>>> @Override
>>>  public int size() {
>>> Integer size = 0;
>>> try {
>>>  Long id =
>>> Long.parseLong(table.getControlLink().getParameter("productId"));
>>> size =
>>> Integer.parseInt(dashboardSBBeanLocal.getProductOptionCount(id).toString());
>>>  } catch (SystemException e) {
>>> logger.error(e.getMessage(), e);
>>> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>>  }
>>> return size;
>>> }
>>>
>>>  @Override
>>> public List<ProductOptionEnt> getData() {
>>> List<ProductOptionEnt> productOptions = null;
>>>  int start = table.getFirstRow();
>>> int count = table.getPageSize();
>>> String sortColumn = table.getSortedColumn();
>>>  boolean ascending = table.isSortedAscending();
>>> Long id =
>>> Long.parseLong(table.getControlLink().getParameter("productId"));
>>>  try {
>>> productOptions = dashboardSBBeanLocal.getProductOptions(id, start,
>>> count, sortColumn, ascending);
>>>  } catch (SystemException e) {
>>> logger.error(e.getMessage(), e);
>>> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>>  }
>>> return productOptions;
>>> }
>>> };
>>>  table.setDataProvider(dataProvider);
>>>
>>> List<ProductOptionEnt> rowList = table.getRowList();
>>>  for (ProductOptionEnt productOptionEnt : rowList) {
>>> com.farheap.jsi.utils.Constants.Option optionType =
>>> productOptionEnt.getOptionType();
>>>  List<EnumI> enums = optionType.getEnums();
>>> Select select = new Select();
>>> for (EnumI enumI : enums) {
>>>  select.add(enumI.name());
>>> }
>>> FieldColumn column = (FieldColumn) table.getColumn("value");
>>>  column.setField(select);
>>>
>>> }
>>> }
>>>
>>> } catch (SystemException | NumberFormatException e) {
>>> logger.error(e.getMessage(), e);
>>>  setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>> return;
>>> }
>>>
>>> }
>>>
>>> private void validateSize(final Field field, final int size) {
>>> if (field.getValue().length() > size) {
>>>  field.setError("Maximum " + field.getName() + " size exceeded (" +
>>> size + ").");
>>> }
>>>  }
>>>
>>> @Override
>>> public void validate() {
>>>  super.validate();
>>> validateSize(productName, VALIDATE_NAME_LENGTH);
>>> }
>>>
>>> }
>>>
>>> public boolean onSaveTableForm() throws SystemException {
>>> if (table.getForm().isValid()) {
>>>  List<ProductOptionEnt> productOptions = table.getRowList();
>>> dashboardSBBeanLocal.saveProductOptions(productOptions);
>>>  }
>>> HashMap<String, String> m = new HashMap<String, String>();
>>> *m.put("productId", // NEED THE PRODUCT ID HERE...  );*
>>>  setRedirect(EditProduct.class, m);
>>> return true;
>>> }
>>>
>>> public boolean onCancelTableForm() throws SystemException {
>>> table.setDataProvider(dataProvider);
>>>  // As form was cancelled, don't render the user submitted values
>>> table.setRenderSubmittedValues(false);
>>>  return true;
>>> }
>>>
>>> @Override
>>> public String getHelp() {
>>>  return getContext().getRequest().getContextPath() +
>>> "/provider/edit-product-help.htm";
>>> }
>>>
>>>  public boolean onDeleteClick() throws SystemException {
>>> Long id = deleteLink.getValueLong();
>>> dashboardSBBeanLocal.deleteProductOption(id);
>>>  HashMap<String, String> m = new HashMap<String, String>();
>>> m.put("productId", deleteLink.getParameter("productId"));
>>>  setRedirect(EditProduct.class, m);
>>> return true;
>>> }
>>>
>>> }
>>>
>>
>>
>
>
> --
> Best regards
>
> Kristian Lind
>

Re: Request Parameter

Posted by Kristian Lind <kl...@gmail.com>.
Hi, I have been looking at the links, and I did some changes to my code.
But I can not get it to work.

When I press the save button the OnGet() method is called with no ID, and
after that the onSave() method is called, but now the idField is empty...

I want to stay on the same page after save is pressed... also when the save
buttons for the form tables are pressed.

[image: Inline image 1]



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

private static final String PAGE_TITLE_CREATE = "Add Product";
private static final String PAGE_TITLE_EDIT = "Edit Product";
private static final int MIN_NAME_LENGTH = 3;
private static final int VALIDATE_NAME_LENGTH = 50;

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 ActionLink deleteOptionLink = new ActionLink("deleteOptionLink",
"Delete", this, "onDeleteProductOptionClick");
private ActionLink deleteQuantityLink = new
ActionLink("deleteQuantityLink", "Delete", this,
"onDeleteProductQuantityClick");
private PagingDataProvider<ProductOptionEnt> productOptionDataProvider =
null;
private PagingDataProvider<ProductOptionEnt> productQuantityDataProvider =
null;
private TextField productName = new TextField("name", "Product name ",
true);
protected HiddenField idField = new HiddenField("id", Long.class);

public Long productId;

public EditProduct() {
try {
form.add(idField);
productName.setMinLength(MIN_NAME_LENGTH);
productName.addStyleClass("dash_input");
form.add(productName);

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

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');");
addControl(deleteQuantityLink);

setOptionFormTable();
addControl(optionFormTable);
setQuantityFormTable();
addControl(quantityFormTable);

PageLink addLink = new PageLink("add", " New Option ", EditProduct.class);
addLink.setId("new_option");
addLink.addStyleClass("btn btn-primary");
addControl(addLink);

form.setButtonAlign("right");
Button button = new Submit("save", "Save", this, "onSave");
button.addStyleClass("btn btn-primary");
form.add(button);

PageSubmit pageSubmit = new PageSubmit("cancel", "Cancel", Products.class);
pageSubmit.addStyleClass("btn btn-primary");
form.add(pageSubmit);

// optionFormTable
button = new Submit("save", "Save", this, "onSaveOptionFormTable");
button.addStyleClass("btn btn-primary");
optionFormTable.getForm().add(button);

button = new Submit("cancel", "Cancel", this, "onCancelOptionFormTable");
button.addStyleClass("btn btn-primary");
optionFormTable.getForm().add(button);

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

button = new Submit("cancel", "Cancel", this, "onCancelQuantityFormTable");
button.addStyleClass("btn btn-primary");
quantityFormTable.getForm().add(button);

addControl(form);

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

}

@SuppressWarnings("unchecked")
@Override
public void onGet() {
try {
if (productId != null) {
idField.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());
}

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

@Override
public List<ProductOptionEnt> getData() {
List<ProductOptionEnt> productOptions = null;
int start = optionFormTable.getFirstRow();
int count = optionFormTable.getPageSize();
String sortColumn = optionFormTable.getSortedColumn();
boolean ascending = optionFormTable.isSortedAscending();
Long id =
Long.parseLong(optionFormTable.getControlLink().getParameter("productId"));
try {
productOptions = dashboardSBBeanLocal.getProductOptions(id, start, count,
sortColumn, ascending);
} catch (SystemException e) {
logger.error(e.getMessage(), e);
setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
}
return productOptions;
}
};
optionFormTable.setDataProvider(productOptionDataProvider);

List<ProductOptionEnt> rowList = optionFormTable.getRowList();
for (ProductOptionEnt productOptionEnt : rowList) {
com.farheap.jsi.utils.Constants.Option optionType =
productOptionEnt.getOptionType();
List<EnumI> enums = optionType.getEnums();
Select select = new Select();
for (EnumI enumI : enums) {
select.add(enumI.name());
}
FieldColumn column = (FieldColumn) optionFormTable.getColumn("value");
column.setField(select);

}

productQuantityDataProvider = new PagingDataProvider() {
@Override
public int size() {
Integer size = 0;
try {
Long id =
Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
size =
Integer.parseInt(dashboardSBBeanLocal.getProductQuantityCount(id).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();
boolean ascending = quantityFormTable.isSortedAscending();
Long id =
Long.parseLong(quantityFormTable.getControlLink().getParameter("productId"));
try {
productQuantities = dashboardSBBeanLocal.getProductQuantities(id, 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);
}
} catch (NumberFormatException | SystemException e) {
logger.error(e.getMessage(), e);
setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
return;
}

}

public final boolean onSave() throws SystemException {
if (form.isValid()) {
Long id = Long.parseLong(idField.getValue());
if (id == null) {
ProductEnt productEnt = new ProductEnt();
PrintProviderEnt printProviderEnt =
dashboardSBBeanLocal.getPrintProviderByName(getContext().getRequest().getUserPrincipal().getName());
productEnt.setPrintProviderEnt(printProviderEnt);
form.copyTo(productEnt);
dashboardSBBeanLocal.saveProduct(productEnt);
} else {
ProductEnt productEnt = dashboardSBBeanLocal.getProduct(id);
form.copyTo(productEnt);
dashboardSBBeanLocal.saveProduct(productEnt);
}
form.clearValues();
HashMap<String, String> m = new HashMap<String, String>();
m.put("productId", idField.getValue());
setRedirect(EditProduct.class);
}
return true;
}

public boolean onSaveOptionFormTable() throws SystemException {
if (optionFormTable.getForm().isValid()) {
List<ProductOptionEnt> productOptions = optionFormTable.getRowList();
dashboardSBBeanLocal.saveProductOptions(productOptions);
}
System.out.println(productId);
// HashMap<String, String> m = new HashMap<String, String>();
// m.put("productId", "1");
setRedirect(EditProduct.class);
return true;
}

public boolean onCancelOptionFormTable() throws SystemException {
optionFormTable.setDataProvider(productOptionDataProvider);
// As form was cancelled, don't render the user submitted values
optionFormTable.setRenderSubmittedValues(false);
return true;
}

public boolean onSaveQuantityFormTable() throws SystemException {
if (quantityFormTable.getForm().isValid()) {
List<ProductQuantityEnt> productQuantities = quantityFormTable.getRowList();
dashboardSBBeanLocal.saveProductQuantities(productQuantities);
}
System.out.println(productId);
// HashMap<String, String> m = new HashMap<String, String>();
// m.put("productId", "1");
setRedirect(EditProduct.class);
return true;
}

public boolean onCancelQuantityFormTable() throws SystemException {
quantityFormTable.setDataProvider(productQuantityDataProvider);
// As form was cancelled, don't render the user submitted values
quantityFormTable.setRenderSubmittedValues(false);
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;
}

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

@Override
public String getHelp() {
return getContext().getRequest().getContextPath() +
"/provider/edit-product-help.htm";
}

private void setQuantityFormTable() {
                .....
}

private void setOptionFormTable() {
               ......

}

}






On Tue, Aug 6, 2013 at 11:51 AM, Gilberto <gi...@gmail.com> wrote:

> I think you misunderstood the page flow[1] and onInit method usage[2].
> Take a look at this example [3], there you will see some work division.
> It shows some click + cayenne/jpa integration code, just my learning
> repository.
>
> Regards,
>
> Gilberto
>
> [1]
> http://click.apache.org/docs/user-guide/htmlsingle/click-book.html#execution
> [2]
> http://click.apache.org/docs/click-api/org/apache/click/Page.html#onInit%28%29
> [3]
> https://code.google.com/p/construtor/source/browse/trunk/park-samples/park-cayenne/src/main/java/park/web/page/EditVehicle.java
>
>
> 2013/8/6 Kristian Lind <kl...@gmail.com>
>
>> Hi, I have this page, it contains a product that has a list of options.
>>
>> The product has a name that can be changed, and you can change the
>> options.
>>
>> My Problem is when submitting the table form save button, I do not have
>> the product id anymore.
>> So in the OnInit method I can not get the data.
>>
>>
>> [image: Inline image 1]
>>
>>
>> /**
>>  */
>> public class EditProduct extends BorderedPage {
>> private static final Logger logger =
>> LoggerFactory.getLogger(EditProduct.class);
>>
>> private static final String PAGE_TITLE_CREATE = "Add Product";
>> private static final String PAGE_TITLE_EDIT = "Edit Product";
>>  private static final int MIN_NAME_LENGTH = 3;
>> private static final int VALIDATE_NAME_LENGTH = 50;
>>
>>  private DashboardSBBeanLocal dashboardSBBeanLocal =
>> SessionBeanManager.getDashboardSBBeanLocal();
>> private EditProductForm form = null;
>>  private ProductEnt product;
>> private FormTable table = new FormTable("table");
>> private ActionLink deleteLink = new ActionLink("delete", this,
>> "onDeleteClick");
>>  private PagingDataProvider<ProductOptionEnt> dataProvider = null;
>>  @Bindable
>>  private Long productId;
>>  public EditProduct() {
>>  form = new EditProductForm("form");
>> form.setButtonAlign("right");
>> Button button = new Submit("save", "Save", this, "onSave");
>>  button.addStyleClass("btn btn-primary");
>> form.add(button);
>>
>> PageSubmit pageSubmit = new PageSubmit("cancel", "Cancel",
>> Products.class);
>>  pageSubmit.addStyleClass("btn btn-primary");
>> form.add(pageSubmit);
>>
>> // TableForm
>>  button = new Submit("save", "Save", this, "onSaveTableForm");
>> button.addStyleClass("btn btn-primary");
>>  table.getForm().add(button);
>>
>> button = new Submit("cancel", "Cancel", this, "onCancelTableForm");
>>  button.addStyleClass("btn btn-primary");
>> table.getForm().add(button);
>>  addControl(form);
>> }
>>
>> @Override
>>  public final String getTitle() {
>> if (product == null) {
>> return PAGE_TITLE_CREATE;
>>  } else {
>> return PAGE_TITLE_EDIT;
>> }
>> }
>>
>> public final boolean onSave() throws SystemException {
>> if (form.isValid()) {
>> Long id = form.getProductId();
>>  if (id == null) {
>> ProductEnt productEnt = new ProductEnt();
>> PrintProviderEnt printProviderEnt =
>> dashboardSBBeanLocal.getPrintProviderByName(getContext().getRequest().getUserPrincipal().getName());
>>  productEnt.setPrintProviderEnt(printProviderEnt);
>> form.copyTo(productEnt);
>> dashboardSBBeanLocal.saveProduct(productEnt);
>>  } else {
>> ProductEnt productEnt = dashboardSBBeanLocal.getProduct(id);
>> form.copyTo(productEnt);
>>  dashboardSBBeanLocal.saveProduct(productEnt);
>> }
>> form.clearValues();
>>  HashMap<String, String> m = new HashMap<String, String>();
>> m.put("productId", "" + id);
>>  setRedirect(EditProduct.class, m);
>> }
>> return true;
>>  }
>>
>> private class EditProductForm extends Form {
>> private TextField productName = new TextField("name", "Product name ",
>> true);
>>  private Field productIdField = new HiddenField("id", Long.class);
>>
>> public EditProductForm(final String name) {
>>  super(name);
>> add(productIdField);
>> productName.setMinLength(MIN_NAME_LENGTH);
>>  productName.addStyleClass("dash_input");
>> add(productName);
>> deleteLink.addStyleClass("btn_small btn-primary");
>>  deleteLink.setTitle("Delete record");
>> deleteLink.setAttribute("onclick", "return window.confirm('Are you sure
>> you want to delete this record?', 'Delete Product Option');");
>>  addControl(deleteLink);
>>
>> AbstractLink[] links = new AbstractLink[] { deleteLink };
>> table.getForm().setButtonAlign(Form.ALIGN_RIGHT);
>>  table.setClass(Table.CLASS_ITS);
>> table.setPageSize(Constants.MAX_PAGE_SIZE);
>> table.setSortable(true);
>>  table.addStyleClass("dash_table");
>>
>> table.addColumn(new Column("id", "id"));
>>  table.addColumn(new Column("optionType", "Options"));
>>
>> Select select = new Select();
>>  Column column = new FieldColumn("value", select);
>> table.addColumn(column);
>>
>>  Column c = new Column("action", "");
>> c.setTextAlign("center");
>> c.setWidth("120");
>>  c.setSortable(false);
>> c.setDecorator(new LinkDecorator(table, links, "id", "id"));
>>  table.addColumn(c);
>>
>> table.getControlLink().setActionListener(new ActionListener() {
>>  public boolean onAction(final Control source) {
>> table.saveState(getContext());
>> return true;
>>  }
>> });
>>
>> table.restoreState(getContext());
>>
>> addControl(table);
>>
>> PageLink addLink = new PageLink("add", " New Option ", EditProduct.class);
>>  addLink.setId("new_option");
>> addLink.addStyleClass("btn btn-primary");
>> addControl(addLink);
>>
>> }
>>
>> public Long getProductId() {
>> return (Long) productIdField.getValueObject();
>>  }
>>
>> public TextField getProductName() {
>> return productName;
>>  }
>>
>> @Override
>> public void onInit() {
>>  super.onInit();
>> try {
>> Long productId = null;
>>  String value = getContext().getRequestParameter("productId");
>> getContext().setRequestAttribute("productId", value);
>>  if (!StringUtils.isBlank(value)) {
>> productId = Long.parseLong(value);
>> } else {
>>  value = table.getControlLink().getParameter("productId");
>> if (!StringUtils.isBlank(value)) {
>>  productId = Long.parseLong(value);
>> }
>> }
>>  if (productId != null) {
>> deleteLink.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());
>> table.getControlLink().setParameter("productId", product.getId());
>>  }
>> dataProvider = new PagingDataProvider() {
>> @Override
>>  public int size() {
>> Integer size = 0;
>> try {
>>  Long id =
>> Long.parseLong(table.getControlLink().getParameter("productId"));
>> size =
>> Integer.parseInt(dashboardSBBeanLocal.getProductOptionCount(id).toString());
>>  } catch (SystemException e) {
>> logger.error(e.getMessage(), e);
>> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>  }
>> return size;
>> }
>>
>>  @Override
>> public List<ProductOptionEnt> getData() {
>> List<ProductOptionEnt> productOptions = null;
>>  int start = table.getFirstRow();
>> int count = table.getPageSize();
>> String sortColumn = table.getSortedColumn();
>>  boolean ascending = table.isSortedAscending();
>> Long id =
>> Long.parseLong(table.getControlLink().getParameter("productId"));
>>  try {
>> productOptions = dashboardSBBeanLocal.getProductOptions(id, start, count,
>> sortColumn, ascending);
>>  } catch (SystemException e) {
>> logger.error(e.getMessage(), e);
>> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>>  }
>> return productOptions;
>> }
>> };
>>  table.setDataProvider(dataProvider);
>>
>> List<ProductOptionEnt> rowList = table.getRowList();
>>  for (ProductOptionEnt productOptionEnt : rowList) {
>> com.farheap.jsi.utils.Constants.Option optionType =
>> productOptionEnt.getOptionType();
>>  List<EnumI> enums = optionType.getEnums();
>> Select select = new Select();
>> for (EnumI enumI : enums) {
>>  select.add(enumI.name());
>> }
>> FieldColumn column = (FieldColumn) table.getColumn("value");
>>  column.setField(select);
>>
>> }
>> }
>>
>> } catch (SystemException | NumberFormatException e) {
>> logger.error(e.getMessage(), e);
>>  setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>> return;
>> }
>>
>> }
>>
>> private void validateSize(final Field field, final int size) {
>> if (field.getValue().length() > size) {
>>  field.setError("Maximum " + field.getName() + " size exceeded (" + size
>> + ").");
>> }
>>  }
>>
>> @Override
>> public void validate() {
>>  super.validate();
>> validateSize(productName, VALIDATE_NAME_LENGTH);
>> }
>>
>> }
>>
>> public boolean onSaveTableForm() throws SystemException {
>> if (table.getForm().isValid()) {
>>  List<ProductOptionEnt> productOptions = table.getRowList();
>> dashboardSBBeanLocal.saveProductOptions(productOptions);
>>  }
>> HashMap<String, String> m = new HashMap<String, String>();
>> *m.put("productId", // NEED THE PRODUCT ID HERE...  );*
>>  setRedirect(EditProduct.class, m);
>> return true;
>> }
>>
>> public boolean onCancelTableForm() throws SystemException {
>> table.setDataProvider(dataProvider);
>>  // As form was cancelled, don't render the user submitted values
>> table.setRenderSubmittedValues(false);
>>  return true;
>> }
>>
>> @Override
>> public String getHelp() {
>>  return getContext().getRequest().getContextPath() +
>> "/provider/edit-product-help.htm";
>> }
>>
>>  public boolean onDeleteClick() throws SystemException {
>> Long id = deleteLink.getValueLong();
>> dashboardSBBeanLocal.deleteProductOption(id);
>>  HashMap<String, String> m = new HashMap<String, String>();
>> m.put("productId", deleteLink.getParameter("productId"));
>>  setRedirect(EditProduct.class, m);
>> return true;
>> }
>>
>> }
>>
>
>


-- 
Best regards

Kristian Lind

Re: Request Parameter

Posted by Gilberto <gi...@gmail.com>.
I think you misunderstood the page flow[1] and onInit method usage[2]. Take
a look at this example [3], there you will see some work division.
It shows some click + cayenne/jpa integration code, just my learning
repository.

Regards,

Gilberto

[1]
http://click.apache.org/docs/user-guide/htmlsingle/click-book.html#execution
[2]
http://click.apache.org/docs/click-api/org/apache/click/Page.html#onInit%28%29
[3]
https://code.google.com/p/construtor/source/browse/trunk/park-samples/park-cayenne/src/main/java/park/web/page/EditVehicle.java


2013/8/6 Kristian Lind <kl...@gmail.com>

> Hi, I have this page, it contains a product that has a list of options.
>
> The product has a name that can be changed, and you can change the
> options.
>
> My Problem is when submitting the table form save button, I do not have
> the product id anymore.
> So in the OnInit method I can not get the data.
>
>
> [image: Inline image 1]
>
>
> /**
>  */
> public class EditProduct extends BorderedPage {
> private static final Logger logger =
> LoggerFactory.getLogger(EditProduct.class);
>
> private static final String PAGE_TITLE_CREATE = "Add Product";
> private static final String PAGE_TITLE_EDIT = "Edit Product";
>  private static final int MIN_NAME_LENGTH = 3;
> private static final int VALIDATE_NAME_LENGTH = 50;
>
>  private DashboardSBBeanLocal dashboardSBBeanLocal =
> SessionBeanManager.getDashboardSBBeanLocal();
> private EditProductForm form = null;
>  private ProductEnt product;
> private FormTable table = new FormTable("table");
> private ActionLink deleteLink = new ActionLink("delete", this,
> "onDeleteClick");
>  private PagingDataProvider<ProductOptionEnt> dataProvider = null;
>  @Bindable
>  private Long productId;
>  public EditProduct() {
>  form = new EditProductForm("form");
> form.setButtonAlign("right");
> Button button = new Submit("save", "Save", this, "onSave");
>  button.addStyleClass("btn btn-primary");
> form.add(button);
>
> PageSubmit pageSubmit = new PageSubmit("cancel", "Cancel", Products.class);
>  pageSubmit.addStyleClass("btn btn-primary");
> form.add(pageSubmit);
>
> // TableForm
>  button = new Submit("save", "Save", this, "onSaveTableForm");
> button.addStyleClass("btn btn-primary");
>  table.getForm().add(button);
>
> button = new Submit("cancel", "Cancel", this, "onCancelTableForm");
>  button.addStyleClass("btn btn-primary");
> table.getForm().add(button);
>  addControl(form);
> }
>
> @Override
>  public final String getTitle() {
> if (product == null) {
> return PAGE_TITLE_CREATE;
>  } else {
> return PAGE_TITLE_EDIT;
> }
> }
>
> public final boolean onSave() throws SystemException {
> if (form.isValid()) {
> Long id = form.getProductId();
>  if (id == null) {
> ProductEnt productEnt = new ProductEnt();
> PrintProviderEnt printProviderEnt =
> dashboardSBBeanLocal.getPrintProviderByName(getContext().getRequest().getUserPrincipal().getName());
>  productEnt.setPrintProviderEnt(printProviderEnt);
> form.copyTo(productEnt);
> dashboardSBBeanLocal.saveProduct(productEnt);
>  } else {
> ProductEnt productEnt = dashboardSBBeanLocal.getProduct(id);
> form.copyTo(productEnt);
>  dashboardSBBeanLocal.saveProduct(productEnt);
> }
> form.clearValues();
>  HashMap<String, String> m = new HashMap<String, String>();
> m.put("productId", "" + id);
>  setRedirect(EditProduct.class, m);
> }
> return true;
>  }
>
> private class EditProductForm extends Form {
> private TextField productName = new TextField("name", "Product name ",
> true);
>  private Field productIdField = new HiddenField("id", Long.class);
>
> public EditProductForm(final String name) {
>  super(name);
> add(productIdField);
> productName.setMinLength(MIN_NAME_LENGTH);
>  productName.addStyleClass("dash_input");
> add(productName);
> deleteLink.addStyleClass("btn_small btn-primary");
>  deleteLink.setTitle("Delete record");
> deleteLink.setAttribute("onclick", "return window.confirm('Are you sure
> you want to delete this record?', 'Delete Product Option');");
>  addControl(deleteLink);
>
> AbstractLink[] links = new AbstractLink[] { deleteLink };
> table.getForm().setButtonAlign(Form.ALIGN_RIGHT);
>  table.setClass(Table.CLASS_ITS);
> table.setPageSize(Constants.MAX_PAGE_SIZE);
> table.setSortable(true);
>  table.addStyleClass("dash_table");
>
> table.addColumn(new Column("id", "id"));
>  table.addColumn(new Column("optionType", "Options"));
>
> Select select = new Select();
>  Column column = new FieldColumn("value", select);
> table.addColumn(column);
>
>  Column c = new Column("action", "");
> c.setTextAlign("center");
> c.setWidth("120");
>  c.setSortable(false);
> c.setDecorator(new LinkDecorator(table, links, "id", "id"));
>  table.addColumn(c);
>
> table.getControlLink().setActionListener(new ActionListener() {
>  public boolean onAction(final Control source) {
> table.saveState(getContext());
> return true;
>  }
> });
>
> table.restoreState(getContext());
>
> addControl(table);
>
> PageLink addLink = new PageLink("add", " New Option ", EditProduct.class);
>  addLink.setId("new_option");
> addLink.addStyleClass("btn btn-primary");
> addControl(addLink);
>
> }
>
> public Long getProductId() {
> return (Long) productIdField.getValueObject();
>  }
>
> public TextField getProductName() {
> return productName;
>  }
>
> @Override
> public void onInit() {
>  super.onInit();
> try {
> Long productId = null;
>  String value = getContext().getRequestParameter("productId");
> getContext().setRequestAttribute("productId", value);
>  if (!StringUtils.isBlank(value)) {
> productId = Long.parseLong(value);
> } else {
>  value = table.getControlLink().getParameter("productId");
> if (!StringUtils.isBlank(value)) {
>  productId = Long.parseLong(value);
> }
> }
>  if (productId != null) {
> deleteLink.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());
> table.getControlLink().setParameter("productId", product.getId());
>  }
> dataProvider = new PagingDataProvider() {
> @Override
>  public int size() {
> Integer size = 0;
> try {
>  Long id =
> Long.parseLong(table.getControlLink().getParameter("productId"));
> size =
> Integer.parseInt(dashboardSBBeanLocal.getProductOptionCount(id).toString());
>  } catch (SystemException e) {
> logger.error(e.getMessage(), e);
> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>  }
> return size;
> }
>
>  @Override
> public List<ProductOptionEnt> getData() {
> List<ProductOptionEnt> productOptions = null;
>  int start = table.getFirstRow();
> int count = table.getPageSize();
> String sortColumn = table.getSortedColumn();
>  boolean ascending = table.isSortedAscending();
> Long id = Long.parseLong(table.getControlLink().getParameter("productId"));
>  try {
> productOptions = dashboardSBBeanLocal.getProductOptions(id, start, count,
> sortColumn, ascending);
>  } catch (SystemException e) {
> logger.error(e.getMessage(), e);
> setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
>  }
> return productOptions;
> }
> };
>  table.setDataProvider(dataProvider);
>
> List<ProductOptionEnt> rowList = table.getRowList();
>  for (ProductOptionEnt productOptionEnt : rowList) {
> com.farheap.jsi.utils.Constants.Option optionType =
> productOptionEnt.getOptionType();
>  List<EnumI> enums = optionType.getEnums();
> Select select = new Select();
> for (EnumI enumI : enums) {
>  select.add(enumI.name());
> }
> FieldColumn column = (FieldColumn) table.getColumn("value");
>  column.setField(select);
>
> }
> }
>
> } catch (SystemException | NumberFormatException e) {
> logger.error(e.getMessage(), e);
>  setRedirect(com.farheap.jsi.dashboard.pages.errors.Error.class);
> return;
> }
>
> }
>
> private void validateSize(final Field field, final int size) {
> if (field.getValue().length() > size) {
>  field.setError("Maximum " + field.getName() + " size exceeded (" + size
> + ").");
> }
>  }
>
> @Override
> public void validate() {
>  super.validate();
> validateSize(productName, VALIDATE_NAME_LENGTH);
> }
>
> }
>
> public boolean onSaveTableForm() throws SystemException {
> if (table.getForm().isValid()) {
>  List<ProductOptionEnt> productOptions = table.getRowList();
> dashboardSBBeanLocal.saveProductOptions(productOptions);
>  }
> HashMap<String, String> m = new HashMap<String, String>();
> *m.put("productId", // NEED THE PRODUCT ID HERE...  );*
>  setRedirect(EditProduct.class, m);
> return true;
> }
>
> public boolean onCancelTableForm() throws SystemException {
> table.setDataProvider(dataProvider);
>  // As form was cancelled, don't render the user submitted values
> table.setRenderSubmittedValues(false);
>  return true;
> }
>
> @Override
> public String getHelp() {
>  return getContext().getRequest().getContextPath() +
> "/provider/edit-product-help.htm";
> }
>
>  public boolean onDeleteClick() throws SystemException {
> Long id = deleteLink.getValueLong();
> dashboardSBBeanLocal.deleteProductOption(id);
>  HashMap<String, String> m = new HashMap<String, String>();
> m.put("productId", deleteLink.getParameter("productId"));
>  setRedirect(EditProduct.class, m);
> return true;
> }
>
> }
>