Search in sources :

Example 6 with Voidable

use of org.openmrs.Voidable in project openmrs-core by openmrs.

the class RequiredDataAdvice method before.

/**
 * @see org.springframework.aop.MethodBeforeAdvice#before(java.lang.reflect.Method,
 *      java.lang.Object[], java.lang.Object)
 * @should not fail on update method with no arguments
 */
@Override
@SuppressWarnings("unchecked")
public void before(Method method, Object[] args, Object target) throws Throwable {
    String methodName = method.getName();
    // skip out early if there are no arguments
    if (args == null || args.length == 0) {
        return;
    }
    Object mainArgument = args[0];
    // fail early on a null parameter
    if (mainArgument == null) {
        return;
    }
    // not updating the primary argument. eg: ConceptService.updateConceptWord(Concept)
    if (methodName.startsWith("save") || methodName.startsWith("create")) {
        // if the first argument is an OpenmrsObject, handle it now
        Reflect reflect = new Reflect(OpenmrsObject.class);
        if (reflect.isSuperClass(mainArgument)) {
            // fail early if the method name is not like saveXyz(Xyz)
            if (!methodNameEndsWithClassName(method, mainArgument.getClass())) {
                return;
            }
            // if a second argument exists, pass that to the save handler as well
            // (with current code, it means we're either in an obs save or a user save)
            String other = null;
            if (args.length > 1 && args[1] instanceof String) {
                other = (String) args[1];
            }
            ValidateUtil.validate(mainArgument);
            recursivelyHandle(SaveHandler.class, (OpenmrsObject) mainArgument, other);
        } else // if the first argument is a list of openmrs objects, handle them all now
        if (Reflect.isCollection(mainArgument) && isOpenmrsObjectCollection(mainArgument)) {
            // ideally we would fail early if the method name is not like savePluralOfXyz(Collection<Xyz>)
            // but this only occurs once in the API (AdministrationService.saveGlobalProperties
            // so it is not worth handling this case
            // if a second argument exists, pass that to the save handler as well
            // (with current code, it means we're either in an obs save or a user save)
            String other = null;
            if (args.length > 1) {
                other = (String) args[1];
            }
            Collection<OpenmrsObject> openmrsObjects = (Collection<OpenmrsObject>) mainArgument;
            for (OpenmrsObject object : openmrsObjects) {
                ValidateUtil.validate(mainArgument);
                recursivelyHandle(SaveHandler.class, object, other);
            }
        }
    } else {
        // with Patients or Concepts as the first argument
        if (!methodNameEndsWithClassName(method, mainArgument.getClass())) {
            return;
        }
        if (methodName.startsWith("void")) {
            Voidable voidable = (Voidable) args[0];
            Date dateVoided = voidable.getDateVoided() == null ? new Date() : voidable.getDateVoided();
            String voidReason = (String) args[1];
            recursivelyHandle(VoidHandler.class, voidable, Context.getAuthenticatedUser(), dateVoided, voidReason, null);
        } else if (methodName.startsWith("unvoid")) {
            Voidable voidable = (Voidable) args[0];
            Date originalDateVoided = voidable.getDateVoided();
            User originalVoidingUser = voidable.getVoidedBy();
            recursivelyHandle(UnvoidHandler.class, voidable, originalVoidingUser, originalDateVoided, null, null);
        } else if (methodName.startsWith("retire")) {
            Retireable retirable = (Retireable) args[0];
            String retireReason = (String) args[1];
            recursivelyHandle(RetireHandler.class, retirable, retireReason);
        } else if (methodName.startsWith("unretire")) {
            Retireable retirable = (Retireable) args[0];
            Date originalDateRetired = retirable.getDateRetired();
            recursivelyHandle(UnretireHandler.class, retirable, Context.getAuthenticatedUser(), originalDateRetired, null, null);
        }
    }
}
Also used : ConceptNameSaveHandler(org.openmrs.api.handler.ConceptNameSaveHandler) SaveHandler(org.openmrs.api.handler.SaveHandler) User(org.openmrs.User) Retireable(org.openmrs.Retireable) Reflect(org.openmrs.util.Reflect) OpenmrsObject(org.openmrs.OpenmrsObject) Collection(java.util.Collection) OpenmrsObject(org.openmrs.OpenmrsObject) Voidable(org.openmrs.Voidable) UnretireHandler(org.openmrs.api.handler.UnretireHandler) Date(java.util.Date) UnvoidHandler(org.openmrs.api.handler.UnvoidHandler)

