use of siena.SienaException in project siena by mandubian.
the class GaeQueryUtils method addSearchFilterIn.
public static void addSearchFilterIn(com.google.appengine.api.datastore.Query q, Field field, List<String> matches) {
String[] columns = ClassInfo.getColumnNames(field);
if (columns.length > 1)
throw new SienaException("Search not possible for multi-column fields in GAE: only one field with one column");
q.addFilter(columns[0], FilterOperator.IN, matches);
}
use of siena.SienaException in project siena by mandubian.
the class GaeMappingUtils method getKey.
public static Key getKey(Object obj) {
Class<?> clazz = obj.getClass();
ClassInfo info = ClassInfo.getClassInfo(clazz);
try {
Field idField = info.getIdField();
Object value = Util.readField(obj, idField);
// TODO verify that returning NULL is not a bad thing
if (value == null)
return null;
Class<?> type = idField.getType();
if (idField.isAnnotationPresent(Id.class)) {
Id id = idField.getAnnotation(Id.class);
switch(id.value()) {
case NONE:
// long or string goes toString
return KeyFactory.createKey(ClassInfo.getClassInfo(clazz).tableName, value.toString());
case AUTO_INCREMENT:
// as a string with auto_increment can't exist, it is not cast into long
if (Long.TYPE == type || Long.class.isAssignableFrom(type)) {
return KeyFactory.createKey(ClassInfo.getClassInfo(clazz).tableName, (Long) value);
}
return KeyFactory.createKey(ClassInfo.getClassInfo(clazz).tableName, value.toString());
case UUID:
return KeyFactory.createKey(ClassInfo.getClassInfo(clazz).tableName, value.toString());
default:
throw new SienaException("Id Generator " + id.value() + " not supported");
}
} else
throw new SienaException("Field " + idField.getName() + " is not an @Id field");
} catch (Exception e) {
throw new SienaException(e);
}
}
use of siena.SienaException in project siena by mandubian.
the class GaeMappingUtils method fillEntity.
public static void fillEntity(Object obj, Entity entity) {
Class<?> clazz = obj.getClass();
for (Field field : ClassInfo.getClassInfo(clazz).updateFields) {
String property = ClassInfo.getColumnNames(field)[0];
Object value = Util.readField(obj, field);
Class<?> fieldClass = field.getType();
if (ClassInfo.isModel(fieldClass) && !ClassInfo.isEmbedded(field)) /*&& !ClassInfo.isAggregated(field)
&& !ClassInfo.isOwned(field)*/
{
if (value == null) {
entity.setProperty(property, null);
} else {
Key key = getKey(value);
entity.setProperty(property, key);
}
} else {
if (value != null) {
if (fieldClass == Json.class) {
value = value.toString();
} else if (value instanceof String) {
String s = (String) value;
if (s.length() > 500)
value = new Text(s);
} else if (value instanceof byte[]) {
byte[] arr = (byte[]) value;
// GAE Blob doesn't accept more than 1MB
if (arr.length < 1000000)
value = new Blob(arr);
else
value = new Blob(Arrays.copyOf(arr, 1000000));
} else if (ClassInfo.isEmbedded(field)) {
Embedded embed = field.getAnnotation(Embedded.class);
switch(embed.mode()) {
case SERIALIZE_JSON:
value = JsonSerializer.serialize(value).toString();
String s = (String) value;
if (s.length() > 500)
value = new Text(s);
break;
case SERIALIZE_JAVA:
// this embedding mode doesn't manage @EmbedIgnores
try {
byte[] b = JavaSerializer.serialize(value);
// if length is less than 1Mb, can store in a blob else???
if (b.length <= 1000000) {
value = new Blob(b);
} else {
throw new SienaException("object can be java serialized because it's too large >1mb");
}
} catch (IOException ex) {
throw new SienaException(ex);
}
break;
case NATIVE:
GaeNativeSerializer.embed(entity, ClassInfo.getSingleColumnName(field), value, 0);
// has set several new properties in entity so go to next field
continue;
}
} else /*else if (ClassInfo.isAggregated(field)){
// can't save it now as it requires its parent key to be mapped
// so don't do anything for the time being
continue;
}
else if (ClassInfo.isOwned(field)){
// can't save it now as it requires its parent key to be mapped
// so don't do anything for the time being
continue;
}*/
if (fieldClass == BigDecimal.class) {
DecimalPrecision ann = field.getAnnotation(DecimalPrecision.class);
if (ann == null) {
value = ((BigDecimal) value).toPlainString();
} else {
switch(ann.storageType()) {
case DOUBLE:
value = ((BigDecimal) value).doubleValue();
break;
case STRING:
case NATIVE:
value = ((BigDecimal) value).toPlainString();
break;
}
}
} else // don't know if anyone will use it but it will work :)
if (Enum.class.isAssignableFrom(field.getType())) {
value = value.toString();
}
}
Unindexed ui = field.getAnnotation(Unindexed.class);
if (ui == null) {
entity.setProperty(property, value);
} else {
entity.setUnindexedProperty(property, value);
}
}
}
}
use of siena.SienaException in project siena by mandubian.
the class GaeMappingUtils method makeKey.
public static Key makeKey(ClassInfo info, Object object) {
Field idField = info.getIdField();
Object idVal = Util.readField(object, idField);
if (idVal == null)
throw new SienaException("Id Field " + idField.getName() + " value null");
return makeKeyFromId(object.getClass(), idVal);
}
use of siena.SienaException 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;
}
Aggregations