/src/com/google/appengine/datanucleus/JDODatastoreBridge.java

http://datanucleus-appengine.googlecode.com/ · Java · 121 lines · 55 code · 10 blank · 56 comment · 9 complexity · 5bcd8170a3118998b9405b79cbc81971 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 Google Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.appengine.datanucleus;
  17. import com.google.appengine.api.datastore.Cursor;
  18. import com.google.appengine.api.datastore.Entity;
  19. import com.google.appengine.api.datastore.FetchOptions;
  20. import com.google.appengine.api.datastore.Key;
  21. import com.google.appengine.api.datastore.PreparedQuery;
  22. import com.google.appengine.api.datastore.QueryResultIterable;
  23. import com.google.appengine.api.datastore.QueryResultList;
  24. import org.datanucleus.ClassLoaderResolver;
  25. import org.datanucleus.api.jdo.JDOPersistenceManager;
  26. import org.datanucleus.metadata.AbstractClassMetaData;
  27. import org.datanucleus.ExecutionContext;
  28. import org.datanucleus.state.ObjectProvider;
  29. import java.util.List;
  30. import javax.jdo.PersistenceManager;
  31. /**
  32. * Utilities for converting between the low-level datastore api and JDO.<br>
  33. * This class is part of the public api of the DataNucleus App Engine plugin and can be safely used.
  34. *
  35. * @author Max Ross <max.ross@gmail.com>
  36. */
  37. public final class JDODatastoreBridge extends PojoDatastoreBridge {
  38. /**
  39. * Convert the result of a low-level datastore query into a List of JDO entities.
  40. *
  41. * @param pm The PersistenceManager with which the conversion results will be associated.
  42. * @param cls The type of object to which the {@link Entity Entities} in the result list should be converted.
  43. * @param queryResultList The result of the low-level datastore query.
  44. * @return The {@link Entity Entities} in the provided result list, converted to JDO entities.
  45. *
  46. * @see PreparedQuery#asQueryResultList(FetchOptions)
  47. */
  48. public <T> List<T> toJDOResult(PersistenceManager pm, Class<T> cls, QueryResultList<Entity> queryResultList) {
  49. return toJDOResult(pm, cls, queryResultList, queryResultList.getCursor());
  50. }
  51. /**
  52. * Convert the result of a low-level datastore query into a List of JDO entities.
  53. *
  54. * @param pm The PersistenceManager with which the conversion results will be associated.
  55. * @param cls The type of object to which the {@link Entity Entities} in the
  56. * result iterable should be converted.
  57. * @param queryResultIterable The result of the low-level datastore query.
  58. * @return The {@link Entity Entities} in the provided result iterable, converted to JDO entities.
  59. *
  60. * @see PreparedQuery#asQueryResultIterable()
  61. */
  62. public <T> List<T> toJDOResult(PersistenceManager pm, Class<T> cls, QueryResultIterable<Entity> queryResultIterable) {
  63. return toJDOResult(pm, cls, queryResultIterable, null);
  64. }
  65. private <T> List<T> toJDOResult(PersistenceManager pm, Class<T> cls, Iterable<Entity> queryResultIterable, Cursor endCursor) {
  66. ExecutionContext ec = ((JDOPersistenceManager) pm).getExecutionContext();
  67. return toPojoResult(ec, cls, queryResultIterable, endCursor);
  68. }
  69. /**
  70. * Convenience method to return the Entity for this <b>managed</b> JDO object.
  71. * @param pc The JDO object (managed by the provided PM)
  72. * @param pm The Persistence Manager
  73. * @return The Entity (if accessible)
  74. */
  75. public Entity getEntityFromJDO(Object pc, PersistenceManager pm) {
  76. ExecutionContext ec = ((JDOPersistenceManager)pm).getExecutionContext();
  77. ObjectProvider op = ec.findObjectProvider(pc);
  78. if (op != null) {
  79. DatastoreManager storeMgr = (DatastoreManager) ec.getStoreManager();
  80. DatastoreTransaction txn = storeMgr.getDatastoreTransaction(ec);
  81. if (txn != null) {
  82. Entity entity = (Entity)op.getAssociatedValue(txn);
  83. if (entity != null) {
  84. return entity;
  85. } else {
  86. Key key = EntityUtils.getPkAsKey(op);
  87. return EntityUtils.getEntityFromDatastore(storeMgr.getDatastoreServiceForReads(ec), op, key);
  88. }
  89. } else {
  90. Key key = EntityUtils.getPkAsKey(op);
  91. return EntityUtils.getEntityFromDatastore(storeMgr.getDatastoreServiceForReads(ec), op, key);
  92. }
  93. } else {
  94. // TODO Cater for detached objects
  95. throw new UnsupportedOperationException("Not yet supported getting Entity for detached/unmanaged object");
  96. }
  97. }
  98. /**
  99. * Convenience method to return a managed (POJO) object for the provided Entity for the PersistenceManager.
  100. * @param entity The entity
  101. * @param pm The PersistenceManager
  102. * @param cls The POJO class being represented here
  103. * @return The POJO
  104. */
  105. public Object getJDOFromEntity(Entity entity, PersistenceManager pm, Class cls) {
  106. ExecutionContext ec = ((JDOPersistenceManager)pm).getExecutionContext();
  107. ClassLoaderResolver clr = ec.getClassLoaderResolver();
  108. AbstractClassMetaData cmd = ec.getMetaDataManager().getMetaDataForClass(cls, clr);
  109. return EntityUtils.entityToPojo(entity, cmd, clr, ec, false, ec.getFetchPlan());
  110. }
  111. }