String idType = play.modules.morphia.utils.IdGenerator.getIdTypeName();
CtField idField = new CtField(classPool.get(idType), "_id", ctClass);
idField.setModifiers(Modifier.PRIVATE);
AnnotationsAttribute aa = new AnnotationsAttribute(ctClass.getClassFile().getConstPool(),
AnnotationsAttribute.visibleTag);
Annotation idAnn = new Annotation(Id.class.getName(), ctClass.getClassFile().getConstPool());
aa.addAnnotation(idAnn);
idField.getFieldInfo().addAttribute(aa);
ctClass.addField(idField);
Logger.trace("ID field added to entity[%2$s]: %1$s", idField.getSignature(), ctClass.getName());
// id()
CtMethod getId = CtMethod.make("public Object getId() { return _id;}", ctClass);
ctClass.addMethod(getId);
// setId
CtMethod setId = CtMethod.make("protected void setId_(Object id) { _id = (" + idType + ")play.modules.morphia.utils.IdGenerator.processId(id);}", ctClass);
ctClass.addMethod(setId);
} else {
Logger.trace("adding id methods to user defined id entity: %1$s", ctClass.getName());
// a general id() method for user marked Id field
boolean hasGetId = false;
for (CtMethod cm: ctClass.getDeclaredMethods()) {
if ("getId".equals(cm.getName()) && cm.getDeclaringClass().equals(ctClass)) {
// user has defined getId already
hasGetId = true;
break;
}
}
if (!hasGetId) {
CtMethod getId = CtMethod.make("public Object getId() { return mf.keyValue(this);}", ctClass);
ctClass.addMethod(getId);
}
// setId - for user marked Id entity, setId method needs to be override
CtMethod isUserDefinedId = CtMethod.make("protected boolean isUserDefinedId_() {return super.isUserDefinedId_();}", ctClass);
ctClass.addMethod(isUserDefinedId);
}
// create timestamp?
if (autoTS) {
ClassFile classFile = ctClass.getClassFile();
ConstPool cp = classFile.getConstPool();
AnnotationsAttribute attribute = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
Annotation indexAnnotation = new Annotation(cp, ClassPool.getDefault().get("com.google.code.morphia.annotations.Indexed"));
EnumMemberValue val = new EnumMemberValue(cp);
val.setType(IndexDirection.class.getName());
val.setValue(IndexDirection.DESC.name());
indexAnnotation.addMemberValue("value", val);
attribute.addAnnotation(indexAnnotation);
Logger.trace("create timestamp fields automatically");
CtField createdField = new CtField(CtClass.longType, "_created", ctClass);
createdField.getFieldInfo().addAttribute(attribute);
createdField.setModifiers(Modifier.PRIVATE);
ctClass.addField(createdField);
CtField modifiedField = new CtField(CtClass.longType, "_modified", ctClass);
modifiedField.getFieldInfo().addAttribute(attribute);
modifiedField.setModifiers(Modifier.PRIVATE);
ctClass.addField(modifiedField);
CtMethod persistTs = CtMethod.make("void _updateTimestamp() { long now = System.currentTimeMillis(); if (0 == _created) {_created = now;} ;_modified = now;}", ctClass);
AnnotationsAttribute aa = new AnnotationsAttribute(ctClass.getClassFile().getConstPool(),
AnnotationsAttribute.visibleTag);
Annotation prePersistAnn = new Annotation(PrePersist.class.getName(), ctClass.getClassFile().getConstPool());
aa.addAnnotation(prePersistAnn);
persistTs.getMethodInfo().addAttribute(aa);
ctClass.addMethod(persistTs);
CtMethod getCreated = CtMethod.make("public long _getCreated() { return _created; }", ctClass);