• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 21763664900

06 Feb 2026 07:45PM UTC coverage: 63.49% (+0.1%) from 63.392%
21763664900

push

github

ibacher
TRUNK-6536: Replace string concatenation with query building (#5753)

39 of 95 new or added lines in 5 files covered. (41.05%)

4 existing lines in 2 files now uncovered.

23156 of 36472 relevant lines covered (63.49%)

0.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

1.35
/api/src/main/java/org/openmrs/util/databasechange/MigrateAllergiesChangeSet.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.util.databasechange;
11

12
import java.sql.PreparedStatement;
13
import java.sql.ResultSet;
14
import java.util.UUID;
15

16
import org.openmrs.AllergySeverity;
17

18
import liquibase.change.custom.CustomTaskChange;
19
import liquibase.database.Database;
20
import liquibase.database.jvm.JdbcConnection;
21
import liquibase.exception.CustomChangeException;
22
import liquibase.exception.SetupException;
23
import liquibase.exception.ValidationErrors;
24
import liquibase.resource.ResourceAccessor;
25

26
/**
27
 * Moves un voided allergies from the old active_list and active_list_allergy tables to the new
28
 * allergy and allergy_recation tables
29
 */
30
public class MigrateAllergiesChangeSet implements CustomTaskChange {
1✔
31
        
32
        private Integer mildConcept;
33
        private Integer moderateConcept;
34
        private Integer severeConcept;
35
        
36
        @Override
37
        public String getConfirmationMessage() {
38
                return "Successfully moved un voided allergies from old to new tables";
×
39
        }
40
        
41
        @Override
42
        public void setUp() throws SetupException {
43
                
44
        }
×
45
        
46
        @Override
47
        public void setFileOpener(ResourceAccessor resourceAccessor) {
48
                
49
        }
×
50
        
51
        @Override
52
        public ValidationErrors validate(Database database) {
53
                return null;
×
54
        }
55
        
56
        @Override
57
        public void execute(Database database) throws CustomChangeException {
58
                try {
59
                        loadSeverityConcepts(database);
×
60

61
                        JdbcConnection connection = (JdbcConnection) database.getConnection();
×
62

63
                        int allergyTypeId;
NEW
64
                        try (PreparedStatement typeStmt = connection.prepareStatement(
×
65
                                        "select active_list_type_id from active_list_type where name = ?")) {
NEW
66
                                typeStmt.setString(1, "Allergy");
×
NEW
67
                                try (ResultSet rs = typeStmt.executeQuery()) {
×
NEW
68
                                        if (!rs.next()) {
×
NEW
69
                                                throw new CustomChangeException("Failed to find row with name 'Allergy' in the active_list_type");
×
70
                                        }
NEW
71
                                        allergyTypeId = rs.getInt(1);
×
72
                                }
73
                        }
74

NEW
75
                        try (PreparedStatement allergyInsertStatement = connection.prepareStatement(
×
76
                                        "insert into allergy (patient_id, coded_allergen, severity_concept_id, creator, date_created, uuid, comment, allergen_type) "
77
                                                + "values(?,?,?,?,?,?,?,?)");
NEW
78
                                PreparedStatement reactionInsertStatement = connection.prepareStatement(
×
79
                                        "insert into allergy_reaction (allergy_id, reaction_concept_id, uuid) "
80
                                                + "values (?,?,?)");
NEW
81
                                PreparedStatement allergySelectStatement = connection.prepareStatement(
×
82
                                        "select allergy_id from allergy where uuid = ?");
NEW
83
                                PreparedStatement activeListStmt = connection.prepareStatement(
×
84
                                        "select person_id, concept_id, comments, creator, date_created, uuid, reaction_concept_id, severity, allergy_type "
85
                                                + "from active_list al inner join active_list_allergy ala on al.active_list_id=ala.active_list_id "
86
                                                + "where voided = 0 and active_list_type_id = ?")) {
87

NEW
88
                                activeListStmt.setInt(1, allergyTypeId);
×
NEW
89
                                try (ResultSet rs = activeListStmt.executeQuery()) {
×
NEW
90
                                while (rs.next()) {
×
NEW
91
                                        String uuid = rs.getString("uuid");
×
92

93
                                        //insert allergy
NEW
94
                                        allergyInsertStatement.setInt(1, rs.getInt("person_id"));
×
NEW
95
                                        allergyInsertStatement.setInt(2, rs.getInt("concept_id"));
×
96

NEW
97
                                        Integer severityConcept = null;
×
NEW
98
                                        String severity = rs.getString("severity");
×
NEW
99
                                        if (AllergySeverity.MILD.name().equals(severity)) {
×
NEW
100
                                                severityConcept = mildConcept;
×
101
                                        }
NEW
102
                                        else if (AllergySeverity.MODERATE.name().equals(severity)) {
×
NEW
103
                                                severityConcept = moderateConcept;
×
104
                                        }
NEW
105
                                        else if (AllergySeverity.SEVERE.name().equals(severity)) {
×
NEW
106
                                                severityConcept = severeConcept;
×
107
                                        }
108
                                        //TODO what do we do with the other severities?
109

NEW
110
                                        if (severityConcept != null) {
×
NEW
111
                                                allergyInsertStatement.setInt(3, severityConcept);
×
112
                                        } else {
NEW
113
                                                allergyInsertStatement.setNull(3, java.sql.Types.INTEGER);
×
114
                                        }
115

NEW
116
                                        allergyInsertStatement.setInt(4, rs.getInt("creator"));
×
NEW
117
                                        allergyInsertStatement.setDate(5, rs.getDate("date_created"));
×
NEW
118
                                        allergyInsertStatement.setString(6, uuid);
×
NEW
119
                                        allergyInsertStatement.setString(7, rs.getString("comments"));
×
120

NEW
121
                                        String allergyType = rs.getString("allergy_type");
×
NEW
122
                                        if (allergyType == null) {
×
NEW
123
                                                allergyType = "DRUG";
×
124
                                        }
NEW
125
                                        else if ("ENVIRONMENTAL".equals(allergyType)) {
×
NEW
126
                                                allergyType = "ENVIRONMENT";
×
127
                                        }
128

NEW
129
                                        allergyInsertStatement.setString(8, allergyType);
×
130

NEW
131
                                        allergyInsertStatement.execute();
×
132

133
                                        //get inserted allergy_id
NEW
134
                                        allergySelectStatement.setString(1, uuid);
×
NEW
135
                                        try (ResultSet rs2 = allergySelectStatement.executeQuery()) {
×
NEW
136
                                                rs2.next();
×
137

138
                                                //insert reaction
NEW
139
                                                reactionInsertStatement.setInt(1, rs2.getInt(1));
×
140
                                        }
NEW
141
                                        reactionInsertStatement.setInt(2, rs.getInt("reaction_concept_id"));
×
NEW
142
                                        reactionInsertStatement.setString(3, UUID.randomUUID().toString());
×
143

144
                                        //some active lists do not have reactions recorded
NEW
145
                                        if (!rs.wasNull()) {
×
NEW
146
                                                reactionInsertStatement.execute();
×
147
                                        }
UNCOV
148
                                }
×
149
                                }
150
                        }
151
                }
152
                catch (Exception ex) {
×
153
                        throw new CustomChangeException(ex);
×
154
                }
×
155
        }
×
156
        
157
        private void loadSeverityConcepts(Database database) throws Exception {
158
                mildConcept = getConceptByGlobalProperty(database, "allergy.concept.severity.mild");
×
159
                moderateConcept = getConceptByGlobalProperty(database, "allergy.concept.severity.moderate");
×
160
                severeConcept = getConceptByGlobalProperty(database, "allergy.concept.severity.severe");
×
161
        }
×
162
        
163
        private Integer getConceptByGlobalProperty(Database database, String globalPropertyName) throws Exception {
164
                JdbcConnection connection = (JdbcConnection) database.getConnection();
×
NEW
165
                try (PreparedStatement gpStmt = connection.prepareStatement(
×
166
                                "SELECT property_value FROM global_property WHERE property = ?")) {
NEW
167
                        gpStmt.setString(1, globalPropertyName);
×
NEW
168
                        try (ResultSet rs = gpStmt.executeQuery()) {
×
NEW
169
                                if (rs.next()) {
×
NEW
170
                                        String uuid = rs.getString("property_value");
×
171

NEW
172
                                        try (PreparedStatement conceptStmt = connection.prepareStatement(
×
173
                                                        "SELECT concept_id FROM concept WHERE uuid = ?")) {
NEW
174
                                                conceptStmt.setString(1, uuid);
×
NEW
175
                                                try (ResultSet conceptRs = conceptStmt.executeQuery()) {
×
NEW
176
                                                        if (conceptRs.next()) {
×
NEW
177
                                                                return conceptRs.getInt("concept_id");
×
178
                                                        }
179
                                                }
180
                                        }
181
                                }
182
                        }
183
                }
184

185
                throw new IllegalStateException("Configuration required: " + globalPropertyName);
×
186
        }
187
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc