package org.dynamic.web.page.server;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.UUID;
import javax.jdo.PersistenceManager;
import javax.persistence.EntityNotFoundException;
import org.dynamic.web.page.client.EntityService;
import org.dynamic.web.page.server.util.JDOPersistenceManagerFactory;
import org.dynamic.web.page.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
@SuppressWarnings("serial")
public class EntityServiceImpl extends RemoteServiceServlet implements EntityService {
public String create(String db, String table, HashMap<String, Object> fields) throws IllegalArgumentException, Exception {
User currentUser = UserServiceFactory.getUserService().getCurrentUser();
String email = currentUser != null ? currentUser.getEmail() : "";
String userId = currentUser != null ? currentUser.getUserId() : "key-1234";
String id = null;
//if (!dbExist(db)) {
id = UUID.randomUUID().toString();
Key dbKey = KeyFactory.createKey(db, userId); // to be replaced by user id
Date date = new Date();
Entity entity = new Entity(table, dbKey);
entity.setProperty("id", id);
//entity.setProperty("date", date);
entity.setProperty("name", table);
if (fields != null && !fields.isEmpty()) {
for (String field : fields.keySet()) {
entity.setProperty(field, fields.get(field));
}
}
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(entity);
//}
return id;
}
// https://developers.google.com/appengine/docs/java/datastore/queries
public HashMap<String, HashMap<String, Object>> read(String db, String table, String id) {
User currentUser = UserServiceFactory.getUserService().getCurrentUser();
String email = currentUser != null ? currentUser.getEmail() : "";
String userId = currentUser != null ? currentUser.getUserId() : "key-1234";
Key dbKey = KeyFactory.createKey(db, userId);
HashMap<String, HashMap<String, Object>> attributes = new HashMap<String, HashMap<String, Object>>();
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Filter idFilter = new FilterPredicate("id", FilterOperator.EQUAL, id);
Query query = new Query(table, dbKey)
.addSort("name", SortDirection.ASCENDING)
.setFilter(idFilter);
PreparedQuery pq = datastore.prepare(query);
Entity entity = pq.asSingleEntity();
if (entity != null) {
HashMap<String, Object> properties = new HashMap<String, Object>();
properties.putAll(entity.getProperties());
attributes.put(table, properties);
}
return attributes;
}
public boolean update(String db, String table, String id) {
boolean success = false;
User currentUser = UserServiceFactory.getUserService().getCurrentUser();
String email = currentUser != null ? currentUser.getEmail() : "";
String userId = currentUser != null ? currentUser.getUserId() : "key-1234";
// Key dbKey = KeyFactory.createKey(db, userId);
// HashMap<String, HashMap<String, Object>> attributes = new HashMap<String, HashMap<String, Object>>();
// DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//
// Filter idFilter = new FilterPredicate("id", FilterOperator.EQUAL, id);
// Query query = new Query(table, dbKey)
// .addSort("name", SortDirection.ASCENDING)
// .setFilter(idFilter);
//
// PreparedQuery pq = datastore.prepare(query);
// Entity entity = pq.asSingleEntity();
//
// if (entity != null) {
// HashMap<String, Object> properties = new HashMap<String, Object>();
// properties.putAll(entity.getProperties());
// attributes.put(table, properties);
// success = true;
// }
return success;
}
public boolean delete(String db, String table, String id) {
boolean success = false;
// User currentUser = UserServiceFactory.getUserService().getCurrentUser();
// String email = currentUser != null ? currentUser.getEmail() : "";
// String userId = currentUser != null ? currentUser.getUserId() : "key-1234";
//
// Key dbKey = KeyFactory.createKey(db, userId);
// DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
// datastore.delete(dbKey);
return success;
}
public HashMap<String, HashMap<String, Object>> list(String db, String table) {
User currentUser = UserServiceFactory.getUserService().getCurrentUser();
String email = currentUser != null ? currentUser.getEmail() : "";
String userId = currentUser != null ? currentUser.getUserId() : "key-1234";
String loginUrl = UserServiceFactory.getUserService().createLoginURL("/DynamicWebPage.html");
String logoutUrl = UserServiceFactory.getUserService().createLogoutURL("/DynamicWebPage.html");
String nickname = currentUser != null ? currentUser.getNickname() : "";
Key dbKey = KeyFactory.createKey(db, userId);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query query = new Query(table, dbKey).addSort("name", Query.SortDirection.DESCENDING);
//Query query = new Query(table).addSort("date", Query.SortDirection.DESCENDING);
List<Entity> tables = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
HashMap<String, HashMap<String, Object>> entities = null;
if (table != null && !table.isEmpty()) {
entities = new HashMap<String, HashMap<String, Object>>();
for (Entity entity : tables) {
HashMap<String, Object> properties = new HashMap<String, Object>();
properties.putAll(entity.getProperties());
entities.put("" + KeyFactory.keyToString(entity.getKey()), properties);
}
}
return entities;
}
private boolean dbExist(String db) throws com.google.appengine.api.datastore.EntityNotFoundException {
User currentUser = UserServiceFactory.getUserService().getCurrentUser();
String email = currentUser != null ? currentUser.getEmail() : "";
String userId = currentUser != null ? currentUser.getUserId() : "key-1234";
boolean success = false;
Key dbKey = KeyFactory.createKey(db, userId);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity entity = datastore.get(dbKey);
if (entity != null) {
success = true;
}
return success;
}
}