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);
}
}