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

http://thoughtsite.googlecode.com/ · Java · 91 lines · 56 code · 9 blank · 26 comment · 8 complexity · e2ae931c8cd4c29e92333e8d63f6e2ef 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.AdminRequestDao;
  17. import com.google.ie.business.domain.AdminRequest;
  18. import com.google.ie.dto.RetrievalInfo;
  19. import org.apache.commons.lang.StringUtils;
  20. import org.apache.log4j.Logger;
  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.List;
  27. import java.util.Map;
  28. import javax.jdo.Query;
  29. /**
  30. * A JDO implementation object for {@link AdminRequestDao}.
  31. *
  32. * @author asirohi
  33. *
  34. */
  35. public class AdminRequestDaoImpl extends BaseDaoImpl implements AdminRequestDao {
  36. private static Logger logger = Logger.getLogger(AdminRequestDaoImpl.class);
  37. @Override
  38. @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  39. public boolean saveRequest(AdminRequest adminRequest) {
  40. adminRequest = getJdoTemplate().makePersistent(adminRequest);
  41. if (adminRequest != null && !StringUtils.isBlank(adminRequest.getKey())) {
  42. logger.info("Admin Request for " + adminRequest.getRequestType() + " "
  43. + adminRequest.getEntityType() + " saved successfully");
  44. return true;
  45. }
  46. logger.error("Saving of AdminRequest object failed");
  47. return false;
  48. }
  49. @SuppressWarnings("unchecked")
  50. @Override
  51. public List<AdminRequest> getAllAdminRequests(RetrievalInfo retrievalInfo) {
  52. Query query = null;
  53. try {
  54. query = getJdoTemplate().getPersistenceManagerFactory()
  55. .getPersistenceManager().newQuery(AdminRequest.class);
  56. /*
  57. * Add the start index to the number of records required since
  58. * internally the second argument is treated as the index up to
  59. * which
  60. * the entities are to be fetched
  61. */
  62. query.setRange(retrievalInfo.getStartIndex(), retrievalInfo.getStartIndex()
  63. + retrievalInfo.getNoOfRecords());
  64. query.setFilter("status == '" + AdminRequest.STATUS_PENDING + "'");
  65. Map<String, Object> mapOfFilterValues = new HashMap<String, Object>();
  66. mapOfFilterValues.put("status", AdminRequest.STATUS_PENDING);
  67. Collection<AdminRequest> collection = getJdoTemplate().find(query.toString(),
  68. mapOfFilterValues);
  69. if (collection != null && collection.size() > DaoConstants.ZERO) {
  70. logger.info(collection.size() + " admin requests found for the Idea or Comment");
  71. List<AdminRequest> requestList = (new ArrayList<AdminRequest>(collection));
  72. return requestList;
  73. }
  74. } finally {
  75. if (query != null) {
  76. query.closeAll();
  77. }
  78. }
  79. logger.info("No admin requests for the Idea or Comment");
  80. return null;
  81. }
  82. }