use of siena.SienaException in project siena by mandubian.
the class GaeMappingUtils method mapEntitiesKeysOnly.
public static <T> List<T> mapEntitiesKeysOnly(Iterable<Entity> entities, Class<T> clazz) {
Field id = ClassInfo.getIdField(clazz);
List<T> list = new ArrayList<T>();
for (Entity entity : entities) {
T obj;
try {
obj = Util.createObjectInstance(clazz);
list.add(obj);
setIdFromKey(id, obj, entity.getKey());
} catch (SienaException e) {
throw e;
} catch (Exception e) {
throw new SienaException(e);
}
}
return list;
}
use of siena.SienaException 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.SienaException in project siena by mandubian.
the class GaeMappingUtils method makeKeyFromParent.
public static Key makeKeyFromParent(ClassInfo info, Object object, Key parentKey, ClassInfo parentInfo, Field parentField) {
try {
Field idField = info.getIdField();
Object idVal = Util.readField(object, idField);
if (idVal == null)
throw new SienaException("Id Field " + idField.getName() + " value null");
if (idField.isAnnotationPresent(Id.class)) {
Id id = idField.getAnnotation(Id.class);
switch(id.value()) {
case NONE:
// long or string goes toString
return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), idVal.toString());
case AUTO_INCREMENT:
Class<?> type = idField.getType();
// 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(parentKey, getKindWithAncestorField(info, parentInfo, parentField), (Long) idVal);
}
return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), idVal.toString());
case UUID:
return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), idVal.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 mapEntities.
public static <T> List<T> mapEntities(List<Entity> entities, Class<T> clazz) {
Field id = ClassInfo.getIdField(clazz);
List<T> list = new ArrayList<T>(entities.size());
for (Entity entity : entities) {
T obj;
try {
obj = Util.createObjectInstance(clazz);
fillModel(obj, entity);
list.add(obj);
setIdFromKey(id, obj, entity.getKey());
} catch (SienaException e) {
throw e;
} catch (Exception e) {
throw new SienaException(e);
}
}
return list;
}
use of siena.SienaException in project siena by mandubian.
the class GaeMappingUtils method getKeyFromParent.
public static Key getKeyFromParent(Object obj, Key parentKey, ClassInfo parentInfo, Field parentField) {
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(parentKey, getKindWithAncestorField(info, parentInfo, parentField), 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(parentKey, getKindWithAncestorField(info, parentInfo, parentField), (Long) value);
}
return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), value.toString());
case UUID:
return KeyFactory.createKey(parentKey, getKindWithAncestorField(info, parentInfo, parentField), 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);
}
}
Aggregations