AddIngredientForm.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* ===================================================================
  2. * AddIngredientForm.java
  3. *
  4. * Copyright (c) 2004 Matt Magoffin. Created Aug 18, 2004 7:03:26 PM.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. * 02111-1307 USA
  20. * ===================================================================
  21. * $Id: AddIngredientForm.java 28 2009-05-04 01:19:45Z msqr $
  22. * ===================================================================
  23. */
  24. package magoffin.matt.ieat.web;
  25. import java.util.HashMap;
  26. import java.util.LinkedHashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpServletResponse;
  31. import magoffin.matt.ieat.biz.BizContext;
  32. import magoffin.matt.ieat.domain.Ingredient;
  33. import magoffin.matt.ieat.domain.UiData;
  34. import magoffin.matt.ieat.domain.UiEdit;
  35. import magoffin.matt.ieat.domain.UiIngredientMatch;
  36. import magoffin.matt.ieat.domain.UiSearchResults;
  37. import magoffin.matt.ieat.util.IngredientCriteriaImpl;
  38. import org.springframework.validation.BindException;
  39. import org.springframework.validation.Errors;
  40. import org.springframework.validation.Validator;
  41. import org.springframework.web.bind.ServletRequestDataBinder;
  42. import org.springframework.web.servlet.ModelAndView;
  43. /**
  44. * Controller for adding a new ingredient.
  45. *
  46. * @author Matt Magoffin (spamsqr@msqr.us)
  47. * @version $Revision: 28 $ $Date: 2009-05-04 13:19:45 +1200 (Mon, 04 May 2009) $
  48. */
  49. public class AddIngredientForm extends AbstractEatWizardForm {
  50. @Override
  51. @SuppressWarnings("unchecked")
  52. protected Map referenceData(HttpServletRequest request, int page)
  53. throws Exception {
  54. Map<String, Object> m = new LinkedHashMap<String, Object>();
  55. UiData ui = getDomainObjectFactory().getUiDataInstance();
  56. m.put("uidata",ui);
  57. List<Ingredient> l = recipeBiz.getAvailableIngredients();
  58. ui.getIngredient().addAll(l);
  59. return m;
  60. }
  61. @Override
  62. protected Object formBackingObject(HttpServletRequest request)
  63. throws Exception
  64. {
  65. UiEdit edit = getDomainObjectFactory().getUiEditInstance();
  66. Ingredient ingredient = getDomainObjectFactory().getIngredientInstance();
  67. edit.setIngredient(ingredient);
  68. ServletRequestDataBinder binder = createBinder(request, edit);
  69. binder.bind(request);
  70. return edit;
  71. }
  72. @Override
  73. protected ModelAndView processFinish(HttpServletRequest request,
  74. HttpServletResponse response, Object command, BindException errors)
  75. throws Exception
  76. {
  77. // store recipe in backend!
  78. UiEdit edit = (UiEdit)command;
  79. Ingredient ingredient = edit.getIngredient();
  80. BizContext context = WebUtil.getBizContext(request, false);
  81. ingredient = recipeBiz.storeIngredient(ingredient,context);
  82. edit.setIngredient(ingredient);
  83. Map<String, Object> params = new HashMap<String, Object>();
  84. params.put(WebConstants.DEFALUT_MODEL_OBJECT, edit);
  85. return new ModelAndView(getSuccessView(),params);
  86. }
  87. @SuppressWarnings("unchecked")
  88. @Override
  89. protected void validatePage(Object command, Errors errors, int page) {
  90. UiEdit edit = (UiEdit)command;
  91. Ingredient ingredient = edit.getIngredient();
  92. Validator val = getValidator();
  93. val.validate(ingredient, errors);
  94. // as long as there are no other validation errors, validate not similar to other
  95. // ingredient names already available
  96. if ( page == 0 && errors.getErrorCount() < 1 && !edit.isIgnoreWarnings()
  97. && ingredient.getName() != null && ingredient.getName().trim().length() > 0 ) {
  98. // search for like-named recipes and display validation error if any found
  99. IngredientCriteriaImpl criteria = new IngredientCriteriaImpl(ingredient.getName());
  100. UiSearchResults results = recipeSearchBiz.findIngredientsSimilarlyNamed(criteria);
  101. if ( results.getTotalResults().intValue() > 0 ) {
  102. for ( UiIngredientMatch match : (List<UiIngredientMatch>)results.getIngredient() ) {
  103. errors.rejectValue("ingredient.name", "edit-ingredient.similar.name",
  104. new Object[] {match.getName()}, "name.displayName");
  105. }
  106. edit.setIgnoreWarnings(true);
  107. }
  108. }
  109. }
  110. }