Search in sources :

Example 1 with OrderFrequency

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

the class HibernateOrderDAO method getOrderFrequencyByConcept.

/**
 * @see org.openmrs.api.db.OrderDAO#getOrderFrequencyByConcept(org.openmrs.Concept)
 */
@Override
public OrderFrequency getOrderFrequencyByConcept(Concept concept) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(OrderFrequency.class);
    criteria.add(Restrictions.eq("concept", concept));
    return (OrderFrequency) criteria.uniqueResult();
}
Also used : Criteria(org.hibernate.Criteria) OrderFrequency(org.openmrs.OrderFrequency)

Example 2 with OrderFrequency

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

the class OrderServiceTest method saveOrderFrequency_shouldNotAllowEditingAnExistingOrderFrequencyThatIsInUse.

/**
 * @see OrderService#saveOrderFrequency(OrderFrequency)
 */
@Test
public void saveOrderFrequency_shouldNotAllowEditingAnExistingOrderFrequencyThatIsInUse() {
    OrderFrequency orderFrequency = orderService.getOrderFrequency(1);
    assertNotNull(orderFrequency);
    orderFrequency.setFrequencyPerDay(4d);
    expectedException.expect(CannotUpdateObjectInUseException.class);
    expectedException.expectMessage("Order.frequency.cannot.edit");
    orderService.saveOrderFrequency(orderFrequency);
}
Also used : OrderFrequency(org.openmrs.OrderFrequency) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Example 3 with OrderFrequency

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

the class OrderServiceTest method saveOrderFrequency_shouldEditAnExistingOrderFrequencyThatIsNotInUse.

/**
 * @see OrderService#saveOrderFrequency(OrderFrequency)
 */
@Test
public void saveOrderFrequency_shouldEditAnExistingOrderFrequencyThatIsNotInUse() {
    executeDataSet(OTHER_ORDER_FREQUENCIES_XML);
    OrderFrequency orderFrequency = orderService.getOrderFrequency(100);
    assertNotNull(orderFrequency);
    orderFrequency.setFrequencyPerDay(4d);
    orderService.saveOrderFrequency(orderFrequency);
}
Also used : OrderFrequency(org.openmrs.OrderFrequency) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Example 4 with OrderFrequency

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

the class OrderServiceTest method saveOrderFrequency_shouldAddANewOrderFrequencyToTheDatabase.

/**
 * @see OrderService#saveOrderFrequency(OrderFrequency)
 */
@Test
public void saveOrderFrequency_shouldAddANewOrderFrequencyToTheDatabase() {
    Concept concept = new Concept();
    concept.addName(new ConceptName("new name", Context.getLocale()));
    concept.addDescription(new ConceptDescription("some description", null));
    concept.setDatatype(new ConceptDatatype(1));
    concept.setConceptClass(conceptService.getConceptClassByName("Frequency"));
    concept = conceptService.saveConcept(concept);
    Integer originalSize = orderService.getOrderFrequencies(true).size();
    OrderFrequency orderFrequency = new OrderFrequency();
    orderFrequency.setConcept(concept);
    orderFrequency.setFrequencyPerDay(2d);
    orderFrequency = orderService.saveOrderFrequency(orderFrequency);
    assertNotNull(orderFrequency.getId());
    assertNotNull(orderFrequency.getUuid());
    assertNotNull(orderFrequency.getCreator());
    assertNotNull(orderFrequency.getDateCreated());
    assertEquals(originalSize + 1, orderService.getOrderFrequencies(true).size());
}
Also used : Concept(org.openmrs.Concept) ConceptName(org.openmrs.ConceptName) ConceptDescription(org.openmrs.ConceptDescription) ConceptDatatype(org.openmrs.ConceptDatatype) OrderFrequency(org.openmrs.OrderFrequency) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) OrderUtilTest(org.openmrs.order.OrderUtilTest) Test(org.junit.Test)

Example 5 with OrderFrequency

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

the class OrderFrequencyValidator method validate.

/**
 * Checks the order frequency object for any inconsistencies/errors
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail if orderFrequency is null
 * @should fail if concept is null
 * @should fail if the concept is not of class frequency
 * @should fail if concept is used by another frequency
 * @should pass for a valid new order frequency
 * @should pass for a valid existing order frequency
 * @should be invoked when an order frequency is saved
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object obj, Errors errors) {
    OrderFrequency orderFrequency = (OrderFrequency) obj;
    if (orderFrequency == null) {
        errors.reject("error.general");
    } else {
        ValidationUtils.rejectIfEmpty(errors, "concept", "Concept.noConceptSelected");
        Concept concept = orderFrequency.getConcept();
        if (concept != null) {
            if (!ConceptClass.FREQUENCY_UUID.equals(concept.getConceptClass().getUuid())) {
                errors.rejectValue("concept", "OrderFrequency.concept.shouldBeClassFrequency");
            }
            OrderFrequency of = Context.getOrderService().getOrderFrequencyByConcept(concept);
            if (of != null && !of.equals(orderFrequency)) {
                errors.rejectValue("concept", "OrderFrequency.concept.shouldNotBeShared");
            }
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "retireReason");
    }
}
Also used : Concept(org.openmrs.Concept) OrderFrequency(org.openmrs.OrderFrequency)

Aggregations

OrderFrequency (org.openmrs.OrderFrequency)19 Test (org.junit.Test)17 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)17 OrderUtilTest (org.openmrs.order.OrderUtilTest)8 BindException (org.springframework.validation.BindException)8 Errors (org.springframework.validation.Errors)8 Concept (org.openmrs.Concept)6 ConceptDescription (org.openmrs.ConceptDescription)5 ConceptName (org.openmrs.ConceptName)5 ConceptService (org.openmrs.api.ConceptService)3 Locale (java.util.Locale)1 Criteria (org.hibernate.Criteria)1 ConceptDatatype (org.openmrs.ConceptDatatype)1