EmailRecipeForm.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* ===================================================================
  2. * EmailRecipeForm.java
  3. *
  4. * Created Mar 5, 2005 6:38:05 PM
  5. *
  6. * Copyright (c) 2005 Matt Magoffin (spamsqr@msqr.us)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. * ===================================================================
  23. * $Id: EmailRecipeForm.java 28 2009-05-04 01:19:45Z msqr $
  24. * ===================================================================
  25. */
  26. package magoffin.matt.ieat.web;
  27. import java.util.LinkedHashMap;
  28. import java.util.Map;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpServletResponse;
  31. import magoffin.matt.ieat.biz.RecipeIOBiz;
  32. import magoffin.matt.ieat.domain.Recipe;
  33. import magoffin.matt.ieat.domain.UiData;
  34. import magoffin.matt.ieat.domain.UiSearchResults;
  35. import magoffin.matt.ieat.util.EmailRecipeCommand;
  36. import org.springframework.validation.BindException;
  37. import org.springframework.validation.Errors;
  38. import org.springframework.validation.ObjectError;
  39. import org.springframework.web.servlet.ModelAndView;
  40. /**
  41. * Form controller for emailing a recipe to someone.
  42. *
  43. * @author Matt Magoffin (spamsqr@msqr.us)
  44. * @version $Revision: 28 $ $Date: 2009-05-04 13:19:45 +1200 (Mon, 04 May 2009) $
  45. */
  46. public class EmailRecipeForm extends AbstractEatForm {
  47. private RecipeIOBiz recipeIOBiz;
  48. @SuppressWarnings("unchecked")
  49. @Override
  50. protected Map referenceData(HttpServletRequest request, Object command,
  51. Errors errors) throws Exception {
  52. WebUtil.getBizContext(request,true);
  53. Map<String, Object> m = new LinkedHashMap<String, Object>();
  54. UiData ui = getDomainObjectFactory().getUiDataInstance();
  55. m.put("uidata",ui);
  56. // add recipe to UI
  57. EmailRecipeCommand emailCommand = (EmailRecipeCommand)command;
  58. Recipe r = recipeBiz.getRecipeById(emailCommand.getRecipeId());
  59. ui.getRecipe().add(r);
  60. if ( emailCommand.getIndex() != null || emailCommand.getCriteria() != null ) {
  61. UiSearchResults sr = getDomainObjectFactory().getEmbeddedSearchResultsInstance();
  62. WebUtil.initSearchResults(sr,emailCommand.getIndex(),emailCommand.getCriteria(),
  63. getDomainObjectFactory());
  64. ui.setSearchResults(sr);
  65. }
  66. return m;
  67. }
  68. @Override
  69. protected ModelAndView onSubmit(HttpServletRequest request,
  70. HttpServletResponse response, Object command, BindException errors)
  71. throws Exception {
  72. EmailRecipeCommand emailCommand = (EmailRecipeCommand)command;
  73. // get the recipe
  74. Recipe recipe = recipeBiz.getRecipeById(emailCommand.getRecipeId());
  75. emailCommand.setRecipe(recipe);
  76. recipeIOBiz.emailRecipe(emailCommand,WebUtil.getBizContext(request,true));
  77. // save message that email has been sent
  78. ObjectError msg = new ObjectError(null,
  79. new String[] {"email-recipe.sent"},null,
  80. "The email has been sent.");
  81. WebUtil.saveError(request,msg);
  82. Map<String, Object> model = new LinkedHashMap<String, Object>();
  83. WebUtil.saveRecipeCommand(model,emailCommand);
  84. return new ModelAndView(getSuccessView(),model);
  85. }
  86. @Override
  87. protected ModelAndView processCancel(HttpServletRequest request,
  88. HttpServletResponse response, Object command, BindException errors) {
  89. EmailRecipeCommand emailCommand = (EmailRecipeCommand)command;
  90. Map<String, Object> model = new LinkedHashMap<String, Object>();
  91. WebUtil.saveRecipeCommand(model,emailCommand);
  92. return new ModelAndView(getCancelView(),model);
  93. }
  94. /**
  95. * @return the recipeIOBiz
  96. */
  97. public RecipeIOBiz getRecipeIOBiz() {
  98. return recipeIOBiz;
  99. }
  100. /**
  101. * @param recipeIOBiz the recipeIOBiz to set
  102. */
  103. public void setRecipeIOBiz(RecipeIOBiz recipeIOBiz) {
  104. this.recipeIOBiz = recipeIOBiz;
  105. }
  106. }