Example 7 with Voidable

use of org.openmrs.Voidable in project openmrs-core by openmrs.

the class BaseVoidHandlerTest method handle_shouldSetDateVoided.

/**
 * @see BaseVoidHandler#handle(Voidable,User,Date,String)
 */
@Test
public void handle_shouldSetDateVoided() {
    Date d = new Date();
    VoidHandler<Voidable> handler = new BaseVoidHandler();
    Voidable voidable = new Person();
    handler.handle(voidable, null, d, " ");
    Assert.assertEquals(d, voidable.getDateVoided());
}
Also used : Voidable(org.openmrs.Voidable) Person(org.openmrs.Person) Date(java.util.Date) Test(org.junit.Test)

Example 8 with Voidable

use of org.openmrs.Voidable in project openmrs-core by openmrs.

the class BaseVoidHandlerTest method handle_shouldNotSetVoidedByIfNonNull.

/**
 * @see BaseVoidHandler#handle(Voidable,User,Date,String)
 */
@Test
public void handle_shouldNotSetVoidedByIfNonNull() {
    VoidHandler<Voidable> handler = new BaseVoidHandler();
    Voidable voidable = new Person();
    voidable.setVoidedBy(new User(3));
    handler.handle(voidable, new User(2), null, " ");
    Assert.assertEquals(3, voidable.getVoidedBy().getId().intValue());
}
Also used : User(org.openmrs.User) Voidable(org.openmrs.Voidable) Person(org.openmrs.Person) Test(org.junit.Test)

Example 9 with Voidable

use of org.openmrs.Voidable in project openmrs-core by openmrs.

the class BaseVoidHandlerTest method handle_shouldNotSetDateVoidedIfNonNull.

/**
 * @see BaseVoidHandler#handle(Voidable,User,Date,String)
 */
@Test
public void handle_shouldNotSetDateVoidedIfNonNull() {
    // a time that is not "now"
    Date d = new Date(new Date().getTime() - 1000);
    VoidHandler<Voidable> handler = new BaseVoidHandler();
    Voidable voidable = new Person();
    // make dateVoided non null
    voidable.setDateVoided(d);
    handler.handle(voidable, null, new Date(), " ");
    Assert.assertEquals(d, voidable.getDateVoided());
}
Also used : Voidable(org.openmrs.Voidable) Person(org.openmrs.Person) Date(java.util.Date) Test(org.junit.Test)

Example 10 with Voidable

use of org.openmrs.Voidable in project openmrs-core by openmrs.

the class BaseVoidHandlerTest method handle_shouldNotSetTheVoidReasonIfAlreadyVoided.

/**
 * @see BaseVoidHandler#handle(Voidable,User,Date,String)
 */
@Test
public void handle_shouldNotSetTheVoidReasonIfAlreadyVoided() {
    VoidHandler<Voidable> handler = new BaseVoidHandler();
    Voidable voidable = new Person();
    voidable.setVoided(true);
    voidable.setVoidedBy(new User(1));
    handler.handle(voidable, null, null, "THE REASON");
    Assert.assertNull(voidable.getVoidReason());
}
Also used : User(org.openmrs.User) Voidable(org.openmrs.Voidable) Person(org.openmrs.Person) Test(org.junit.Test)

Aggregations

Voidable (org.openmrs.Voidable)16 Test (org.junit.Test)14 Person (org.openmrs.Person)14 Date (java.util.Date)5 User (org.openmrs.User)5 OpenmrsObject (org.openmrs.OpenmrsObject)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 Obs (org.openmrs.Obs)1 Retireable (org.openmrs.Retireable)1 AllowEmptyStrings (org.openmrs.annotation.AllowEmptyStrings)1 AllowLeadingOrTrailingWhitespace (org.openmrs.annotation.AllowLeadingOrTrailingWhitespace)1 APIException (org.openmrs.api.APIException)1 ConceptNameSaveHandler (org.openmrs.api.handler.ConceptNameSaveHandler)1 SaveHandler (org.openmrs.api.handler.SaveHandler)1 UnretireHandler (org.openmrs.api.handler.UnretireHandler)1 UnvoidHandler (org.openmrs.api.handler.UnvoidHandler)1 Reflect (org.openmrs.util.Reflect)1