/src/main/java/com/google/ie/business/dao/impl/IdeaDaoImpl.java

http://thoughtsite.googlecode.com/ · Java · 188 lines · 122 code · 19 blank · 47 comment · 17 complexity · f4c81037f99e16017270532d4d14a3f6 MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. package com.google.ie.business.dao.impl;
  16. import com.google.ie.business.dao.IdeaDao;
  17. import com.google.ie.business.domain.Idea;
  18. import com.google.ie.business.domain.User;
  19. import com.google.ie.dto.RetrievalInfo;
  20. import org.springframework.transaction.annotation.Propagation;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import java.util.ArrayList;
  23. import java.util.Collection;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import javax.jdo.Query;
  29. /**
  30. * A JDO implementation object for IdeaDao.
  31. *
  32. * @author Sachneet
  33. */
  34. public class IdeaDaoImpl extends BaseDaoImpl implements IdeaDao {
  35. @Override
  36. public Idea getIdea(Idea idea) {
  37. return getJdoTemplate().getObjectById(Idea.class, idea.getKey());
  38. }
  39. @Override
  40. public List<Idea> getIdeas(RetrievalInfo retrievalInfo, Set<String> statusOfIdeas) {
  41. /* Execute query and return published ideas */
  42. return executeGetIdeas(null, retrievalInfo, statusOfIdeas);
  43. }
  44. @Override
  45. public List<Idea> getUserIdeas(User user, Set<String> statusOfIdeas, RetrievalInfo retrievalInfo) {
  46. /* Execute query and return result */
  47. return executeGetIdeas(user, retrievalInfo,
  48. statusOfIdeas);
  49. }
  50. @SuppressWarnings("unchecked")
  51. public List<Idea> getIdeasByTagKey(String tagKey, Set<String> statusOfIdeas,
  52. RetrievalInfo retrievalInfo) {
  53. Query query = getJdoTemplate().getPersistenceManagerFactory()
  54. .getPersistenceManager().newQuery(Idea.class);
  55. /*
  56. * Add the start index to the number of records required since
  57. * internally the second argument is treated as the index up to which
  58. * the entities are to be fetched
  59. */
  60. query.setRange(retrievalInfo.getStartIndex(), retrievalInfo.getStartIndex()
  61. + retrievalInfo.getNoOfRecords());
  62. query.setOrdering("" + retrievalInfo.getOrderBy() + " " + retrievalInfo.getOrderType());
  63. query.setFilter("tagKeys == :tagKey && status == :statusOfIdeas");
  64. Map<String, Object> mapOfValues = new HashMap<String, Object>();
  65. mapOfValues.put("statusOfIdeas", statusOfIdeas);
  66. mapOfValues.put("tagKey", tagKey);
  67. Collection<Idea> collection = getJdoTemplate()
  68. .find(query.toString(), mapOfValues);
  69. if (collection != null) {
  70. return new ArrayList<Idea>(collection);
  71. }
  72. return null;
  73. }
  74. /**
  75. * Method to retrieve the list of ideas for different cases.
  76. *
  77. * @param user The user object.
  78. * @param retrievalInfo the {@link RetrievalInfo} object having information
  79. * of startIndex and total number of records.
  80. * @param statusOfProject {@link Set} of strings holding the Idea
  81. * status.
  82. * @return List of idea objects
  83. */
  84. @SuppressWarnings("unchecked")
  85. private List<Idea> executeGetIdeas(User user, RetrievalInfo retrievalInfo,
  86. Set<String> statusOfIdeas) {
  87. Query query = null;
  88. Collection<Idea> collection;
  89. try {
  90. query = getJdoTemplate().getPersistenceManagerFactory()
  91. .getPersistenceManager().newQuery(Idea.class);
  92. /*
  93. * Add the start index to the number of records required since
  94. * internally the second argument is treated as the index up to
  95. * which the entities are to be fetched
  96. */
  97. query.setRange(retrievalInfo.getStartIndex(), retrievalInfo.getStartIndex()
  98. + retrievalInfo.getNoOfRecords());
  99. query.setOrdering("" + retrievalInfo.getOrderBy() + " " + retrievalInfo.getOrderType());
  100. Map<String, Object> mapOfFilterValues = new HashMap<String, Object>();
  101. if (user != null && user.getUserKey() != null) {
  102. query.setFilter("status == :statusOfIdeas && creatorKey == :creatorKeyParam");
  103. mapOfFilterValues.put("creatorKeyParam", user.getUserKey());
  104. } else {
  105. query.setFilter("status == :statusOfIdeas");
  106. }
  107. mapOfFilterValues.put("statusOfIdeas", statusOfIdeas);
  108. collection = getJdoTemplate()
  109. .find(query.toString(), mapOfFilterValues);
  110. return new ArrayList<Idea>(collection);
  111. } finally {
  112. if (query != null) {
  113. query.closeAll();
  114. }
  115. }
  116. }
  117. @Override
  118. @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  119. public Idea saveIdea(Idea idea) {
  120. idea = getJdoTemplate().makePersistent(idea);
  121. return idea;
  122. }
  123. @Override
  124. @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
  125. public boolean updateStatus(Idea idea) {
  126. String status = idea.getStatus();
  127. idea = getJdoTemplate().getObjectById(Idea.class, idea.getKey());
  128. idea.setStatus(status);
  129. idea = getJdoTemplate().makePersistent(idea);
  130. if (idea != null)
  131. return true;
  132. return false;
  133. }
  134. /**
  135. * Update of the idea vote points of the given idea.
  136. *
  137. * @param idea Idea object containing updated status and key.
  138. * @return boolean return true or false on the basis of successful update.
  139. */
  140. @Override
  141. @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
  142. public boolean updateIdeaPoints(final Idea idea) {
  143. Idea originalIdea = getJdoTemplate().getObjectById(Idea.class, idea.getKey());
  144. boolean flag = false;
  145. if (originalIdea != null) {
  146. originalIdea.setTotalVotes(idea.getTotalVotes());
  147. originalIdea.setTotalPositiveVotes(idea.getTotalPositiveVotes());
  148. originalIdea.setTotalNegativeVotes(idea.getTotalNegativeVotes());
  149. if (getJdoTemplate().makePersistent(originalIdea) != null) {
  150. flag = true;
  151. }
  152. }
  153. return flag;
  154. }
  155. @SuppressWarnings("unchecked")
  156. @Override
  157. public List<Idea> getIdeasByCategoryKey(String ideaCategoryKey, String statusOfIdeas,
  158. RetrievalInfo retrievalInfo) {
  159. Query query = getJdoTemplate().getPersistenceManagerFactory()
  160. .getPersistenceManager().newQuery(Idea.class);
  161. query.setFilter("ideaCategoryKey == '" + ideaCategoryKey + "' && status == '"
  162. + statusOfIdeas + "'");
  163. List<Idea> collection = (List<Idea>) query.execute();
  164. if (collection != null) {
  165. return new ArrayList<Idea>(collection);
  166. }
  167. return null;
  168. }
  169. }