|
| 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 | +} |
0 commit comments