ListManagerForm.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /* ===================================================================
  2. * ListManagerForm.java
  3. *
  4. * Created Sep 7, 2005 7:25:20 AM
  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: ListManagerForm.java 62 2009-05-12 07:37:44Z msqr $
  24. * ===================================================================
  25. */
  26. package magoffin.matt.ieat.web.setup;
  27. import java.util.ArrayList;
  28. import java.util.Collections;
  29. import java.util.Iterator;
  30. import java.util.LinkedHashMap;
  31. import java.util.LinkedHashSet;
  32. import java.util.List;
  33. import java.util.Locale;
  34. import java.util.Map;
  35. import java.util.Set;
  36. import java.util.regex.Pattern;
  37. import javax.servlet.http.HttpServletRequest;
  38. import javax.servlet.http.HttpServletResponse;
  39. import magoffin.matt.ieat.biz.DomainObjectFactory;
  40. import magoffin.matt.ieat.biz.UserBiz;
  41. import magoffin.matt.ieat.dao.BaseDao;
  42. import magoffin.matt.ieat.dao.CourseDao;
  43. import magoffin.matt.ieat.dao.DifficultyDao;
  44. import magoffin.matt.ieat.dao.EthnicityDao;
  45. import magoffin.matt.ieat.dao.IngredientDao;
  46. import magoffin.matt.ieat.dao.PrepTimeDao;
  47. import magoffin.matt.ieat.dao.RecipeDao;
  48. import magoffin.matt.ieat.domain.Base;
  49. import magoffin.matt.ieat.domain.Course;
  50. import magoffin.matt.ieat.domain.Difficulty;
  51. import magoffin.matt.ieat.domain.Ethnicity;
  52. import magoffin.matt.ieat.domain.Ingredient;
  53. import magoffin.matt.ieat.domain.PrepTime;
  54. import magoffin.matt.ieat.web.WebUtil;
  55. import magoffin.matt.xweb.XwebParameter;
  56. import magoffin.matt.xweb.util.DynamicInitializer;
  57. import magoffin.matt.xweb.util.DynamicInitializerRequestDataBinder;
  58. import magoffin.matt.xweb.util.XwebParamDao;
  59. import org.apache.log4j.Logger;
  60. import org.springframework.beans.BeanWrapper;
  61. import org.springframework.beans.BeanWrapperImpl;
  62. import org.springframework.util.StringUtils;
  63. import org.springframework.validation.BindException;
  64. import org.springframework.validation.Errors;
  65. import org.springframework.validation.Validator;
  66. import org.springframework.web.bind.ServletRequestDataBinder;
  67. import org.springframework.web.servlet.ModelAndView;
  68. import org.springframework.web.servlet.mvc.SimpleFormController;
  69. /**
  70. * Form controller for managing list data.
  71. *
  72. * @author Matt Magoffin (spamsqr@msqr.us)
  73. * @version $Revision: 62 $ $Date: 2009-05-12 19:37:44 +1200 (Tue, 12 May 2009) $
  74. */
  75. public class ListManagerForm extends SimpleFormController {
  76. /** The original value prefix. */
  77. public static final String ORIGINAL_VALUE_PREFIX = "_orig_";
  78. /** A key for deletion. */
  79. public static final String DELETE_KEY = "_delete_";
  80. /** Domain class. */
  81. public static class PropertyMetaData {
  82. private boolean primaryKey = false;
  83. private String name = null;
  84. private String displayName = null;
  85. /** Type of property: 1 = String, 2 = number, 3 = boolean */
  86. private int type = 1;
  87. private int length = 0;
  88. /**
  89. * @return the displayName
  90. */
  91. public String getDisplayName() {
  92. return displayName;
  93. }
  94. /**
  95. * @param displayName the displayName to set
  96. */
  97. public void setDisplayName(String displayName) {
  98. this.displayName = displayName;
  99. }
  100. /**
  101. * @return the length
  102. */
  103. public int getLength() {
  104. return length;
  105. }
  106. /**
  107. * @param length the length to set
  108. */
  109. public void setLength(int length) {
  110. this.length = length;
  111. }
  112. /**
  113. * @return the name
  114. */
  115. public String getName() {
  116. return name;
  117. }
  118. /**
  119. * @param name the name to set
  120. */
  121. public void setName(String name) {
  122. this.name = name;
  123. }
  124. /**
  125. * @return the primaryKey
  126. */
  127. public boolean isPrimaryKey() {
  128. return primaryKey;
  129. }
  130. /**
  131. * @param primaryKey the primaryKey to set
  132. */
  133. public void setPrimaryKey(boolean primaryKey) {
  134. this.primaryKey = primaryKey;
  135. }
  136. /**
  137. * @return the type
  138. */
  139. public int getType() {
  140. return type;
  141. }
  142. /**
  143. * @param type the type to set
  144. */
  145. public void setType(int type) {
  146. this.type = type;
  147. }
  148. }
  149. /** Metadata about our list of beans. */
  150. public static class ListMetaData {
  151. private PropertyMetaData[] properties;
  152. /**
  153. * @return the properties
  154. */
  155. public PropertyMetaData[] getProperties() {
  156. return properties;
  157. }
  158. /**
  159. * @param properties the properties to set
  160. */
  161. public void setProperties(PropertyMetaData[] properties) {
  162. this.properties = properties;
  163. }
  164. }
  165. /** The command object. */
  166. public static class ListCommand {
  167. private Class<?> beanClass = null;
  168. private Integer reassignPrimaryKey = null;
  169. private List<Map<String, Object>> beans = new ArrayList<Map<String,Object>>();
  170. private Map<String, Object> newBean = new LinkedHashMap<String, Object>();
  171. /**
  172. * @return the beanClass
  173. */
  174. public Class<?> getBeanClass() {
  175. return beanClass;
  176. }
  177. /**
  178. * @param beanClass the beanClass to set
  179. */
  180. public void setBeanClass(Class<?> beanClass) {
  181. this.beanClass = beanClass;
  182. }
  183. /**
  184. * @return the beans
  185. */
  186. public List<Map<String, Object>> getBeans() {
  187. return beans;
  188. }
  189. /**
  190. * @param beans the beans to set
  191. */
  192. public void setBeans(List<Map<String, Object>> beans) {
  193. this.beans = beans;
  194. }
  195. /**
  196. * @return the newBean
  197. */
  198. public Map<String, Object> getNewBean() {
  199. return newBean;
  200. }
  201. /**
  202. * @param newBean the newBean to set
  203. */
  204. public void setNewBean(Map<String, Object> newBean) {
  205. this.newBean = newBean;
  206. }
  207. /**
  208. * @return the reassignPrimaryKey
  209. */
  210. public Integer getReassignPrimaryKey() {
  211. return reassignPrimaryKey;
  212. }
  213. /**
  214. * @param reassignPrimaryKey the reassignPrimaryKey to set
  215. */
  216. public void setReassignPrimaryKey(Integer reassignPrimaryKey) {
  217. this.reassignPrimaryKey = reassignPrimaryKey;
  218. }
  219. }
  220. /** A dynamic initializer. */
  221. public static class ListInitializer implements DynamicInitializer {
  222. /* (non-Javadoc)
  223. * @see magoffin.matt.ieat.util.DynamicInitializer#newInstance(java.lang.Object, java.lang.String)
  224. */
  225. public Object newInstance(Object bean, String property) {
  226. return new LinkedHashMap<String, Object>();
  227. }
  228. }
  229. /** A validator. */
  230. public class ListValidator implements Validator {
  231. /* (non-Javadoc)
  232. * @see org.springframework.validation.Validator#supports(java.lang.Class)
  233. */
  234. @SuppressWarnings("unchecked")
  235. public boolean supports(Class clazz) {
  236. return ListCommand.class.isAssignableFrom(clazz);
  237. }
  238. /* (non-Javadoc)
  239. * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
  240. */
  241. public void validate(Object obj, Errors errors) {
  242. // verify that if any item marked for deletion, reassigned properly
  243. ListCommand listCmd = (ListCommand)obj;
  244. if ( listCmd.beans == null ) return;
  245. Set<Object> itemsToDelete = new LinkedHashSet<Object>();
  246. PropertyMetaData primaryKeyProp = getPrimaryKeyProperty(
  247. meta.get(listCmd.beanClass.getName()));
  248. for ( Iterator<Map<String, Object>> itr = listCmd.beans.iterator(); itr.hasNext(); ) {
  249. Map<String, Object> beanMap = itr.next();
  250. if ( beanMap.containsKey(DELETE_KEY) ) {
  251. itemsToDelete.add(beanMap.get(primaryKeyProp.name));
  252. }
  253. }
  254. if ( itemsToDelete.size() > 0 ) {
  255. if ( listCmd.reassignPrimaryKey == null ) {
  256. errors.reject("lists.reassign.required","Reassign item required.");
  257. } else if ( itemsToDelete.contains(listCmd.reassignPrimaryKey.toString()) ) {
  258. errors.reject("lists.reassign.illegal","Can't reassign to an item to delete.");
  259. }
  260. }
  261. }
  262. }
  263. private Map<String, ListMetaData> meta = null; // map of Class name to ListMetaData
  264. private RecipeDao recipeDao = null;
  265. private BaseDao baseDao = null;
  266. private CourseDao courseDao = null;
  267. private DifficultyDao difficultyDao = null;
  268. private EthnicityDao ethnicityDao = null;
  269. private IngredientDao ingredientDao = null;
  270. private PrepTimeDao prepTimeDao = null;
  271. private XwebParamDao parameterDao = null;
  272. private UserBiz userBiz = null;
  273. private DomainObjectFactory domainObjectFactory = null;
  274. private final Logger log = Logger.getLogger(getClass());
  275. private static final Pattern MSG_FORMAT = Pattern.compile("\\W");
  276. @Override
  277. protected void initApplicationContext() {
  278. setValidator(new ListValidator());
  279. }
  280. @Override
  281. protected ServletRequestDataBinder createBinder(HttpServletRequest request, Object command)
  282. throws Exception {
  283. Map<String, DynamicInitializer> dynamicMap
  284. = new LinkedHashMap<String, DynamicInitializer>();
  285. dynamicMap.put("beans",new ListInitializer());
  286. DynamicInitializerRequestDataBinder binder =
  287. new DynamicInitializerRequestDataBinder(command,getCommandName(),
  288. dynamicMap);
  289. if (getMessageCodesResolver() != null) {
  290. binder.setMessageCodesResolver(getMessageCodesResolver());
  291. }
  292. initBinder(request, binder);
  293. return binder;
  294. }
  295. @SuppressWarnings("unchecked")
  296. @Override
  297. protected ModelAndView onSubmit(HttpServletRequest request,
  298. HttpServletResponse response, Object command, BindException errors)
  299. throws Exception {
  300. WebUtil.getAdminBizContext(request,userBiz);
  301. ListCommand listCmd = (ListCommand)command;
  302. ListMetaData lmd = meta.get(listCmd.beanClass.getName());
  303. PropertyMetaData primaryKeyProp = getPrimaryKeyProperty(lmd);
  304. int updateCount = 0;
  305. // for any changed rows, update in DB
  306. for ( Iterator itr = listCmd.beans.iterator(); itr.hasNext(); ) {
  307. Map beanMap = (Map)itr.next();
  308. boolean changed = false;
  309. String pkValue = (String)beanMap.get(primaryKeyProp.name);
  310. if ( beanMap.containsKey(DELETE_KEY) ) {
  311. deleteBean(beanMap,request.getLocale(),listCmd.beanClass,
  312. pkValue,listCmd.reassignPrimaryKey);
  313. updateCount++;
  314. continue;
  315. }
  316. BeanWrapper bean = getBean(listCmd.beanClass,pkValue);
  317. for ( Iterator beanMapItr = beanMap.keySet().iterator(); beanMapItr.hasNext(); ) {
  318. String propName = (String)beanMapItr.next();
  319. if ( propName.startsWith("_") || propName.equals(primaryKeyProp.name)) {
  320. continue; // skip internal values and primary key
  321. }
  322. String propValue = (String)beanMap.get(propName);
  323. String propOrigValue = (String)beanMap.get(ORIGINAL_VALUE_PREFIX+propName);
  324. if ( !propValue.equals(propOrigValue) ) {
  325. changed = true;
  326. if ( log.isDebugEnabled() ) {
  327. log.debug("Changing property [" +propName +"] to ["
  328. +propValue +"] on bean ["
  329. +listCmd.beanClass.getName() +"]");
  330. }
  331. bean.setPropertyValue(propName,propValue);
  332. }
  333. }
  334. if ( changed ) {
  335. saveBean(bean.getWrappedInstance(),beanMap,request.getLocale());
  336. updateCount++;
  337. }
  338. }
  339. // see if a new bean has been added
  340. boolean hasNew = false;
  341. BeanWrapper bean = getBean(listCmd.beanClass,null);
  342. for ( int i = 0; i < lmd.properties.length; i++ ) {
  343. PropertyMetaData pmd = lmd.properties[i];
  344. if ( pmd.primaryKey ) continue;
  345. String propValue = (String)listCmd.newBean.get(pmd.name);
  346. if ( StringUtils.hasText(propValue) ) {
  347. hasNew = true;
  348. }
  349. bean.setPropertyValue(pmd.name,propValue);
  350. }
  351. if ( hasNew ) {
  352. saveBean(bean.getWrappedInstance(),listCmd.newBean,request.getLocale());
  353. updateCount++;
  354. }
  355. Map<String, Object> model = errors.getModel();
  356. Map<String, Object> refData = referenceData(request,command,errors);
  357. model.putAll(refData);
  358. if ( updateCount > 0 ) {
  359. model.put("updateCount", updateCount);
  360. }
  361. return new ModelAndView(getSuccessView(), model);
  362. }
  363. private void deleteBean(Map<String, Object> beanMap, Locale locale, Class<?> beanClass,
  364. String pkValue, Integer reassignPrimaryKey) {
  365. Integer pk = Integer.valueOf(pkValue);
  366. String removeSettingKey = null;
  367. if ( Base.class.isAssignableFrom(beanClass) ) {
  368. recipeDao.reassignBase(pk,reassignPrimaryKey);
  369. baseDao.delete(baseDao.get(pk));
  370. String origValueKey = ORIGINAL_VALUE_PREFIX +"value";
  371. if ( beanMap.containsKey(origValueKey) ) {
  372. removeSettingKey = "msg:base." +locale.getLanguage() +":"
  373. +MSG_FORMAT.matcher(
  374. (String)beanMap.get(origValueKey)).replaceAll("");
  375. }
  376. } else if ( Course.class.isAssignableFrom(beanClass) ) {
  377. recipeDao.reassignCourse(pk,reassignPrimaryKey);
  378. courseDao.delete(courseDao.get(pk));
  379. String origValueKey = ORIGINAL_VALUE_PREFIX +"value";
  380. if ( beanMap.containsKey(origValueKey) ) {
  381. removeSettingKey = "msg:course." +locale.getLanguage() +":"
  382. +MSG_FORMAT.matcher(
  383. (String)beanMap.get(origValueKey)).replaceAll("");
  384. }
  385. } else if ( Difficulty.class.isAssignableFrom(beanClass) ) {
  386. recipeDao.reassignDifficulty(pk,reassignPrimaryKey);
  387. difficultyDao.delete(difficultyDao.get(pk));
  388. String origValueKey = ORIGINAL_VALUE_PREFIX +"value";
  389. if ( beanMap.containsKey(origValueKey) ) {
  390. removeSettingKey = "msg:difficulty." +locale.getLanguage() +":"
  391. +MSG_FORMAT.matcher(
  392. (String)beanMap.get(origValueKey)).replaceAll("");
  393. }
  394. } else if ( Ethnicity.class.isAssignableFrom(beanClass) ) {
  395. recipeDao.reassignEthnicity(pk,reassignPrimaryKey);
  396. ethnicityDao.delete(ethnicityDao.get(pk));
  397. String origValueKey = ORIGINAL_VALUE_PREFIX +"value";
  398. if ( beanMap.containsKey(origValueKey) ) {
  399. removeSettingKey = "msg:ethnicity." +locale.getLanguage() +":"
  400. +MSG_FORMAT.matcher(
  401. (String)beanMap.get(origValueKey)).replaceAll("");
  402. }
  403. } else if ( Ingredient.class.isAssignableFrom(beanClass) ) {
  404. recipeDao.reassignIngredient(pk,reassignPrimaryKey);
  405. ingredientDao.delete(ingredientDao.get(pk));
  406. String origValueKey = ORIGINAL_VALUE_PREFIX +"name";
  407. if ( beanMap.containsKey(origValueKey) ) {
  408. removeSettingKey = "msg:ingredient." +locale.getLanguage() +":"
  409. +MSG_FORMAT.matcher(
  410. (String)beanMap.get(origValueKey)).replaceAll("");
  411. }
  412. } else if ( PrepTime.class.isAssignableFrom(beanClass) ) {
  413. recipeDao.reassignPrepTime(pk,reassignPrimaryKey);
  414. prepTimeDao.delete(prepTimeDao.get(pk));
  415. String origValueKey = ORIGINAL_VALUE_PREFIX +"value";
  416. if ( beanMap.containsKey(origValueKey) ) {
  417. removeSettingKey = "msg:course." +locale.getLanguage() +":"
  418. +MSG_FORMAT.matcher(
  419. (String)beanMap.get(origValueKey)).replaceAll("");
  420. }
  421. }
  422. if ( removeSettingKey != null ) {
  423. parameterDao.removeParameter(removeSettingKey);
  424. }
  425. }
  426. private void saveBean(Object bean, Map<String, Object> beanMap, Locale locale) {
  427. String settingKey = "msg:" +locale.getLanguage() +":";
  428. String origSettingKey = "msg:" +locale.getLanguage() +":";
  429. String settingValue = null;
  430. if ( bean instanceof Base ) {
  431. Base base = (Base)bean;
  432. baseDao.store(base);
  433. settingKey += "base." +MSG_FORMAT.matcher(base.getValue()).replaceAll("");
  434. settingValue = base.getValue();
  435. String origValueKey = ORIGINAL_VALUE_PREFIX +"value";
  436. if ( beanMap.containsKey(origValueKey) ) {
  437. origSettingKey += "base." +MSG_FORMAT.matcher(
  438. (String)beanMap.get(origValueKey)).replaceAll("");
  439. }
  440. } else if ( bean instanceof Course ) {
  441. Course course = (Course)bean;
  442. courseDao.store(course);
  443. settingKey += "course." +MSG_FORMAT.matcher(course.getValue()).replaceAll("");
  444. settingValue = course.getValue();
  445. String origValueKey = ORIGINAL_VALUE_PREFIX +"value";
  446. if ( beanMap.containsKey(origValueKey) ) {
  447. origSettingKey += "course." +MSG_FORMAT.matcher(
  448. (String)beanMap.get(origValueKey)).replaceAll("");
  449. }
  450. } else if ( bean instanceof Difficulty ) {
  451. Difficulty difficulty = (Difficulty)bean;
  452. difficultyDao.store(difficulty);
  453. settingKey += "difficulty." +MSG_FORMAT.matcher(difficulty.getValue()).replaceAll("");
  454. settingValue = difficulty.getValue();
  455. String origValueKey = ORIGINAL_VALUE_PREFIX +"value";
  456. if ( beanMap.containsKey(origValueKey) ) {
  457. origSettingKey += "difficulty." +MSG_FORMAT.matcher(
  458. (String)beanMap.get(origValueKey)).replaceAll("");
  459. }
  460. } else if ( bean instanceof Ethnicity ) {
  461. Ethnicity ethnicity = (Ethnicity)bean;
  462. ethnicityDao.store(ethnicity);
  463. settingKey += "ethnicity." +MSG_FORMAT.matcher(ethnicity.getValue()).replaceAll("");
  464. settingValue = ethnicity.getValue();
  465. String origValueKey = ORIGINAL_VALUE_PREFIX +"value";
  466. if ( beanMap.containsKey(origValueKey) ) {
  467. origSettingKey += "ethnicity." +MSG_FORMAT.matcher(
  468. (String)beanMap.get(origValueKey)).replaceAll("");
  469. }
  470. } else if ( bean instanceof Ingredient ) {
  471. Ingredient ingredient = (Ingredient)bean;
  472. ingredientDao.store(ingredient);
  473. settingKey += "ingredient." +MSG_FORMAT.matcher(ingredient.getName()).replaceAll("");
  474. settingValue = ingredient.getName();
  475. String origValueKey = ORIGINAL_VALUE_PREFIX +"name";
  476. if ( beanMap.containsKey(origValueKey) ) {
  477. origSettingKey += "ingredient." +MSG_FORMAT.matcher(
  478. (String)beanMap.get(origValueKey)).replaceAll("");
  479. }
  480. } else if ( bean instanceof PrepTime ) {
  481. PrepTime prepTime = (PrepTime)bean;
  482. prepTimeDao.store(prepTime);
  483. settingKey += "prepTime." +MSG_FORMAT.matcher(prepTime.getValue()).replaceAll("");
  484. settingValue = prepTime.getValue();
  485. String origValueKey = ORIGINAL_VALUE_PREFIX +"value";
  486. if ( beanMap.containsKey(origValueKey) ) {
  487. origSettingKey += "prepTime." +MSG_FORMAT.matcher(
  488. (String)beanMap.get(origValueKey)).replaceAll("");
  489. }
  490. }
  491. // remove old setting key (if changed)
  492. if ( !origSettingKey.equals(settingKey) ) {
  493. parameterDao.removeParameter(origSettingKey);
  494. }
  495. XwebParameter setting = this.domainObjectFactory.newXwebParameterInstance();
  496. setting.setKey(settingKey);
  497. setting.setValue(settingValue);
  498. parameterDao.updateParameter(setting);
  499. }
  500. private BeanWrapper getBean(Class<?> beanClass, String pkValue) {
  501. BeanWrapper result = null;
  502. if ( StringUtils.hasText(pkValue) ) {
  503. Integer pk = Integer.valueOf(pkValue);
  504. if ( Base.class.isAssignableFrom(beanClass) ) {
  505. result = new BeanWrapperImpl(baseDao.get(pk));
  506. } else if ( Course.class.isAssignableFrom(beanClass) ) {
  507. result = new BeanWrapperImpl(courseDao.get(pk));
  508. } else if ( Difficulty.class.isAssignableFrom(beanClass) ) {
  509. result = new BeanWrapperImpl(difficultyDao.get(pk));
  510. } else if ( Ethnicity.class.isAssignableFrom(beanClass) ) {
  511. result = new BeanWrapperImpl(ethnicityDao.get(pk));
  512. } else if ( Ingredient.class.isAssignableFrom(beanClass) ) {
  513. result = new BeanWrapperImpl(ingredientDao.get(pk));
  514. } else if ( PrepTime.class.isAssignableFrom(beanClass)) {
  515. result = new BeanWrapperImpl(prepTimeDao.get(pk));
  516. }
  517. } else {
  518. try {
  519. Object o = beanClass.newInstance();
  520. result = new BeanWrapperImpl(o);
  521. } catch (Exception e) {
  522. throw new RuntimeException("Unable to instantiate bean",e);
  523. }
  524. }
  525. return result;
  526. }
  527. private PropertyMetaData getPrimaryKeyProperty(ListMetaData lmd) {
  528. for ( int i = 0; i < lmd.properties.length; i++ ) {
  529. if ( lmd.properties[i].primaryKey ) {
  530. return lmd.properties[i];
  531. }
  532. }
  533. throw new IllegalArgumentException("No primary key found for ListMetaData");
  534. }
  535. @SuppressWarnings("unchecked")
  536. @Override
  537. protected Map referenceData(HttpServletRequest request, Object command,
  538. Errors errors) throws Exception {
  539. WebUtil.getAdminBizContext(request,userBiz);
  540. ListCommand listCmd = (ListCommand)command;
  541. // pull data from backend DB
  542. if ( listCmd.beanClass == null ) {
  543. return null;
  544. }
  545. List beanList = Collections.EMPTY_LIST;
  546. if ( Base.class.isAssignableFrom(listCmd.beanClass) ) {
  547. beanList = baseDao.getBases();
  548. } else if ( Course.class.isAssignableFrom(listCmd.beanClass) ) {
  549. beanList = courseDao.getCourses();
  550. } else if ( Difficulty.class.isAssignableFrom(listCmd.beanClass) ) {
  551. beanList = difficultyDao.getDifficulties();
  552. } else if ( Ethnicity.class.isAssignableFrom(listCmd.beanClass) ) {
  553. beanList = ethnicityDao.getEthnicities();
  554. } else if ( Ingredient.class.isAssignableFrom(listCmd.beanClass) ) {
  555. beanList = ingredientDao.getIngredients();
  556. } else if ( PrepTime.class.isAssignableFrom(listCmd.beanClass) ) {
  557. beanList = prepTimeDao.getPrepTimes();
  558. }
  559. listCmd.beans = new ArrayList<Map<String,Object>>(beanList.size()*2);
  560. for ( Iterator itr = beanList.iterator(); itr.hasNext(); ) {
  561. Object bean = itr.next();
  562. BeanWrapper wrapper = new BeanWrapperImpl(bean);
  563. Map<String, Object> beanMap = new LinkedHashMap<String, Object>();
  564. listCmd.beans.add(beanMap);
  565. ListMetaData lmd = this.meta.get(listCmd.beanClass.getName());
  566. for ( int j = 0; j < lmd.properties.length; j++ ) {
  567. Object value = wrapper.getPropertyValue(lmd.properties[j].name);
  568. value = value == null ? "" : value.toString();
  569. String name = lmd.properties[j].name;
  570. beanMap.put(name,value);
  571. beanMap.put(ORIGINAL_VALUE_PREFIX+name,value);
  572. }
  573. }
  574. Map<String, Object> data = new LinkedHashMap<String, Object>();
  575. data.put("meta",this.meta);
  576. return data;
  577. }
  578. /**
  579. * @return the baseDao
  580. */
  581. public BaseDao getBaseDao() {
  582. return baseDao;
  583. }
  584. /**
  585. * @param baseDao the baseDao to set
  586. */
  587. public void setBaseDao(BaseDao baseDao) {
  588. this.baseDao = baseDao;
  589. }
  590. /**
  591. * @return the courseDao
  592. */
  593. public CourseDao getCourseDao() {
  594. return courseDao;
  595. }
  596. /**
  597. * @param courseDao the courseDao to set
  598. */
  599. public void setCourseDao(CourseDao courseDao) {
  600. this.courseDao = courseDao;
  601. }
  602. /**
  603. * @return the difficultyDao
  604. */
  605. public DifficultyDao getDifficultyDao() {
  606. return difficultyDao;
  607. }
  608. /**
  609. * @param difficultyDao the difficultyDao to set
  610. */
  611. public void setDifficultyDao(DifficultyDao difficultyDao) {
  612. this.difficultyDao = difficultyDao;
  613. }
  614. /**
  615. * @return the domainObjectFactory
  616. */
  617. public DomainObjectFactory getDomainObjectFactory() {
  618. return domainObjectFactory;
  619. }
  620. /**
  621. * @param domainObjectFactory the domainObjectFactory to set
  622. */
  623. public void setDomainObjectFactory(DomainObjectFactory domainObjectFactory) {
  624. this.domainObjectFactory = domainObjectFactory;
  625. }
  626. /**
  627. * @return the ethnicityDao
  628. */
  629. public EthnicityDao getEthnicityDao() {
  630. return ethnicityDao;
  631. }
  632. /**
  633. * @param ethnicityDao the ethnicityDao to set
  634. */
  635. public void setEthnicityDao(EthnicityDao ethnicityDao) {
  636. this.ethnicityDao = ethnicityDao;
  637. }
  638. /**
  639. * @return the ingredientDao
  640. */
  641. public IngredientDao getIngredientDao() {
  642. return ingredientDao;
  643. }
  644. /**
  645. * @param ingredientDao the ingredientDao to set
  646. */
  647. public void setIngredientDao(IngredientDao ingredientDao) {
  648. this.ingredientDao = ingredientDao;
  649. }
  650. /**
  651. * @return the meta
  652. */
  653. public Map<String, ListMetaData> getMeta() {
  654. return meta;
  655. }
  656. /**
  657. * @param meta the meta to set
  658. */
  659. public void setMeta(Map<String, ListMetaData> meta) {
  660. this.meta = meta;
  661. }
  662. /**
  663. * @return the prepTimeDao
  664. */
  665. public PrepTimeDao getPrepTimeDao() {
  666. return prepTimeDao;
  667. }
  668. /**
  669. * @param prepTimeDao the prepTimeDao to set
  670. */
  671. public void setPrepTimeDao(PrepTimeDao prepTimeDao) {
  672. this.prepTimeDao = prepTimeDao;
  673. }
  674. /**
  675. * @return the recipeDao
  676. */
  677. public RecipeDao getRecipeDao() {
  678. return recipeDao;
  679. }
  680. /**
  681. * @param recipeDao the recipeDao to set
  682. */
  683. public void setRecipeDao(RecipeDao recipeDao) {
  684. this.recipeDao = recipeDao;
  685. }
  686. /**
  687. * @return the userBiz
  688. */
  689. public UserBiz getUserBiz() {
  690. return userBiz;
  691. }
  692. /**
  693. * @param userBiz the userBiz to set
  694. */
  695. public void setUserBiz(UserBiz userBiz) {
  696. this.userBiz = userBiz;
  697. }
  698. /**
  699. * @return the parameterDao
  700. */
  701. public XwebParamDao getParameterDao() {
  702. return parameterDao;
  703. }
  704. /**
  705. * @param parameterDao the parameterDao to set
  706. */
  707. public void setParameterDao(XwebParamDao parameterDao) {
  708. this.parameterDao = parameterDao;
  709. }
  710. }