package ch.hslu.testproject;
import ch.hslu.testproject.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 = "birdendpoint", namespace = @ApiNamespace(ownerDomain = "hslu.ch", ownerName = "hslu.ch", packagePath = "testproject") ) public class BirdEndpoint {
/**
* 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 = "listBird")
public CollectionResponse<Bird> listBird(@Nullable @Named("cursor") String cursorString,
@Nullable @Named("limit") Integer limit) {
PersistenceManager mgr = null;
Cursor cursor = null;
List<Bird> execute = null;
try {
mgr = getPersistenceManager();
Query query = mgr.newQuery(Bird.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<Bird>) 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 (Bird obj : execute)
;
// CUSTOM
if(execute.isEmpty()) {
Bird testBird = new Bird() {{
englishName = "Test";
germanName = "Test";
frenchName = "Test";
italianName = "Test";
}};
mgr.makePersistent(testBird);
}
} finally {
mgr.close();
}
return CollectionResponse.<Bird> 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 = "getBird")
public Bird getBird(@Named("id") Long id) {
PersistenceManager mgr = getPersistenceManager();
Bird bird = null;
try {
bird = mgr.getObjectById(Bird.class, id);
} finally {
mgr.close();
}
return bird;
}
/**
* 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 bird the entity to be inserted.
* @return The inserted entity.
*/
@ApiMethod(name = "insertBird")
public Bird insertBird(Bird bird) {
PersistenceManager mgr = getPersistenceManager();
try {
if (containsBird(bird)) {
throw new EntityExistsException("Object already exists");
}
mgr.makePersistent(bird);
} finally {
mgr.close();
}
return bird;
}
/**
* 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 bird the entity to be updated.
* @return The updated entity.
*/
@ApiMethod(name = "updateBird")
public Bird updateBird(Bird bird) {
PersistenceManager mgr = getPersistenceManager();
try {
if (!containsBird(bird)) {
throw new EntityNotFoundException("Object does not exist");
}
mgr.makePersistent(bird);
} finally {
mgr.close();
}
return bird;
}
/**
* 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 = "removeBird")
public void removeBird(@Named("id") Long id) {
PersistenceManager mgr = getPersistenceManager();
try {
Bird bird = mgr.getObjectById(Bird.class, id);
mgr.deletePersistent(bird);
} finally {
mgr.close();
}
}
private boolean containsBird(Bird bird) {
PersistenceManager mgr = getPersistenceManager();
boolean contains = true;
try {
mgr.getObjectById(Bird.class, bird.getId());
} catch (javax.jdo.JDOObjectNotFoundException ex) {
contains = false;
} finally {
mgr.close();
}
return contains;
}
private static PersistenceManager getPersistenceManager() {
return PMF.get().getPersistenceManager();
}
}