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

openmrs / openmrs-core / 41717837886

03 Nov 2026 08:59PM UTC coverage: 68.971% (+0.05%) from 68.922%
41717837886

push

github

ibacher
TRUNK-6863: Remove the generation token before clearing a node's cache locally (#6791)

1 of 1 new or added line in 1 file covered. (100.0%)

413 existing lines in 30 files now uncovered.

26445 of 38342 relevant lines covered (68.97%)

0.69 hits per line

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

75.86
/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

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

29
import org.apache.commons.lang3.StringUtils;
30
import org.hibernate.annotations.LazyCollection;
31
import org.hibernate.annotations.LazyCollectionOption;
32
import org.hibernate.envers.Audited;
33
import org.openmrs.util.CollectionUtil;
34
import org.openmrs.util.OpenmrsUtil;
35
import org.openmrs.util.UuidUtil;
36

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

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

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

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

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

383
    private List<Concept> getReactionConcepts(){
384
        List<Concept> reactionConcepts = new ArrayList<>(getReactions().size());
1 ✔
385
        for (AllergyReaction ar : getReactions()) {
1 ✔
386
            reactionConcepts.add(ar.getReaction());
1 ✔
387
        }
1 ✔
388
        return reactionConcepts;
1 ✔
389
    }
390
    
391
    /**
392
         * @return Returns the reactionNonCoded
393
         */
394
        public String getReactionNonCoded() {
395
                for (AllergyReaction reaction : reactions) {
1 ✔
396
                        if (StringUtils.isNotBlank(reaction.getReactionNonCoded())) {
1 ✔
397
                                return reaction.getReactionNonCoded();
1 ✔
398
                        }
UNCOV
399
                }
×
400
                return null;
1 ✔
401
        }
402
        
403
        /**
404
         * Gets the reaction with a given concept
405
         * 
406
         * @param concept the concept
407
         * @return the reaction if any exists
408
         */
409
        public AllergyReaction getReaction(Concept concept) {
UNCOV
410
                for (AllergyReaction reaction : reactions) {
×
UNCOV
411
                        if (reaction.getReaction().equals(concept)) {
×
UNCOV
412
                                return reaction;
×
413
                        }
UNCOV
414
                }
×
UNCOV
415
                return null;
×
416
        }
417
        
418
        /**
419
         * Checks if we have the same allergen as that in the given allergy
420
         * 
421
         * @param allergy the given allergy whose allergen to check
422
         * @return true if the same, else false
423
         */
424
        public boolean hasSameAllergen(Allergy allergy) {
425
                if (allergen == null || allergy.getAllergen() == null) {
1 ✔
426
                        return false;
1 ✔
427
                }
428
                return allergen.isSameAllergen(allergy.getAllergen());
1 ✔
429
        }
430
        
431
        /**
432
         * Basic property getter for encounter
433
         * 
434
         * @return encounter - the associated encounter
435
         * @since 2.5.0
436
         */
437
        public Encounter getEncounter() {
UNCOV
438
                return encounter;
×
439
        }
440
        
441
        /**
442
         * Basic property setter for encounter
443
         *  
444
         * @param encounter - the encounter to set
445
         * @since 2.5.0
446
         */
447
        public void setEncounter(Encounter encounter) {
448
                this.encounter = encounter;
1 ✔
449
        }
1 ✔
450
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc