PageRenderTime 47ms CodeModel.GetById 31ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 103 lines | 66 code | 14 blank | 23 comment | 1 complexity | 9cdb25b0cf1ea5b892ded002ca553177 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;
 20
 21import org.datanucleus.ClassLoaderResolver;
 22import org.datanucleus.ExecutionContext;
 23import org.datanucleus.store.StoreManager;
 24import org.datanucleus.store.connection.ManagedConnection;
 25import org.datanucleus.store.connection.ManagedConnectionResourceListener;
 26import org.datanucleus.metadata.AbstractClassMetaData;
 27
 28import com.google.appengine.datanucleus.query.DatastoreQuery;
 29
 30import org.datanucleus.store.query.AbstractJavaQuery;
 31import org.datanucleus.store.query.AbstractQueryResult;
 32
 33import java.util.List;
 34import java.util.Map;
 35
 36/**
 37 * Utilities for converting between the low-level datastore api and pojos.
 38 *
 39 * @author Max Ross <max.ross@gmail.com>
 40 */
 41class PojoDatastoreBridge {
 42
 43  <T> List<T> toPojoResult(final ExecutionContext ec, Class<T> cls, Iterable<Entity> queryResultIterable, Cursor endCursor) {
 44    final ClassLoaderResolver clr = ec.getClassLoaderResolver();
 45    final AbstractClassMetaData acmd = ec.getMetaDataManager().getMetaDataForClass(cls, clr);
 46    Utils.Function<Entity, Object> func = new Utils.Function<Entity, Object>() {
 47      public Object apply(Entity from) {
 48        return EntityUtils.entityToPojo(from, acmd, clr, ec, true, ec.getFetchPlan().getCopy());
 49      }
 50    };
 51
 52    AbstractJavaQuery query = new DummyQuery(ec.getStoreManager(), ec);
 53    final ManagedConnection mconn = ec.getStoreManager().getConnection(ec);
 54    try {
 55      List<T> results = (List<T>) DatastoreQuery.newStreamingQueryResultForEntities(
 56          queryResultIterable, func, endCursor, query);
 57
 58      if (results instanceof AbstractQueryResult) {
 59        // Lazy loading results : add listener to the connection so we can get a callback when the connection is flushed.
 60        final AbstractQueryResult qr = (AbstractQueryResult)results;
 61        ManagedConnectionResourceListener listener = new ManagedConnectionResourceListener() {
 62          public void managedConnectionPreClose() {
 63            // Disconnect the query from this ManagedConnection (read in unread rows etc)
 64            qr.disconnect();
 65          }
 66          public void managedConnectionPostClose() {}
 67          public void resourcePostClose() {
 68            mconn.removeListener(this);
 69          }
 70          public void transactionFlushed() {}
 71          public void transactionPreClose() {
 72            // Disconnect the query from this ManagedConnection (read in unread rows etc)
 73            qr.disconnect();
 74          }
 75        };
 76        mconn.addListener(listener);
 77        qr.addConnectionListener(listener);
 78      }
 79      return results;
 80    } finally {
 81      mconn.release();
 82    }
 83  }
 84
 85  private static final class DummyQuery extends AbstractJavaQuery {
 86
 87    private DummyQuery(StoreManager storeMgr, ExecutionContext ec) {
 88      super(storeMgr, ec);
 89    }
 90
 91    public String getSingleStringQuery() {
 92      throw new UnsupportedOperationException();
 93    }
 94
 95    protected void compileInternal(Map parameterValues) {
 96      throw new UnsupportedOperationException();
 97    }
 98
 99    protected Object performExecute(Map parameters) {
100      throw new UnsupportedOperationException();
101    }
102  }
103}