Search in sources :

Example 16 with SienaException

use of siena.SienaException in project siena by mandubian.

the class JdbcMappingUtils method mapObjectKeys.

public static <T> T mapObjectKeys(Class<T> clazz, ResultSet rs, String tableName, List<Field> joinFields) {
    try {
        T obj = Util.createObjectInstance(clazz);
        mapObjectKeys(obj, rs, tableName, joinFields);
        return obj;
    } catch (SienaException e) {
        throw e;
    } catch (Exception e) {
        throw new SienaException(e);
    }
}
Also used : SienaException(siena.SienaException) IOException(java.io.IOException) SQLException(java.sql.SQLException) SienaException(siena.SienaException)

Example 17 with SienaException

use of siena.SienaException in project siena by mandubian.

the class JdbcMappingUtils method mapField.

public static void mapField(Object obj, Field field, ResultSet rs, String tableName, List<Field> joinFields) {
    Class<?> type = field.getType();
    // field.setAccessible(true);
    try {
        if (ClassInfo.isModel(type) && !ClassInfo.isEmbedded(field)) {
            JdbcClassInfo fieldClassInfo = JdbcClassInfo.getClassInfo(type);
            if (joinFields == null || joinFields.size() == 0 || !joinFields.contains(field)) {
                String[] fks = ClassInfo.getColumnNames(field, tableName);
                Object rel = Util.createObjectInstance(type);
                boolean none = false;
                int i = 0;
                checkForeignKeyMapping(fieldClassInfo.keys, fks, obj.getClass(), field);
                for (Field f : fieldClassInfo.keys) {
                    Object o = rs.getObject(JdbcClassInfo.aliasFromCol(fks[i++]));
                    if (o == null) {
                        none = true;
                        break;
                    }
                    setFromObject(rel, f, o);
                }
                if (!none) {
                    Util.setField(obj, field, rel);
                // field.set(obj, rel);
                }
            } else {
                // this is a JOIN field
                // first verifies the field is not null
                Object val = rs.getObject(JdbcClassInfo.aliasFromCol(ClassInfo.getColumnNames(field, tableName)[0]));
                if (val == null) {
                    Util.setField(obj, field, null);
                    return;
                }
                // uses join field alias
                // Object rel = mapObject(type, rs, fieldClassInfo.tableName, null);
                Object rel = mapObject(type, rs, fieldClassInfo.joinFieldAliases.get(field.getName()), null);
                Util.setField(obj, field, rel);
            }
        } else {
            Object val = rs.getObject(ClassInfo.getColumnNames(field, tableName)[0].replace('.', '_'));
            setFromObject(obj, field, val);
        }
    } catch (SienaException e) {
        throw e;
    } catch (Exception e) {
        throw new SienaException(e);
    }
}
Also used : Field(java.lang.reflect.Field) JdbcClassInfo(siena.jdbc.JdbcPersistenceManager.JdbcClassInfo) SienaException(siena.SienaException) IOException(java.io.IOException) SQLException(java.sql.SQLException) SienaException(siena.SienaException)

Example 18 with SienaException

use of siena.SienaException in project siena by mandubian.

the class RemotePersistenceManager method sendAndParse.

@SuppressWarnings("unchecked")
private <T> List<T> sendAndParse(Document request) {
    Document response = send(request);
    Element root = response.getRootElement();
    if ("error".equals(root.getName())) {
        throw new SienaException(root.attributeValue("class") + " " + root.getText());
    }
    List<Element> result = response.getRootElement().elements("object");
    List<T> list = new ArrayList<T>(result.size());
    for (Element element : result) {
        list.add((T) Common.parseEntity(element, null));
    }
    return list;
}
Also used : Element(org.dom4j.Element) ArrayList(java.util.ArrayList) SienaException(siena.SienaException) Document(org.dom4j.Document)

Example 19 with SienaException

use of siena.SienaException in project siena by mandubian.

the class RemotePersistenceManager method send.

protected Document send(Document request) {
    try {
        connector.connect();
        serializer.serialize(request, connector.getOutputStream());
        Document response = serializer.deserialize(connector.getInputStream());
        connector.close();
        return response;
    } catch (IOException e) {
        throw new SienaException(e);
    }
}
Also used : IOException(java.io.IOException) SienaException(siena.SienaException) Document(org.dom4j.Document)

Example 20 with SienaException

use of siena.SienaException in project siena by mandubian.

the class BaseAsyncTest method testOffsetLimitStatelessPaginate2Iter.

public void testOffsetLimitStatelessPaginate2Iter() {
    Discovery[] discs = new Discovery[300];
    for (int i = 0; i < 300; i++) {
        discs[i] = new Discovery("Disc_" + i, LongAutoID_CURIE);
    }
    pm.insert((Object[]) discs).get();
    QueryAsync<Discovery> query = pm.createQuery(Discovery.class).order("id");
    SienaFuture<Iterable<Discovery>> future = query.limit(50).offset(12).iter();
    Iterable<Discovery> iter = future.get();
    Iterator<Discovery> it = iter.iterator();
    int i = 12;
    while (it.hasNext()) {
        Discovery disc = it.next();
        assertEquals(discs[i++], disc);
    }
    assertEquals(62, i);
    future = query.offset(13).limit(30).iter();
    iter = future.get();
    it = iter.iterator();
    i = 13;
    while (it.hasNext()) {
        Discovery disc = it.next();
        assertEquals(discs[i++], disc);
    }
    assertEquals(43, i);
    future = query.offset(10).limit(30).iter(15);
    iter = future.get();
    it = iter.iterator();
    i = 10;
    while (it.hasNext()) {
        Discovery disc = it.next();
        assertEquals(discs[i++], disc);
    }
    assertEquals(25, i);
    future = query.paginate(6).iter();
    iter = future.get();
    it = iter.iterator();
    i = 0;
    while (it.hasNext()) {
        Discovery disc = it.next();
        assertEquals(discs[i++], disc);
    }
    assertEquals(6, i);
    future = query.nextPage().iter();
    iter = future.get();
    it = iter.iterator();
    i = 6;
    while (it.hasNext()) {
        Discovery disc = it.next();
        assertEquals(discs[i++], disc);
    }
    assertEquals(12, i);
    future = query.nextPage().iter();
    iter = future.get();
    it = iter.iterator();
    i = 12;
    while (it.hasNext()) {
        Discovery disc = it.next();
        assertEquals(discs[i++], disc);
    }
    assertEquals(18, i);
    future = query.previousPage().iter();
    iter = future.get();
    it = iter.iterator();
    i = 6;
    while (it.hasNext()) {
        Discovery disc = it.next();
        assertEquals(discs[i++], disc);
    }
    assertEquals(12, i);
    future = query.offset(10).iter(10);
    iter = future.get();
    it = iter.iterator();
    i = 10;
    while (it.hasNext()) {
        Discovery disc = it.next();
        assertEquals(discs[i++], disc);
    }
    assertEquals(20, i);
    try {
        future = query.nextPage().iter();
    } catch (SienaException ex) {
        future = query.paginate(8).iter();
        iter = future.get();
        it = iter.iterator();
        i = 0;
        while (it.hasNext()) {
            Discovery disc = it.next();
            assertEquals(discs[i++], disc);
        }
        assertEquals(8, i);
        future = query.nextPage().iter();
        iter = future.get();
        it = iter.iterator();
        i = 8;
        while (it.hasNext()) {
            Discovery disc = it.next();
            assertEquals(discs[i++], disc);
        }
        assertEquals(16, i);
        return;
    }
    fail();
}
Also used : Discovery(siena.base.test.model.Discovery) SienaException(siena.SienaException)

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