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

http://thoughtsite.googlecode.com/ · Java · 92 lines · 45 code · 11 blank · 36 comment · 6 complexity · de4c0e46bcdb5fd56d68c82b861f779e 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.ShardedCounterDao;
  17. import com.google.ie.business.domain.ShardedCounter;
  18. import java.util.List;
  19. import javax.jdo.Query;
  20. /**
  21. * A data access object specification base class for all data access objects
  22. * that use JDO to interact with the datastore.
  23. *
  24. * @author gmaurya
  25. *
  26. */
  27. public class ShardedCounterDaoImpl extends BaseDaoImpl implements ShardedCounterDao {
  28. /**
  29. * Get the shards related to an entity.
  30. *
  31. * @param parentKey the key of the parent entity
  32. *
  33. * @return List of {@link ShardedCounter} objects
  34. */
  35. @SuppressWarnings("unchecked")
  36. public List<ShardedCounter> getShardsByParentKey(String parentKey) {
  37. Query query = null;
  38. List<ShardedCounter> shards = null;
  39. try {
  40. query = getJdoTemplate().getPersistenceManagerFactory()
  41. .getPersistenceManager().newQuery(ShardedCounter.class);
  42. query.setFilter("parentKey == :parentName");
  43. shards = (List<ShardedCounter>) query.execute(parentKey);
  44. } finally {
  45. if (query != null)
  46. query.closeAll();
  47. }
  48. return shards;
  49. }
  50. /**
  51. * Get shards by parent key and shard number.
  52. *
  53. * @param parentKey the key of the parent entity
  54. * @param shardNum an int specifying the shard number
  55. *
  56. * @return List of {@link ShardedCounter} objects
  57. */
  58. @SuppressWarnings("unchecked")
  59. public List<ShardedCounter> getShardsByParentKeyAndShardNum(String parentKey, int shardNum) {
  60. Query query = null;
  61. List<ShardedCounter> shards = null;
  62. try {
  63. query = getJdoTemplate().getPersistenceManagerFactory()
  64. .getPersistenceManager().newQuery(ShardedCounter.class);
  65. query.setFilter("parentKey == '" + parentKey + "' && " + "shardNumber == " + shardNum);
  66. shards = (List<ShardedCounter>)
  67. query.execute();
  68. } finally {
  69. if (query != null)
  70. query.closeAll();
  71. }
  72. return shards;
  73. }
  74. @Override
  75. public ShardedCounter createOrUpdateShardedCounter(ShardedCounter shardedCounter) {
  76. if (shardedCounter == null)
  77. return null;
  78. shardedCounter = getJdoTemplate().makePersistent(shardedCounter);
  79. return shardedCounter;
  80. }
  81. }