RecipeDaoImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /* ===================================================================
  2. * RecipeDaoImpl.java
  3. *
  4. * Created Aug 4, 2004 10:36:11 AM
  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: RecipeDaoImpl.java 41 2009-05-06 23:34:29Z msqr $
  24. * ===================================================================
  25. */
  26. package magoffin.matt.ieat.dao.hbm;
  27. import java.sql.ResultSet;
  28. import java.sql.SQLException;
  29. import java.util.List;
  30. import magoffin.matt.dao.hbm.GenericHibernateDao;
  31. import magoffin.matt.ieat.biz.DomainObjectFactory;
  32. import magoffin.matt.ieat.dao.RecipeDao;
  33. import magoffin.matt.ieat.domain.Base;
  34. import magoffin.matt.ieat.domain.Course;
  35. import magoffin.matt.ieat.domain.Difficulty;
  36. import magoffin.matt.ieat.domain.Ethnicity;
  37. import magoffin.matt.ieat.domain.PrepTime;
  38. import magoffin.matt.ieat.domain.Recipe;
  39. import magoffin.matt.ieat.domain.RecipeIngredient;
  40. import magoffin.matt.ieat.domain.UiSearchResults;
  41. import magoffin.matt.ieat.domain.impl.RecipeImpl;
  42. import magoffin.matt.util.StringUtil;
  43. import org.hibernate.HibernateException;
  44. import org.hibernate.Query;
  45. import org.hibernate.Session;
  46. import org.springframework.jdbc.core.JdbcTemplate;
  47. import org.springframework.jdbc.core.RowCallbackHandler;
  48. import org.springframework.orm.hibernate3.HibernateCallback;
  49. /**
  50. * RecipeDao implementation using Hibernate.
  51. *
  52. * @author Matt Magoffin (spamsqr@msqr.us)
  53. * @version $Revision: 41 $ $Date: 2009-05-07 11:34:29 +1200 (Thu, 07 May 2009) $
  54. */
  55. public class RecipeDaoImpl extends GenericHibernateDao<Recipe, Long>
  56. implements RecipeDao {
  57. /** The query to find all Recipe objects. */
  58. public static final String FIND_ALL = "RecipeAll";
  59. /** The query to find all Recipe objects. */
  60. public static final String FIND_USED_AS_INGREDIENT = "RecipeUsedAsIngredient";
  61. /** The query to find the count of all Recipe objects. */
  62. public static final String FIND_ALL_COUNT = "RecipeAllCount";
  63. private JdbcTemplate jdbcTemplate = null;
  64. private String sqlIndexAll = null;
  65. private String sqlReassignBase = null;
  66. private String sqlReassignCourse = null;
  67. private String sqlReassignDifficulty = null;
  68. private String sqlReassignEthnicity = null;
  69. private String sqlReassignIngredient = null;
  70. private String sqlReassignPrepTime = null;
  71. private DomainObjectFactory domainObjectFactory = null;
  72. /**
  73. * Default constructor.
  74. */
  75. public RecipeDaoImpl() {
  76. super(RecipeImpl.class);
  77. }
  78. /**
  79. * Method to call after all dependency injection has occured.
  80. */
  81. public void init() {
  82. if ( jdbcTemplate == null ) {
  83. throw new RuntimeException("jdbcTemplate not configured");
  84. }
  85. if ( sqlIndexAll == null ) {
  86. throw new RuntimeException("sqlIndexAll not configured");
  87. }
  88. }
  89. @Override
  90. protected Long getPrimaryKey(Recipe domainObject) {
  91. if ( domainObject == null ) return null;
  92. return domainObject.getRecipeId();
  93. }
  94. /* (non-Javadoc)
  95. * @see magoffin.matt.ieat.dao.RecipeDao#findRecipesUsingRecipeAsIngredient(java.lang.Long)
  96. */
  97. public List<Recipe> findRecipesUsingRecipeAsIngredient(Long recipeId) {
  98. return findByNamedQuery(FIND_USED_AS_INGREDIENT, new Object[] {recipeId});
  99. }
  100. /* (non-Javadoc)
  101. * @see magoffin.matt.ieat.dao.RecipeDao#getAllRecipes(magoffin.matt.ieat.domain.UiPaginationSupport)
  102. */
  103. @SuppressWarnings({ "unchecked" })
  104. public UiSearchResults getAllRecipes(int pageSize, int pageOffset) {
  105. UiSearchResults results = domainObjectFactory.getEmbeddedSearchResultsInstance();
  106. if ( pageSize < 1 ) {
  107. // return all available
  108. List<Recipe> allRecipes = findByNamedQuery(FIND_ALL);
  109. results.getRecipe().addAll(allRecipes);
  110. results.setTotalResults(allRecipes.size());
  111. return results;
  112. }
  113. Integer count = (Integer)getHibernateTemplate().execute(new HibernateCallback() {
  114. public Object doInHibernate(Session session) throws HibernateException, SQLException {
  115. Query query = session.getNamedQuery(FIND_ALL_COUNT);
  116. Number result = (Number)query.iterate().next();
  117. return result.intValue();
  118. }
  119. });
  120. List<Recipe> pagedList = findByNamedQuery(FIND_ALL, (Object[])null,
  121. pageOffset, pageSize);
  122. results.getRecipe().addAll(pagedList);
  123. results.setTotalResults(count);
  124. return results;
  125. }
  126. /* (non-Javadoc)
  127. * @see magoffin.matt.ieat.dao.RecipeDao#index(magoffin.matt.ieat.dao.RecipeDao.RecipeIndexCallback)
  128. */
  129. public void index(final RecipeIndexCallback callback) {
  130. if ( log.isDebugEnabled() ) {
  131. log.debug("Executing SQL for index all recipes: " +sqlIndexAll);
  132. }
  133. final RecipeIndexCallbackDataImpl callbackData = new RecipeIndexCallbackDataImpl();
  134. jdbcTemplate.query(sqlIndexAll,
  135. new RowCallbackHandler() {
  136. private ResultSet myRs;
  137. public void processRow(ResultSet rs) throws SQLException {
  138. if ( myRs == null ) {
  139. myRs = rs;
  140. }
  141. callbackData.recipeId = Long.valueOf(rs.getLong(1));
  142. callbackData.name = rs.getString(2);
  143. callbackData.excerpt = rs.getString(3);
  144. callbackData.directions = rs.getString(4);
  145. callbackData.step = rs.getString(5);
  146. int aInt = rs.getInt(6);
  147. if ( !rs.wasNull() ) {
  148. callbackData.ingredientId = aInt;
  149. } else {
  150. callbackData.ingredientId = null;
  151. }
  152. aInt = rs.getInt(7);
  153. if ( !rs.wasNull() ) {
  154. short aShort = rs.getShort(8);
  155. if ( !rs.wasNull() ) {
  156. callbackData.ratingUserId = aInt;
  157. callbackData.rating = aShort;
  158. } else {
  159. callbackData.ratingUserId = null;
  160. callbackData.rating = null;
  161. }
  162. } else {
  163. callbackData.ratingUserId = null;
  164. callbackData.rating = null;
  165. }
  166. aInt = rs.getInt(9);
  167. if ( !rs.wasNull() ) {
  168. callbackData.ownerId = aInt;
  169. } else {
  170. callbackData.ownerId = null;
  171. }
  172. callback.handle(callbackData);
  173. }
  174. }
  175. );
  176. callback.finish();
  177. }
  178. /* (non-Javadoc)
  179. * @see magoffin.matt.ieat.dao.RecipeDao#reassignBase(java.lang.Integer, java.lang.Integer)
  180. */
  181. public void reassignBase(Integer baseId, Integer newBaseId) {
  182. getHibernateTemplate().flush();
  183. jdbcTemplate.update(this.sqlReassignBase, new Object[] {newBaseId,baseId});
  184. try {
  185. Recipe r = domainObjectFactory.getRecipeInstance();
  186. getHibernateTemplate().getSessionFactory().evict(r.getClass());
  187. Base b = domainObjectFactory.getBaseInstance();
  188. getHibernateTemplate().getSessionFactory().evict(b.getClass());
  189. } catch ( HibernateException e ) {
  190. throw new RuntimeException(e);
  191. }
  192. }
  193. /* (non-Javadoc)
  194. * @see magoffin.matt.ieat.dao.RecipeDao#reassignCourse(java.lang.Integer, java.lang.Integer)
  195. */
  196. public void reassignCourse(Integer courseId, Integer newCourseId) {
  197. getHibernateTemplate().flush();
  198. jdbcTemplate.update(this.sqlReassignCourse, new Object[] {newCourseId,courseId});
  199. try {
  200. Recipe r = domainObjectFactory.getRecipeInstance();
  201. getHibernateTemplate().getSessionFactory().evict(r.getClass());
  202. Course c = domainObjectFactory.getCourseInstance();
  203. getHibernateTemplate().getSessionFactory().evict(c.getClass());
  204. } catch ( HibernateException e ) {
  205. throw new RuntimeException(e);
  206. }
  207. }
  208. /* (non-Javadoc)
  209. * @see magoffin.matt.ieat.dao.RecipeDao#reassignDifficulty(java.lang.Integer, java.lang.Integer)
  210. */
  211. public void reassignDifficulty(Integer difficultyId, Integer newDifficultyId) {
  212. getHibernateTemplate().flush();
  213. jdbcTemplate.update(this.sqlReassignDifficulty, new Object[] {newDifficultyId,difficultyId});
  214. try {
  215. Recipe r = domainObjectFactory.getRecipeInstance();
  216. getHibernateTemplate().getSessionFactory().evict(r.getClass());
  217. Difficulty d = domainObjectFactory.getDifficultyInstance();
  218. getHibernateTemplate().getSessionFactory().evict(d.getClass());
  219. } catch ( HibernateException e ) {
  220. throw new RuntimeException(e);
  221. }
  222. }
  223. /* (non-Javadoc)
  224. * @see magoffin.matt.ieat.dao.RecipeDao#reassignEthnicity(java.lang.Integer, java.lang.Integer)
  225. */
  226. public void reassignEthnicity(Integer ethnicityId, Integer newEthnicityId) {
  227. getHibernateTemplate().flush();
  228. jdbcTemplate.update(this.sqlReassignEthnicity, new Object[] {newEthnicityId,ethnicityId});
  229. try {
  230. Recipe r = domainObjectFactory.getRecipeInstance();
  231. getHibernateTemplate().getSessionFactory().evict(r.getClass());
  232. Ethnicity e = domainObjectFactory.getEthnicityInstance();
  233. getHibernateTemplate().getSessionFactory().evict(e.getClass());
  234. } catch ( HibernateException e ) {
  235. throw new RuntimeException(e);
  236. }
  237. }
  238. /* (non-Javadoc)
  239. * @see magoffin.matt.ieat.dao.RecipeDao#reassignIngredient(java.lang.Integer, java.lang.Integer)
  240. */
  241. public void reassignIngredient(Integer ingredientId, Integer newIngredientId) {
  242. getHibernateTemplate().flush();
  243. jdbcTemplate.update(this.sqlReassignIngredient, new Object[] {newIngredientId,ingredientId});
  244. try {
  245. Recipe r = domainObjectFactory.getRecipeInstance();
  246. getHibernateTemplate().getSessionFactory().evict(r.getClass());
  247. RecipeIngredient i = domainObjectFactory.getRecipeIngredientInstance();
  248. getHibernateTemplate().getSessionFactory().evict(i.getClass());
  249. } catch ( HibernateException e ) {
  250. throw new RuntimeException(e);
  251. }
  252. }
  253. /* (non-Javadoc)
  254. * @see magoffin.matt.ieat.dao.RecipeDao#reassignPrepTime(java.lang.Integer, java.lang.Integer)
  255. */
  256. public void reassignPrepTime(Integer prepTimeId, Integer newPrepTimeId) {
  257. getHibernateTemplate().flush();
  258. jdbcTemplate.update(this.sqlReassignPrepTime, new Object[] {newPrepTimeId,prepTimeId});
  259. try {
  260. Recipe r = domainObjectFactory.getRecipeInstance();
  261. getHibernateTemplate().getSessionFactory().evict(r.getClass());
  262. PrepTime p = domainObjectFactory.getPrepTimeInstance();
  263. getHibernateTemplate().getSessionFactory().evict(p.getClass());
  264. } catch ( HibernateException e ) {
  265. throw new RuntimeException(e);
  266. }
  267. }
  268. private static class RecipeIndexCallbackDataImpl implements RecipeIndexCallbackData {
  269. private Long recipeId;
  270. private String name;
  271. private String excerpt;
  272. private String directions;
  273. private String step;
  274. private Integer ingredientId;
  275. private Short rating;
  276. private Integer ratingUserId;
  277. private String comment;
  278. private Integer commentUserId;
  279. private Integer ownerId;
  280. public String getName() {
  281. return name;
  282. }
  283. public Long getRecipeId() {
  284. return recipeId;
  285. }
  286. public String getDirections() {
  287. return StringUtil.trimToNull(directions);
  288. }
  289. public String getExcerpt() {
  290. return StringUtil.trimToNull(excerpt);
  291. }
  292. public String getStep() {
  293. return StringUtil.trimToNull(step);
  294. }
  295. public Integer getIngredientId() {
  296. return ingredientId;
  297. }
  298. public Short getRating() {
  299. return rating;
  300. }
  301. public Integer getRatingUserId() {
  302. return ratingUserId;
  303. }
  304. public String getComment() {
  305. return comment;
  306. }
  307. public Integer getCommentUserId() {
  308. return commentUserId;
  309. }
  310. public Integer getOwnerId() {
  311. return ownerId;
  312. }
  313. @Override
  314. public String toString() {
  315. return "RecipeIndexCallbackDataImpl{recipeId=" +recipeId +",name=" +name +"}";
  316. }
  317. }
  318. /**
  319. * @return the domainObjectFactory
  320. */
  321. public DomainObjectFactory getDomainObjectFactory() {
  322. return domainObjectFactory;
  323. }
  324. /**
  325. * @param domainObjectFactory the domainObjectFactory to set
  326. */
  327. public void setDomainObjectFactory(DomainObjectFactory domainObjectFactory) {
  328. this.domainObjectFactory = domainObjectFactory;
  329. }
  330. /**
  331. * @return the jdbcTemplate
  332. */
  333. public JdbcTemplate getJdbcTemplate() {
  334. return jdbcTemplate;
  335. }
  336. /**
  337. * @param jdbcTemplate the jdbcTemplate to set
  338. */
  339. public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  340. this.jdbcTemplate = jdbcTemplate;
  341. }
  342. /**
  343. * @return the sqlIndexAll
  344. */
  345. public String getSqlIndexAll() {
  346. return sqlIndexAll;
  347. }
  348. /**
  349. * @param sqlIndexAll the sqlIndexAll to set
  350. */
  351. public void setSqlIndexAll(String sqlIndexAll) {
  352. this.sqlIndexAll = sqlIndexAll;
  353. }
  354. /**
  355. * @return the sqlReassignBase
  356. */
  357. public String getSqlReassignBase() {
  358. return sqlReassignBase;
  359. }
  360. /**
  361. * @param sqlReassignBase the sqlReassignBase to set
  362. */
  363. public void setSqlReassignBase(String sqlReassignBase) {
  364. this.sqlReassignBase = sqlReassignBase;
  365. }
  366. /**
  367. * @return the sqlReassignCourse
  368. */
  369. public String getSqlReassignCourse() {
  370. return sqlReassignCourse;
  371. }
  372. /**
  373. * @param sqlReassignCourse the sqlReassignCourse to set
  374. */
  375. public void setSqlReassignCourse(String sqlReassignCourse) {
  376. this.sqlReassignCourse = sqlReassignCourse;
  377. }
  378. /**
  379. * @return the sqlReassignDifficulty
  380. */
  381. public String getSqlReassignDifficulty() {
  382. return sqlReassignDifficulty;
  383. }
  384. /**
  385. * @param sqlReassignDifficulty the sqlReassignDifficulty to set
  386. */
  387. public void setSqlReassignDifficulty(String sqlReassignDifficulty) {
  388. this.sqlReassignDifficulty = sqlReassignDifficulty;
  389. }
  390. /**
  391. * @return the sqlReassignEthnicity
  392. */
  393. public String getSqlReassignEthnicity() {
  394. return sqlReassignEthnicity;
  395. }
  396. /**
  397. * @param sqlReassignEthnicity the sqlReassignEthnicity to set
  398. */
  399. public void setSqlReassignEthnicity(String sqlReassignEthnicity) {
  400. this.sqlReassignEthnicity = sqlReassignEthnicity;
  401. }
  402. /**
  403. * @return the sqlReassignIngredient
  404. */
  405. public String getSqlReassignIngredient() {
  406. return sqlReassignIngredient;
  407. }
  408. /**
  409. * @param sqlReassignIngredient the sqlReassignIngredient to set
  410. */
  411. public void setSqlReassignIngredient(String sqlReassignIngredient) {
  412. this.sqlReassignIngredient = sqlReassignIngredient;
  413. }
  414. /**
  415. * @return the sqlReassignPrepTime
  416. */
  417. public String getSqlReassignPrepTime() {
  418. return sqlReassignPrepTime;
  419. }
  420. /**
  421. * @param sqlReassignPrepTime the sqlReassignPrepTime to set
  422. */
  423. public void setSqlReassignPrepTime(String sqlReassignPrepTime) {
  424. this.sqlReassignPrepTime = sqlReassignPrepTime;
  425. }
  426. }