Skip to content

Instantly share code, notes, and snippets.

@rominirani
Created January 12, 2014 10:58
Show Gist options
  • Save rominirani/8383214 to your computer and use it in GitHub Desktop.
Save rominirani/8383214 to your computer and use it in GitHub Desktop.
package com.mindstorm.famousquotes.entity;
import com.mindstorm.famousquotes.entity.PMF;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.response.CollectionResponse;
import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.datanucleus.query.JDOCursorHelper;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Nullable;
import javax.inject.Named;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
@Api(name = "quoteendpoint", namespace = @ApiNamespace(ownerDomain = "mindstorm.com", ownerName = "mindstorm.com", packagePath = "famousquotes.entity"))
public class QuoteEndpoint {
/**
* This method lists all the entities inserted in datastore.
* It uses HTTP GET method and paging support.
*
* @return A CollectionResponse class containing the list of all entities
* persisted and a cursor to the next page.
*/
@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "listQuote")
public CollectionResponse<Quote> listQuote(
@Nullable @Named("cursor") String cursorString,
@Nullable @Named("limit") Integer limit) {
PersistenceManager mgr = null;
Cursor cursor = null;
List<Quote> execute = null;
try {
mgr = getPersistenceManager();
Query query = mgr.newQuery(Quote.class);
if (cursorString != null && cursorString != "") {
cursor = Cursor.fromWebSafeString(cursorString);
HashMap<String, Object> extensionMap = new HashMap<String, Object>();
extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor);
query.setExtensions(extensionMap);
}
if (limit != null) {
query.setRange(0, limit);
}
execute = (List<Quote>) query.execute();
cursor = JDOCursorHelper.getCursor(execute);
if (cursor != null)
cursorString = cursor.toWebSafeString();
// Tight loop for fetching all entities from datastore and accomodate
// for lazy fetch.
for (Quote obj : execute)
;
} finally {
mgr.close();
}
return CollectionResponse.<Quote> builder().setItems(execute)
.setNextPageToken(cursorString).build();
}
/**
* This method gets the entity having primary key id. It uses HTTP GET method.
*
* @param id the primary key of the java bean.
* @return The entity with primary key id.
*/
@ApiMethod(name = "getQuote")
public Quote getQuote(@Named("id") Long id) {
PersistenceManager mgr = getPersistenceManager();
Quote quote = null;
try {
quote = mgr.getObjectById(Quote.class, id);
} finally {
mgr.close();
}
return quote;
}
/**
* This inserts a new entity into App Engine datastore. If the entity already
* exists in the datastore, an exception is thrown.
* It uses HTTP POST method.
*
* @param quote the entity to be inserted.
* @return The inserted entity.
*/
@ApiMethod(name = "insertQuote")
public Quote insertQuote(Quote quote) {
PersistenceManager mgr = getPersistenceManager();
try {
if (quote.getId() != null) {
if (containsQuote(quote)) {
throw new EntityExistsException("Object already exists");
}
}
mgr.makePersistent(quote);
} finally {
mgr.close();
}
return quote;
}
/**
* This method is used for updating an existing entity. If the entity does not
* exist in the datastore, an exception is thrown.
* It uses HTTP PUT method.
*
* @param quote the entity to be updated.
* @return The updated entity.
*/
@ApiMethod(name = "updateQuote")
public Quote updateQuote(Quote quote) {
PersistenceManager mgr = getPersistenceManager();
try {
if (!containsQuote(quote)) {
throw new EntityNotFoundException("Object does not exist");
}
mgr.makePersistent(quote);
} finally {
mgr.close();
}
return quote;
}
/**
* This method removes the entity with primary key id.
* It uses HTTP DELETE method.
*
* @param id the primary key of the entity to be deleted.
*/
@ApiMethod(name = "removeQuote")
public void removeQuote(@Named("id") Long id) {
PersistenceManager mgr = getPersistenceManager();
try {
Quote quote = mgr.getObjectById(Quote.class, id);
mgr.deletePersistent(quote);
} finally {
mgr.close();
}
}
private boolean containsQuote(Quote quote) {
PersistenceManager mgr = getPersistenceManager();
boolean contains = true;
try {
mgr.getObjectById(Quote.class, quote.getId());
} catch (javax.jdo.JDOObjectNotFoundException ex) {
contains = false;
} finally {
mgr.close();
}
return contains;
}
private static PersistenceManager getPersistenceManager() {
return PMF.get().getPersistenceManager();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment