Search in sources :

Example 56 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.

the class DatastoreViewerServlet method getKinds.

/**
 * Return all kinds in the current namespace.
 */
List<String> getKinds() {
    List<String> kinds = new ArrayList<String>();
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Query q = new Query(Query.KIND_METADATA_KIND).addSort(Entity.KEY_RESERVED_PROPERTY, SortDirection.ASCENDING);
    for (Entity e : ds.prepare(q).asIterable()) {
        kinds.add(e.getKey().getName());
    }
    return kinds;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) DatastoreService(com.google.appengine.api.datastore.DatastoreService) LocalDatastoreService(com.google.appengine.api.datastore.dev.LocalDatastoreService) ArrayList(java.util.ArrayList)

Example 57 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.

the class DatastoreViewerServlet method doGetEntityDetails.

private void doGetEntityDetails(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String key = req.getParameter(KEY);
    String keyName = null;
    Long keyId = null;
    String kind = null;
    String parentKey = null;
    String parentKind = null;
    if (key != null) {
        Key k = KeyFactory.stringToKey(key);
        if (k.getName() != null) {
            keyName = k.getName();
        } else {
            keyId = k.getId();
        }
        kind = k.getKind();
        if (k.getParent() != null) {
            parentKey = KeyFactory.keyToString(k.getParent());
            parentKind = k.getParent().getKind();
        }
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        Entity e;
        try {
            e = ds.get(KeyFactory.stringToKey(key));
        } catch (EntityNotFoundException e1) {
            throw new RuntimeException("Could not locate entity " + key);
        }
        req.setAttribute("entity", new EntityDetailsView(e));
    } else {
    // TODO Handle creation case
    }
    String url = String.format("/_ah/adminConsole?subsection=entityDetails&" + "key=%s&keyName=%s&keyId=%d&kind=%s&parentKey=%s&parentKind=%s", key, keyName, keyId, kind, parentKey, parentKind);
    try {
        getServletContext().getRequestDispatcher(url).forward(req, resp);
    } catch (ServletException e) {
        throw new RuntimeException("Could not forward request", e);
    }
}
Also used : ServletException(javax.servlet.ServletException) Entity(com.google.appengine.api.datastore.Entity) DatastoreService(com.google.appengine.api.datastore.DatastoreService) LocalDatastoreService(com.google.appengine.api.datastore.dev.LocalDatastoreService) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) Key(com.google.appengine.api.datastore.Key)

Example 58 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.

the class DatastoreViewerServlet method getEntityViews.

/**
 * Retrieve all EntityViews of the given kind for display, sorted by the
 * (possibly null) given order.
 */
List<EntityView> getEntityViews(String kind, String order, int start, int numPerPage) {
    List<EntityView> entityViews = new ArrayList<EntityView>();
    Query q = new Query(kind);
    SortDirection dir = SortDirection.ASCENDING;
    if (order != null) {
        // If the order string begins with a dash, sort in descending order.
        if (order.charAt(0) == '-') {
            dir = SortDirection.DESCENDING;
            order = order.substring(1);
        }
        q.addSort(order, dir);
    }
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    FetchOptions opts = FetchOptions.Builder.withOffset(start).limit(numPerPage);
    for (Entity e : ds.prepare(q).asIterable(opts)) {
        entityViews.add(new EntityView(e));
    }
    return entityViews;
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Entity(com.google.appengine.api.datastore.Entity) SortDirection(com.google.appengine.api.datastore.Query.SortDirection) Query(com.google.appengine.api.datastore.Query) DatastoreService(com.google.appengine.api.datastore.DatastoreService) LocalDatastoreService(com.google.appengine.api.datastore.dev.LocalDatastoreService) ArrayList(java.util.ArrayList)

Example 59 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project xuml-tools by davidmoten.

the class DatastoreTextGae method put.

@Override
public void put(String kind, String name, String property, String value) {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Key k = KeyFactory.createKey(kind, name);
    System.out.println("putting " + k + "=" + value);
    Entity ent = new Entity(k);
    ent.setProperty(property, new Text(value));
    ds.put(ent);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) DatastoreService(com.google.appengine.api.datastore.DatastoreService) DatastoreText(xuml.tools.datastore.DatastoreText) Text(com.google.appengine.api.datastore.Text) Key(com.google.appengine.api.datastore.Key)

Example 60 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project nigori by ucam-cl-dtg.

the class AppEngineDatabase method getRevisions.

@Override
public Collection<byte[]> getRevisions(User user, byte[] index) throws IOException {
    PersistenceManager pm = pmfInstance.getPersistenceManager();
    try {
        // TODO(drt24): cleanup this method
        // TODO(drt24): we can do this faster with a key only lookup
        Key lookupKey = getLookupKey(user, index);
        // If this doesn't exist there is no key so null gets returned by JDOObjectNotFoundException
        Lookup lookup = pm.getObjectById(Lookup.class, lookupKey);
        List<byte[]> answer = new ArrayList<byte[]>();
        Query getRevisionValues = new Query(AppEngineRecord.class.getSimpleName());
        getRevisionValues.setAncestor(lookupKey);
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        List<Entity> results = datastore.prepare(getRevisionValues).asList(FetchOptions.Builder.withDefaults());
        for (Entity result : results) {
            ByteArrayInputStream bais = new ByteArrayInputStream(((Blob) result.getProperty("revision")).getBytes());
            ObjectInputStream ndis = new ObjectInputStream(bais);
            answer.add(((Revision) ndis.readObject()).getBytes());
        }
        if (lookup != null) {
            return answer;
        } else {
            return null;
        }
    } catch (JDOObjectNotFoundException e) {
        return null;
    } catch (ClassNotFoundException e) {
        throw new IOException(e);
    } finally {
        pm.close();
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PersistenceManager(javax.jdo.PersistenceManager) DatastoreService(com.google.appengine.api.datastore.DatastoreService) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) ByteArrayInputStream(java.io.ByteArrayInputStream) Key(com.google.appengine.api.datastore.Key) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

DatastoreService (com.google.appengine.api.datastore.DatastoreService)64 Entity (com.google.appengine.api.datastore.Entity)43 Test (org.junit.Test)29 Key (com.google.appengine.api.datastore.Key)25 Query (com.google.appengine.api.datastore.Query)25 Transaction (com.google.appengine.api.datastore.Transaction)15 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)6 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)6 LocalDatastoreService (com.google.appengine.api.datastore.dev.LocalDatastoreService)6 ArrayList (java.util.ArrayList)6 Date (java.util.Date)5 PersistenceManager (javax.jdo.PersistenceManager)5 Filter (com.google.appengine.api.datastore.Query.Filter)4 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)4 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)3 FilterChain (javax.servlet.FilterChain)3 BlobKey (com.google.appengine.api.blobstore.BlobKey)2 Blob (com.google.appengine.api.datastore.Blob)2 DatastoreServiceConfig (com.google.appengine.api.datastore.DatastoreServiceConfig)2 FetchOptions (com.google.appengine.api.datastore.FetchOptions)2