Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -307,6 +307,17 @@ public <Ord extends Order> List<Ord> getActiveOrders(Patient patient, Class<Ord> | |
@Authorized(PrivilegeConstants.VIEW_ORDER_FREQUENCIES) | ||
public OrderFrequency getOrderFrequencyByUuid(String uuid); | ||
|
||
/** | ||
* Gets an OrderFrequency that matches the specified concept | ||
* | ||
* @param concept the concept to match against | ||
* @return OrderFrequency | ||
* @since 1.10 | ||
* @should return the order frequency that matches the specified concept | ||
*/ | ||
@Authorized(PrivilegeConstants.VIEW_ORDER_FREQUENCIES) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
wluyima
Member
|
||
public OrderFrequency getOrderFrequencyByConcept(Concept concept); | ||
|
||
/** | ||
* Gets all order frequencies | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* The contents of this file are subject to the OpenMRS Public License | ||
* Version 1.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* http://license.openmrs.org | ||
* | ||
* Software distributed under the License is distributed on an "AS IS" | ||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing rights and limitations | ||
* under the License. | ||
* | ||
* Copyright (C) OpenMRS, LLC. All Rights Reserved. | ||
*/ | ||
package org.openmrs.validator; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.openmrs.Concept; | ||
import org.openmrs.OrderFrequency; | ||
import org.openmrs.annotation.Handler; | ||
import org.openmrs.api.context.Context; | ||
import org.springframework.validation.Errors; | ||
import org.springframework.validation.ValidationUtils; | ||
|
||
/** | ||
* Validates the {@link OrderFrequency} class. | ||
* | ||
* @since 1.10 | ||
*/ | ||
@Handler(supports = { OrderFrequency.class }) | ||
public class OrderFrequencyValidator { | ||
|
||
/** Log for this class and subclasses */ | ||
protected final Log log = LogFactory.getLog(getClass()); | ||
|
||
/** | ||
* Determines if the command object being submitted is a valid type | ||
* | ||
* @see org.springframework.validation.Validator#supports(java.lang.Class) | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public boolean supports(Class c) { | ||
return OrderFrequency.class.isAssignableFrom(c); | ||
} | ||
|
||
/** | ||
* 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 if all fields are correct | ||
*/ | ||
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 (concept.getConceptClass() == null) { | ||
errors.rejectValue("concept", "OrderFrequency.concept.shouldBeClassFrequency"); | ||
This comment has been minimized.
Sorry, something went wrong.
wluyima
Member
|
||
} else { | ||
if (!"Frequency".equals(concept.getConceptClass().getName())) { | ||
This comment has been minimized.
Sorry, something went wrong.
wluyima
Member
|
||
errors.rejectValue("concept", "OrderFrequency.concept.shouldBeClassFrequency"); | ||
} | ||
} | ||
|
||
OrderFrequency of = Context.getOrderService().getOrderFrequencyByConcept(concept); | ||
if (of != null && !of.equals(orderFrequency)) { | ||
errors.rejectValue("concept", "OrderFrequency.concept.shouldNotBeShared"); | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* The contents of this file are subject to the OpenMRS Public License | ||
* Version 1.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* http://license.openmrs.org | ||
* | ||
* Software distributed under the License is distributed on an "AS IS" | ||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing rights and limitations | ||
* under the License. | ||
* | ||
* Copyright (C) OpenMRS, LLC. All Rights Reserved. | ||
*/ | ||
package org.openmrs.validator; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.openmrs.Concept; | ||
import org.openmrs.OrderFrequency; | ||
import org.openmrs.api.context.Context; | ||
import org.openmrs.test.BaseContextSensitiveTest; | ||
import org.openmrs.test.Verifies; | ||
import org.springframework.validation.BindException; | ||
import org.springframework.validation.Errors; | ||
|
||
/** | ||
* Tests methods on the {@link OrderFrequencyValidator} class. | ||
*/ | ||
public class OrderFrequencyValidatorTest extends BaseContextSensitiveTest { | ||
|
||
/** | ||
* @see {@link OrderFrequencyValidator#validate(Object,Errors)} | ||
*/ | ||
@Test | ||
@Verifies(value = "should fail if orderFrequency is null", method = "validate(Object,Errors)") | ||
public void validate_shouldFailIfOrderFrequencyIsNull() throws Exception { | ||
Errors errors = new BindException(new OrderFrequency(), "orderFrequency"); | ||
new OrderFrequencyValidator().validate(null, errors); | ||
|
||
Assert.assertTrue(errors.hasErrors()); | ||
} | ||
|
||
/** | ||
* @see {@link OrderFrequencyValidator#validate(Object,Errors)} | ||
*/ | ||
@Test | ||
@Verifies(value = "should fail if concept is null", method = "validate(Object,Errors)") | ||
public void validate_shouldFailIfConceptIsNull() throws Exception { | ||
OrderFrequency orderFrequency = new OrderFrequency(); | ||
|
||
Errors errors = new BindException(orderFrequency, "orderFrequency"); | ||
new OrderFrequencyValidator().validate(orderFrequency, errors); | ||
|
||
Assert.assertTrue(errors.hasFieldErrors("concept")); | ||
} | ||
|
||
/** | ||
* @see {@link OrderFrequencyValidator#validate(Object,Errors)} | ||
*/ | ||
@Test | ||
@Verifies(value = "should fail if the concept is not of class frequency", method = "validate(Object,Errors)") | ||
public void validate_shouldFailIfConceptIsNotOfClassFrequency() throws Exception { | ||
OrderFrequency orderFrequency = new OrderFrequency(); | ||
orderFrequency.setConcept(Context.getConceptService().getConcept(88)); | ||
Errors errors = new BindException(orderFrequency, "orderFrequency"); | ||
new OrderFrequencyValidator().validate(orderFrequency, errors); | ||
|
||
Assert.assertTrue(errors.hasFieldErrors("concept")); | ||
} | ||
|
||
/** | ||
* @see {@link OrderFrequencyValidator#validate(Object,Errors)} | ||
*/ | ||
@Test | ||
@Verifies(value = "should fail if concept is used by another frequency", method = "validate(Object,Errors)") | ||
public void validate_shouldFailIfConceptIsUsedByAnotherFrequency() throws Exception { | ||
OrderFrequency orderFrequency = new OrderFrequency(); | ||
orderFrequency.setConcept(Context.getConceptService().getConcept(113)); | ||
Errors errors = new BindException(orderFrequency, "orderFrequency"); | ||
new OrderFrequencyValidator().validate(orderFrequency, errors); | ||
|
||
Assert.assertTrue(errors.hasFieldErrors("concept")); | ||
} | ||
|
||
/** | ||
* @see {@link OrderFrequencyValidator#validate(Object,Errors)} | ||
*/ | ||
@Test | ||
@Verifies(value = "should pass if all fields are correct", method = "validate(Object,Errors)") | ||
public void validate_shouldPassIfAllFieldsAreCorrect() throws Exception { | ||
Concept concept = new Concept(1); | ||
concept.setConceptClass(Context.getConceptService().getConceptClass(19)); | ||
|
||
OrderFrequency orderFrequency = new OrderFrequency(); | ||
orderFrequency.setConcept(concept); | ||
Errors errors = new BindException(orderFrequency, "orderFrequency"); | ||
new OrderFrequencyValidator().validate(orderFrequency, errors); | ||
|
||
Assert.assertFalse(errors.hasErrors()); | ||
} | ||
} |
Hmmm.... i think this can be private for now until people ask for it