PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 16package com.google.appengine.datanucleus;
 17
 18import com.google.appengine.api.datastore.Cursor;
 19import com.google.appengine.api.datastore.Entity;
 20import com.google.appengine.api.datastore.FetchOptions;
 21import com.google.appengine.api.datastore.Key;
 22import com.google.appengine.api.datastore.PreparedQuery;
 23import com.google.appengine.api.datastore.QueryResultIterable;
 24import com.google.appengine.api.datastore.QueryResultList;
 25
 26import org.datanucleus.ClassLoaderResolver;
 27import org.datanucleus.api.jpa.JPAEntityManager;
 28import org.datanucleus.metadata.AbstractClassMetaData;
 29import org.datanucleus.ExecutionContext;
 30import org.datanucleus.state.ObjectProvider;
 31
 32import java.util.List;
 33
 34import javax.persistence.EntityManager;
 35
 36/**
 37 * Utilities for converting between the low-level datastore api and JPA. <br>
 38 * This class is part of the public api of the DataNucleus App Engine plugin and can be safely used.
 39 *
 40 * @author Max Ross <max.ross@gmail.com>
 41 */
 42public final class JPADatastoreBridge extends PojoDatastoreBridge {
 43
 44  /**
 45   * Convert the result of a low-level datastore query into a List of JPA
 46   * entities.
 47   *
 48   * @param em The EntityManager with which the conversion results will be associated.
 49   * @param cls The type of object to which the {@link Entity Entities} in the
 50   * result list should be converted.
 51   * @param queryResultList The result of the low-level datastore query.
 52   * @return The {@link Entity Entities} in the provided result list, converted to JPA entities.
 53   *
 54   * @see PreparedQuery#asQueryResultList(FetchOptions)
 55   */
 56  public <T> List<T> toJPAResult(EntityManager em, Class<T> cls, QueryResultList<Entity> queryResultList) {
 57    return toJPAResult(em, cls, queryResultList, queryResultList.getCursor());
 58  }
 59
 60  /**
 61   * Convert the result of a low-level datastore query into a List of JPA entities.
 62   *
 63   * @param em The EntityManager with which the conversion results will be associated.
 64   * @param cls The type of object to which the {@link Entity Entities} in the
 65   * result iterable should be converted.
 66   * @param queryResultIterable The result of the low-level datastore query.
 67   * @return The {@link Entity Entities} in the provided result iterable, converted to JPA entities.
 68   *
 69   * @see PreparedQuery#asQueryResultIterable()
 70   */
 71  public <T> List<T> toJPAResult(EntityManager em, Class<T> cls, QueryResultIterable<Entity> queryResultIterable) {
 72    return toJPAResult(em, cls, queryResultIterable, null);
 73  }
 74
 75  private <T> List<T> toJPAResult(EntityManager em, Class<T> cls, Iterable<Entity> queryResultIterable, Cursor endCursor) {
 76    ExecutionContext ec = ((JPAEntityManager) em).getExecutionContext();
 77    return toPojoResult(ec, cls, queryResultIterable, endCursor);
 78  }
 79
 80  /**
 81   * Convenience method to return the Entity for this <b>managed</b> JDO object.
 82   * @param pc The JDO object (managed by the provided PM)
 83   * @param pm The Persistence Manager
 84   * @return The Entity (if accessible)
 85   */
 86  public Entity getEntityFromJPA(Object pc, EntityManager em) {
 87    ExecutionContext ec = ((JPAEntityManager)em).getExecutionContext();
 88    ObjectProvider op = ec.findObjectProvider(pc);
 89    if (op != null) {
 90      DatastoreManager storeMgr = (DatastoreManager) ec.getStoreManager();
 91      DatastoreTransaction txn = storeMgr.getDatastoreTransaction(ec);
 92      if (txn != null) {
 93        Entity entity = (Entity)op.getAssociatedValue(txn);
 94        if (entity != null) {
 95          return entity;
 96        } else {
 97          Key key = EntityUtils.getPkAsKey(op);
 98          return EntityUtils.getEntityFromDatastore(storeMgr.getDatastoreServiceForReads(ec), op, key);
 99        }
100      } else {
101        Key key = EntityUtils.getPkAsKey(op);
102        return EntityUtils.getEntityFromDatastore(storeMgr.getDatastoreServiceForReads(ec), op, key);
103      }
104    } else {
105      // TODO Cater for detached objects
106      throw new UnsupportedOperationException("Not yet supported getting Entity for detached/unmanaged object");
107    }
108  }
109
110  /**
111   * Convenience method to return a managed (POJO) object for the provided Entity for the EntityManager.
112   * @param entity The entity
113   * @param em The EntityManager
114   * @param cls The POJO class being represented here
115   * @return The POJO
116   */
117  public Object getJPAFromEntity(Entity entity, EntityManager em, Class cls) {
118    ExecutionContext ec = ((JPAEntityManager)em).getExecutionContext();
119    ClassLoaderResolver clr = ec.getClassLoaderResolver();
120    AbstractClassMetaData cmd = ec.getMetaDataManager().getMetaDataForClass(cls, clr);
121    return EntityUtils.entityToPojo(entity, cmd, clr, ec, false, ec.getFetchPlan());
122  }
123}