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

http://datanucleus-appengine.googlecode.com/ · Java · 123 lines · 55 code · 10 blank · 58 comment · 9 complexity · 6e3550bd8c6c5b88dd5b5b74525c8af2 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.jpa.JPAEntityManager;
  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.persistence.EntityManager;
  31. /**
  32. * Utilities for converting between the low-level datastore api and JPA. <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 JPADatastoreBridge extends PojoDatastoreBridge {
  38. /**
  39. * Convert the result of a low-level datastore query into a List of JPA
  40. * entities.
  41. *
  42. * @param em The EntityManager with which the conversion results will be associated.
  43. * @param cls The type of object to which the {@link Entity Entities} in the
  44. * result list should be converted.
  45. * @param queryResultList The result of the low-level datastore query.
  46. * @return The {@link Entity Entities} in the provided result list, converted to JPA entities.
  47. *
  48. * @see PreparedQuery#asQueryResultList(FetchOptions)
  49. */
  50. public <T> List<T> toJPAResult(EntityManager em, Class<T> cls, QueryResultList<Entity> queryResultList) {
  51. return toJPAResult(em, cls, queryResultList, queryResultList.getCursor());
  52. }
  53. /**
  54. * Convert the result of a low-level datastore query into a List of JPA entities.
  55. *
  56. * @param em The EntityManager with which the conversion results will be associated.
  57. * @param cls The type of object to which the {@link Entity Entities} in the
  58. * result iterable should be converted.
  59. * @param queryResultIterable The result of the low-level datastore query.
  60. * @return The {@link Entity Entities} in the provided result iterable, converted to JPA entities.
  61. *
  62. * @see PreparedQuery#asQueryResultIterable()
  63. */
  64. public <T> List<T> toJPAResult(EntityManager em, Class<T> cls, QueryResultIterable<Entity> queryResultIterable) {
  65. return toJPAResult(em, cls, queryResultIterable, null);
  66. }
  67. private <T> List<T> toJPAResult(EntityManager em, Class<T> cls, Iterable<Entity> queryResultIterable, Cursor endCursor) {
  68. ExecutionContext ec = ((JPAEntityManager) em).getExecutionContext();
  69. return toPojoResult(ec, cls, queryResultIterable, endCursor);
  70. }
  71. /**
  72. * Convenience method to return the Entity for this <b>managed</b> JDO object.
  73. * @param pc The JDO object (managed by the provided PM)
  74. * @param pm The Persistence Manager
  75. * @return The Entity (if accessible)
  76. */
  77. public Entity getEntityFromJPA(Object pc, EntityManager em) {
  78. ExecutionContext ec = ((JPAEntityManager)em).getExecutionContext();
  79. ObjectProvider op = ec.findObjectProvider(pc);
  80. if (op != null) {
  81. DatastoreManager storeMgr = (DatastoreManager) ec.getStoreManager();
  82. DatastoreTransaction txn = storeMgr.getDatastoreTransaction(ec);
  83. if (txn != null) {
  84. Entity entity = (Entity)op.getAssociatedValue(txn);
  85. if (entity != null) {
  86. return entity;
  87. } else {
  88. Key key = EntityUtils.getPkAsKey(op);
  89. return EntityUtils.getEntityFromDatastore(storeMgr.getDatastoreServiceForReads(ec), op, key);
  90. }
  91. } else {
  92. Key key = EntityUtils.getPkAsKey(op);
  93. return EntityUtils.getEntityFromDatastore(storeMgr.getDatastoreServiceForReads(ec), op, key);
  94. }
  95. } else {
  96. // TODO Cater for detached objects
  97. throw new UnsupportedOperationException("Not yet supported getting Entity for detached/unmanaged object");
  98. }
  99. }
  100. /**
  101. * Convenience method to return a managed (POJO) object for the provided Entity for the EntityManager.
  102. * @param entity The entity
  103. * @param em The EntityManager
  104. * @param cls The POJO class being represented here
  105. * @return The POJO
  106. */
  107. public Object getJPAFromEntity(Entity entity, EntityManager em, Class cls) {
  108. ExecutionContext ec = ((JPAEntityManager)em).getExecutionContext();
  109. ClassLoaderResolver clr = ec.getClassLoaderResolver();
  110. AbstractClassMetaData cmd = ec.getMetaDataManager().getMetaDataForClass(cls, clr);
  111. return EntityUtils.entityToPojo(entity, cmd, clr, ec, false, ec.getFetchPlan());
  112. }
  113. }