Search in sources :

Example 1 with Auditable

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);
}
Also used : Auditable(org.openmrs.Auditable) User(org.openmrs.User) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 2 with Auditable

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]);
}
Also used : Auditable(org.openmrs.Auditable) User(org.openmrs.User) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 3 with Auditable

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]);
}
Also used : Auditable(org.openmrs.Auditable) User(org.openmrs.User) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 4 with Auditable

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;
}
Also used : DAOException(org.openmrs.api.db.DAOException) Auditable(org.openmrs.Auditable) SerializedObject(org.openmrs.api.db.SerializedObject) SerializationException(org.openmrs.serialization.SerializationException) OpenmrsData(org.openmrs.OpenmrsData) OpenmrsMetadata(org.openmrs.OpenmrsMetadata) Date(java.util.Date)

Aggregations

Auditable (org.openmrs.Auditable)4 Test (org.junit.Test)3 User (org.openmrs.User)3 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)3 Date (java.util.Date)1 OpenmrsData (org.openmrs.OpenmrsData)1 OpenmrsMetadata (org.openmrs.OpenmrsMetadata)1 DAOException (org.openmrs.api.db.DAOException)1 SerializedObject (org.openmrs.api.db.SerializedObject)1 SerializationException (org.openmrs.serialization.SerializationException)1