Skip to content

Instantly share code, notes, and snippets.

@Clemzd
Last active August 29, 2015 14:09
Show Gist options
  • Save Clemzd/ee021e8ba501f0c002c8 to your computer and use it in GitHub Desktop.
Save Clemzd/ee021e8ba501f0c002c8 to your computer and use it in GitHub Desktop.
TPGoogleAppEngine
package com.zenika.restx.persistence.objectify;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.zenika.restx.domain.ZenUser;
import java.util.List;
/**
* Created by Clement on 18/11/2014.
*/
public class ContactDaoObjectify {
private static ContactDaoObjectify INSTANCE = new ContactDaoObjectify();
public static ContactDaoObjectify getInstance(){
ObjectifyService.factory().register(ZenUser.class);
return INSTANCE;
}
public Long save(ZenUser contact){
return ObjectifyService.ofy().save().entity(contact).now().getId();
}
public void delete(Long id){
ObjectifyService.ofy().delete().key(Key.create(ZenUser.class, id)).now();
}
public ZenUser get(long id){
return ObjectifyService.ofy().load().key(Key.create(ZenUser.class, id)).now();
}
public List<ZenUser> getAll(){
return ObjectifyService.ofy().load().type(ZenUser.class).list();
}
}
package com.zenika.restx.persistence;
import com.google.appengine.api.datastore.*;
import com.zenika.restx.domain.ZenUser;
import com.zenika.restx.resource.ContactResource;
import java.util.ArrayList;
import java.util.List;
public class ContactRepository {
private static ContactRepository INSTANCE = new ContactRepository();
public static ContactRepository getInstance() {
return INSTANCE;
}
public DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
private Key agenda = KeyFactory.createKey("Agenda", 1);
public long save(ZenUser contact) {
Entity e = new Entity("Contact", agenda);
if (contact.id != null) {
Key k = KeyFactory.createKey(agenda, "Contact", contact.id);
try {
e = datastore.get(k);
} catch (EntityNotFoundException e1) {
}
} else {
e = new Entity("Contact", agenda);
}
e.setProperty("firstname", contact.firstName);
e.setProperty("lastname", contact.lastName);
e.setProperty("email", contact.email);
e.setProperty("notes", contact.notes);
if (contact.birthdate != null) {
e.setProperty("birthdate", contact.birthdate);
}
Transaction txn = datastore.beginTransaction();
Key key;
try {
key = datastore.put(e);
txn.commit();
} catch (javax.persistence.EntityNotFoundException e2) {
txn.rollback();
throw new RuntimeException();
}
return key.getId();
}
public List<ZenUser> getAll() {
List<ZenUser> contacts = new ArrayList<>();
Query q = new Query("Contact")
.addProjection(new PropertyProjection("firstname", String.class))
.addProjection(new PropertyProjection("lastname", String.class))
.addProjection(new PropertyProjection("email", String.class))
.addProjection(new PropertyProjection("notes", String.class));
PreparedQuery pq = datastore.prepare(q);
for (Entity e : pq.asIterable()) {
contacts.add(ZenUser.create()
.id(e.getKey().getId())
.firstName((String) e.getProperty("firstname"))
.lastName((String) e.getProperty("lastname"))
.email((String) e.getProperty("email"))
.notes((String) e.getProperty("notes"))
);
}
return contacts;
}
public void deleteUser(Long id){
}
}
package com.zenika.restx.resource;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.zenika.restx.domain.ZenUser;
import com.zenika.restx.persistence.ContactRepository;
import com.zenika.restx.persistence.UserRepository;
import restx.annotations.DELETE;
import restx.annotations.GET;
import restx.annotations.POST;
import restx.annotations.RestxResource;
import restx.factory.Component;
import restx.security.PermitAll;
import java.util.List;
/**
* Created by Clement on 18/11/2014.
*/
@Component
@RestxResource
public class ContactResource {
@GET("/v1/users")
@PermitAll
/**
* Get the users defined in a arraylist
*/
public List<ZenUser> getAll() {
return ContactRepository.getInstance().getAll();
}
@POST("/v1/users")
@PermitAll
public Long save(final ZenUser user) {
return ContactRepository.getInstance().save(user);
}
@DELETE("/v1/users/{id}")
@PermitAll
public void deleteUser(final Long id) {
ContactRepository.getInstance().deleteUser(id);
}
}
package com.zenika.restx.resource;
import com.google.appengine.api.memcache.AsyncMemcacheService;
import com.google.appengine.api.memcache.ErrorHandlers;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.zenika.restx.domain.ZenUser;
import com.zenika.restx.persistence.ContactRepository;
import com.zenika.restx.persistence.objectify.ContactDaoObjectify;
import restx.annotations.*;
import restx.factory.Component;
import restx.security.PermitAll;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Level;
/**
* Created by Clement on 18/11/2014.
*/
@Component
@RestxResource
public class ContactRessource2 {
public ContactRessource2(){
}
@GET("/v2/users")
@PermitAll
/**
* Get the users defined in a arraylist
*/
public List<ZenUser> getAll() {
return ContactDaoObjectify.getInstance().getAll();
}
@GET("/v2/users/{id}")
@PermitAll
public ZenUser get(Long id){
ZenUser user = null;
// Solution synchrone
// MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
// syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
//
// ZenUser user = (ZenUser) syncCache.get(id);
// if(user == null){
// user = ContactDaoObjectify.getInstance().get(id);
// syncCache.put(id, user);
// }
// Using the asynchronous cache
AsyncMemcacheService asyncCache = MemcacheServiceFactory.getAsyncMemcacheService();
asyncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
Future<Object> futureValue = asyncCache.get(id); // read from cache
// ... do other work in parallel to cache retrieval
try {
user = (ZenUser) futureValue.get();
if (user == null) {
user = ContactDaoObjectify.getInstance().get(id);
asyncCache.put(id, user);
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return user;
}
@PUT("/v2/users/{id}")
@PermitAll
public void edit(final Long id, final ZenUser user){
ContactDaoObjectify.getInstance().save(user);
}
@POST("/v2/users")
@PermitAll
public Long save(final ZenUser user) {
return ContactDaoObjectify.getInstance().save(user);
}
@DELETE("/v2/users/{id}")
@PermitAll
public void deleteUser(final Long id) {
ContactDaoObjectify.getInstance().delete(id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment