use of org.openmrs.Auditable in project openmrs-core by openmrs.
the class AuditableInterceptorTest method onFlushDirty_shouldNotFailWithNullPreviousState.
/**
* @see AuditableInterceptor#onFlushDirty(Object,Serializable,Object[],Object[],String[],Type[])
*/
@Test
public void onFlushDirty_shouldNotFailWithNullPreviousState() {
AuditableInterceptor interceptor = new AuditableInterceptor();
User u = new User();
// sanity check
Assert.assertTrue(u instanceof Auditable);
String[] propertyNames = new String[] { "changedBy", "dateChanged" };
Object[] currentState = new Object[] { "", null };
interceptor.onFlushDirty(u, null, currentState, null, propertyNames, null);
}
use of org.openmrs.Auditable in project openmrs-core by openmrs.
the class AuditableInterceptorTest method onFlushDirty_shouldSetTheChangedByField.
/**
* @see AuditableInterceptor#onFlushDirty(Object,Serializable,Object[],Object[],String[],Type[])
*/
@Test
public void onFlushDirty_shouldSetTheChangedByField() {
AuditableInterceptor interceptor = new AuditableInterceptor();
User u = new User();
// sanity check
Assert.assertTrue(u instanceof Auditable);
String[] propertyNames = new String[] { "changedBy", "dateChanged" };
Object[] currentState = new Object[] { "", null };
Object[] previousState = new Object[] { "", null };
interceptor.onFlushDirty(u, null, currentState, previousState, propertyNames, null);
Assert.assertNotNull(currentState[0]);
}
use of org.openmrs.Auditable in project openmrs-core by openmrs.
the class AuditableInterceptorTest method onFlushDirty_shouldSetTheDateChangedField.
/**
* @see AuditableInterceptor#onFlushDirty(Object,Serializable,Object[],Object[],String[],Type[])
*/
@Test
public void onFlushDirty_shouldSetTheDateChangedField() {
AuditableInterceptor interceptor = new AuditableInterceptor();
User u = new User();
// sanity check
Assert.assertTrue(u instanceof Auditable);
String[] propertyNames = new String[] { "changedBy", "dateChanged" };
Object[] currentState = new Object[] { "", null };
Object[] previousState = new Object[] { "", null };
interceptor.onFlushDirty(u, null, currentState, previousState, propertyNames, null);
Assert.assertNotNull(currentState[1]);
}
use of org.openmrs.Auditable 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