Search in sources :

Example 91 with SienaException

use of siena.SienaException in project siena by mandubian.

the class PostgresqlPersistenceManager method save.

@Override
public void save(Object obj) {
    JdbcClassInfo classInfo = JdbcClassInfo.getClassInfo(obj.getClass());
    List<String> keyNames = new ArrayList<String>();
    for (Field field : classInfo.keys) {
        keyNames.add(field.getName());
    }
    PreparedStatement ps = null;
    try {
        Field idField = classInfo.info.getIdField();
        Object idVal = Util.readField(obj, idField);
        if (idVal == null) {
            insert(obj);
        } else {
            // !!! insert or update pour postgres : the less worst solution I found!!!!
            // INSERT INTO myTable (myKey) SELECT myKeyValue WHERE myKeyValue NOT IN (SELECT myKey FROM myTable);
            // UPDATE myTable SET myUpdateCol = myUpdateColValue WHERE myKey = myKeyValue;
            ps = getConnection().prepareStatement("INSERT INTO " + classInfo.tableName + " (" + Util.join(keyNames, ",") + ") " + "SELECT ? WHERE ? NOT IN (SELECT " + Util.join(keyNames, ",") + " FROM " + classInfo.tableName + ");" + classInfo.updateSQL);
            int i = 1;
            i = addParameters(obj, classInfo.keys, ps, i);
            i = addParameters(obj, classInfo.keys, ps, i);
            i = addParameters(obj, classInfo.updateFields, ps, i);
            addParameters(obj, classInfo.keys, ps, i);
            ps.executeUpdate();
        }
    } catch (SienaException e) {
        throw e;
    } catch (Exception e) {
        throw new SienaException(e);
    } finally {
        JdbcDBUtils.closeStatementAndConnection(this, ps);
    }
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) SienaException(siena.SienaException) SQLException(java.sql.SQLException) SienaException(siena.SienaException)

Example 92 with SienaException

use of siena.SienaException in project siena by mandubian.

the class ThreadedConnectionManager method closeConnection.

