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

openmrs / openmrs-core / 10946298164

19 Sep 2024 05:33PM UTC coverage: 63.759% (+0.09%) from 63.671%
10946298164

push

github

mogoodrich
TRUNK-6265: Bugs in Concept.getPreferredName(Locale) method (#4749)

(cherry picked from commit bc725f242)

14 of 16 new or added lines in 1 file covered. (87.5%)

12 existing lines in 9 files now uncovered.

21692 of 34022 relevant lines covered (63.76%)

0.64 hits per line

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

85.33
/api/src/main/java/org/openmrs/PatientIdentifier.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.io.Serializable;
13
import java.lang.reflect.InvocationTargetException;
14
import java.lang.reflect.Method;
15
import java.util.Comparator;
16

17
import org.codehaus.jackson.annotate.JsonIgnore;
18
import org.hibernate.search.annotations.Analyzer;
19
import org.hibernate.search.annotations.Boost;
20
import org.hibernate.search.annotations.DocumentId;
21
import org.hibernate.search.annotations.Field;
22
import org.hibernate.search.annotations.Fields;
23
import org.hibernate.search.annotations.Indexed;
24
import org.hibernate.search.annotations.IndexedEmbedded;
25
import org.hibernate.search.annotations.SortableField;
26
import org.openmrs.api.db.hibernate.search.LuceneAnalyzers;
27
import org.openmrs.util.OpenmrsUtil;
28
import org.slf4j.Logger;
29
import org.slf4j.LoggerFactory;
30

31
/**
32
 * A <code>Patient</code> can have zero to n identifying PatientIdentifier(s). PatientIdentifiers
33
 * are anything from medical record numbers, to social security numbers, to driver's licenses. The
34
 * type of identifier is defined by the PatientIdentifierType. A PatientIdentifier also contains a
35
 * Location.
36
 *
37
 * @see org.openmrs.PatientIdentifierType
38
 */
39
@Indexed
40
public class PatientIdentifier extends BaseChangeableOpenmrsData implements java.io.Serializable, Cloneable, Comparable<PatientIdentifier> {
41
        
42
        public static final long serialVersionUID = 1123121L;
43
        
44
        private static final Logger log = LoggerFactory.getLogger(PatientIdentifier.class);
1✔
45
        
46
        // Fields
47
        
48
        /**
49
         * @since 1.5
50
         */
51
        @DocumentId
52
        private Integer patientIdentifierId;
53

54
        @IndexedEmbedded(includeEmbeddedObjectId = true)
55
        private Patient patient;
56

57
        @Fields({
58
                        @Field(name = "identifierPhrase", analyzer = @Analyzer(definition = LuceneAnalyzers.PHRASE_ANALYZER), boost = @Boost(8f)),
59
                        @Field(name = "identifierExact", analyzer = @Analyzer(definition = LuceneAnalyzers.EXACT_ANALYZER), boost = @Boost(4f)),
60
                        @Field(name = "identifierStart", analyzer = @Analyzer(definition = LuceneAnalyzers.START_ANALYZER), boost = @Boost(2f)),
61
                        @Field(name = "identifierAnywhere", analyzer = @Analyzer(definition = LuceneAnalyzers.ANYWHERE_ANALYZER))
62
        })
63
        @SortableField(forField = "identifierExact")
64
        private String identifier;
65

66
        @IndexedEmbedded(includeEmbeddedObjectId = true)
67
        private PatientIdentifierType identifierType;
68
        
69
        private Location location;
70

71
        @Field
1✔
72
        private Boolean preferred = false;
1✔
73
        
74
        /** default constructor */
75
        public PatientIdentifier() {
1✔
76
        }
1✔
77
        
78
        /**
79
         * Convenience constructor for creating a basic identifier
80
         *
81
         * @param identifier String identifier
82
         * @param type PatientIdentifierType
83
         * @param location Location of the identifier
84
         */
85
        public PatientIdentifier(String identifier, PatientIdentifierType type, Location location) {
1✔
86
                this.identifier = identifier;
1✔
87
                this.identifierType = type;
1✔
88
                this.location = location;
1✔
89
        }
1✔
90
        
91
        /**
92
         * Compares this PatientIdentifier object to the given otherIdentifier. This method differs from
93
         * {@link #equals(Object)} in that this method compares the inner fields of each identifier for
94
         * equality. Note: Null/empty fields on <code>otherIdentifier</code> /will not/ cause a false
95
         * value to be returned
96
         *
97
         * @param otherIdentifier PatientiIdentifier with which to compare
98
         * @return boolean true/false whether or not they are the same names
99
         */
100
        public boolean equalsContent(PatientIdentifier otherIdentifier) {
101
                boolean returnValue = true;
1✔
102
                
103
                // these are the methods to compare.
104
                String[] methods = { "getIdentifier", "getIdentifierType", "getLocation" };
1✔
105
                
106
                Class<? extends PatientIdentifier> identifierClass = this.getClass();
1✔
107
                
108
                // loop over all of the selected methods and compare this and other
109
                for (String methodName : methods) {
1✔
110
                        try {
111
                                Method method = identifierClass.getMethod(methodName);
1✔
112
                                
113
                                Object thisValue = method.invoke(this);
1✔
114
                                Object otherValue = method.invoke(otherIdentifier);
1✔
115
                                
116
                                if (otherValue != null) {
1✔
117
                                        returnValue &= otherValue.equals(thisValue);
1✔
118
                                }
119
                                
120
                        }
121
                        catch (NoSuchMethodException e) {
×
122
                                log.warn("No such method for comparison " + methodName, e);
×
123
                        }
124
                        catch (IllegalAccessException | InvocationTargetException e) {
×
125
                                log.error("Error while comparing identifiers", e);
×
126
                        }
1✔
127

128
                }
129
                
130
                return returnValue;
1✔
131
        }
132
        
133
        //property accessors
134
        
135
        /**
136
         * @return Returns the identifier.
137
         */
138
        public String getIdentifier() {
139
                return identifier;
1✔
140
        }
141
        
142
        /**
143
         * @param identifier The identifier to set.
144
         */
145
        public void setIdentifier(String identifier) {
146
                this.identifier = identifier;
1✔
147
        }
1✔
148
        
149
        /**
150
         * @return Returns the identifierType.
151
         */
152
        public PatientIdentifierType getIdentifierType() {
153
                return identifierType;
1✔
154
        }
155
        
156
        /**
157
         * @param identifierType The identifierType to set.
158
         */
159
        public void setIdentifierType(PatientIdentifierType identifierType) {
160
                this.identifierType = identifierType;
1✔
161
        }
1✔
162
        
163
        /**
164
         * @return Returns the location.
165
         */
166
        public Location getLocation() {
167
                return location;
1✔
168
        }
169
        
170
        /**
171
         * @param location The location to set.
172
         */
173
        public void setLocation(Location location) {
174
                this.location = location;
1✔
175
        }
1✔
176
        
177
        /**
178
         * @return Returns the patient.
179
         */
180
        public Patient getPatient() {
181
                return patient;
1✔
182
        }
183
        
184
        /**
185
         * @param patient The patient to set.
186
         */
187
        public void setPatient(Patient patient) {
188
                this.patient = patient;
1✔
189
        }
1✔
190
        
191
        @Override
192
        public String toString() {
193
                return this.identifier;
1✔
194
        }
195
        
196
        /**
197
         * @return Returns the preferred.
198
         */
199
        public Boolean getPreferred() {
200
                return preferred;
1✔
201
        }
202
        
203
        /**
204
         * @param preferred The preferred to set.
205
         */
206
        public void setPreferred(Boolean preferred) {
207
                this.preferred = preferred;
1✔
208
        }
1✔
209
        
210
        /**
211
         * @return the preferred status
212
         * 
213
         * @deprecated as of 2.0, use {@link #getPreferred()}
214
         */
215
        @Deprecated
216
        @JsonIgnore
217
        public Boolean isPreferred() {
UNCOV
218
                return getPreferred();
×
219
        }
220
        
221
        /**
222
         * @see java.lang.Comparable#compareTo(java.lang.Object)
223
         * @deprecated since 1.12. Use DefaultComparator instead.
224
         * Note: this comparator imposes orderings that are inconsistent with equals.
225
         */
226
        @Deprecated
227
        @Override
228
        @SuppressWarnings("squid:S1210")
229
        public int compareTo(PatientIdentifier other) {
230
                DefaultComparator piDefaultComparator = new DefaultComparator();
1✔
231
                return piDefaultComparator.compare(this, other);
1✔
232
        }
233
        
234
        /**
235
         * @since 1.5
236
         * @see org.openmrs.OpenmrsObject#getId()
237
         */
238
        @Override
239
        public Integer getId() {
240
                return getPatientIdentifierId();
1✔
241
        }
242
        
243
        /**
244
         * @since 1.5
245
         * @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
246
         */
247
        @Override
248
        public void setId(Integer id) {
249
                setPatientIdentifierId(id);
×
250
        }
×
251
        
252
        /**
253
         * @since 1.5
254
         * @return the patientIdentifierId
255
         */
256
        public Integer getPatientIdentifierId() {
257
                return patientIdentifierId;
1✔
258
        }
259
        
260
        /**
261
         * @since 1.5
262
         * @param patientIdentifierId the patientIdentifierId to set
263
         */
264
        public void setPatientIdentifierId(Integer patientIdentifierId) {
265
                this.patientIdentifierId = patientIdentifierId;
1✔
266
        }
1✔
267

268
        /**
269
         * bitwise copy of the PatientIdentifier object. NOTICE: THIS WILL NOT COPY THE PATIENT OBJECT. The
270
         * PatientIdentifier.patient object in this object AND the cloned object will point at the same
271
         * patient
272
         *
273
         * @return New PatientIdentifier object
274
         * @since 2.2.0
275
         */
276
        @Override
277
        public Object clone() {
278
                try {
279
                        return super.clone();
1✔
280
                }
281
                catch (CloneNotSupportedException e) {
×
282
                        throw new InternalError("PatientIdentifier should be cloneable");
×
283
                }
284
        }
285
        
286
        /**
287
         Provides a default comparator.
288
         @since 1.12
289
         **/
290
        public static class DefaultComparator implements Comparator<PatientIdentifier>, Serializable {
1✔
291

292
                private static final long serialVersionUID = 1L;
293
                
294
                @Override
295
                public int compare(PatientIdentifier pi1, PatientIdentifier pi2) {
296
                        int retValue = 0;
1✔
297
                        if (pi2 != null) {
1✔
298
                                retValue = pi1.getVoided().compareTo(pi2.getVoided());
1✔
299
                                if (retValue == 0) {
1✔
300
                                        retValue = pi1.getPreferred().compareTo(pi2.getPreferred());
1✔
301
                                }
302
                                if (retValue == 0) {
1✔
303
                                        retValue = OpenmrsUtil.compareWithNullAsLatest(pi1.getDateCreated(), pi2.getDateCreated());
1✔
304
                                }
305
                                if (pi1.getIdentifierType() == null && pi2.getIdentifierType() == null) {
1✔
306
                                        return 0;
1✔
307
                                }
308
                                if (pi1.getIdentifierType() == null && pi2.getIdentifierType() != null) {
1✔
309
                                        retValue = 1;
×
310
                                }
311
                                if (pi1.getIdentifierType() == null && pi2.getIdentifierType() != null) {
1✔
312
                                        retValue = -1;
×
313
                                }
314
                                if (retValue == 0) {
1✔
315
                                        retValue = OpenmrsUtil.compareWithNullAsGreatest(pi1.getIdentifierType().getPatientIdentifierTypeId(),
1✔
316
                                            pi2.getIdentifierType().getPatientIdentifierTypeId());
1✔
317
                                }
318
                                if (retValue == 0) {
1✔
319
                                        retValue = OpenmrsUtil.compareWithNullAsGreatest(pi1.getIdentifier(), pi2.getIdentifier());
1✔
320
                                }
321
                                
322
                                // if we've gotten this far, just check all identifier values.  If they are
323
                                // equal, leave the objects at 0.  If not, arbitrarily pick retValue=1
324
                                // and return that (they are not equal).
325
                                if (retValue == 0 && !pi1.equalsContent(pi2)) {
1✔
326
                                        retValue = 1;
1✔
327
                                }
328
                        }
329
                        
330
                        return retValue;
1✔
331
                }
332
        }
333
}
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