Search in sources :

Example 1 with Entity

use of com.google.appengine.api.datastore.Entity in project qi4j-sdk by Qi4j.

the class GaeEntityStoreMixin method get.

@Override
public Reader get(EntityReference ref) throws EntityStoreException {
    try {
        Key key = KeyFactory.createKey(entityKind, ref.toURI());
        Entity entity = datastore.get(key);
        Text serializedState = (Text) entity.getProperty("value");
        if (serializedState == null) {
            throw new EntityNotFoundException(ref);
        }
        return new StringReader(serializedState.getValue());
    } catch (com.google.appengine.api.datastore.EntityNotFoundException e) {
        e.printStackTrace();
        throw new EntityNotFoundException(ref);
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) StringReader(java.io.StringReader) Text(com.google.appengine.api.datastore.Text) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) Key(com.google.appengine.api.datastore.Key)

Example 2 with Entity

use of com.google.appengine.api.datastore.Entity in project siena by mandubian.

the class GaeMappingUtils method createEntityInstanceFromParent.

public static Entity createEntityInstanceFromParent(Field idField, ClassInfo info, Object obj, Key parentKey, ClassInfo parentInfo, Field parentField) {
    Entity entity = null;
    Id id = idField.getAnnotation(Id.class);
    Class<?> type = idField.getType();
    if (id != null) {
        switch(id.value()) {
            case NONE:
                Object idVal = null;
                idVal = Util.readField(obj, idField);
                if (idVal == null)
                    throw new SienaException("Id Field " + idField.getName() + " value null");
                String keyVal = Util.toString(idField, idVal);
                entity = new Entity(getKindWithAncestorField(info, parentInfo, parentField), keyVal, parentKey);
                break;
            case AUTO_INCREMENT:
                // manages String ID as not long!!!
                if (Long.TYPE == type || Long.class.isAssignableFrom(type)) {
                    entity = new Entity(getKindWithAncestorField(info, parentInfo, parentField), parentKey);
                } else {
                    Object idStringVal = null;
                    idStringVal = Util.readField(obj, idField);
                    if (idStringVal == null)
                        throw new SienaException("Id Field " + idField.getName() + " value null");
                    String keyStringVal = Util.toString(idField, idStringVal);
                    entity = new Entity(getKindWithAncestorField(info, parentInfo, parentField), keyStringVal, parentKey);
                }
                break;
            case UUID:
                entity = new Entity(getKindWithAncestorField(info, parentInfo, parentField), UUID.randomUUID().toString(), parentKey);
                break;
            default:
                throw new SienaRestrictedApiException("DB", "createEntityInstance", "Id Generator " + id.value() + " not supported");
        }
    } else
        throw new SienaException("Field " + idField.getName() + " is not an @Id field");
    return entity;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) SienaRestrictedApiException(siena.SienaRestrictedApiException) Id(siena.Id) SienaException(siena.SienaException)

Example 3 with Entity

use of com.google.appengine.api.datastore.Entity in project siena by mandubian.

the class GaeMappingUtils method mapEntitiesKeysOnly.

public static <T> List<T> mapEntitiesKeysOnly(Iterable<Entity> entities, Class<T> clazz) {
    Field id = ClassInfo.getIdField(clazz);
    List<T> list = new ArrayList<T>();
    for (Entity entity : entities) {
        T obj;
        try {
            obj = Util.createObjectInstance(clazz);
            list.add(obj);
            setIdFromKey(id, obj, entity.getKey());
        } catch (SienaException e) {
            throw e;
        } catch (Exception e) {
            throw new SienaException(e);
        }
    }
    return list;
}
Also used : Field(java.lang.reflect.Field) Entity(com.google.appengine.api.datastore.Entity) ArrayList(java.util.ArrayList) SienaException(siena.SienaException) SienaException(siena.SienaException) IOException(java.io.IOException) SienaRestrictedApiException(siena.SienaRestrictedApiException)

Example 4 with Entity

use of com.google.appengine.api.datastore.Entity in project siena by mandubian.

the class GaeMappingUtils method createEntityInstanceForUpdate.

public static Entity createEntityInstanceForUpdate(ClassInfo info, Object obj) {
    Key key = makeKey(info, obj);
    Entity entity = new Entity(key);
    return entity;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Key(com.google.appengine.api.datastore.Key)

Example 5 with Entity

use of com.google.appengine.api.datastore.Entity in project siena by mandubian.

the class GaeMappingUtils method mapEntities.

public static <T> List<T> mapEntities(List<Entity> entities, Class<T> clazz) {
    Field id = ClassInfo.getIdField(clazz);
    List<T> list = new ArrayList<T>(entities.size());
    for (Entity entity : entities) {
        T obj;
        try {
            obj = Util.createObjectInstance(clazz);
            fillModel(obj, entity);
            list.add(obj);
            setIdFromKey(id, obj, entity.getKey());
        } catch (SienaException e) {
            throw e;
        } catch (Exception e) {
            throw new SienaException(e);
        }
    }
    return list;
}
Also used : Field(java.lang.reflect.Field) Entity(com.google.appengine.api.datastore.Entity) ArrayList(java.util.ArrayList) SienaException(siena.SienaException) SienaException(siena.SienaException) IOException(java.io.IOException) SienaRestrictedApiException(siena.SienaRestrictedApiException)

Aggregations

Entity (com.google.appengine.api.datastore.Entity)50 Key (com.google.appengine.api.datastore.Key)34 ArrayList (java.util.ArrayList)25 Field (java.lang.reflect.Field)22 ClassInfo (siena.ClassInfo)17 SienaException (siena.SienaException)15 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)9 QueryResultList (com.google.appengine.api.datastore.QueryResultList)9 HashMap (java.util.HashMap)9 List (java.util.List)9 SienaFutureContainer (siena.core.async.SienaFutureContainer)9 SienaFutureWrapper (siena.core.async.SienaFutureWrapper)9 Map (java.util.Map)8 SienaRestrictedApiException (siena.SienaRestrictedApiException)6 DatastoreService (com.google.appengine.api.datastore.DatastoreService)5 IOException (java.io.IOException)5 Date (java.util.Date)4 Query (com.google.appengine.api.datastore.Query)3 ShortBlob (com.google.appengine.api.datastore.ShortBlob)3 NotImplementedException (org.apache.commons.lang.NotImplementedException)3