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

http://thoughtsite.googlecode.com/ · Java · 100 lines · 60 code · 15 blank · 25 comment · 8 complexity · d83ad32565f547e1263d0c9f5a23a163 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.appengine.api.datastore.Key;
  17. import com.google.ie.business.dao.EntityIndexDao;
  18. import com.google.ie.business.domain.EntityIndex;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.List;
  22. import javax.jdo.PersistenceManager;
  23. import javax.jdo.Query;
  24. /**
  25. * A JDO implementation object for {@link EntityIndexDao}.
  26. *
  27. * @author Ashish K. Dahiya
  28. */
  29. public class EntityIndexDaoImpl extends BaseDaoImpl implements EntityIndexDao {
  30. public EntityIndexDaoImpl() {
  31. }
  32. @Override
  33. public EntityIndex updateEntityIndex(EntityIndex entityIndex) {
  34. if (entityIndex != null) {
  35. /*
  36. * Parent key is reset to null as either id or parent key can be
  37. * set for an entity
  38. */
  39. entityIndex.setParentKey(null);
  40. return getJdoTemplate().makePersistent(entityIndex);
  41. }
  42. return null;
  43. }
  44. @SuppressWarnings("unchecked")
  45. @Override
  46. public EntityIndex getUnIndexedEntity() {
  47. Query query = null;
  48. try {
  49. PersistenceManager pm = getJdoTemplate().getPersistenceManagerFactory()
  50. .getPersistenceManager();
  51. query = pm.newQuery(EntityIndex.class);
  52. query.setFilter("indexed == 0");
  53. // query.setRange(0, 1);
  54. List<EntityIndex> results = (List<EntityIndex>) query.execute();
  55. if (results != null && results.size() > 0) {
  56. EntityIndex entityIndex = results.get(DaoConstants.ZERO);
  57. entityIndex = pm.detachCopy(entityIndex);
  58. return entityIndex;
  59. }
  60. } finally {
  61. if (query != null)
  62. query.closeAll();
  63. }
  64. return null;
  65. }
  66. @Override
  67. public EntityIndex createEntityIndex(String parentKey) {
  68. EntityIndex entityIndex = new EntityIndex(parentKey);
  69. /* Set the indexd flag to UnIndexed */
  70. entityIndex.setIndexed(DaoConstants.UNINDEXED);
  71. return getJdoTemplate().makePersistent(entityIndex);
  72. }
  73. @Override
  74. public EntityIndex findEntityByPrimaryKey(Key key) {
  75. Collection<EntityIndex> results = getJdoTemplate().find(EntityIndex.class,
  76. "key== :keyParam", null, key);
  77. results = getJdoTemplate().detachCopyAll(results);
  78. List<EntityIndex> list = new ArrayList<EntityIndex>(results);
  79. if (list.size() > DaoConstants.ZERO) {
  80. EntityIndex entity = list.get(DaoConstants.ZERO);
  81. return entity;
  82. }
  83. return null;
  84. }
  85. }