PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/testing/library/src/com/google/appengine/library/JDOBookDataService.java

http://datanucleus-appengine.googlecode.com/
Java | 50 lines | 35 code | 11 blank | 4 comment | 0 complexity | 050b28553602d378a4e6577986da923f MD5 | raw file
 1// Copyright 2008 Google Inc. All Rights Reserved.
 2
 3package com.google.appengine.library;
 4
 5import java.util.Collection;
 6
 7import javax.jdo.JDOHelper;
 8import javax.jdo.PersistenceManager;
 9import javax.jdo.PersistenceManagerFactory;
10import javax.jdo.Query;
11
12/**
13 * @author kjin@google.com (Kevin Jin)
14 */
15final class JDOBookDataService implements BookDataService {
16
17  private static final PersistenceManagerFactory pmf =
18      JDOHelper.getPersistenceManagerFactory("transactional");
19  private final PersistenceManager pm = pmf.getPersistenceManager();
20  private static final String FROM_CLAUSE = "SELECT FROM " + Book.class.getName();
21
22  @SuppressWarnings("unchecked")
23  public Iterable<Book> asIterable(String jpqlQuery) {
24    return (Iterable<Book>) pm.newQuery("javax.jdo.query.JPQL", FROM_CLAUSE + jpqlQuery).execute();
25  }
26
27  @SuppressWarnings("unchecked")
28  public Iterable<Book> asIterable(String jpqlQuery, int limit, int offset) {
29    Query query = pm.newQuery("javax.jdo.query.JPQL", FROM_CLAUSE + jpqlQuery);
30    query.setRange(offset, offset + limit);
31    return (Iterable<Book>) query.execute();
32  }
33
34  @SuppressWarnings("unchecked")
35  public int countEntities(String jpqlQuery) {
36    return ((Collection) asIterable(jpqlQuery)).size();
37  }
38
39  public void delete(Book book) {
40    pm.deletePersistent(book);
41  }
42
43  public void put(Book book) {
44    pm.makePersistent(book);
45  }
46
47  public void close() {
48    pm.close();
49  }
50}