public void closeConnection() {
    try {
        Connection c = currentConnection.get();
        if (c != null) {
            currentConnection.remove();
            c.close();
        }
    } catch (SQLException e) {
        throw new SienaException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SienaException(siena.SienaException)

Example 93 with SienaException

use of siena.SienaException in project siena by mandubian.

the class JsonSerializer method deserialize.

public static Object deserialize(Field f, Json data) {
    if (data == null || data.isNull())
        return deserializePlain(f.getType(), data);
    Class<?> clazz = f.getType();
    if (Map.class.isAssignableFrom(clazz)) {
        if (!data.isMap()) {
            throw new SienaException("Error while deserializating field " + f.getDeclaringClass() + "." + f.getName() + " of type " + clazz + ". A Json map is needed but found: " + data);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        for (String key : data.keys()) {
            map.put(key, deserialize(Util.getGenericClass(f, 1), data.get(key)));
        }
        return map;
    } else if (Collection.class.isAssignableFrom(clazz)) {
        if (!data.isList()) {
            throw new SienaException("Error while deserializating field " + f.getDeclaringClass() + "." + f.getName() + " of type " + clazz + ". A Json list is needed but found: " + data);
        }
        Collection<Object> collection = null;
        if (clazz == List.class) {
            collection = new ArrayList<Object>(data.size());
        } else {
            collection = new HashSet<Object>();
        }
        for (Json value : data) {
            collection.add(deserialize(Util.getGenericClass(f, 0), value));
        }
        return collection;
    } else if (Json.class.isAssignableFrom(clazz)) {
        return data;
    } else if (Field.class.isAssignableFrom(clazz)) {
        String fieldName = data.get("fieldName").asString();
        String parentClass = data.get("parentClass").asString();
        try {
            Class<?> cl = Class.forName(parentClass);
            Field fi = cl.getField(fieldName);
            return fi;
        } catch (ClassNotFoundException ex) {
            throw new SienaException(ex);
        } catch (NoSuchFieldException ex) {
            throw new SienaException(ex);
        }
    } else if (clazz.isArray()) {
        Class<?> arrClazz = clazz.getComponentType();
        Object arr = Array.newInstance(arrClazz, data.size());
        int i = 0;
        for (Json value : data) {
            Array.set(arr, i++, deserialize(arrClazz, value));
        }
        return arr;
    } else if (clazz == Class.class) {
        String className = data.get("className").asString();
        try {
            Class<?> cl = Class.forName(className);
            return cl;
        } catch (ClassNotFoundException ex) {
            throw new SienaException(ex);
        }
    }
    Format format = f.getAnnotation(Format.class);
    if (format != null) {
        if (f.getType() == Date.class) {
            SimpleDateFormat sdf = new SimpleDateFormat(format.value());
            try {
                return sdf.parse(data.str());
            } catch (ParseException e) {
                throw new SienaException(e);
            }
        }
    }
    JsonDeserializeAs as = f.getAnnotation(JsonDeserializeAs.class);
    if (as != null) {
        if (as.value() == String.class) {
            return data.asString();
        } else {
            return deserialize(as.value(), data);
        }
    }
    return deserialize(clazz, data);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Json(siena.Json) Field(java.lang.reflect.Field) SimpleDateFormat(java.text.SimpleDateFormat) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) SienaException(siena.SienaException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) HashSet(java.util.HashSet)

Example 94 with SienaException

use of siena.SienaException in project siena by mandubian.

the class JsonSerializer method deserializeMap.

public static Object deserializeMap(Class<?> clazz, Json data) {
    if (!data.isMap()) {
        throw new SienaException("Error while deserializating class " + clazz + ". A Json map is needed but found: " + data);
    }
    Object obj = Util.createObjectInstance(clazz);
    Field[] fields = clazz.getDeclaredFields();
    for (Field f : fields) {
        if (mustIgnore(f))
            continue;
        Key key = f.getAnnotation(Key.class);
        if (key != null)
            Util.setField(obj, f, deserialize(f, data.get(key.value())));
        else
            Util.setField(obj, f, deserialize(f, data.get(f.getName())));
    }
    // deserializes super classes
    Class<?> superclazz = obj.getClass().getSuperclass();
    while (superclazz != null) {
        fields = superclazz.getDeclaredFields();
        for (Field f : fields) {
            if (mustIgnore(f))
                continue;
            Key key = f.getAnnotation(Key.class);
            if (key != null)
                Util.setField(obj, f, deserialize(f, data.get(key.value())));
            else
                Util.setField(obj, f, deserialize(f, data.get(f.getName())));
        }
        superclazz = superclazz.getSuperclass();
    }
    return obj;
}
Also used : Field(java.lang.reflect.Field) SienaException(siena.SienaException)

Example 95 with SienaException

use of siena.SienaException in project siena by mandubian.

the class GoogleSqlPersistenceManager method addParameters.

@Override
protected int addParameters(Object obj, List<Field> fields, PreparedStatement ps, int i) throws SQLException {
    for (Field field : fields) {
        Class<?> type = field.getType();
        if (ClassInfo.isModel(type) && !ClassInfo.isEmbedded(field)) {
            JdbcClassInfo ci = JdbcClassInfo.getClassInfo(type);
            Object rel = Util.readField(obj, field);
            for (Field f : ci.keys) {
                if (rel != null) {
                    Object value = Util.readField(rel, f);
                    if (value instanceof Json)
                        value = ((Json) value).toString();
                    setParameter(ps, i++, value, f);
                } else {
                    setParameter(ps, i++, null, f);
                }
            }
        } else {
            Object value = Util.readField(obj, field);
            if (value != null) {
                if (Json.class.isAssignableFrom(type)) {
                    value = ((Json) value).toString();
                } else if (field.getAnnotation(Embedded.class) != null) {
                    value = JsonSerializer.serialize(value).toString();
                } else if (field.getAnnotation(Polymorphic.class) != null) {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    ObjectOutput out;
                    try {
                        out = new ObjectOutputStream(bos);
                        out.writeObject(value);
                        out.close();
                    } catch (IOException e) {
                        throw new SienaException(e);
                    }
                    value = bos.toByteArray();
                } else if (Enum.class.isAssignableFrom(type)) {
                    value = value.toString();
                } else if (BigDecimal.class == type) {
                    DecimalPrecision ann = field.getAnnotation(DecimalPrecision.class);
                    if (ann == null) {
                        value = (BigDecimal) value;
                    } else {
                        switch(ann.storageType()) {
                            case DOUBLE:
                                value = ((BigDecimal) value).doubleValue();
                                break;
                            case STRING:
                                value = ((BigDecimal) value).toPlainString();
                                break;
                            case NATIVE:
                                value = (BigDecimal) value;
                                break;
                        }
                    }
                }
            }
            setParameter(ps, i++, value, field);
        }
    }
    return i;
}
Also used : ObjectOutput(java.io.ObjectOutput) DecimalPrecision(siena.core.DecimalPrecision) Json(siena.Json) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Field(java.lang.reflect.Field) SienaException(siena.SienaException) Polymorphic(siena.core.Polymorphic)

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