Skip to content

Commit 27d6c00

Browse files
committedFeb 27, 2014
Add an OrderFrequencyValidator - TRUNK-4192
1 parent c8a5026 commit 27d6c00

File tree

8 files changed

+224
-3
lines changed

8 files changed

+224
-3
lines changed
 

‎api/src/main/java/org/openmrs/api/OrderService.java

+11
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,17 @@ public <Ord extends Order> List<Ord> getActiveOrders(Patient patient, Class<Ord>
307307
@Authorized(PrivilegeConstants.VIEW_ORDER_FREQUENCIES)
308308
public OrderFrequency getOrderFrequencyByUuid(String uuid);
309309

310+
/**
311+
* Gets an OrderFrequency that matches the specified concept
312+
*
313+
* @param concept the concept to match against
314+
* @return OrderFrequency
315+
* @since 1.10
316+
* @should return the order frequency that matches the specified concept
317+
*/
318+
@Authorized(PrivilegeConstants.VIEW_ORDER_FREQUENCIES)
Has conversations. Original line has conversations.
319+
public OrderFrequency getOrderFrequencyByConcept(Concept concept);
320+
310321
/**
311322
* Gets all order frequencies
312323
*

‎api/src/main/java/org/openmrs/api/db/OrderDAO.java

+5
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,9 @@ public List<OrderFrequency> getOrderFrequencies(String searchPhrase, Locale loca
154154
* @return true if in use, else false
155155
*/
156156
public boolean isOrderFrequencyInUse(OrderFrequency orderFrequency);
157+
158+
/**
159+
* @See OrderService#getOrderFrequencyByConcept
160+
*/
161+
public OrderFrequency getOrderFrequencyByConcept(Concept concept);
157162
}

‎api/src/main/java/org/openmrs/api/db/hibernate/HibernateOrderDAO.java

+10
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,14 @@ public boolean isOrderFrequencyInUse(OrderFrequency orderFrequency) {
396396

397397
return false;
398398
}
399+
400+
/**
401+
* @see org.openmrs.api.db.OrderDAO#getOrderFrequencyByConcept(org.openmrs.Concept)
402+
*/
403+
@Override
404+
public OrderFrequency getOrderFrequencyByConcept(Concept concept) {
405+
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(OrderFrequency.class);
406+
criteria.add(Restrictions.eq("concept", concept));
407+
return (OrderFrequency) criteria.uniqueResult();
408+
}
399409
}

‎api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java

