use of siena.SienaRestrictedApiException 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;
}
use of siena.SienaRestrictedApiException in project siena by mandubian.
the class GaeMappingUtils method setIdFromKey.
public static void setIdFromKey(Field idField, Object obj, Key key) {
Id id = idField.getAnnotation(Id.class);
Class<?> type = idField.getType();
if (id != null) {
switch(id.value()) {
case NONE:
//idField.setAccessible(true);
Object val = null;
if (Long.TYPE == type || Long.class.isAssignableFrom(type)) {
val = Long.parseLong((String) key.getName());
} else if (String.class.isAssignableFrom(type)) {
val = key.getName();
} else {
throw new SienaRestrictedApiException("DB", "setKey", "Id Type " + idField.getType() + " not supported");
}
Util.setField(obj, idField, val);
break;
case AUTO_INCREMENT:
// Long value means key.getId()
if (Long.TYPE == type || Long.class.isAssignableFrom(idField.getType())) {
Util.setField(obj, idField, key.getId());
} else {
idField.setAccessible(true);
Object val2 = null;
if (Long.TYPE == type || Long.class.isAssignableFrom(idField.getType())) {
val = Long.parseLong((String) key.getName());
} else if (String.class.isAssignableFrom(idField.getType())) {
val = key.getName();
} else {
throw new SienaRestrictedApiException("DB", "setKey", "Id Type " + idField.getType() + " not supported");
}
Util.setField(obj, idField, val2);
}
break;
case UUID:
Util.setField(obj, idField, key.getName());
break;
default:
throw new SienaException("Id Generator " + id.value() + " not supported");
}
} else
throw new SienaException("Field " + idField.getName() + " is not an @Id field");
}
use of siena.SienaRestrictedApiException in project siena by mandubian.
the class DdlGenerator method createColumn.
private Column createColumn(Class<?> clazz, Field field, String col) {
Class<?> type = field.getType();
Column column = new Column();
column.setName(col);
int columnType;
if (type == Byte.class || type == Byte.TYPE)
columnType = Types.TINYINT;
else if (type == Short.class || type == Short.TYPE)
columnType = Types.SMALLINT;
else if (type == Integer.class || type == Integer.TYPE)
columnType = Types.INTEGER;
else if (type == Long.class || type == Long.TYPE)
columnType = Types.BIGINT;
else if (// TODO verify
type == Float.class || type == Float.TYPE)
// TODO verify
columnType = Types.FLOAT;
else if (// TODO verify
type == Double.class || type == Double.TYPE)
// TODO verify
columnType = Types.DOUBLE;
else if (type == String.class) {
if (field.getAnnotation(Text.class) != null) {
columnType = Types.LONGVARCHAR;
} else {
columnType = Types.VARCHAR;
Max max = field.getAnnotation(Max.class);
if (max == null) {
//throw new SienaRestrictedApiException(DB, "createColumn", "Field "+field.getName()+" in class "
// +clazz.getName()+" doesn't have a @Max annotation");
// default is 255 chars as in hibernate
column.setSize("255");
} else
column.setSize("" + max.value());
}
} else if (type == Boolean.class || type == Boolean.TYPE)
columnType = Types.BOOLEAN;
else if (type == Date.class) {
if (field.getAnnotation(DateTime.class) != null)
columnType = Types.TIMESTAMP;
else if (field.getAnnotation(Time.class) != null)
columnType = Types.TIME;
else if (field.getAnnotation(SimpleDate.class) != null)
columnType = Types.DATE;
else
columnType = Types.TIMESTAMP;
} else if (type == Json.class) {
columnType = Types.LONGVARCHAR;
} else if (type == byte[].class) {
columnType = Types.BLOB;
} else if (Enum.class.isAssignableFrom(type)) {
// enums are stored as string
columnType = Types.VARCHAR;
Max max = field.getAnnotation(Max.class);
if (max == null)
// fixes by default to this value in order to prevent alter tables every time
column.setSize("" + 255);
else
column.setSize("" + max.value());
} else if (type == BigDecimal.class) {
DecimalPrecision an = field.getAnnotation(DecimalPrecision.class);
if (an == null) {
columnType = Types.DECIMAL;
column.setSizeAndScale(19, 2);
} else {
if (an.storageType() == DecimalPrecision.StorageType.NATIVE) {
columnType = Types.DECIMAL;
column.setSizeAndScale(an.size(), an.scale());
} else if (an.storageType() == DecimalPrecision.StorageType.STRING) {
columnType = Types.VARCHAR;
// should be an.size+"."+sign
column.setSize((an.size() + 2) + "");
} else if (an.storageType() == DecimalPrecision.StorageType.DOUBLE) {
columnType = Types.DOUBLE;
} else {
columnType = Types.DECIMAL;
column.setSizeAndScale(19, 2);
}
}
} else {
Embedded embedded = field.getAnnotation(Embedded.class);
if (embedded != null) {
if ("h2".equals(DB)) {
columnType = Types.CLOB;
} else {
columnType = Types.LONGVARCHAR;
}
} else if (field.isAnnotationPresent(Polymorphic.class)) {
columnType = Types.BLOB;
} else {
throw new SienaRestrictedApiException(DB, "createColumn", "Unsupported type for field " + clazz.getName() + "." + field.getName());
}
}
column.setTypeCode(columnType);
return column;
}
use of siena.SienaRestrictedApiException in project siena by mandubian.
the class JdbcPersistenceManager method appendSqlWhere.
public <T> void appendSqlWhere(Query<T> query, StringBuilder sql, List<Object> parameters) {
Class<T> clazz = query.getQueriedClass();
JdbcClassInfo info = JdbcClassInfo.getClassInfo(clazz);
List<QueryFilter> filters = query.getFilters();
if (filters.isEmpty()) {
return;
}
sql.append(JdbcDBUtils.WHERE);
boolean first = true;
for (QueryFilter filter : filters) {
if (QueryFilterSimple.class.isAssignableFrom(filter.getClass())) {
QueryFilterSimple qf = (QueryFilterSimple) filter;
String op = qf.operator;
Object value = qf.value;
Field f = qf.field;
if (!first) {
sql.append(JdbcDBUtils.AND);
}
first = false;
String[] columns = ClassInfo.getColumnNames(f, info.tableName);
if ("IN".equals(op)) {
if (!Collection.class.isAssignableFrom(value.getClass()))
throw new SienaException("Collection needed when using IN operator in filter() query");
StringBuilder s = new StringBuilder();
Collection<?> col = (Collection<?>) value;
for (Object object : col) {
// TODO: if object isModel
parameters.add(object);
s.append(",?");
}
sql.append(columns[0] + " IN(" + s.toString().substring(1) + ")");
} else if (ClassInfo.isModel(f.getType())) {
if (!op.equals("=")) {
throw new SienaException("Unsupported operator for relationship: " + op);
}
JdbcClassInfo classInfo = JdbcClassInfo.getClassInfo(f.getType());
int i = 0;
JdbcMappingUtils.checkForeignKeyMapping(classInfo.keys, columns, query.getQueriedClass(), f);
for (Field key : classInfo.keys) {
if (value == null) {
sql.append(columns[i++] + JdbcDBUtils.IS_NULL);
} else {
sql.append(columns[i++] + "=?");
key.setAccessible(true);
Object o;
try {
o = key.get(value);
parameters.add(o);
} catch (Exception e) {
throw new SienaException(e);
}
}
}
} else {
if (value == null && op.equals("=")) {
sql.append(columns[0] + JdbcDBUtils.IS_NULL);
} else if (value == null && op.equals("!=")) {
sql.append(columns[0] + JdbcDBUtils.IS_NOT_NULL);
} else {
sql.append(columns[0] + op + "?");
if (value == null) {
parameters.add(Types.NULL);
} else {
if (value instanceof Date) {
value = Util.translateDate(f, (Date) value);
} else if (value instanceof Enum) {
value = value.toString();
}
parameters.add(value);
}
}
}
} else if (QueryFilterSearch.class.isAssignableFrom(filter.getClass())) {
// TODO MYSQL implementation manages only 1 search in a query
if (query.getSearches().size() > 1) {
throw new SienaRestrictedApiException(DB, "search", "MySQL implementation manages only on single search at a time in a query");
}
// adds querysearch
QueryFilterSearch qf = (QueryFilterSearch) filter;
appendSqlSearch(qf, clazz, info, sql, parameters);
}
}
}
use of siena.SienaRestrictedApiException in project siena by mandubian.
the class JdbcDBUtils method setObject.
public static void setObject(PreparedStatement ps, int index, Object value, Field field, String DB) throws SQLException {
if (value == null) {
ps.setNull(index, JdbcDBUtils.toSqlType(value, field, DB));
return;
}
Class<?> type = field.getType();
if (type == Byte.class || type == Byte.TYPE)
ps.setByte(index, (Byte) value);
else if (type == Short.class || type == Short.TYPE)
ps.setShort(index, (Short) value);
else if (type == Integer.class || type == Integer.TYPE)
ps.setInt(index, (Integer) value);
else if (type == Long.class || type == Long.TYPE)
ps.setLong(index, (Long) value);
else if (type == Float.class || type == Float.TYPE)
ps.setFloat(index, (Float) value);
else if (type == Double.class || type == Double.TYPE)
ps.setDouble(index, (Double) value);
else if (type == String.class) {
ps.setString(index, (String) value);
} else if (type == Boolean.class || type == Boolean.TYPE)
ps.setBoolean(index, (Boolean) value);
else if (type == Date.class) {
if (field.getAnnotation(DateTime.class) != null) {
java.sql.Timestamp ts = new java.sql.Timestamp(((Date) value).getTime());
ps.setTimestamp(index, ts);
} else if (field.getAnnotation(Time.class) != null) {
java.sql.Time ts = new java.sql.Time(((Date) value).getTime());
ps.setTime(index, ts);
} else if (field.getAnnotation(SimpleDate.class) != null) {
java.sql.Date d = new java.sql.Date(((Date) value).getTime());
ps.setDate(index, d);
} else {
java.sql.Timestamp ts = new java.sql.Timestamp(((Date) value).getTime());
ps.setTimestamp(index, ts);
}
} else if (type == Json.class) {
ps.setString(index, (String) value);
} else if (type == byte[].class) {
ByteArrayInputStream bis = new ByteArrayInputStream((byte[]) value);
ps.setBlob(index, bis);
} else if (Enum.class.isAssignableFrom(type)) {
ps.setString(index, (String) value);
} else if (type == BigDecimal.class) {
DecimalPrecision an = field.getAnnotation(DecimalPrecision.class);
if (an == null) {
ps.setObject(index, value);
} else {
if (an.storageType() == DecimalPrecision.StorageType.NATIVE) {
ps.setBigDecimal(index, (BigDecimal) value);
} else if (an.storageType() == DecimalPrecision.StorageType.STRING) {
ps.setString(index, ((BigDecimal) value).toPlainString());
} else if (an.storageType() == DecimalPrecision.StorageType.DOUBLE) {
ps.setDouble(index, ((BigDecimal) value).doubleValue());
} else {
ps.setBigDecimal(index, (BigDecimal) value);
}
}
} else {
Embedded embedded = field.getAnnotation(Embedded.class);
if (embedded != null) {
if ("h2".equals(DB)) {
StringReader reader = new StringReader((String) value);
ps.setClob(index, reader);
} else {
ps.setString(index, (String) value);
}
} else if (field.isAnnotationPresent(Polymorphic.class)) {
ByteArrayInputStream bis = new ByteArrayInputStream((byte[]) value);
ps.setBlob(index, bis);
} else {
throw new SienaRestrictedApiException(DB, "createColumn", "Unsupported type for field " + type.getName() + "." + field.getName());
}
}
}
Aggregations