use of siena.Id 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.Id 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.Id 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.Id 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.Id 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