use of org.openmrs.api.db.DAOException in project openmrs-core by openmrs.
the class HibernateSerializedObjectDAO method saveObject.
/**
* @see SerializedObjectDAO#saveObject(OpenmrsObject, OpenmrsSerializer)
*/
@Override
public <T extends OpenmrsObject> T saveObject(T object, OpenmrsSerializer serializer) throws DAOException {
Class<? extends OpenmrsObject> baseType = getRegisteredTypeForObject(object);
if (baseType == null) {
throw new DAOException("SerializedObjectDAO does not support saving objects of type <" + object.getClass() + ">");
}
SerializedObject serializedObject = getSerializedObject(object.getId());
if (serializedObject == null) {
serializedObject = new SerializedObject();
}
if (serializer == null) {
serializer = getSerializer(serializedObject);
}
if (object instanceof Auditable) {
Auditable auditableObj = (Auditable) object;
if (auditableObj.getCreator() == null) {
auditableObj.setCreator(Context.getAuthenticatedUser());
}
serializedObject.setCreator(auditableObj.getCreator());
if (auditableObj.getDateCreated() == null) {
auditableObj.setDateCreated(new Date());
}
serializedObject.setDateCreated(auditableObj.getDateCreated());
serializedObject.setChangedBy(auditableObj.getChangedBy());
serializedObject.setDateChanged(auditableObj.getDateChanged());
}
String data;
try {
data = serializer.serialize(object);
} catch (SerializationException e) {
throw new DAOException("Unable to save object <" + object + "> because serialization failed.", e);
}
serializedObject.setUuid(object.getUuid());
serializedObject.setType(baseType.getName());
serializedObject.setSubtype(object.getClass().getName());
serializedObject.setSerializationClass(serializer.getClass());
serializedObject.setSerializedData(data);
if (object instanceof OpenmrsMetadata) {
OpenmrsMetadata metaObj = (OpenmrsMetadata) object;
serializedObject.setName(metaObj.getName());
serializedObject.setDescription(metaObj.getDescription());
serializedObject.setRetired(metaObj.getRetired());
serializedObject.setRetiredBy(metaObj.getRetiredBy());
serializedObject.setDateRetired(metaObj.getDateRetired());
serializedObject.setRetireReason(metaObj.getRetireReason());
}
if (object instanceof OpenmrsData) {
OpenmrsData dataObj = (OpenmrsData) object;
serializedObject.setRetired(dataObj.getVoided());
serializedObject.setRetiredBy(dataObj.getVoidedBy());
serializedObject.setDateRetired(dataObj.getDateVoided());
serializedObject.setRetireReason(dataObj.getVoidReason());
}
sessionFactory.getCurrentSession().saveOrUpdate(serializedObject);
object.setId(serializedObject.getId());
return object;
}
Aggregations