AbstractEatCommandController.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* ===================================================================
  2. * AbstractEatCommandController.java
  3. *
  4. * Created Sep 19, 2004 4:52:17 PM
  5. *
  6. * Copyright (c) 2004 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: AbstractEatCommandController.java 28 2009-05-04 01:19:45Z msqr $
  24. * ===================================================================
  25. */
  26. package magoffin.matt.ieat.web;
  27. import java.lang.reflect.Constructor;
  28. import javax.servlet.http.HttpServletRequest;
  29. import magoffin.matt.ieat.biz.DomainObjectFactory;
  30. import magoffin.matt.ieat.biz.RecipeBiz;
  31. import magoffin.matt.ieat.biz.RecipeSearchBiz;
  32. import magoffin.matt.ieat.biz.UserBiz;
  33. import magoffin.matt.util.StringUtil;
  34. import magoffin.matt.xweb.util.MessagesSource;
  35. import magoffin.matt.xweb.util.ServletRequestDataBinderTemplate;
  36. import org.springframework.validation.DataBinder;
  37. import org.springframework.web.bind.ServletRequestDataBinder;
  38. import org.springframework.web.servlet.mvc.AbstractCommandController;
  39. /**
  40. * Abstract base class for command controllers.
  41. *
  42. * @author Matt Magoffin (spamsqr@msqr.us)
  43. * @version $Revision: 28 $ $Date: 2009-05-04 13:19:45 +1200 (Mon, 04 May 2009) $
  44. */
  45. public abstract class AbstractEatCommandController extends AbstractCommandController {
  46. /** The RecipeBiz. */
  47. protected RecipeBiz recipeBiz = null;
  48. /** The RecipeSearchBiz. */
  49. protected RecipeSearchBiz recipeSearchBiz = null;
  50. /** The UserBiz. */
  51. protected UserBiz userBiz = null;
  52. private String messagesSourceBeanName = "messageSource";
  53. private String recipeBizBeanName = "recipeBiz";
  54. private String recipeSearchBizBeanName = "recipeSearchBiz";
  55. private String userBizBeanName = "userBiz";
  56. private String domainObjectFactoryBeanName = "domainObjectFactory";
  57. private ServletRequestDataBinderTemplate binderTemplate = null;
  58. private String successView = null;
  59. private String errorView = null;
  60. private DomainObjectFactory domainObjectFactory = null;
  61. private MessagesSource messagesSource = null;
  62. /**
  63. * Default constructor.
  64. */
  65. public AbstractEatCommandController() {
  66. super();
  67. }
  68. /**
  69. * Construct with a command class.
  70. *
  71. * @param commandClass the command class
  72. */
  73. public AbstractEatCommandController(Class<?> commandClass) {
  74. super(commandClass);
  75. }
  76. /**
  77. * Construct with a command class and name.
  78. *
  79. * @param commandClass the command class
  80. * @param commandName the command name
  81. */
  82. public AbstractEatCommandController(Class<?> commandClass, String commandName) {
  83. super(commandClass, commandName);
  84. }
  85. @Override
  86. protected void initApplicationContext() {
  87. super.initApplicationContext();
  88. if (recipeBiz == null) {
  89. recipeBiz = (RecipeBiz)getApplicationContext()
  90. .getBean(recipeBizBeanName);
  91. }
  92. if (recipeSearchBiz == null) {
  93. recipeSearchBiz = (RecipeSearchBiz)getApplicationContext()
  94. .getBean(recipeSearchBizBeanName);
  95. }
  96. if (userBiz == null) {
  97. userBiz = (UserBiz)getApplicationContext()
  98. .getBean(userBizBeanName);
  99. }
  100. if ( messagesSource == null ) {
  101. messagesSource = (MessagesSource)getApplicationContext()
  102. .getBean(messagesSourceBeanName);
  103. }
  104. if ( domainObjectFactory == null ) {
  105. domainObjectFactory = (DomainObjectFactory)getApplicationContext()
  106. .getBean(domainObjectFactoryBeanName);
  107. }
  108. String cmdName = StringUtil.trimToNull(getCommandName());
  109. if ( cmdName == null || cmdName.equals("command") ) {
  110. // default
  111. setCommandName(WebConstants.DEFALUT_MODEL_OBJECT);
  112. }
  113. }
  114. /**
  115. * Create a DataBinder object based on the <code>dataBinderClass</code> property.
  116. *
  117. * <p>If the <code>dataBinderClass</code> property is set, this method will
  118. * attempt to instantiate that class by calling a constructor with a method
  119. * signature of <code>ServletRequestDataBinder(Object,String,Map)</code>.
  120. * The Object and String passed into the constructor are the standard
  121. * command and command name objects normally passed to ServetRequestDataBinder
  122. * implementations. The Map argument will be the <code>dataBinderInitializerMap</code>
  123. * object configured in this controller instance.</p>
  124. */
  125. @Override
  126. protected ServletRequestDataBinder createBinder(HttpServletRequest request, Object command)
  127. throws Exception {
  128. if ( binderTemplate == null ) {
  129. return super.createBinder(request,command);
  130. }
  131. Constructor<?> c = binderTemplate.getClass().getConstructor(
  132. new Class[] {Object.class,String.class,DataBinder.class});
  133. ServletRequestDataBinder binder = (ServletRequestDataBinder)c.newInstance(
  134. new Object[] {command,getCommandName(),binderTemplate});
  135. if (getMessageCodesResolver() != null) {
  136. binder.setMessageCodesResolver(getMessageCodesResolver());
  137. }
  138. initBinder(request, binder);
  139. return binder;
  140. }
  141. /**
  142. * @return the recipeBiz
  143. */
  144. public RecipeBiz getRecipeBiz() {
  145. return recipeBiz;
  146. }
  147. /**
  148. * @param recipeBiz the recipeBiz to set
  149. */
  150. public void setRecipeBiz(RecipeBiz recipeBiz) {
  151. this.recipeBiz = recipeBiz;
  152. }
  153. /**
  154. * @return the recipeSearchBiz
  155. */
  156. public RecipeSearchBiz getRecipeSearchBiz() {
  157. return recipeSearchBiz;
  158. }
  159. /**
  160. * @param recipeSearchBiz the recipeSearchBiz to set
  161. */
  162. public void setRecipeSearchBiz(RecipeSearchBiz recipeSearchBiz) {
  163. this.recipeSearchBiz = recipeSearchBiz;
  164. }
  165. /**
  166. * @return the userBiz
  167. */
  168. public UserBiz getUserBiz() {
  169. return userBiz;
  170. }
  171. /**
  172. * @param userBiz the userBiz to set
  173. */
  174. public void setUserBiz(UserBiz userBiz) {
  175. this.userBiz = userBiz;
  176. }
  177. /**
  178. * @return the messagesSourceBeanName
  179. */
  180. public String getMessagesSourceBeanName() {
  181. return messagesSourceBeanName;
  182. }
  183. /**
  184. * @param messagesSourceBeanName the messagesSourceBeanName to set
  185. */
  186. public void setMessagesSourceBeanName(String messagesSourceBeanName) {
  187. this.messagesSourceBeanName = messagesSourceBeanName;
  188. }
  189. /**
  190. * @return the recipeBizBeanName
  191. */
  192. public String getRecipeBizBeanName() {
  193. return recipeBizBeanName;
  194. }
  195. /**
  196. * @param recipeBizBeanName the recipeBizBeanName to set
  197. */
  198. public void setRecipeBizBeanName(String recipeBizBeanName) {
  199. this.recipeBizBeanName = recipeBizBeanName;
  200. }
  201. /**
  202. * @return the recipeSearchBizBeanName
  203. */
  204. public String getRecipeSearchBizBeanName() {
  205. return recipeSearchBizBeanName;
  206. }
  207. /**
  208. * @param recipeSearchBizBeanName the recipeSearchBizBeanName to set
  209. */
  210. public void setRecipeSearchBizBeanName(String recipeSearchBizBeanName) {
  211. this.recipeSearchBizBeanName = recipeSearchBizBeanName;
  212. }
  213. /**
  214. * @return the userBizBeanName
  215. */
  216. public String getUserBizBeanName() {
  217. return userBizBeanName;
  218. }
  219. /**
  220. * @param userBizBeanName the userBizBeanName to set
  221. */
  222. public void setUserBizBeanName(String userBizBeanName) {
  223. this.userBizBeanName = userBizBeanName;
  224. }
  225. /**
  226. * @return the domainObjectFactoryBeanName
  227. */
  228. public String getDomainObjectFactoryBeanName() {
  229. return domainObjectFactoryBeanName;
  230. }
  231. /**
  232. * @param domainObjectFactoryBeanName the domainObjectFactoryBeanName to set
  233. */
  234. public void setDomainObjectFactoryBeanName(String domainObjectFactoryBeanName) {
  235. this.domainObjectFactoryBeanName = domainObjectFactoryBeanName;
  236. }
  237. /**
  238. * @return the binderTemplate
  239. */
  240. public ServletRequestDataBinderTemplate getBinderTemplate() {
  241. return binderTemplate;
  242. }
  243. /**
  244. * @param binderTemplate the binderTemplate to set
  245. */
  246. public void setBinderTemplate(ServletRequestDataBinderTemplate binderTemplate) {
  247. this.binderTemplate = binderTemplate;
  248. }
  249. /**
  250. * @return the successView
  251. */
  252. public String getSuccessView() {
  253. return successView;
  254. }
  255. /**
  256. * @param successView the successView to set
  257. */
  258. public void setSuccessView(String successView) {
  259. this.successView = successView;
  260. }
  261. /**
  262. * @return the errorView
  263. */
  264. public String getErrorView() {
  265. return errorView;
  266. }
  267. /**
  268. * @param errorView the errorView to set
  269. */
  270. public void setErrorView(String errorView) {
  271. this.errorView = errorView;
  272. }
  273. /**
  274. * @return the domainObjectFactory
  275. */
  276. public DomainObjectFactory getDomainObjectFactory() {
  277. return domainObjectFactory;
  278. }
  279. /**
  280. * @param domainObjectFactory the domainObjectFactory to set
  281. */
  282. public void setDomainObjectFactory(DomainObjectFactory domainObjectFactory) {
  283. this.domainObjectFactory = domainObjectFactory;
  284. }
  285. /**
  286. * @return the messagesSource
  287. */
  288. public MessagesSource getMessagesSource() {
  289. return messagesSource;
  290. }
  291. /**
  292. * @param messagesSource the messagesSource to set
  293. */
  294. public void setMessagesSource(MessagesSource messagesSource) {
  295. this.messagesSource = messagesSource;
  296. }
  297. }