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

openmrs / openmrs-core / 24403205906

14 Apr 2026 01:59PM UTC coverage: 63.88% (+0.04%) from 63.836%
24403205906

push

github

ibacher
Fix compilation issue

0 of 2 new or added lines in 1 file covered. (0.0%)

193 existing lines in 14 files now uncovered.

22202 of 34756 relevant lines covered (63.88%)

0.64 hits per line

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

75.63
/api/src/main/java/org/openmrs/Allergy.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;
11

12
import java.lang.reflect.InvocationTargetException;
13
import java.util.ArrayList;
14
import java.util.Date;
15
import java.util.List;
16
import java.util.UUID;
17

18
import javax.persistence.CascadeType;
19
import javax.persistence.Column;
20
import javax.persistence.Embedded;
21
import javax.persistence.Entity;
22
import javax.persistence.GeneratedValue;
23
import javax.persistence.GenerationType;
24
import javax.persistence.Id;
25
import javax.persistence.JoinColumn;
26
import javax.persistence.ManyToOne;
27
import javax.persistence.OneToMany;
28
import javax.persistence.Table;
29

30
import org.apache.commons.lang3.StringUtils;
31
import org.hibernate.annotations.LazyCollection;
32
import org.hibernate.annotations.LazyCollectionOption;
33
import org.openmrs.util.OpenmrsUtil;
34

35
/**
36
 * Represent allergy
37
 */
