Search in sources :

Example 56 with SienaException

use of siena.SienaException in project siena by mandubian.

the class BaseTest method testTransactionInsertBatchFailure.

public void testTransactionInsertBatchFailure() {
    TransactionAccountFrom accFrom = new TransactionAccountFrom(1000L);
    TransactionAccountTo accTo = new TransactionAccountTo(1000L);
    try {
        pm.beginTransaction(Connection.TRANSACTION_READ_COMMITTED);
        accFrom.amount = 1000L;
        accTo.amount = 100L;
        pm.insert(accFrom, accTo);
        throw new SienaException("test");
    } catch (SienaException e) {
        pm.rollbackTransaction();
    } finally {
        pm.closeConnection();
    }
    TransactionAccountFrom accFromAfter = pm.getByKey(TransactionAccountFrom.class, accFrom.id);
    assertNull(accFromAfter);
    TransactionAccountTo accToAfter = pm.getByKey(TransactionAccountTo.class, accTo.id);
    assertNull(accToAfter);
}
Also used : TransactionAccountTo(siena.base.test.model.TransactionAccountTo) TransactionAccountFrom(siena.base.test.model.TransactionAccountFrom) SienaException(siena.SienaException)

Example 57 with SienaException

use of siena.SienaException in project siena by mandubian.

the class GaeMappingUtils method mapRelation.

public static <T> T mapRelation(Query<T> query, T obj, ClassInfo info) {
    // aggregators/owners
    List<QueryAggregated> aggregs = query.getAggregatees();
    List<QueryOwned> ownees = query.getOwnees();
    if (aggregs.isEmpty() && ownees.isEmpty()) {
        return obj;
    }
    if (aggregs.size() == 1) {
        // aggregators
        QueryAggregated aggreg = aggregs.get(0);
        Relation rel = new Relation(RelationMode.AGGREGATION, aggreg.aggregator, aggreg.field);
        Util.setField(obj, info.aggregator, rel);
    } else if (aggregs.size() > 1) {
        throw new SienaException("Only one aggregation per query allowed");
    }
    if (ownees.size() == 1) {
        // owners
        QueryOwned ownee = ownees.get(0);
        Util.setField(obj, ownee.field, ownee.owner);
    } else if (ownees.size() > 1) {
        throw new SienaException("Only one owner per query allowed");
    }
    return obj;
}
Also used : QueryOwned(siena.QueryOwned) Relation(siena.core.Relation) SienaException(siena.SienaException) QueryAggregated(siena.QueryAggregated)

Example 58 with SienaException

use of siena.SienaException in project siena by mandubian.

the class GaeMappingUtils method fillModelAndKey.

public static void fillModelAndKey(Object obj, Entity entity) {
    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    Field id = info.getIdField();
    Class<?> fieldClass = id.getType();
    Key key = entity.getKey();
    if (key != null) {
        setIdFromKey(id, obj, key);
    }
    for (Field field : info.updateFields) {
        String property = ClassInfo.getColumnNames(field)[0];
        try {
            fieldClass = field.getType();
            if (ClassInfo.isModel(fieldClass) && !ClassInfo.isEmbedded(field)) {
                /*if(!ClassInfo.isAggregated(field)){*/
                key = (Key) entity.getProperty(property);
                if (key != null) {
                    Object value = Util.createObjectInstance(fieldClass);
                    id = ClassInfo.getIdField(fieldClass);
                    setIdFromKey(id, value, key);
                    Util.setField(obj, field, value);
                }
            /*}*/
            } else if (ClassInfo.isEmbedded(field) && field.getAnnotation(Embedded.class).mode() == Embedded.Mode.NATIVE) {
                Object value = GaeNativeSerializer.unembed(field.getType(), ClassInfo.getSingleColumnName(field), entity, 0);
                Util.setField(obj, field, value);
            } else /*else if(ClassInfo.isAggregated(field)){
					// does nothing for the time being
				}
				else if (ClassInfo.isOwned(field)){
					// does nothing for the time being
				}*/
            {
                setFromObject(obj, field, entity.getProperty(property));
            }
        } catch (Exception e) {
            throw new SienaException(e);
        }
    }
}
Also used : Field(java.lang.reflect.Field) SienaException(siena.SienaException) Key(com.google.appengine.api.datastore.Key) SienaException(siena.SienaException) IOException(java.io.IOException) SienaRestrictedApiException(siena.SienaRestrictedApiException) ClassInfo(siena.ClassInfo)

Example 59 with SienaException

use of siena.SienaException in project siena by mandubian.

the class GaePersistenceManager method mapJoins.

