PageRenderTime 27ms CodeModel.GetById 2ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

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