Skip to content

Commit 2e3c7cf

Browse files
rkorytkowskiwluyima
authored andcommittedDec 19, 2013
TRUNK-4129 Modify datatype of drug_order.quantity column and add
quantity_units column to drug_order
1 parent a2e4eb1 commit 2e3c7cf

File tree

7 files changed

+251
-5
lines changed

7 files changed

+251
-5
lines changed
 

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

+22-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public enum DosingType {
4242

4343
private Boolean asNeeded = false;
4444

45-
private Integer quantity;
45+
private Double quantity;
46+
47+
private Concept quantityUnits;
4648

4749
private Drug drug;
4850

@@ -90,6 +92,7 @@ protected DrugOrder copyHelper(DrugOrder target) {
9092
target.asNeeded = getAsNeeded();
9193
target.asNeededCondition = getAsNeededCondition();
9294
target.quantity = getQuantity();
95+
target.quantityUnits = getQuantityUnits();
9396
target.drug = getDrug();
9497
target.dosingType = getDosingType();
9598
target.dosingInstructions = getDosingInstructions();
@@ -207,7 +210,7 @@ public void setComplex(Boolean complex) {
207210
*
208211
* @return quantity
209212
*/
210-
public Integer getQuantity() {
213+
public Double getQuantity() {
211214
return this.quantity;
212215
}
213216

@@ -216,10 +219,26 @@ public Integer getQuantity() {
216219
*
217220
* @param quantity
218221
*/
219-
public void setQuantity(Integer quantity) {
222+
public void setQuantity(Double quantity) {
220223
this.quantity = quantity;
221224
}
222225

226+
/**
227+
* @since 1.10
228+
* @return concept
229+
*/
230+
public Concept getQuantityUnits() {
231+
return quantityUnits;
232+
}
233+
234+
/**
235+
* @since 1.10
236+
* @param quantityUnits
237+
*/
238+
public void setQuantityUnits(Concept quantityUnits) {
239+
this.quantityUnits = quantityUnits;
240+
}
241+
223242
/**
224243
* Gets the drug
225244
*

‎api/src/main/java/org/openmrs/util/DatabaseUtil.java

+55
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,59 @@ public static List<List<Object>> executeSQL(Connection conn, String sql, boolean
151151

152152
return results;
153153
}
154+
155+
/**
156+
* Returns conceptId for the given units from OpenmrsConstants#GP_ORDER_ENTRY_UNITS_TO_CONCEPTS_MAPPINGS
157+
* global property.
158+
*
159+
* @param connection
160+
* @param units
161+
* @return conceptId
162+
* @throws DAOException
163+
* @should return concept_id for drug_order_quantity_units
164+
* @should fail if units is not specified
165+
*/
166+
public static Integer getConceptIdForUnits(Connection connection, String units) throws DAOException {
167+
PreparedStatement unitsToConceptsQuery;
168+
try {
169+
unitsToConceptsQuery = connection
170+
.prepareStatement("select property_value from global_property where property = ?");
171+
unitsToConceptsQuery.setString(1, OpenmrsConstants.GP_ORDER_ENTRY_UNITS_TO_CONCEPTS_MAPPINGS);
172+
ResultSet unitsToConceptsResult = unitsToConceptsQuery.executeQuery();
173+
if (!unitsToConceptsResult.next()) {
174+
throw new DAOException(
175+
OpenmrsConstants.GP_ORDER_ENTRY_UNITS_TO_CONCEPTS_MAPPINGS
176+
+ " global property must be specified before upgrading. Please refer to upgrade instructions for more details.");
177+
}
178+
179+
String unitsToConceptsGP = unitsToConceptsResult.getString(1);
180+
String[] unitsToConcepts = unitsToConceptsGP.split(",");
181+
for (String unitsToConcept : unitsToConcepts) {
182+
if (unitsToConcept.startsWith(units)) {
183+
String concept = unitsToConcept.substring(units.length() + 1);// '+ 1' stands for ':'
184+
185+
if (concept.toLowerCase().equals("null")) {
186+
return null;
187+
}
188+
189+
try {
190+
return Integer.valueOf(concept);
191+
}
192+
catch (NumberFormatException e) {
193+
throw new DAOException(OpenmrsConstants.GP_ORDER_ENTRY_UNITS_TO_CONCEPTS_MAPPINGS
194+
+ " global property contains invalid mapping from " + units + " to concept ID " + concept
195+
+ ". ID must be an integer or null. Please refer to upgrade instructions for more details.",
196+
e);
197+
}
198+
}
199+
}
200+
}
201+
catch (SQLException e) {
202+
throw new DAOException(e);
203+
}
204+
205+
throw new DAOException(OpenmrsConstants.GP_ORDER_ENTRY_UNITS_TO_CONCEPTS_MAPPINGS
206+
+ " global property does not have mapping for " + units
207+
+ ". Please refer to upgrade instructions for more details.");
208+
}
154209
}

‎api/src/main/java/org/openmrs/util/OpenmrsConstants.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1052,10 +1052,12 @@ public static final Collection<String> AUTO_ROLES() {
10521052

10531053
public static final String GLOBAL_PROPERTY_NEXT_ORDER_NUMBER = "orderEntry.nextOrderNumber";
10541054

1055+
public static final String GP_ORDER_ENTRY_UNITS_TO_CONCEPTS_MAPPINGS = "orderEntry.unitsToConceptsMappings";
1056+
10551057
/**
10561058
* At OpenMRS startup these global properties/default values/descriptions are inserted into the
10571059
* database if they do not exist yet.
1058-
*
1060+
*
10591061
* @return List<GlobalProperty> of the core global properties
10601062
*/
10611063
public static final List<GlobalProperty> CORE_GLOBAL_PROPERTIES() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.util.databasechange;
15+
16+
import liquibase.change.custom.CustomTaskChange;
17+
import liquibase.database.Database;
18+
import liquibase.database.jvm.JdbcConnection;
19+
import liquibase.exception.CustomChangeException;
20+
import liquibase.exception.DatabaseException;
21+
import liquibase.exception.SetupException;
22+
import liquibase.exception.ValidationErrors;
23+
import liquibase.resource.ResourceAccessor;
24+
import org.openmrs.util.DatabaseUtil;
25+
import org.openmrs.util.OpenmrsConstants;
26+
27+
import java.sql.PreparedStatement;
28+
import java.sql.SQLException;
29+
import java.sql.Types;
30+
31+
/**
32+
* Applies quantity units to drug orders created pre 1.10.x based on a concept specified in a global property.
33+
*
34+
* @see OpenmrsConstants#GP_ORDER_ENTRY_UNITS_TO_CONCEPTS_MAPPINGS
35+
*/
36+
public class MigrateDrugOrderQuantityToCodedQuantityUnitsChangeset implements CustomTaskChange {
37+
38+
@Override
39+
public void execute(Database database) throws CustomChangeException {
40+
JdbcConnection connection = (JdbcConnection) database.getConnection();
41+
42+
Integer conceptId = DatabaseUtil.getConceptIdForUnits(connection.getUnderlyingConnection(),
43+
"drug_order_quantity_units");
44+
45+
try {
46+
PreparedStatement update = connection
47+
.prepareStatement("update drug_order set quantity_units = ? where quantity_units is NULL");
48+
if (conceptId == null) {
49+
update.setNull(1, Types.INTEGER);
50+
} else {
51+
update.setInt(1, conceptId);
52+
}
53+
update.executeUpdate();
54+
}
55+
catch (DatabaseException e) {
56+
throw new CustomChangeException(e);
57+
}
58+
catch (SQLException e) {
59+
throw new CustomChangeException(e);
60+
}
61+
}
62+
63+
@Override
64+
public String getConfirmationMessage() {
65+
return "Finished migrating drug order quantity to coded quantity units";
66+
}
67+
68+
@Override
69+
public void setUp() throws SetupException {
70+
}
71+
72+
@Override
73+
public void setFileOpener(ResourceAccessor resourceAccessor) {
74+
}
75+
76+
@Override
77+
public ValidationErrors validate(Database database) {
78+
return null;
79+
}
80+
}

‎api/src/main/resources/liquibase-update-to-latest.xml

+31
Original file line numberDiff line numberDiff line change
@@ -6659,4 +6659,35 @@
66596659
referencedTableName="concept" referencedColumnNames="concept_id" />
66606660
</changeSet>
66616661

6662+
<changeSet id="201312161618-TRUNK-4129" author="rkorytkowski">
6663+
<preConditions onFail="MARK_RAN">
6664+
<not><foreignKeyConstraintExists foreignKeyName="drug_order_quantity_units"/></not>
6665+
</preConditions>
6666+
<comment>Adding quantity_units column to drug_order table</comment>
6667+
<addColumn tableName="drug_order">
6668+
<column name="quantity_units" type="int" value="NULL"/>
6669+
</addColumn>
6670+
<addForeignKeyConstraint baseTableName="drug_order" baseColumnNames="quantity_units"
6671+
constraintName="drug_order_quantity_units"
6672+
referencedTableName="concept"
6673+
referencedColumnNames="concept_id"/>
6674+
</changeSet>
6675+
6676+
<changeSet id="201312161713-TRUNK-4129" author="rkorytkowski">
6677+
<comment>Changing quantity column of drug_order to double</comment>
6678+
<modifyDataType tableName="drug_order" columnName="quantity" newDataType="double"/>
6679+
</changeSet>
6680+
6681+
<changeSet id="201312191209-TRUNK-4129" author="rkorytkowski">
6682+
<preConditions onFail="MARK_RAN">
6683+
<not>
6684+
<sqlCheck expectedResult="0">
6685+
select count(*) from drug_order;
6686+
</sqlCheck>
6687+
</not>
6688+
</preConditions>
6689+
<comment>Migrating drug order quantity to coded quantity units</comment>
6690+
<customChange class="org.openmrs.util.databasechange.MigrateDrugOrderQuantityToCodedQuantityUnitsChangeset" />
6691+
</changeSet>
6692+
66626693
</databaseChangeLog>

‎api/src/main/resources/org/openmrs/api/db/hibernate/Order.hbm.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@
101101
<property name="frequency" type="java.lang.String" column="frequency" length="255"/>
102102
<property name="asNeeded" type="boolean" column="as_needed" length="1" not-null="true"/>
103103
<property name="asNeededCondition" type="string" column="as_needed_condition" length="255" />
104-
<property name="quantity" type="int" column="quantity" length="11"/>
104+
<property name="quantity" type="double" column="quantity" length="22"/>
105+
<many-to-one name="quantityUnits" class="org.openmrs.Concept" column="quantity_units" />
105106
<!-- bi-directional many-to-one association to Drug -->
106107
<many-to-one name="drug" class="org.openmrs.Drug" not-null="false">
107108
<column name="drug_inventory_id" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.util;
15+
16+
import org.hamcrest.core.Is;
17+
import org.junit.Assert;
18+
import org.junit.Test;
19+
import org.openmrs.GlobalProperty;
20+
import org.openmrs.api.AdministrationService;
21+
import org.openmrs.api.context.Context;
22+
import org.openmrs.api.db.DAOException;
23+
import org.openmrs.test.BaseContextSensitiveTest;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
26+
public class DatabaseUtilIntegrationTest extends BaseContextSensitiveTest {
27+
28+
@Autowired
29+
AdministrationService adminService;
30+
31+
/**
32+
* @verifies return concept_id for drug_order_quantity_units
33+
* @see DatabaseUtil#getConceptIdForUnits(java.sql.Connection, String)
34+
*/
35+
@Test
36+
public void getConceptIdForUnits_shouldReturnConcept_idForDrug_order_quantity_units() throws Exception {
37+
adminService.saveGlobalProperty(new GlobalProperty(OpenmrsConstants.GP_ORDER_ENTRY_UNITS_TO_CONCEPTS_MAPPINGS,
38+
"mg:5401,drug_order_quantity_units:5403,ounces:5402"));
39+
Context.flushSession();
40+
41+
Integer conceptId = DatabaseUtil.getConceptIdForUnits(getConnection(), "drug_order_quantity_units");
42+
43+
Assert.assertThat(conceptId, Is.is(5403));
44+
}
45+
46+
/**
47+
* @verifies fail if units is not specified
48+
* @see DatabaseUtil#getConceptIdForUnits(java.sql.Connection, String)
49+
*/
50+
@Test(expected = DAOException.class)
51+
public void getConceptIdForUnits_shouldFailIfUnitsIsNotSpecified() throws Exception {
52+
adminService.saveGlobalProperty(new GlobalProperty(OpenmrsConstants.GP_ORDER_ENTRY_UNITS_TO_CONCEPTS_MAPPINGS,
53+
"mg:5401,ounces:5402"));
54+
Context.flushSession();
55+
56+
DatabaseUtil.getConceptIdForUnits(getConnection(), "drug_order_quantity_units");
57+
}
58+
}

0 commit comments

Comments
 (0)
Please sign in to comment.