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 <[email protected]> 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 <[email protected]> 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 <[email protected]>
>>
>>> 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

Reply via email to