38
@Entity
39
@Table(name = "allergy")
40
public class Allergy extends BaseFormRecordableOpenmrsData {
41
        
42
        public static final long serialVersionUID = 1;
43
        
44
        @Id
45
        @GeneratedValue(strategy = GenerationType.IDENTITY)
46
        @Column(name = "allergy_id")
47
        private Integer allergyId;
48
        
49
        @ManyToOne(optional = false)
50
        @JoinColumn(name = "patient_id")
51
        private Patient patient;
52
        
53
        @Embedded
54
        private Allergen allergen;
55
        
56
        @ManyToOne
57
        @JoinColumn(name = "severity_concept_id")
58
        private Concept severity;
59
        
60
        @Column(name = "comments", length = 1024)
61
        private String comments;
62
        
63

64
        @OneToMany(mappedBy = "allergy", cascade = CascadeType.ALL, orphanRemoval = true)
1✔
65
        @LazyCollection(LazyCollectionOption.FALSE)
66
        private List<AllergyReaction> reactions = new ArrayList<>();
67
        
68
        @ManyToOne(optional = true)
69
        @JoinColumn(name = "encounter_id")
70
        private Encounter encounter;
71
        
72
        /**
73
         * Default constructor
74
         */
75
        public Allergy(){
1✔
76
        }
1✔
77
        
78
        /**
79
         * @param patient the patient to set.
80
         * @param allergen the allergen to set
81
         * @param severity the severity to set
82
         * @param comments the comment to set
83
         * @param reactions the reactions to set
84
         */
85
        public Allergy(Patient patient, Allergen allergen, Concept severity, String comments, List<AllergyReaction> reactions) {
1✔
86
                this.patient = patient;
1✔
87
                this.allergen = allergen;
1✔
88
                this.severity = severity;
1✔
89
                this.comments = comments;
1✔
90
                
91
                //we do not allow to be in a state where reactions is null
92
                if (reactions != null) {
1✔
93
                        this.reactions = reactions;
1✔
94
                }
95
        }
1✔
96
        
97
    /**
98
     * @return the allergyId
99
     */
100
    public Integer getAllergyId() {
101
            return allergyId;
1✔
102
    }
103

104
    /**
105
     * @param allergyId the allergyId to set
106
     */
107
    public void setAllergyId(Integer allergyId) {
108
            this.allergyId = allergyId;
1✔
109
    }
1✔
110

111
        /**
112
         * @see org.openmrs.OpenmrsObject#getId()
113
         */
114
        @Override
115
        public Integer getId() {
116
                return allergyId;
×
117
        }
118
        
119
        /**
120
         * @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
121
         */
122
        @Override
123
        public void setId(Integer allergyId) {
124
                this.allergyId = allergyId;
×
125
        }
×
126
        
127
        
128
        /**
129
         * @return Returns the patient
130
         */
131
        public Patient getPatient() {
132
                return patient;
1✔
133
        }
134
        
135
        /**
136
         * @param patient the patient to set
137
         */
138
        public void setPatient(Patient patient) {
139
                this.patient = patient;
1✔
140
        }
1✔
141
        /**
142
         * @return the allergyType
143
         */
144
        public AllergenType getAllergenType() {
145
                return allergen.getAllergenType();
×
146
        }
147
        
148
        /**
149
         * set the allergyType of the Allergy
150
         * @param allergenType the allergyType to set
151
         */
152
        public void setAllergenType(AllergenType allergenType) {
153
                this.allergen.setAllergenType(allergenType);
×
154
        }
×
155
        
156
        /**
157
         * set the allergyType of the Allergy. Here the allergy type will be chosen from the enum values in the {@link AllergenType}, according to the given String type. 
158
         * @param type the allergyType to set   
159
         */
160
        public void setAllergenType(String type) {
161
                this.allergen.setAllergenType(StringUtils.isBlank(type) ? null : AllergenType.valueOf(type));
×
162
        }
×
163
        
164
        /**
165
         * @return Returns the allergen
166
         */
167
        public Allergen getAllergen() {
168
                return allergen;
1✔
169
        }
170
        
171
        /**
172
         * @param allergen the allergen to set
173
         */
174
        public void setAllergen(Allergen allergen) {
175
                this.allergen = allergen;
1✔
176
        }
1✔
177
        /**
178
         * @return Returns the severity
179
         */
180
        public Concept getSeverity() {
181
                return severity;
1✔
182
        }
183
        
184
        /**
185
         * @param severity the severity to set
186
         */
187
        public void setSeverity(Concept severity) {
188
                this.severity = severity;
1✔
189
        }
1✔
190
        
191
        /**
192
         * @return Returns the comment
193
         * @deprecated as of 2.3.0, replaced by {@link #getComments()}
194
         */
195
        @Deprecated
196
        public String getComment() {
197
                return getComments();
1✔
198
        }
199
        
200
        /**
201
         * @param comment the comment to set
202
         * @deprecated as of 2.3.0, replaced by {@link #setComments(String)}
203
         */
204
        @Deprecated
205
        public void setComment(String comment) {
206
                setComments(comment);
1✔
207
        }
1✔
208
        
209
        /**
210
         * @return Returns the comments
211
         * @since 2.3.0
212
         */
213
        public String getComments() {
214
                return comments;
1✔
215
        }
216
        
217
        /**
218
         * @param comments the comments to set
219
         * @since 2.3.0
220
         */
221
        public void setComments(String comments) {
222
                this.comments = comments;
1✔
223
        }
1✔
224
        /**
225
         * @return Returns the reactions
226
         */
227
        public List<AllergyReaction> getReactions() {
228
                return reactions;
1✔
229
        }
230
        
231
        /**
232
         * @param reactions the reactions to set
233
         */
234
        public void setReactions(List<AllergyReaction> reactions) {
235
                //we do not allow to be in a state where reactions is null
236
                if (reactions != null) {
1✔
237
                        this.reactions = reactions;
1✔
238
                }
239
                else {
240
                        this.reactions.clear();
×
241
                }
242
        }
1✔
243

244
        /**
245
         * Adds a new allergy reaction
246
         * 
247
         * @param reaction the reaction to add
248
         * @return true if the reaction was added, else false
249
         */
250
        public boolean addReaction(AllergyReaction reaction) {
251
        if(getReactionConcepts().contains(reaction.getReaction())){
1✔
252
            return false;
×
253
        }
254
                reaction.setAllergy(this);
1✔
255
                return getReactions().add(reaction);
1✔
256
        }
257
        
258
        /**
259
         * Removes an allergy reaction
260
         * 
261
         * @param reaction the reaction to remove
262
         * @return true if the reaction was found and removed, else false.
263
         */
264
        public boolean removeReaction(AllergyReaction reaction) {
265
                return getReactions().remove(reaction);
×
266
        }
267
        
268
        public Date getDateLastUpdated() {
269
                if (getDateChanged() != null) {
×
270
                        return getDateChanged();
×
271
                }
272
                return getDateCreated();
×
273
        }
274
        
275
        /**
276
         * Checks if this allergy has the same values as a given one.
277
         * 
278
         * @param allergy the allergy whose values to compare with
279
         * @return true if the values match, else false
280
         */
281
        public boolean hasSameValues(Allergy allergy) {
282
                if (!OpenmrsUtil.nullSafeEquals(getAllergyId(), allergy.getAllergyId())) {
1✔
283
                        return false;
×
284
                }
285
                if (!OpenmrsUtil.nullSafeEquals(getPatient(), allergy.getPatient())) {
1✔
286
                        //if object instances are different but with the same patient id, then not changed
287
                        if (getPatient() != null && allergy.getPatient() != null) {
×
288
                                if (!OpenmrsUtil.nullSafeEquals(getPatient().getPatientId(), allergy.getPatient().getPatientId())) {
×
289
                                        return false;
×
290
                                }
291
                        }
292
                        else {
293
                                return false;
×
294
                        }
295
                }
296
                if (!OpenmrsUtil.nullSafeEquals(getAllergen().getCodedAllergen(), allergy.getAllergen().getCodedAllergen())) {
1✔
297
                        //if object instances are different but with the same concept id, then not changed
298
                        if (getAllergen().getCodedAllergen() != null && allergy.getAllergen().getCodedAllergen() != null) {
1✔
299
                                if (!OpenmrsUtil.nullSafeEquals(getAllergen().getCodedAllergen().getConceptId(), allergy.getAllergen().getCodedAllergen().getConceptId())) {
1✔
300
                                        return false;
1✔
301
                                }
302
                        }
303
                        else {
304
                                return false;
1✔
305
                        }
306
                }
307
                if (!OpenmrsUtil.nullSafeEquals(getAllergen().getNonCodedAllergen(), allergy.getAllergen().getNonCodedAllergen())) {
1✔
308
                        return false;
×
309
                }
310
                if (!OpenmrsUtil.nullSafeEquals(getSeverity(), allergy.getSeverity())) {
1✔
311
                        //if object instances are different but with the same concept id, then not changed
312
                        if (getSeverity() != null && allergy.getSeverity() != null) {
1✔
313
                                if (!OpenmrsUtil.nullSafeEquals(getSeverity().getConceptId(), allergy.getSeverity().getConceptId())) {
1✔
314
                                        return false;
1✔
315
                                }
316
                        }
317
                        else {
318
                                return false;
×
319
                        }
320
                }
321
                if (!OpenmrsUtil.nullSafeEquals(getComment(), allergy.getComment())) {
1✔
322
                        return false;
1✔
323
                }
324
                return hasSameReactions(allergy);
1✔
325
        }
326
        
327
        /**
328
         * Checks if this allergy has the same reaction values as those in the given one
329
         * 
330
         * @param allergy the allergy who reaction values to compare with
331
         * @return true if the values match, else false
332
         */
333
        private boolean hasSameReactions(Allergy allergy) {
334
                if (getReactions().size() != allergy.getReactions().size()) {
1✔
335
                        return false;
1✔
336
                }
337
                
338
                for (AllergyReaction reaction : getReactions()) {
1✔
339
                        AllergyReaction rc = allergy.getAllergyReaction(reaction.getAllergyReactionId());
1✔
340
                        if (!reaction.hasSameValues(rc)) {
1✔
341
                                return false;
1✔
342
                        }
343
                }
1✔
344
                
345
                return true;
1✔
346
        }
347
        
348
        /**
349
         * Gets an allergy reaction with a given id
350
         * 
351
         * @param allergyReactionId the allergy reaction id
352
         * @return the allergy reaction with a matching id
353
         */
354
        public AllergyReaction getAllergyReaction(Integer allergyReactionId) {
355
                for (AllergyReaction reaction : reactions) {
1✔
356
                        if (OpenmrsUtil.nullSafeEquals(reaction.getAllergyReactionId(), allergyReactionId)) {
1✔
357
                                return reaction;
1✔
358
                        }
359
                }
1✔
360
                
361
                return null;
×
362
        }
363
        
364
        /**
365
         * Copies all property values, apart from the id and uuid,
366
         * from the given allergy into this object
367
         * 
368
         * @param allergy the allergy whose property values to copy
369
         */
370
        public void copy(Allergy allergy) {
371
                setAllergyId(null);
1✔
372
                setUuid(UUID.randomUUID().toString());
1✔
373
                setPatient(allergy.getPatient());
1✔
374
                setAllergen(allergy.getAllergen());
1✔
375
                setSeverity(allergy.getSeverity());
1✔
376
                setComment(allergy.getComment());
1✔
377
                setReactions(new ArrayList<>());
1✔
378
                
379
                for (AllergyReaction reaction : allergy.getReactions()) {
1✔
380
                        reactions.add(reaction);
1✔
381
                        reaction.setAllergyReactionId(null);
1✔
382
                        reaction.setUuid(UUID.randomUUID().toString());
1✔
383
                }
1✔
384
        }
1✔
385

386
    private List<Concept> getReactionConcepts(){
387
        List<Concept> reactionConcepts = new ArrayList<>(getReactions().size());
1✔
388
        for (AllergyReaction ar : getReactions()) {
1✔
389
            reactionConcepts.add(ar.getReaction());
1✔
390
        }
1✔
391
        return reactionConcepts;
1✔
392
    }
393
    
394
    /**
395
         * @return Returns the reactionNonCoded
396
         */
397
        public String getReactionNonCoded() {
398
                for (AllergyReaction reaction : reactions) {
1✔
399
                        if (StringUtils.isNotBlank(reaction.getReactionNonCoded())) {
1✔
400
                                return reaction.getReactionNonCoded();
1✔
401
                        }
UNCOV
402
                }
×
403
                return null;
1✔
404
        }
405
        
406
        /**
407
         * Gets the reaction with a given concept
408
         * 
409
         * @param concept the concept
410
         * @return the reaction if any exists
411
         */
412
        public AllergyReaction getReaction(Concept concept) {
UNCOV
413
                for (AllergyReaction reaction : reactions) {
×
UNCOV
414
                        if (reaction.getReaction().equals(concept)) {
×
415
                                return reaction;
×
416
                        }
417
                }
×
UNCOV
418
                return null;
×
419
        }
420
        
421
        /**
422
         * Checks if we have the same allergen as that in the given allergy
423
         * 
424
         * @param allergy the given allergy whose allergen to check
425
         * @return true if the same, else false
426
         */
427
        public boolean hasSameAllergen(Allergy allergy) {
428
                if (allergen == null || allergy.getAllergen() == null) {
1✔
429
                        return false;
1✔
430
                }
431
                return allergen.isSameAllergen(allergy.getAllergen());
1✔
432
        }
433
        
434
        /**
435
         * Basic property getter for encounter
436
         * 
437
         * @return encounter - the associated encounter
438
         * @since 2.5.0
439
         */
440
        public Encounter getEncounter() {
UNCOV
441
                return encounter;
×
442
        }
443
        
444
        /**
445
         * Basic property setter for encounter
446
         *  
447
         * @param encounter - the encounter to set
448
         * @since 2.5.0
449
         */
450
        public void setEncounter(Encounter encounter) {
451
                this.encounter = encounter;
1✔
452
        }
1✔
453
}
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