protected <T> List<T> mapJoins(List<T> models) {
    try {
        // join queries
        Map<Field, ArrayList<Key>> fieldMap = null;
        // creates the list of joined entity keys to extract
        for (final T model : models) {
            // initializes fieldMap
            if (fieldMap == null) {
                fieldMap = GaeQueryUtils.buildJoinFieldKeysMap(model);
            }
            for (Field field : fieldMap.keySet()) {
                Object objVal = Util.readField(model, field);
                // our object is not linked to another object...so it doesn't have any key
                if (objVal == null) {
                    continue;
                }
                Key key = GaeMappingUtils.getKey(objVal);
                List<Key> keys = fieldMap.get(field);
                if (!keys.contains(key))
                    keys.add(key);
            }
        }
        Map<Field, Map<Key, Entity>> entityMap = new HashMap<Field, Map<Key, Entity>>();
        try {
            // retrieves all joined entities per field
            for (Field field : fieldMap.keySet()) {
                Map<Key, Entity> entities = ds.get(fieldMap.get(field));
                // gets the future here because we need it so we wait for it
                entityMap.put(field, entities);
            }
        } catch (Exception ex) {
            throw new SienaException(ex);
        }
        // associates linked models to their models
        // linkedModels is just a map to contain entities already mapped
        Map<Key, Object> linkedModels = new HashMap<Key, Object>();
        Object linkedObj;
        Entity entity;
        for (final T model : models) {
            for (Field field : fieldMap.keySet()) {
                Object objVal = Util.readField(model, field);
                // our object is not linked to another object...so it doesn't have any key
                if (objVal == null) {
                    continue;
                }
                Key key = GaeMappingUtils.getKey(objVal);
                linkedObj = linkedModels.get(key);
                if (linkedObj == null) {
                    entity = entityMap.get(field).get(key);
                    linkedObj = objVal;
                    GaeMappingUtils.fillModel(linkedObj, entity);
                    linkedModels.put(key, linkedObj);
                }
                field.set(model, linkedObj);
            }
        }
        return models;
    } catch (IllegalAccessException ex) {
        throw new SienaException(ex);
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SienaException(siena.SienaException) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) Field(java.lang.reflect.Field) SienaException(siena.SienaException) HashMap(java.util.HashMap) Map(java.util.Map) Key(com.google.appengine.api.datastore.Key)

Example 60 with SienaException

use of siena.SienaException in project siena by mandubian.

the class GaePersistenceManager method mapJoins.

protected <T> T mapJoins(T model) {
    try {
        // join queries
        Map<Field, ArrayList<Key>> fieldMap = GaeQueryUtils.buildJoinFieldKeysMap(model);
        // creates the list of joined entity keys to extract
        for (Field field : fieldMap.keySet()) {
            Key key = GaeMappingUtils.getKey(field.get(model));
            List<Key> keys = fieldMap.get(field);
            if (!keys.contains(key))
                keys.add(key);
        }
        Map<Field, Map<Key, Entity>> entityMap = new HashMap<Field, Map<Key, Entity>>();
        try {
            // retrieves all joined entities per field
            for (Field field : fieldMap.keySet()) {
                Map<Key, Entity> entities = ds.get(fieldMap.get(field));
                entityMap.put(field, entities);
            }
        } catch (Exception ex) {
            throw new SienaException(ex);
        }
        // associates linked models to their models
        // linkedModels is just a map to contain entities already mapped
        Map<Key, Object> linkedModels = new HashMap<Key, Object>();
        Object linkedObj;
        Entity entity;
        for (Field field : fieldMap.keySet()) {
            Object objVal = field.get(model);
            Key key = GaeMappingUtils.getKey(objVal);
            linkedObj = linkedModels.get(key);
            if (linkedObj == null) {
                entity = entityMap.get(field).get(key);
                linkedObj = objVal;
                GaeMappingUtils.fillModel(linkedObj, entity);
                linkedModels.put(key, linkedObj);
            }
            field.set(model, linkedObj);
        }
        return model;
    } catch (IllegalAccessException ex) {
        throw new SienaException(ex);
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SienaException(siena.SienaException) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) Field(java.lang.reflect.Field) SienaException(siena.SienaException) HashMap(java.util.HashMap) Map(java.util.Map) Key(com.google.appengine.api.datastore.Key)

Aggregations

SienaException (siena.SienaException)214 Field (java.lang.reflect.Field)65 TransactionAccountFrom (siena.base.test.model.TransactionAccountFrom)52 ArrayList (java.util.ArrayList)46 IOException (java.io.IOException)40 SQLException (java.sql.SQLException)38 TransactionAccountTo (siena.base.test.model.TransactionAccountTo)34 SienaRestrictedApiException (siena.SienaRestrictedApiException)33 PreparedStatement (java.sql.PreparedStatement)25 ClassInfo (siena.ClassInfo)21 HashMap (java.util.HashMap)19 List (java.util.List)18 Discovery (siena.base.test.model.Discovery)18 Entity (com.google.appengine.api.datastore.Entity)15 Key (com.google.appengine.api.datastore.Key)14 ResultSet (java.sql.ResultSet)14 Id (siena.Id)14 AmazonClientException (com.amazonaws.AmazonClientException)12 Collection (java.util.Collection)10 DiscoveryStringId (siena.base.test.model.DiscoveryStringId)9