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

http://thoughtsite.googlecode.com/ · Java · 100 lines · 59 code · 13 blank · 28 comment · 7 complexity · fb22fe7d9f22b56fa0731dd662d874a9 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.CommentDao;
  17. import com.google.ie.business.domain.Comment;
  18. import com.google.ie.business.domain.IdeaComment;
  19. import com.google.ie.business.domain.ProjectComment;
  20. import com.google.ie.dto.RetrievalInfo;
  21. import org.springframework.transaction.annotation.Propagation;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.HashMap;
  26. import java.util.HashSet;
  27. import java.util.List;
  28. import java.util.Set;
  29. import javax.jdo.Query;
  30. /**
  31. * A JDO implementation object for CommentDao.
  32. *
  33. * @author Sachneet
  34. *
  35. */
  36. public class CommentDaoImpl extends BaseDaoImpl implements CommentDao {
  37. private static final String IDEA_KEY = "ideaKey";
  38. public CommentDaoImpl() {
  39. }
  40. @Override
  41. @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  42. public Comment saveComment(Comment comment) {
  43. return getJdoTemplate().makePersistent(comment);
  44. }
  45. @SuppressWarnings("unchecked")
  46. @Override
  47. public List<Comment> getComments(String key, RetrievalInfo retrievalInfo,
  48. String keyType) {
  49. Query query = null;
  50. try {
  51. /* Create the query instance corresponding to the keyType */
  52. if (keyType.equals(IDEA_KEY)) {
  53. query = getJdoTemplate().getPersistenceManagerFactory().getPersistenceManager()
  54. .newQuery(IdeaComment.class);
  55. } else {
  56. query = getJdoTemplate().getPersistenceManagerFactory().getPersistenceManager()
  57. .newQuery(ProjectComment.class);
  58. }
  59. /* Set of status to be matched */
  60. Set<String> setOfStatus = new HashSet<String>();
  61. setOfStatus.add(IdeaComment.STATUS_SAVED);
  62. setOfStatus.add(IdeaComment.STATUS_FLAGGED);
  63. query.setFilter(keyType + " == '" + key + "' && status == :setOfStatus");
  64. query.setOrdering(retrievalInfo.getOrderBy() + " " + retrievalInfo.getOrderType());
  65. /*
  66. * Add the start index to the number of records required since
  67. * internally the second argument is treated as the index up to
  68. * which
  69. * the entities are to be fetched
  70. */
  71. query.setRange(retrievalInfo.getStartIndex(), retrievalInfo.getStartIndex()
  72. + retrievalInfo.getNoOfRecords());
  73. HashMap<String, Object> hashMap = new HashMap<String, Object>();
  74. hashMap.put("setOfStatus", setOfStatus);
  75. Collection<Comment> collection = getJdoTemplate().find(query.toString(), hashMap);
  76. if (collection != null && collection.size() > DaoConstants.ZERO) {
  77. List<Comment> commentList = (new ArrayList<Comment>(collection));
  78. return commentList;
  79. }
  80. } finally {
  81. if (query != null) {
  82. query.closeAll();
  83. }
  84. }
  85. return null;
  86. }
  87. }