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

http://thoughtsite.googlecode.com/ · Java · 152 lines · 101 code · 18 blank · 33 comment · 11 complexity · e4006b4327f8035ee9129f3839ea3335 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.ProjectDao;
  17. import com.google.ie.business.domain.Project;
  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 for ProjectDao.
  31. *
  32. * @author Charanjeet singh
  33. */
  34. public class ProjectDaoImpl extends BaseDaoImpl implements ProjectDao {
  35. private static final String IDEA_KEY = "ideaKey";
  36. @Override
  37. public Project saveProject(Project project) {
  38. project = getJdoTemplate().makePersistent(project);
  39. return project;
  40. }
  41. @Override
  42. public List<Project> getProjects(RetrievalInfo retrievalInfo, Set<String> statusOfProjects) {
  43. /* Execute query and return projects */
  44. return executeGetProject(null, retrievalInfo, statusOfProjects);
  45. }
  46. @Override
  47. public List<Project> getProjects(User user, RetrievalInfo retrievalInfo,
  48. Set<String> statusOfProject) {
  49. return executeGetProject(user, retrievalInfo, statusOfProject);
  50. }
  51. @Override
  52. public Project getProject(String projectKey) {
  53. return findEntityByPrimaryKey(Project.class, projectKey);
  54. }
  55. @SuppressWarnings("unchecked")
  56. @Override
  57. public List<Project> getProjectsByIdeaKey(String ideaKey) {
  58. Query query = getJdoTemplate().getPersistenceManagerFactory()
  59. .getPersistenceManager().newQuery(Project.class);
  60. Map<String, Object> mapOfFilterValues = new HashMap<String, Object>();
  61. query.setFilter(IDEA_KEY + " == '" + ideaKey + "'");
  62. mapOfFilterValues.put("ideaKey", ideaKey);
  63. Collection<Project> projects = getJdoTemplate()
  64. .find(query.toString(), mapOfFilterValues);
  65. if (projects == null) {
  66. return null;
  67. }
  68. return new ArrayList<Project>(projects);
  69. }
  70. /**
  71. * Method to retrieve the list of Projects.".
  72. *
  73. * @param user The user object.
  74. * @param retrievalInfo RetrievalInfo object having information of
  75. * startIndex and total number of records.
  76. * @param statusOfProject A set of strings holding the project
  77. * status.
  78. *
  79. * @return List of Project objects saved or published by user.
  80. */
  81. @SuppressWarnings("unchecked")
  82. private List<Project> executeGetProject(User user, RetrievalInfo retrievalInfo,
  83. Set<String> statusOfProject) {
  84. Query query = null;
  85. try {
  86. query = getJdoTemplate().getPersistenceManagerFactory()
  87. .getPersistenceManager().newQuery(Project.class);
  88. query.setRange(retrievalInfo.getStartIndex(), retrievalInfo.getStartIndex()
  89. + retrievalInfo.getNoOfRecords());
  90. query.setOrdering("" + retrievalInfo.getOrderBy() + " " + retrievalInfo.getOrderType());
  91. Map<String, Object> mapOfFilterValues = new HashMap<String, Object>();
  92. if (user != null && user.getUserKey() != null) {
  93. query.setFilter("status == :statusOfProject && creatorKey == :creatorKeyParam");
  94. mapOfFilterValues.put("creatorKeyParam", user.getUserKey());
  95. } else {
  96. query.setFilter("status == :statusOfProject");
  97. }
  98. mapOfFilterValues.put("statusOfProject", statusOfProject);
  99. Collection<Project> collection = getJdoTemplate()
  100. .find(query.toString(), mapOfFilterValues);
  101. /* Detach the result from the corresponding persistence manager */
  102. collection = getJdoTemplate().detachCopyAll(collection);
  103. return new ArrayList<Project>(collection);
  104. } finally {
  105. if (query != null) {
  106. query.closeAll();
  107. }
  108. }
  109. }
  110. @SuppressWarnings("unchecked")
  111. @Override
  112. @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
  113. public List<Project> getProjectsByKeys(Set<String> projectKeys, RetrievalInfo retrievalInfo) {
  114. Query query = null;
  115. try {
  116. query = getJdoTemplate().getPersistenceManagerFactory()
  117. .getPersistenceManager().newQuery(Project.class);
  118. query.setFilter("key == :projectKeys");
  119. query.setRange(retrievalInfo.getStartIndex(), retrievalInfo.getNoOfRecords()
  120. + retrievalInfo.getStartIndex());
  121. Map<String, Object> mapOfFilterValues = new HashMap<String, Object>();
  122. mapOfFilterValues.put("projectKeys", projectKeys);
  123. Collection<Project> projects = getJdoTemplate()
  124. .find(query.toString(), mapOfFilterValues);
  125. /* Detach the result from the corresponding persistence manager */
  126. projects = getJdoTemplate().detachCopyAll(projects);
  127. return new ArrayList<Project>(projects);
  128. } finally {
  129. if (query != null) {
  130. query.closeAll();
  131. }
  132. }
  133. }
  134. }