/**
* Copyright 2011 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.appengine.codelab;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entities;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
/**
* This is the utility class for all servlets. It provides method for inserting,
* deleting, searching the entity from data store. Also contains method for
* displaying the entity in JSON format.
*
*/
public class Util {
private static final Logger logger = Logger.getLogger(Util.class.getCanonicalName());
private static DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
private static MemcacheService keycache = MemcacheServiceFactory.getMemcacheService();
/**
* Add the entity to cache and also to the datastore
* @param entity
* : entity to be persisted
*/
public static void persistEntity(Entity entity) {
logger.log(Level.INFO, "Saving entity");
Key key = entity.getKey();
Transaction txn = datastore.beginTransaction();
try {
datastore.put(entity);
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
} else {
addToCache(key, entity);
}
}
}
/**
* Delete the entity from persistent store represented by the key
* Also delete it from cache
*
* @param key
* : key to delete the entity from the persistent store
*/
public static void deleteEntity(Key key) {
logger.log(Level.INFO, "Deleting entity");
Transaction txn = datastore.beginTransaction();
try {
datastore.delete(key);
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
} else {
deleteFromCache(key);
}
}
}
/**
* Delete the entities represented by a list of keys
* Delete the entitites from cache also
*
* @param keys : keys for the entities that are to be deleted
*/
public static void deleteEntity(final List<Key> keys) {
datastore.delete(new Iterable<Key>() {
public Iterator<Key> iterator() {
return keys.iterator();
}
});
deleteFromCache(keys);
}
/**
* Search and return the entity from the cache . If absent , search the
* datastore.
*
* @param key
* : key to find the entity
* @return entity
*/
public static Entity findEntity(Key key) {
logger.log(Level.INFO, "Search the entity");
try {
Entity entity = getFromCache(key);
if (entity != null) {
return entity;
}
return datastore.get(key);
} catch (EntityNotFoundException e) {
return null;
}
}
/***
* Search entities based on search criteria
*
* @param kind
* @param searchBy
* : Searching Criteria (Property)
* @param searchFor
* : Searching Value (Property Value)
* @return List all entities of a kind from the cache or datastore (if not in
* cache) with the specified properties
*/
public static Iterable<Entity> listEntities(String kind, String searchBy, String searchFor) {
logger.log(Level.INFO, "Search entities based on search criteria");
Query query = new Query(kind);
if (searchFor != null && !"".equals(searchFor)) {
query.addFilter(searchBy, FilterOperator.EQUAL, searchFor);
}
PreparedQuery pq = datastore.prepare(query);
return pq.asIterable();
}
//Metodo Adicionado por Anderson - US2
public static Iterable<Entity> listEntitiesFraudulentas(String kind, String searchBy, String trans_data_init, String trans_data_end) {
logger.log(Level.INFO, "Search entities based on search criteria");
Query query = new Query(kind);
if (trans_data_init != null && !"".equals(trans_data_init)) {
Filter f1 = new FilterPredicate(searchBy,
FilterOperator.GREATER_THAN_OR_EQUAL, trans_data_init);
Filter f2 = new FilterPredicate(searchBy,
FilterOperator.LESS_THAN_OR_EQUAL, trans_data_end);
Filter fc = CompositeFilterOperator.and(f1, f2);
query.setFilter(fc);
//query.addFilter(searchBy, FilterOperator.GREATER_THAN, trans_data_init);
}
PreparedQuery pq = datastore.prepare(query);
return pq.asIterable();
}
//Metodo Adicionado por Anderson - US13
public static Iterable<Entity> listEntitiesFraudePeriodoTipo(String trans_data_init,String trans_data_end, String fraude_type) {
logger.log(Level.INFO, "Search entities based on search criteria");
Query query = new Query("Fraude");
Filter f1 = new FilterPredicate("fraude_data_deteccao", FilterOperator.NOT_EQUAL, "");
Filter f2 = new FilterPredicate("fraude_data_deteccao", FilterOperator.NOT_EQUAL, "");
Filter f3 = new FilterPredicate("fraude_type", FilterOperator.NOT_EQUAL, "");
Filter fc = new FilterPredicate("", FilterOperator.NOT_EQUAL, "");
if (trans_data_init != null && !"".equals(trans_data_init)) {
f1 = new FilterPredicate("fraude_data_deteccao",
FilterOperator.GREATER_THAN_OR_EQUAL, trans_data_init);
}
if (trans_data_end != null && !"".equals(trans_data_end)) {
f2 = new FilterPredicate("fraude_data_deteccao",
FilterOperator.LESS_THAN_OR_EQUAL, trans_data_end);
}
if (fraude_type != null && !"".equals(fraude_type)) {
f3 = new FilterPredicate("fraude_type",
FilterOperator.EQUAL, fraude_type);
fc = CompositeFilterOperator.and(f1, f2, f3);
}
else
{
fc = CompositeFilterOperator.and(f1, f2);
}
query.setFilter(fc);
PreparedQuery pq = datastore.prepare(query);
return pq.asIterable();
}
//Metodo Adicionado por Anderson - US213
public static Iterable<Entity> listEntitiesTransacaoPeriodoTipo(String trans_data_init,String trans_data_end, String trans_type) {
logger.log(Level.INFO, "Search entities based on search criteria");
Query query = new Query("Transacao");
List<Filter> filter = new ArrayList<Filter>();
if (trans_data_init != null && !"".equals(trans_data_init)) {
filter.add(new FilterPredicate("trans_data", FilterOperator.GREATER_THAN_OR_EQUAL,
trans_data_init));
}
if (trans_data_end != null && !"".equals(trans_data_end)) {
filter.add(new FilterPredicate("trans_data", FilterOperator.LESS_THAN_OR_EQUAL,
trans_data_end));
}
if (trans_type != null && !"".equals(trans_type)) {
filter.add(new FilterPredicate("trans_type", FilterOperator.EQUAL,
trans_type));
}
if (filter.size() > 0) {
query.setFilter(filter.size() == 1 ? filter.get(0)
: CompositeFilterOperator.and(filter));
}
PreparedQuery pq = datastore.prepare(query);
return pq.asIterable();
}
//Metodo Adicionado por Anderson - US213
public static Iterable<Entity> listEntitiesLocalidadePaisEstadoCidade(String localidade_country,String localidade_state, String localidade_city) {
logger.log(Level.INFO, "Search entities based on search criteria");
Query query = new Query("Localidade");
List<Filter> filter = new ArrayList<Filter>();
if (localidade_country != null && !"".equals(localidade_country)) {
filter.add(new FilterPredicate("localidade_country", FilterOperator.EQUAL,
localidade_country));
}
if (localidade_state != null && !"".equals(localidade_state)) {
filter.add(new FilterPredicate("localidade_state", FilterOperator.EQUAL,
localidade_state));
}
if (localidade_city != null && !"".equals(localidade_city)) {
filter.add(new FilterPredicate("localidade_city", FilterOperator.EQUAL,
localidade_city));
}
if (filter.size() > 0) {
query.setFilter(filter.size() == 1 ? filter.get(0)
: CompositeFilterOperator.and(filter));
}
PreparedQuery pq = datastore.prepare(query);
return pq.asIterable();
}
/**
* Get the list of children from a parent key in the entity group
*
* @param kind : the entity kind of the children that is to be searched for
* @param ancestor : the parent key of the entity group where we need to search
* @return iterable with all children of the parent of the specified kind
*/
public static Iterable<Entity> listChildren(String kind, Key ancestor) {
logger.log(Level.INFO, "Search entities based on parent");
Query query = new Query(kind);
query.setAncestor(ancestor);
query.addFilter(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, ancestor);
PreparedQuery pq = datastore.prepare(query);
return pq.asIterable();
}
/**
* Get the list of keys of all children for a given entity kind in a given entity group
* represented by the parent key
* @param kind : Entity kind of the children that needs to be searched for
* @param ancestor : Parent key of the entity group that needs to be searched for
* @return an iterable with keys of children
*/
public static Iterable<Entity> listChildKeys(String kind, Key ancestor) {
logger.log(Level.INFO, "Search entities based on parent");
Query query = new Query(kind);
query.setAncestor(ancestor).setKeysOnly();
query.addFilter(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN,ancestor);
PreparedQuery pq = datastore.prepare(query);
return pq.asIterable();
}
/**
* List the entities in JSON format
*
* @param entities
* entities to return as JSON strings
*/
public static String writeJSON(Iterable<Entity> entities) {
if( entities == null ){
return "{'data': []}";
}
logger.log(Level.INFO, "creating JSON format object");
StringBuilder sb = new StringBuilder();
int i = 0;
sb.append("{\"data\": [");
for (Entity result : entities) {
Map<String, Object> properties = result.getProperties();
sb.append("{");
if (result.getKey().getName() == null)
sb.append("\"name\" : \"" + result.getKey().getId() + "\",");
else
sb.append("\"name\" : \"" + result.getKey().getName() + "\",");
for (String key : properties.keySet()) {
sb.append("\"" + key + "\" : \"" + properties.get(key) + "\",");
}
sb.deleteCharAt(sb.lastIndexOf(","));
sb.append("},");
i++;
}
if (i > 0) {
sb.deleteCharAt(sb.lastIndexOf(","));
}
sb.append("]}");
return sb.toString();
}
public static String writeJSONTransacoesFraudulentas(Iterable<Entity> entitiesFraude,Iterable<Entity> entitiesTransacao,Iterable<Entity> entitiesLocalidade) {
logger.log(Level.INFO, "creating JSON Fraudes Fraudulentas format object");
StringBuilder sb = new StringBuilder();
int i = 0;
sb.append("{\"data\": [");
for (Entity resultFraude : entitiesFraude) {
Map<String, Object> propertiesFraude = resultFraude.getProperties();
sb.append("{");
for (String keyFraude : propertiesFraude.keySet()) {
sb.append("\"" + keyFraude + "\" : \"" + propertiesFraude.get(keyFraude) + "\",");
for (Entity resultTransacao : entitiesTransacao) {
Map<String, Object> propertiesTransacao = resultTransacao.getProperties();
if(propertiesTransacao.get("trans_id").equals(propertiesFraude.get(keyFraude))){
for (String keyTransacao : propertiesTransacao.keySet()) {
//if(keyTransacao.equals(keyFraude) && propertiesTransacao.get("trans_id").equals(propertiesFraude.get(keyFraude)))
sb.append("\"" + keyTransacao + "\" : \"" + propertiesTransacao.get(keyTransacao) + "\",");
for (Entity resultLocaliddade : entitiesLocalidade) {
Map<String, Object> propertiesLocalidade = resultLocaliddade.getProperties();
if(propertiesLocalidade.get("localidade_id").equals(propertiesTransacao.get(keyTransacao))){
for (String keyLocalidade : propertiesLocalidade.keySet()) {
//if(keyTransacao.equals(keyFraude) && propertiesTransacao.get("trans_id").equals(propertiesFraude.get(keyFraude)))
sb.append("\"" + keyLocalidade + "\" : \"" + propertiesLocalidade.get(keyLocalidade) + "\",");
}
}
}
}
}
}
}
sb.deleteCharAt(sb.lastIndexOf(","));
sb.append("},");
i++;
}
if (i > 0) {
sb.deleteCharAt(sb.lastIndexOf(","));
}
sb.append("]}");
return sb.toString();
}
public static String writeJSONLocalidade(Iterable<Entity> entitiesFraude,Iterable<Entity> entitiesTransacao,Iterable<Entity> entitiesLocalidade) {
logger.log(Level.INFO, "creating JSON Fraudes Fraudulentas format object");
StringBuilder sb = new StringBuilder();
int i = 0;
sb.append("{\"data\": [");
for (Entity resultLocaliddade : entitiesLocalidade) {
Map<String, Object> propertiesLocalidade = resultLocaliddade.getProperties();
sb.append("{");
for (String keyLocalidade : propertiesLocalidade.keySet()) {
sb.append("\"" + keyLocalidade + "\" : \"" + propertiesLocalidade.get(keyLocalidade) + "\",");
for (Entity resultTransacao : entitiesTransacao) {
Map<String, Object> propertiesTransacao = resultTransacao.getProperties();
if(keyLocalidade.equals("localidade_id") && propertiesTransacao.get("localidade_id").equals(propertiesLocalidade.get(keyLocalidade))){
for (String keyTransacao : propertiesTransacao.keySet()) {
sb.append("\"" + keyTransacao + "\" : \"" + propertiesTransacao.get(keyTransacao) + "\",");
for (Entity resultFraude : entitiesFraude) {
Map<String, Object> propertiesFraude = resultFraude.getProperties();
if(keyTransacao.equals("trans_id") && propertiesFraude.get("trans_id").equals(propertiesTransacao.get(keyTransacao))){
for (String keyFraude : propertiesFraude.keySet()) {
sb.append("\"" + keyFraude + "\" : \"" + propertiesFraude.get(keyFraude) + "\",");
}
}
}
}
}
}
}
sb.deleteCharAt(sb.lastIndexOf(","));
sb.append("},");
i++;
}
if (i > 0) {
sb.deleteCharAt(sb.lastIndexOf(","));
}
sb.append("]}");
return sb.toString();
}
/**
* Retrieves Parent and Child entities into JSON String
*
* @param entities
* : List of parent entities
* @param childKind
* : Entity type for Child
* @param fkName
* : foreign-key to the parent in the child entity
* @return JSON string
*/
public static String writeJSON(Iterable<Entity> entities, String childKind, String fkName) {
logger.log(Level.INFO,"creating JSON format object for parent child relation");
StringBuilder sb = new StringBuilder();
int i = 0;
sb.append("{\"data\": [");
for (Entity result : entities) {
Map<String, Object> properties = result.getProperties();
sb.append("{");
if (result.getKey().getName() == null)
sb.append("\"name\" : \"" + result.getKey().getId() + "\",");
else
sb.append("\"name\" : \"" + result.getKey().getName() + "\",");
for (String key : properties.keySet()) {
sb.append("\"" + key + "\" : \"" + properties.get(key) + "\",");
}
Iterable<Entity> child = listEntities(childKind, fkName,String.valueOf(result.getKey().getId()));
for (Entity en : child) {
for (String key : en.getProperties().keySet()) {
sb.append("\"" + key + "\" : \"" + en.getProperties().get(key)+ "\",");
}
}
sb.deleteCharAt(sb.lastIndexOf(","));
sb.append("},");
i++;
}
if (i > 0) {
sb.deleteCharAt(sb.lastIndexOf(","));
}
sb.append("]}");
return sb.toString();
}
/**
* Adds the entity to cache
*
* @param key
* : key of the entity
* @param entity
* : Entity being added
*/
public static void addToCache(Key key, Entity entity) {
logger.log(Level.INFO, "Adding entity to cache");
keycache.put(key, entity);
}
/**
* Delete the entity from cache
*
* @param key : Key of the entity that needs to be deleted
*/
public static void deleteFromCache(Key key) {
logger.log(Level.INFO, "Deleting entity from cache");
keycache.delete(key);
}
/**
* Delete entities based on a set of keys
*
* @param keys : list of keys for the entities that are to be deleted
*/
public static void deleteFromCache(List<Key> keys) {
keycache.deleteAll(keys);
}
/**
* Search for an entity based on key in the cache
*
* @param key : key of the entity that is searched for
* @return the entity
*/
public static Entity getFromCache(Key key) {
logger.log(Level.INFO, "Searching entity in cache");
return (Entity) keycache.get(key);
}
/**
* Utility method to send the error back to UI
*
* @param data
* @param resp
* @throws IOException
*/
public static String getErrorResponse(Exception ex) throws IOException {
return "Error:" + ex.toString();
}
/**
* Utility method to get the datastore service in entities
*
* @return datastore
*/
public static DatastoreService getDatastoreServiceInstance() {
return datastore;
}
/**
* @author cassiano
*
* Utility method to get the number of entity items on an iterable
*
* @param iterable: iterable to have its items count
* @return the items count as an integer
*/
public static int iterableItemsCount(Iterable<Entity> iterable)
{
int total = 0;
if (iterable != null) {
Iterator<Entity> it = iterable.iterator();
while (it.hasNext()) {
it.next();
total++;
}
}
return total;
}
}