+9
Original file line numberDiff line numberDiff line change
@@ -500,4 +500,13 @@ public void purgeOrderFrequency(OrderFrequency orderFrequency) {
500500

501501
dao.purgeOrderFrequency(orderFrequency);
502502
}
503+
504+
/**
505+
* @see org.openmrs.api.OrderService#getOrderFrequencyByConcept(org.openmrs.Concept)
506+
*/
507+
@Override
508+
@Transactional(readOnly = true)
509+
public OrderFrequency getOrderFrequencyByConcept(Concept concept) {
510+
return dao.getOrderFrequencyByConcept(concept);
511+
}
503512
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* The contents of this file are subject to the OpenMRS Public License
3+
* Version 1.0 (the "License"); you may not use this file except in
4+
* compliance with the License. You may obtain a copy of the License at
5+
* http://license.openmrs.org
6+
*
7+
* Software distributed under the License is distributed on an "AS IS"
8+
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9+
* License for the specific language governing rights and limitations
10+
* under the License.
11+
*
12+
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
13+
*/
14+
package org.openmrs.validator;
15+
16+
import org.apache.commons.logging.Log;
17+
import org.apache.commons.logging.LogFactory;
18+
import org.openmrs.Concept;
19+
import org.openmrs.OrderFrequency;
20+
import org.openmrs.annotation.Handler;
21+
import org.openmrs.api.context.Context;
22+
import org.springframework.validation.Errors;
23+
import org.springframework.validation.ValidationUtils;
24+
25+
/**
26+
* Validates the {@link OrderFrequency} class.
27+
*
28+
* @since 1.10
29+
*/
30+
@Handler(supports = { OrderFrequency.class })
31+
public class OrderFrequencyValidator {
32+
33+
/** Log for this class and subclasses */
34+
protected final Log log = LogFactory.getLog(getClass());
35+
36+
/**
37+
* Determines if the command object being submitted is a valid type
38+
*
39+
* @see org.springframework.validation.Validator#supports(java.lang.Class)
40+
*/
41+
@SuppressWarnings("unchecked")
42+
public boolean supports(Class c) {
43+
return OrderFrequency.class.isAssignableFrom(c);
44+
}
45+
46+
/**
47+
* Checks the order frequency object for any inconsistencies/errors
48+
*
49+
* @see org.springframework.validation.Validator#validate(java.lang.Object,
50+
* org.springframework.validation.Errors)
51+
* @should fail if orderFrequency is null
52+
* @should fail if concept is null
53+
* @should fail if the concept is not of class frequency
54+
* @should fail if concept is used by another frequency
55+
* @should pass if all fields are correct
56+
*/
57+
public void validate(Object obj, Errors errors) {
58+
OrderFrequency orderFrequency = (OrderFrequency) obj;
59+
if (orderFrequency == null) {
60+
errors.reject("error.general");
61+
} else {
62+
ValidationUtils.rejectIfEmpty(errors, "concept", "Concept.noConceptSelected");
63+
64+
Concept concept = orderFrequency.getConcept();
65+
if (concept != null) {
66+
if (concept.getConceptClass() == null) {
67+
errors.rejectValue("concept", "OrderFrequency.concept.shouldBeClassFrequency");
Has a conversation. Original line has a conversation.
68+
} else {
69+
if (!"Frequency".equals(concept.getConceptClass().getName())) {
Has a conversation. Original line has a conversation.
70+
errors.rejectValue("concept", "OrderFrequency.concept.shouldBeClassFrequency");
71+
}
72+
}
73+
74+
OrderFrequency of = Context.getOrderService().getOrderFrequencyByConcept(concept);
75+
if (of != null && !of.equals(orderFrequency)) {
76+
errors.rejectValue("concept", "OrderFrequency.concept.shouldNotBeShared");
77+
}
78+
}
79+
}
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* The contents of this file are subject to the OpenMRS Public License
3+
* Version 1.0 (the "License"); you may not use this file except in
4+
* compliance with the License. You may obtain a copy of the License at
5+
* http://license.openmrs.org
6+
*
7+
* Software distributed under the License is distributed on an "AS IS"
8+
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9+
* License for the specific language governing rights and limitations
10+
* under the License.
11+
*
12+
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
13+
*/
14+
package org.openmrs.validator;
15+
16+
import org.junit.Assert;
17+
import org.junit.Test;
18+
import org.openmrs.Concept;
19+
import org.openmrs.OrderFrequency;
20+
import org.openmrs.api.context.Context;
21+
import org.openmrs.test.BaseContextSensitiveTest;
22+
import org.openmrs.test.Verifies;
23+
import org.springframework.validation.BindException;
24+
import org.springframework.validation.Errors;
25+
26+
/**
27+
* Tests methods on the {@link OrderFrequencyValidator} class.
28+
*/
29+
public class OrderFrequencyValidatorTest extends BaseContextSensitiveTest {
30+
31+
/**
32+
* @see {@link OrderFrequencyValidator#validate(Object,Errors)}
33+
*/
34+
@Test
35+
@Verifies(value = "should fail if orderFrequency is null", method = "validate(Object,Errors)")
36+
public void validate_shouldFailIfOrderFrequencyIsNull() throws Exception {
37+
Errors errors = new BindException(new OrderFrequency(), "orderFrequency");
38+
new OrderFrequencyValidator().validate(null, errors);
39+
40+
Assert.assertTrue(errors.hasErrors());
41+
}
42+
43+
/**
44+
* @see {@link OrderFrequencyValidator#validate(Object,Errors)}
45+
*/
46+
@Test
47+
@Verifies(value = "should fail if concept is null", method = "validate(Object,Errors)")
48+
public void validate_shouldFailIfConceptIsNull() throws Exception {
49+
OrderFrequency orderFrequency = new OrderFrequency();
50+
51+
Errors errors = new BindException(orderFrequency, "orderFrequency");
52+
new OrderFrequencyValidator().validate(orderFrequency, errors);
53+
54+
Assert.assertTrue(errors.hasFieldErrors("concept"));
55+
}
56+
57+
/**
58+
* @see {@link OrderFrequencyValidator#validate(Object,Errors)}
59+
*/
60+
@Test
61+
@Verifies(value = "should fail if the concept is not of class frequency", method = "validate(Object,Errors)")
62+
public void validate_shouldFailIfConceptIsNotOfClassFrequency() throws Exception {
63+
OrderFrequency orderFrequency = new OrderFrequency();
64+
orderFrequency.setConcept(Context.getConceptService().getConcept(88));
65+
Errors errors = new BindException(orderFrequency, "orderFrequency");
66+
new OrderFrequencyValidator().validate(orderFrequency, errors);
67+
68+
Assert.assertTrue(errors.hasFieldErrors("concept"));
69+
}
70+
71+
/**
72+
* @see {@link OrderFrequencyValidator#validate(Object,Errors)}
73+
*/
74+
@Test
75+
@Verifies(value = "should fail if concept is used by another frequency", method = "validate(Object,Errors)")
76+
public void validate_shouldFailIfConceptIsUsedByAnotherFrequency() throws Exception {
77+
OrderFrequency orderFrequency = new OrderFrequency();
78+
orderFrequency.setConcept(Context.getConceptService().getConcept(113));
79+
Errors errors = new BindException(orderFrequency, "orderFrequency");
80+
new OrderFrequencyValidator().validate(orderFrequency, errors);
81+
82+
Assert.assertTrue(errors.hasFieldErrors("concept"));
83+
}
84+
85+
/**
86+
* @see {@link OrderFrequencyValidator#validate(Object,Errors)}
87+
*/
88+
@Test
89+
@Verifies(value = "should pass if all fields are correct", method = "validate(Object,Errors)")
90+
public void validate_shouldPassIfAllFieldsAreCorrect() throws Exception {
91+
Concept concept = new Concept(1);
92+
concept.setConceptClass(Context.getConceptService().getConceptClass(19));
93+
94+
OrderFrequency orderFrequency = new OrderFrequency();
95+
orderFrequency.setConcept(concept);
96+
Errors errors = new BindException(orderFrequency, "orderFrequency");
97+
new OrderFrequencyValidator().validate(orderFrequency, errors);
98+
99+
Assert.assertFalse(errors.hasErrors());
100+
}
101+
}

‎api/src/test/resources/org/openmrs/include/standardTestDataset.xml

+4-3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<concept_class concept_class_id="16" name="Program" description="A treatment program" creator="1" date_created="2008-08-15 13:55:59.0" retired="false" uuid="2a3738f5-26f0-4f97-ae7a-f99e42fa6d44"/>
5252
<concept_class concept_class_id="17" name="Workflow" description="A workflow within a treatment program" creator="1" date_created="2008-08-15 13:56:30.0" retired="false" uuid="dbdf1b12-dbcb-4d41-a8eb-39a551f56e6d"/>
5353
<concept_class concept_class_id="18" name="State" description="A state within a workflow" creator="1" date_created="2008-08-15 13:56:55.0" retired="false" uuid="ac3feb09-e120-4d8c-97ee-f7544720efb8"/>
54+
<concept_class concept_class_id="19" name="Frequency" description="A class for order frequencies" creator="1" date_created="2008-08-15 13:56:55.0" retired="false" uuid="cd9emn09-e120-4d8c-97ee-f7544720efb8"/>
Has a conversation. Original line has a conversation.
5455
<concept_datatype concept_datatype_id="1" name="Numeric" hl7_abbreviation="NM" description="Numeric value, including integer or float (e.g., creatinine, weight)" creator="1" date_created="2004-02-02 00:00:00.0" retired="false" uuid="8d4a4488-c2cc-11de-8d13-0010c6dffd0f"/>
5556
<concept_datatype concept_datatype_id="2" name="Coded" hl7_abbreviation="CWE" description="Value determined by term dictionary lookup (i.e., term identifier)" creator="1" date_created="2004-02-02 00:00:00.0" retired="false" uuid="8d4a48b6-c2cc-11de-8d13-0010c6dffd0f"/>
5657
<concept_datatype concept_datatype_id="3" name="Text" hl7_abbreviation="ST" description="Free text" creator="1" date_created="2004-02-02 00:00:00.0" retired="false" uuid="8d4a4ab4-c2cc-11de-8d13-0010c6dffd0f"/>
@@ -175,11 +176,11 @@
175176
<drug drug_id="2" concept_id="792" name="Triomune-30" combination="true" dose_strength="1.0" units="tab(s)" creator="1" date_created="2005-02-24 00:00:00.0" retired="false" uuid="3cfcf118-931c-46f7-8ff6-7b876f0d4202"/>
176177
<drug drug_id="3" concept_id="88" name="Aspirin" combination="false" dose_strength="325.0" units="mg" creator="1" date_created="2005-02-24 00:00:00.0" retired="false" uuid="05ec820a-d297-44e3-be6e-698531d9dd3f"/>
177178
<drug drug_id="11" concept_id="3" name="NYQUIL" combination="true" units="" creator="1" date_created="2008-08-15 15:34:03.0" retired="true" uuid="7e2323fa-0fa0-461f-9b59-6765997d849e"/>
178-
<concept concept_id="113" retired="true" datatype_id="1" class_id="1" is_set="false" creator="1" date_created="2004-08-12 00:00:00.0" version="" uuid="7e02d1a0-7869-11e3-981f-0800200c9a66"/>
179+
<concept concept_id="113" retired="true" datatype_id="1" class_id="19" is_set="false" creator="1" date_created="2004-08-12 00:00:00.0" version="" uuid="7e02d1a0-7869-11e3-981f-0800200c9a66"/>
179180
<concept_name concept_id="113" name="1/day x 7 days/week" concept_name_id="1131" locale="en" creator="1" date_created="2008-08-15 13:52:53.0" concept_name_type="FULLY_SPECIFIED" locale_preferred="1" voided="false" uuid="83f24a00-7869-11e3-981f-0800200c9a66"/>
180181
<order_frequency order_frequency_id="1" concept_id="113" creator="1" date_created="2008-08-15 13:52:53.0" retired="false" uuid="28090760-7c38-11e3-baa7-0800200c9a66" />
181-
<order_frequency order_frequency_id="2" concept_id="113" creator="1" date_created="2008-08-15 13:52:53.0" retired="false" uuid="38090760-7c38-11e3-baa7-0800200c9a66" />
182-
<order_frequency order_frequency_id="3" concept_id="113" creator="1" date_created="2008-08-15 13:52:53.0" retired="true" retire_reason="Some Retire Reason" retired_by="1" date_retired="2009-08-15 13:52:53.0" uuid="48090760-7c38-11e3-baa7-0800200c9a66" />
182+
<order_frequency order_frequency_id="2" concept_id="3" creator="1" date_created="2008-08-15 13:52:53.0" retired="false" uuid="38090760-7c38-11e3-baa7-0800200c9a66" />
183+
<order_frequency order_frequency_id="3" concept_id="4" creator="1" date_created="2008-08-15 13:52:53.0" retired="true" retire_reason="Some Retire Reason" retired_by="1" date_retired="2009-08-15 13:52:53.0" uuid="48090760-7c38-11e3-baa7-0800200c9a66" />
183184
<drug_order order_id="1" drug_inventory_id="3" dose="325.0" dose_units="50" as_needed="false" frequency="1" dosing_type="SIMPLE"/>
184185
<drug_order order_id="2" drug_inventory_id="2" dose="1.0" dose_units="51" as_needed="false" frequency="1" dosing_type="SIMPLE"/>
185186
<drug_order order_id="3" drug_inventory_id="2" dose="2.0" dose_units="51" as_needed="false" frequency="1" dosing_type="SIMPLE"/>

‎webapp/src/main/webapp/WEB-INF/messages.properties

+3
Original file line numberDiff line numberDiff line change
@@ -2733,3 +2733,6 @@ Provider.notLinkedToPerson=Provider not linked to person
27332733
Provider.linkToPerson=Link to person
27342734
Provider.unLinkFromPerson=Unlink from person
27352735
Provider.error.duplicateIdentifier=Another provider already has the identifier {0}
2736+
2737+
OrderFrequency.concept.shouldBeClassFrequency=Concept should be of class Frequency
2738+
OrderFrequency.concept.shouldNotBeShared=Only one OrderFrequency should be mapped to a particular concept

0 commit comments

Comments
 (0)
Please sign in to comment.