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

openmrs / openmrs-core / 42639030090

09 Nov 2026 02:53PM UTC coverage: 68.932% (-0.04%) from 68.971%
42639030090

push

github

web-flow
Revert "TRUNK-6848: Reject invalid dates and times in Obs.setValueAsString for DT and TM datatypes (#6732)" (#6817)

This reverts commit 50de2713a.

Reverting while the behavior changes are reviewed. Strict DT parsing rejects
"yyyy-MM-dd HH:mm[:ss]" values that existing clients send for DT obs (e.g.
labtrackingapp), and the ISO fallback now stores a time component on DT obs
instead of truncating to the date. See TRUNK-6848 for details.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

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

5 existing lines in 2 files now uncovered.

26408 of 38310 relevant lines covered (68.93%)

0.69 hits per line

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

83.61
/api/src/main/java/org/openmrs/Obs.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.text.DateFormat;
13
import java.text.DecimalFormat;
14
import java.text.NumberFormat;
15
import java.text.ParseException;
16
import java.text.SimpleDateFormat;
17
import java.util.Date;
18
import java.util.HashSet;
19
import java.util.LinkedHashSet;
20
import java.util.Locale;
21
import java.util.Set;
22

23
import org.apache.commons.lang3.StringUtils;
24
import org.hibernate.envers.Audited;
25
import org.openmrs.annotation.AllowDirectAccess;
26
import org.openmrs.api.APIException;
27
import org.openmrs.api.context.Context;
28
import org.openmrs.api.db.hibernate.HibernateUtil;
29
import org.openmrs.obs.ComplexData;
30
import org.openmrs.obs.ComplexObsHandler;
31
import org.openmrs.util.CollectionUtil;
32
import org.openmrs.util.DateUtil;
33
import org.openmrs.util.Format;
34
import org.openmrs.util.Format.FORMAT_TYPE;
35
import org.openmrs.util.OpenmrsUtil;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

39
/**
40
 * An observation is a single unit of clinical information. <br>
41
 * <br>
42
 * Observations are collected and grouped together into one Encounter (one visit). Obs can be
43
 * grouped in a hierarchical fashion. <br>
44
 * <br>
45
 * <p>
46
 * The {@link #getObsGroup()} method returns an optional parent. That parent object is also an Obs.
47
 * The parent Obs object knows about its child objects through the {@link #getGroupMembers()}
48
 * method.
49
 * </p>
50
 * <p>
51
 * (Multi-level hierarchies are achieved by an Obs parent object being a member of another Obs
52
 * (grand)parent object) Read up on the obs table: http://openmrs.org/wiki/Obs_Table_Primer In an
53
 * OpenMRS installation, there may be an occasion need to change an Obs.
54
 * </p>
55
 * <p>
56
 * For example, a site may decide to replace a concept in the dictionary with a more specific set of
57
 * concepts. An observation is part of the official record of an encounter. There may be legal,
58
 * ethical, and auditing consequences from altering a record. It is recommended that you create a
59
 * new Obs and void the old one:
60
 * </p>
61
 * Obs newObs = Obs.newInstance(oldObs); //copies values from oldObs
62
 * newObs.setPreviousVersion(oldObs);
63
 * Context.getObsService().saveObs(newObs,"Your reason for the change here");
64
 * Context.getObsService().voidObs(oldObs, "Your reason for the change here");
65
 * 
66
 * @see Encounter
67
 */
68
@Audited
69
public class Obs extends BaseFormRecordableOpenmrsData {
70
        
71
        /**
72
         * @since 2.1.0
73
         */
74
        public enum Interpretation {
1 ✔
75
                NORMAL, ABNORMAL, CRITICALLY_ABNORMAL, NEGATIVE, POSITIVE, CRITICALLY_LOW, LOW, HIGH, CRITICALLY_HIGH, VERY_SUSCEPTIBLE, SUSCEPTIBLE, INTERMEDIATE, RESISTANT, SIGNIFICANT_CHANGE_DOWN, SIGNIFICANT_CHANGE_UP, OFF_SCALE_LOW, OFF_SCALE_HIGH
1 ✔
76
        }
77
        
78
        /**
79
         * @since 2.1.0
80
         */
81
        public enum Status {
1 ✔
82
                PRELIMINARY, FINAL, AMENDED
1 ✔
83
        }
84
        
85
        private static final String TIME_PATTERN = "HH:mm";
86
        
87
        private static final String DATE_PATTERN = "yyyy-MM-dd";
88
        
89
        public static final long serialVersionUID = 112342333L;
90
        
91
        private static final Logger log = LoggerFactory.getLogger(Obs.class);
1 ✔
92
        
93
        protected Integer obsId;
94
        
95
        protected Concept concept;
96
        
97
        protected Date obsDatetime;
98
        
99
        protected String accessionNumber;
100
        
101
        /**
102
         * The "parent" of this obs. It is the grouping that brings other obs together. note:
103
         * obsGroup.getConcept().isSet() should be true This will be non-null if this obs is a member of
104
         * another groupedObs
105
         * 
106
         * @see #isObsGrouping() (??)
107
         */
108
        protected Obs obsGroup;
109
        
110
        /**
111
         * The list of obs grouped under this obs.
112
         */
113
        @AllowDirectAccess
114
        protected Set<Obs> groupMembers;
115
        
116
        protected Concept valueCoded;
117
        
118
        protected ConceptName valueCodedName;
119
        
120
        protected Drug valueDrug;
121
        
122
        protected Integer valueGroupId;
123
        
124
        protected Date valueDatetime;
125
        
126
        protected Double valueNumeric;
127
        
128
        protected String valueModifier;
129
        
130
        protected String valueText;
131
        
132
        protected String valueComplex;
133
        
134
        // ComplexData is not persisted in the database.
135
        protected transient ComplexData complexData;
136
        
137
        protected String comment;
138
        
139
        protected transient Integer personId;
140
        
141
        protected Person person;
142
        
143
        protected Order order;
144
        
145
        protected Location location;
146
        
147
        protected Encounter encounter;
148
        
149
        private Obs previousVersion;
150
        
151
        private Boolean dirty = Boolean.FALSE;
1 ✔
152
        
153
        private Interpretation interpretation;
154
        
155
        private Status status = Status.FINAL;
1 ✔
156

157
        private ObsReferenceRange referenceRange;
158

159
        /** default constructor */
160
        public Obs() {
1 ✔
161
        }
1 ✔
162
        
163
        /**
164
         * Required parameters constructor A value is also required, but that can be one of: valueCoded,
165
         * valueDrug, valueNumeric, or valueText
166
         * 
167
         * @param person The Person this obs is acting on
168
         * @param question The question concept this obs is related to
169
         * @param obsDatetime The time this obs took place
170
         * @param location The location this obs took place
171
         */
172
        public Obs(Person person, Concept question, Date obsDatetime, Location location) {
1 ✔
173
                this.person = person;
1 ✔
174
                if (person != null) {
1 ✔
175
                        this.personId = person.getPersonId();
1 ✔
176
                }
177
                this.concept = question;
1 ✔
178
                this.obsDatetime = obsDatetime;
1 ✔
179
                this.location = location;
1 ✔
180
        }
1 ✔
181
        
182
        /** constructor with id */
183
        public Obs(Integer obsId) {
1 ✔
184
                this.obsId = obsId;
1 ✔
185
        }
1 ✔
186
        
187
        /**
188
         * This is an equivalent to a copy constructor. Creates a new copy of the given
189
         * <code>obsToCopy</code> with a null obs id
190
         * 
191
         * @param obsToCopy The Obs that is going to be copied
192
         * @return a new Obs object with all the same attributes as the given obs
193
         */
194
        public static Obs newInstance(Obs obsToCopy) {
195
                Obs newObs = new Obs(obsToCopy.getPerson(), obsToCopy.getConcept(), obsToCopy.getObsDatetime(),
1 ✔
196
                        obsToCopy.getLocation());
1 ✔
197
                
198
                newObs.setObsGroup(obsToCopy.getObsGroup());
1 ✔
199
                newObs.setAccessionNumber(obsToCopy.getAccessionNumber());
1 ✔
200
                newObs.setValueCoded(obsToCopy.getValueCoded());
1 ✔
201
                newObs.setValueDrug(obsToCopy.getValueDrug());
1 ✔
202
                newObs.setValueGroupId(obsToCopy.getValueGroupId());
1 ✔
203
                newObs.setValueDatetime(obsToCopy.getValueDatetime());
1 ✔
204
                newObs.setValueNumeric(obsToCopy.getValueNumeric());
1 ✔
205
                newObs.setValueModifier(obsToCopy.getValueModifier());
1 ✔
206
                newObs.setValueText(obsToCopy.getValueText());
1 ✔
207
                newObs.setComment(obsToCopy.getComment());
1 ✔
208
                newObs.setEncounter(obsToCopy.getEncounter());
1 ✔
209
                newObs.setCreator(obsToCopy.getCreator());
1 ✔
210
                newObs.setDateCreated(obsToCopy.getDateCreated());
1 ✔
211
                newObs.setVoided(obsToCopy.getVoided());
1 ✔
212
                newObs.setVoidedBy(obsToCopy.getVoidedBy());
1 ✔
213
                newObs.setDateVoided(obsToCopy.getDateVoided());
1 ✔
214
                newObs.setVoidReason(obsToCopy.getVoidReason());
1 ✔
215
                newObs.setStatus(obsToCopy.getStatus());
1 ✔
216
                newObs.setInterpretation(obsToCopy.getInterpretation());
1 ✔
217
                newObs.setOrder(obsToCopy.getOrder());
1 ✔
218
                
219
                newObs.setValueComplex(obsToCopy.getValueComplex());
1 ✔
220
                newObs.setComplexData(obsToCopy.getComplexData());
1 ✔
221
                newObs.setFormField(obsToCopy.getFormFieldNamespace(), obsToCopy.getFormFieldPath());
1 ✔
222
                
223
                if (obsToCopy.getReferenceRange() != null) {
1 ✔
224
                        ObsReferenceRange newRange = new ObsReferenceRange();
1 ✔
225
                        ObsReferenceRange srcRange = obsToCopy.getReferenceRange();
1 ✔
226
                        newRange.setHiAbsolute(srcRange.getHiAbsolute());
1 ✔
227
                        newRange.setHiCritical(srcRange.getHiCritical());
1 ✔
228
                        newRange.setHiNormal(srcRange.getHiNormal());
1 ✔
229
                        newRange.setLowAbsolute(srcRange.getLowAbsolute());
1 ✔
230
                        newRange.setLowCritical(srcRange.getLowCritical());
1 ✔
231
                        newRange.setLowNormal(srcRange.getLowNormal());
1 ✔
232
                        newRange.setObs(newObs);
1 ✔
233
                        newObs.setReferenceRange(newRange);
1 ✔
234
                }
235
                
236
                // Copy list of all members, including voided, and put them in respective groups
237
                if (obsToCopy.hasGroupMembers(true)) {
1 ✔
238
                        for (Obs member : obsToCopy.getGroupMembers(true)) {
1 ✔
239
                                // if the obs hasn't been saved yet, no need to duplicate it
240
                                if (member.getObsId() == null) {
1 ✔
241
                                        newObs.addGroupMember(member);
1 ✔
242
                                } else {
243
                                        Obs newMember = Obs.newInstance(member);
1 ✔
244
                                        newMember.setPreviousVersion(member);
1 ✔
245
                                        newObs.addGroupMember(newMember);
1 ✔
246
                                }
247
                        }
1 ✔
248
                }
249
                
250
                return newObs;
1 ✔
251
        }
252
        
253
        // Property accessors
254
        
255
        /**
256
         * @return Returns the comment.
257
         */
258
        public String getComment() {
259
                return comment;
1 ✔
260
        }
261
        
262
        /**
263
         * @param comment The comment to set.
264
         */
265
        public void setComment(String comment) {
266
                markAsDirty(this.comment, comment);
1 ✔
267
                this.comment = comment;
1 ✔
268
        }
1 ✔
269
        
270
        /**
271
         * @return Returns the concept.
272
         */
273
        public Concept getConcept() {
274
                return concept;
1 ✔
275
        }
276
        
277
        /**
278
         * @param concept The concept to set.
279
         */
280
        public void setConcept(Concept concept) {
281
                markAsDirty(this.concept, concept);
1 ✔
282
                this.concept = concept;
1 ✔
283
        }
1 ✔
284
        
285
        /**
286
         * Get the concept description that is tied to the concept name that was used when making this
287
         * observation
288
         * 
289
         * @return ConceptDescription the description used
290
         */
291
        public ConceptDescription getConceptDescription() {
292
                // if we don't have a question for this concept,
293
                // then don't bother looking for a description
294
                if (getConcept() == null) {
×
295
                        return null;
×
296
                }
297
                
298
                // ABKTOD: description in which locale?
299
                return concept.getDescription();
×
300
        }
301
        
302
        /**
303
         * @return Returns the encounter.
304
         */
305
        public Encounter getEncounter() {
306
                return encounter;
1 ✔
307
        }
308
        
309
        /**
310
         * @param encounter The encounter to set.
311
         */
312
        public void setEncounter(Encounter encounter) {
313
                markAsDirty(this.encounter, encounter);
1 ✔
314
                this.encounter = encounter;
1 ✔
315
        }
1 ✔
316
        
317
        /**
318
         * @return Returns the location.
319
         */
320
        public Location getLocation() {
321
                return location;
1 ✔
322
        }
323
        
324
        /**
325
         * @param location The location to set.
326
         */
327
        public void setLocation(Location location) {
328
                markAsDirty(this.location, location);
1 ✔
329
                this.location = location;
1 ✔
330
        }
1 ✔
331
        
332
        /**
333
         * @return Returns the obsDatetime.
334
         */
335
        public Date getObsDatetime() {
336
                return obsDatetime;
1 ✔
337
        }
338
        
339
        /**
340
         * @param obsDatetime The obsDatetime to set.
341
         */
342
        public void setObsDatetime(Date obsDatetime) {
343
                markAsDirty(this.obsDatetime, obsDatetime);
1 ✔
344
                this.obsDatetime = obsDatetime;
1 ✔
345
        }
1 ✔
346
        
347
        /**
348
         * An obs grouping occurs when the question (#getConcept()) is a set. (@link
349
         * org.openmrs.Concept#isSet()) If this is non-null, it means the current Obs is in the list
350
         * returned by <code>obsGroup</code>.{@link #getGroupMembers()}
351
         * 
352
         * @return the Obs that is the grouping factor
353
         */
354
        public Obs getObsGroup() {
355
                return obsGroup;
1 ✔
356
        }
357
        
358
        /**
359
         * This method does NOT add this current obs to the list of obs in obsGroup.getGroupMembers().
360
         * That must be done (and should be done) manually. (I am not doing it here for fear of screwing
361
         * up the normal loading and creation of this object via hibernate/spring)
362
         * 
363
         * @param obsGroup the obsGroup to set
364
         */
365
        public void setObsGroup(Obs obsGroup) {
366
                markAsDirty(this.obsGroup, obsGroup);
1 ✔
367
                this.obsGroup = obsGroup;
1 ✔
368
        }
1 ✔
369
        
370
        /**
371
         * Convenience method that checks for if this obs has 1 or more group members (either voided or
372
         * non-voided) Note this method differs from hasGroupMembers(), as that method excludes voided
373
         * obs; logic is that while a obs that has only voided group members should be seen as
374
         * "having no group members" it still should be considered an "obs grouping"
375
         * <p>
376
         * NOTE: This method could also be called "isObsGroup" for a little less confusion on names.
377
         * However, jstl in a web layer (or any psuedo-getter) access isn't good with both an
378
         * "isObsGroup" method and a "getObsGroup" method. Which one should be returned with a
379
         * simplified jstl call like ${obs.obsGroup} ? With this setup, ${obs.obsGrouping} returns a
380
         * boolean of whether this obs is a parent and has members. ${obs.obsGroup} returns the parent
381
         * object to this obs if this obs is a group member of some other group.
382
         * 
383
         * @return true if this is the parent group of other obs
384
         */
385
        public boolean isObsGrouping() {
386
                return hasGroupMembers(true);
1 ✔
387
        }
388
        
389
        /**
390
         * A convenience method to check for nullity and length to determine if this obs has group
391
         * members. By default, this ignores voided-objects. To include voided, use
392
         * {@link #hasGroupMembers(boolean)} with value true.
393
         * 
394
         * @return true if this is the parent group of other obs
395
         * <strong>Should</strong> not include voided obs
396
         */
397
        public boolean hasGroupMembers() {
398
                return hasGroupMembers(false);
1 ✔
399
        }
400
        
401
        /**
402
         * Convenience method that checks for nullity and length to determine if this obs has group
403
         * members. The parameter specifies if this method whether or not voided obs should be
404
         * considered.
405
         * 
406
         * @param includeVoided determines if Voided members should be considered as group members.
407
         * @return true if this is the parent group of other Obs
408
         * <strong>Should</strong> return true if this obs has group members based on parameter
409
         */
410
        public boolean hasGroupMembers(boolean includeVoided) {
411
                // ! symbol used because if it's not empty, we want true
412
                return !org.springframework.util.CollectionUtils.isEmpty(getGroupMembers(includeVoided));
1 ✔
413
        }
414
        
415
        /**
416
         * Get the non-voided members of the obs group, if this obs is a group. By default this method
417
         * only returns non-voided group members. To get all group members, use
418
         * {@link #getGroupMembers(boolean)} with value true.
419
         * <p>
420
         * If it's not a group (i.e. {@link #getConcept()}.{@link org.openmrs.Concept#getSet()} is not
421
         * true, then this returns null.
422
         * 
423
         * @return a Set&lt;Obs&gt; of the members of this group.
424
         * @see #addGroupMember(Obs)
425
         * @see #hasGroupMembers()
426
         */
427
        public Set<Obs> getGroupMembers() {
428
                //same as just returning groupMembers
429
                return getGroupMembers(false);
1 ✔
430
        }
431
        
432
        /**
433
         * Get the group members of this obs group, if this obs is a group. This method will either
434
         * return all group members, or only non-voided group members, depending on if the argument is
435
         * set to be true or false respectively.
436
         * 
437
         * @param includeVoided
438
         * @return the set of group members in this obs group
439
         * <strong>Should</strong> Get all group members if passed true, and non-voided if passed false
440
         */
441
        public Set<Obs> getGroupMembers(boolean includeVoided) {
442
                if (includeVoided) {
1 ✔
443
                        //just return all group members
444
                        return groupMembers;
1 ✔
445
                }
446
                if (groupMembers == null) {
1 ✔
447
                        //Empty set so return null
448
                        return null;
1 ✔
449
                }
450
                Set<Obs> nonVoided = new LinkedHashSet<>(groupMembers);
1 ✔
451
                nonVoided.removeIf(BaseOpenmrsData::getVoided);
1 ✔
452
                return nonVoided;
1 ✔
453
        }
454
        
455
        /**
456
         * Set the members of the obs group, if this obs is a group.
457
         * <p>
458
         * If it's not a group (i.e. {@link #getConcept()}.{@link org.openmrs.Concept#getSet()} is not
459
         * true, then this returns null.
460
         * 
461
         * @param groupMembers the groupedObs to set
462
         * @see #addGroupMember(Obs)
463
         * @see #hasGroupMembers()
464
         * <strong>Should</strong> mark the obs as dirty when the set is changed from null to a non empty one
465
         * <strong>Should</strong> not mark the obs as dirty when the set is changed from null to an empty one
466
         * <strong>Should</strong> mark the obs as dirty when the set is replaced with another with different members
467
         * <strong>Should</strong> not mark the obs as dirty when the set is replaced with another with same members
468
         */
469
        public void setGroupMembers(Set<Obs> groupMembers) {
470
                //Copy over the entire list
471
                this.groupMembers = CollectionUtil.setCollection(this.groupMembers, groupMembers);
1 ✔
472
                
473
        }
1 ✔
474
        
475
        /**
476
         * Convenience method to add the given <code>obs</code> to this grouping. Will implicitly make
477
         * this obs an ObsGroup.
478
         * 
479
         * @param member Obs to add to this group
480
         * @see #setGroupMembers(Set)
481
         * @see #getGroupMembers()
482
         * <strong>Should</strong> return true when a new obs is added as a member
483
         * <strong>Should</strong> return false when a duplicate obs is added as a member
484
         */
485
        public void addGroupMember(Obs member) {
486
                if (member == null) {
1 ✔
487
                        return;
×
488
                }
489
                
490
                if (getGroupMembers() == null) {
1 ✔
491
                        groupMembers = new HashSet<>();
1 ✔
492
                }
493
                
494
                // a quick sanity check to make sure someone isn't adding
495
                // itself to the group
496
                if (member.equals(this)) {
1 ✔
497
                        throw new APIException("Obs.error.groupCannotHaveItselfAsAMentor", new Object[] { this, member });
1 ✔
498
                }
499
                
500
                member.setObsGroup(this);
1 ✔
501
                groupMembers.add(member);
1 ✔
502
        }
1 ✔
503
        
504
        /**
505
         * Convenience method to remove an Obs from this grouping This also removes the link in the
506
         * given <code>obs</code>object to this obs grouper
507
         * 
508
         * @param member Obs to remove from this group
509
         * @see #setGroupMembers(Set)
510
         * @see #getGroupMembers()
511
         * <strong>Should</strong> return true when an obs is removed
512
         * <strong>Should</strong> return false when a non existent obs is removed
513
         */
514
        public void removeGroupMember(Obs member) {
515
                if (member == null || getGroupMembers() == null) {
1 ✔
516
                        return;
1 ✔
517
                }
518
                
519
                if (groupMembers.remove(member)) {
1 ✔
520
                        member.setObsGroup(null);
1 ✔
521
                }
522
        }
1 ✔
523
        
524
        /**
525
         * Convenience method that returns related Obs If the Obs argument is not an ObsGroup: a
526
         * Set&lt;Obs&gt; will be returned containing all of the children of this Obs' parent that are
527
         * not ObsGroups themselves. This will include this Obs by default, unless getObsGroup() returns
528
         * null, in which case an empty set is returned. If the Obs argument is an ObsGroup: a
529
         * Set&lt;Obs&gt; will be returned containing 1. all of this Obs' group members, and 2. all
530
         * ancestor Obs that are not themselves obsGroups.
531
         * 
532
         * @return Set&lt;Obs&gt;
533
         */
534
        public Set<Obs> getRelatedObservations() {
535
                Set<Obs> ret = new HashSet<>();
1 ✔
536
                if (this.isObsGrouping()) {
1 ✔
537
                        ret.addAll(this.getGroupMembers());
1 ✔
538
                        Obs parentObs = this;
1 ✔
539
                        while (parentObs.getObsGroup() != null) {
1 ✔
540
                                for (Obs obsSibling : parentObs.getObsGroup().getGroupMembers()) {
1 ✔
541
                                        if (!obsSibling.isObsGrouping()) {
1 ✔
542
                                                ret.add(obsSibling);
1 ✔
543
                                        }
544
                                }
1 ✔
545
                                parentObs = parentObs.getObsGroup();
1 ✔
546
                        }
547
                } else if (this.getObsGroup() != null) {
1 ✔
548
                        for (Obs obsSibling : this.getObsGroup().getGroupMembers()) {
1 ✔
549
                                if (!obsSibling.isObsGrouping()) {
1 ✔
550
                                        ret.add(obsSibling);
1 ✔
551
                                }
552
                        }
1 ✔
553
                }
554
                return ret;
1 ✔
555
        }
556
        
557
        /**
558
         * @return Returns the obsId.
559
         */
560
        public Integer getObsId() {
561
                return obsId;
1 ✔
562
        }
563
        
564
        /**
565
         * @param obsId The obsId to set.
566
         */
567
        public void setObsId(Integer obsId) {
568
                this.obsId = obsId;
1 ✔
569
        }
1 ✔
570
        
571
        /**
572
         * @return Returns the order.
573
         */
574
        public Order getOrder() {
575
                return order;
1 ✔
576
        }
577
        
578
        /**
579
         * @param order The order to set.
580
         */
581
        public void setOrder(Order order) {
582
                markAsDirty(this.order, order);
1 ✔
583
                this.order = order;
1 ✔
584
        }
1 ✔
585
        
586
        /**
587
         * The person id of the person on this object. This should be the same as
588
         * <code>{@link #getPerson()}.getPersonId()</code>. It is duplicated here for speed and
589
         * simplicity reasons
590
         * 
591
         * @return the integer person id of the person this obs is acting on
592
         */
593
        public Integer getPersonId() {
594
                return personId;
1 ✔
595
        }
596
        
597
        /**
598
         * Set the person id on this obs object. This method is here for convenience, but really the
599
         * {@link #setPerson(Person)} method should be used like
600
         * <code>setPerson(new Person(personId))</code>
601
         * 
602
         * @see #setPerson(Person)
603
         * @param personId
604
         */
605
        protected void setPersonId(Integer personId) {
606
                markAsDirty(this.personId, personId);
1 ✔
607
                this.personId = personId;
1 ✔
608
        }
1 ✔
609
        
610
        /**
611
         * Get the person object that this obs is acting on.
612
         * 
613
         * @see #getPersonId()
614
         * @return the person object
615
         */
616
        public Person getPerson() {
617
                return person;
1 ✔
618
        }
619
        
620
        /**
621
         * Set the person object to this obs object. This will also set the personId on this obs object
622
         * 
623
         * @see #setPersonId(Integer)
624
         * @param person the Patient/Person object that this obs is acting on
625
         */
626
        public void setPerson(Person person) {
627
                markAsDirty(this.person, person);
1 ✔
628
                this.person = person;
1 ✔
629
                if (person != null) {
1 ✔
630
                        setPersonId(person.getPersonId());
1 ✔
631
                }
632
        }
1 ✔
633
        
634
        /**
635
         * Sets the value of this obs to the specified valueBoolean if this obs has a boolean concept.
636
         * 
637
         * @param valueBoolean the boolean value matching the boolean coded concept to set to
638
         */
639
        public void setValueBoolean(Boolean valueBoolean) {
640
                if (getConcept() != null && getConcept().getDatatype() != null && getConcept().getDatatype().isBoolean()) {
1 ✔
641
                        if (valueBoolean != null) {
1 ✔
642
                                setValueCoded(valueBoolean ? Context.getConceptService().getTrueConcept() : Context.getConceptService()
1 ✔
643
                                        .getFalseConcept());
1 ✔
644
                        } else {
645
                                setValueCoded(null);
×
646
                        }
647
                }
648
        }
1 ✔
649
        
650
        /**
651
         * Coerces a value to a Boolean representation
652
         * 
653
         * @return Boolean representation of the obs value
654
         * <strong>Should</strong> return true for value_numeric concepts if value is 1
655
         * <strong>Should</strong> return false for value_numeric concepts if value is 0
656
         * <strong>Should</strong> return null for value_numeric concepts if value is neither 1 nor 0
657
         */
658
        public Boolean getValueAsBoolean() {
659
                
660
                if (getValueCoded() != null) {
1 ✔
661
                        if (getValueCoded().equals(Context.getConceptService().getTrueConcept())) {
×
662
                                return Boolean.TRUE;
×
663
                        } else if (getValueCoded().equals(Context.getConceptService().getFalseConcept())) {
×
664
                                return Boolean.FALSE;
×
665
                        }
666
                } else if (getValueNumeric() != null) {
1 ✔
667
                        if (getValueNumeric() == 1) {
1 ✔
668
                                return Boolean.TRUE;
1 ✔
669
                        } else if (getValueNumeric() == 0) {
1 ✔
670
                                return Boolean.FALSE;
1 ✔
671
                        }
672
                }
673
                //returning null is preferred to defaulting to false to support validation of user input is from a form
674
                return null;
1 ✔
675
        }
676
        
677
        /**
678
         * Returns the boolean value if the concept of this obs is of boolean datatype
679
         * 
680
         * @return true or false if value is set otherwise null
681
         * <strong>Should</strong> return true if value coded answer concept is true concept
682
         * <strong>Should</strong> return false if value coded answer concept is false concept
683
         */
684
        public Boolean getValueBoolean() {
685
                if (getConcept() != null && valueCoded != null && getConcept().getDatatype().isBoolean()) {
1 ✔
686
                        Concept trueConcept = Context.getConceptService().getTrueConcept();
1 ✔
687
                        return trueConcept != null && valueCoded.getId().equals(trueConcept.getId());
1 ✔
688
                }
689
                
690
                return null;
1 ✔
691
        }
692
        
693
        /**
694
         * @return Returns the valueCoded.
695
         */
696
        
697
        public Concept getValueCoded() {
698
                return valueCoded;
1 ✔
699
        }
700
        
701
        /**
702
         * @param valueCoded The valueCoded to set.
703
         */
704
        public void setValueCoded(Concept valueCoded) {
705
                markAsDirty(this.valueCoded, valueCoded);
1 ✔
706
                this.valueCoded = valueCoded;
1 ✔
707
        }
1 ✔
708
        
709
        /**
710
         * Gets the specific name used for the coded value.
711
         * 
712
         * @return the name of the coded value
713
         */
714
        public ConceptName getValueCodedName() {
715
                return valueCodedName;
1 ✔
716
        }
717
        
718
        /**
719
         * Sets the specific name used for the coded value.
720
         * 
721
         * @param valueCodedName the name of the coded value
722
         */
723
        public void setValueCodedName(ConceptName valueCodedName) {
724
                markAsDirty(this.valueCodedName, valueCodedName);
1 ✔
725
                this.valueCodedName = valueCodedName;
1 ✔
726
        }
1 ✔
727
        
728
        /**
729
         * @return Returns the valueDrug
730
         */
731
        public Drug getValueDrug() {
732
                return valueDrug;
1 ✔
733
        }
734
        
735
        /**
736
         * @param valueDrug The valueDrug to set.
737
         */
738
        public void setValueDrug(Drug valueDrug) {
739
                markAsDirty(this.valueDrug, valueDrug);
1 ✔
740
                this.valueDrug = valueDrug;
1 ✔
741
        }
1 ✔
742
        
743
        /**
744
         * @return Returns the valueDatetime.
745
         */
746
        public Date getValueDatetime() {
747
                return valueDatetime;
1 ✔
748
        }
749
        
750
        /**
751
         * @param valueDatetime The valueDatetime to set.
752
         */
753
        public void setValueDatetime(Date valueDatetime) {
754
                markAsDirty(this.valueDatetime, valueDatetime);
1 ✔
755
                this.valueDatetime = valueDatetime;
1 ✔
756
        }
1 ✔
757
        
758
        /**
759
         * @return the value of this obs as a Date. Note that this uses a java.util.Date, so it includes
760
         *         a time component, that should be ignored.
761
         * @since 1.9
762
         */
763
        public Date getValueDate() {
764
                return valueDatetime;
1 ✔
765
        }
766
        
767
        /**
768
         * @param valueDate The date value to set.
769
         * @since 1.9
770
         */
771
        public void setValueDate(Date valueDate) {
772
                markAsDirty(this.valueDatetime, valueDate);
1 ✔
773
                this.valueDatetime = valueDate;
1 ✔
774
        }
1 ✔
775
        
776
        /**
777
         * @return the time value of this obs. Note that this uses a java.util.Date, so it includes a
778
         *         date component, that should be ignored.
779
         * @since 1.9
780
         */
781
        public Date getValueTime() {
782
                return valueDatetime;
1 ✔
783
        }
784
        
785
        /**
786
         * @param valueTime the time value to set
787
         * @since 1.9
788
         */
789
        public void setValueTime(Date valueTime) {
790
                markAsDirty(this.valueDatetime, valueTime);
1 ✔
791
                this.valueDatetime = valueTime;
1 ✔
792
        }
1 ✔
793
        
794
        /**
795
         * @return Returns the valueGroupId.
796
         */
797
        public Integer getValueGroupId() {
798
                return valueGroupId;
1 ✔
799
        }
800
        
801
        /**
802
         * @param valueGroupId The valueGroupId to set.
803
         */
804
        public void setValueGroupId(Integer valueGroupId) {
805
                markAsDirty(this.valueGroupId, valueGroupId);
1 ✔
806
                this.valueGroupId = valueGroupId;
1 ✔
807
        }
1 ✔
808
        
809
        /**
810
         * @return Returns the valueModifier.
811
         */
812
        public String getValueModifier() {
813
                return valueModifier;
1 ✔
814
        }
815
        
816
        /**
817
         * @param valueModifier The valueModifier to set.
818
         */
819
        public void setValueModifier(String valueModifier) {
820
                markAsDirty(this.valueModifier, valueModifier);
1 ✔
821
                this.valueModifier = valueModifier;
1 ✔
822
        }
1 ✔
823
        
824
        /**
825
         * @return Returns the valueNumeric.
826
         */
827
        public Double getValueNumeric() {
828
                return valueNumeric;
1 ✔
829
        }
830
        
831
        /**
832
         * @param valueNumeric The valueNumeric to set.
833
         */
834
        public void setValueNumeric(Double valueNumeric) {
835
                markAsDirty(this.valueNumeric, valueNumeric);
1 ✔
836
                this.valueNumeric = valueNumeric;
1 ✔
837
        }
1 ✔
838
        
839
        /**
840
         * @return Returns the valueText.
841
         */
842
        public String getValueText() {
843
                return valueText;
1 ✔
844
        }
845
        
846
        /**
847
         * @param valueText The valueText to set.
848
         */
849
        public void setValueText(String valueText) {
850
                markAsDirty(this.valueText, valueText);
1 ✔
851
                this.valueText = valueText;
1 ✔
852
        }
1 ✔
853
        
854
        /**
855
         * @return Returns true if this Obs is complex.
856
         * @since 1.5
857
         * <strong>Should</strong> return true if the concept is complex
858
         */
859
        public boolean isComplex() {
860
                if (getConcept() != null) {
1 ✔
861
                        return getConcept().isComplex();
1 ✔
862
                }
863
                
864
                return false;
1 ✔
865
        }
866
        
867
        /**
868
         * Get the value for the ComplexData. This method is used by the ComplexObsHandler. The
869
         * valueComplex has two parts separated by a bar '|' character: part A) the title; and part B)
870
         * the URI. The title is the readable description of the valueComplex that is returned by
871
         * {@link Obs#getValueAsString(java.util.Locale)}. The URI is the location where the ComplexData is stored.
872
         * 
873
         * @return readable title and URI for the location of the ComplexData binary object.
874
         * @since 1.5
875
         */
876
        public String getValueComplex() {
877
                return this.valueComplex;
1 ✔
878
        }
879
        
880
        /**
881
         * Set the value for the ComplexData. This method is used by the ComplexObsHandler. The
882
         * valueComplex has two parts separated by a bar '|' character: part A) the title; and part B)
883
         * the URI. The title is the readable description of the valueComplex that is returned by
884
         * {@link Obs#getValueAsString(java.util.Locale)}. The URI is the location where the ComplexData is stored.
885
         * 
886
         * @param valueComplex readable title and URI for the location of the ComplexData binary object.
887
         * @since 1.5
888
         */
889
        public void setValueComplex(String valueComplex) {
890
                markAsDirty(this.valueComplex, valueComplex);
1 ✔
891
                this.valueComplex = valueComplex;
1 ✔
892
        }
1 ✔
893
        
894
        /**
895
         * Set the ComplexData for this Obs. The ComplexData is stored in the file system or elsewhere,
896
         * but is not persisted to the database. <br>
897
         * <br>
898
         * {@link ComplexObsHandler}s that are registered to {@link ConceptComplex}s will persist the
899
         * {@link ComplexData#getData()} object to the correct place for the given concept.
900
         * 
901
         * @param complexData
902
         * @since 1.5
903
         */
904
        public void setComplexData(ComplexData complexData) {
905
                markAsDirty(this.complexData, complexData);
1 ✔
906
                this.complexData = complexData;
1 ✔
907
        }
1 ✔
908
        
909
        /**
910
         * Get the ComplexData. This is retrieved by the {@link ComplexObsHandler} from the file system
911
         * or another location, not from the database. <br>
912
         * <br>
913
         * This will be null unless you call:
914
         * 
915
         * <pre>
916
         * Obs obsWithComplexData =
917
         * Context.getObsService().getComplexObs(obsId, OpenmrsConstants.RAW_VIEW);
918
         * 
919
         * <pre/>
920
         *
921
         * @return the complex data for this obs (if its a complex obs)
922
         * @since 1.5
923
         */
924
        public ComplexData getComplexData() {
925
                return this.complexData;
1 ✔
926
        }
927
        
928
        /**
929
         * @return Returns the accessionNumber.
930
         */
931
        public String getAccessionNumber() {
932
                return accessionNumber;
1 ✔
933
        }
934
        
935
        /**
936
         * @param accessionNumber The accessionNumber to set.
937
         */
938
        public void setAccessionNumber(String accessionNumber) {
939
                markAsDirty(this.accessionNumber, accessionNumber);
1 ✔
940
                this.accessionNumber = accessionNumber;
1 ✔
941
        }
1 ✔
942
        
943
        /***************************************************************************
944
         * Convenience methods
945
         **************************************************************************/
946
        
947
        /**
948
         * Convenience method for obtaining the observation's value as a string If the Obs is complex,
949
         * returns the title of the complexData denoted by the section of getValueComplex() before the
950
         * first bar '|' character; or returns the entire getValueComplex() if the bar '|' character is
951
         * missing.
952
         *
953
         * @param locale locale for locale-specific depictions of value
954
         * <strong>Should</strong> return first part of valueComplex for complex obs
955
         * <strong>Should</strong> return first part of valueComplex for non null valueComplexes
956
         * <strong>Should</strong> return non precise values for NumericConcepts
957
         * <strong>Should</strong> return date in correct format
958
         * <strong>Should</strong> not return long decimal numbers as scientific notation
959
         * <strong>Should</strong> use commas or decimal places depending on locale
960
         * <strong>Should</strong> not use thousand separator
961
         * <strong>Should</strong> return regular number for size of zero to or greater than ten digits
962
         * <strong>Should</strong> return regular number if decimal places are as high as six
963
         */
964
        public String getValueAsString(Locale locale) {
965
                // formatting for the return of numbers of type double
966
                NumberFormat nf = NumberFormat.getNumberInstance(locale);
1 ✔
967
                DecimalFormat df = (DecimalFormat) nf;
1 ✔
968
                // formatting style up to 6 digits
969
                df.applyPattern("#0.0#####");
1 ✔
970
                //branch on hl7 abbreviations
971
                if (getConcept() != null) {
1 ✔
972
                        String abbrev = getConcept().getDatatype().getHl7Abbreviation();
1 ✔
973
                        if ("BIT".equals(abbrev)) {
1 ✔
974
                                return getValueAsBoolean() == null ? "" : getValueAsBoolean().toString();
×
975
                        } else if ("CWE".equals(abbrev)) {
1 ✔
976
                                if (getValueCoded() == null) {
1 ✔
977
                                        return "";
×
978
                                }
979
                                if (getValueDrug() != null) {
1 ✔
980
                                        return getValueDrug().getFullName(locale);
×
981
                                } else {
982
                                        ConceptName codedName = getValueCodedName();
1 ✔
983
                                        if (codedName != null) {
1 ✔
984
                                                return getValueCoded().getName(locale, false).getName();
1 ✔
985
                                        } else {
986
                                                ConceptName fallbackName = getValueCoded().getName();
×
987
                                                if (fallbackName != null) {
×
988
                                                        return fallbackName.getName();
×
989
                                                } else {
990
                                                        return "";
×
991
                                                }
992
                                                
993
                                        }
994
                                }
995
                        } else if ("NM".equals(abbrev) || "SN".equals(abbrev)) {
1 ✔
996
                                if (getValueNumeric() == null) {
1 ✔
997
                                        return "";
×
998
                                } else {
999
                                        Concept deproxiedConcept = HibernateUtil.getRealObjectFromProxy(getConcept());
1 ✔
1000
                                        if (deproxiedConcept instanceof ConceptNumeric) {
1 ✔
1001
                                                ConceptNumeric cn = (ConceptNumeric) deproxiedConcept;
1 ✔
1002
                                                if (!cn.getAllowDecimal()) {
1 ✔
1003
                                                        double d = getValueNumeric();
1 ✔
1004
                                                        int i = (int) d;
1 ✔
1005
                                                        return Integer.toString(i);
1 ✔
1006
                                                } else {
1007
                                                        df.format(getValueNumeric());
×
1008
                                                }
1009
                                        }
1010
                                }
×
1011
                        } else if ("DT".equals(abbrev)) {
1 ✔
1012
                                DateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN);
1 ✔
1013
                                return (getValueDatetime() == null ? "" : dateFormat.format(getValueDatetime()));
1 ✔
1014
                        } else if ("TM".equals(abbrev)) {
1 ✔
1015
                                return (getValueDatetime() == null ? "" : Format.format(getValueDatetime(), locale, FORMAT_TYPE.TIME));
×
1016
                        } else if ("TS".equals(abbrev)) {
1 ✔
1017
                                return (getValueDatetime() == null ? "" : Format.format(getValueDatetime(), locale, FORMAT_TYPE.TIMESTAMP));
×
1018
                        } else if ("ST".equals(abbrev)) {
1 ✔
1019
                                return getValueText();
×
1020
                        } else if ("ED".equals(abbrev) && getValueComplex() != null) {
1 ✔
1021
                                String[] valuesComplex = getValueComplex().split("\\|");
×
1022
                                for (String value : valuesComplex) {
×
1023
                                        if (StringUtils.isNotEmpty(value)) {
×
1024
                                                return value.trim();
×
1025
                                        }
1026
                                }
1027
                        }
1028
                }
1029
                
1030
                // if the datatype is 'unknown', default to just returning what is not null
1031
                if (getValueNumeric() != null) {
1 ✔
1032
                        return df.format(getValueNumeric());
1 ✔
1033
                } else if (getValueCoded() != null) {
×
1034
                        if (getValueDrug() != null) {
×
1035
                                return getValueDrug().getFullName(locale);
×
1036
                        } else {
1037
                                ConceptName valudeCodedName = getValueCodedName();
×
1038
                                if (valudeCodedName != null) {
×
1039
                                        return valudeCodedName.getName();
×
1040
                                } else {
1041
                                        return "";
×
1042
                                }
1043
                        }
1044
                } else if (getValueDatetime() != null) {
×
1045
                        return Format.format(getValueDatetime(), locale, FORMAT_TYPE.DATE);
×
1046
                } else if (getValueText() != null) {
×
1047
                        return getValueText();
×
1048
                } else if (hasGroupMembers()) {
×
1049
                        // all of the values are null and we're an obs group...so loop
1050
                        // over the members and just do a getValueAsString on those
1051
                        // this could potentially cause an infinite loop if an obs group
1052
                        // is a member of its own group at some point in the hierarchy
1053
                        StringBuilder sb = new StringBuilder();
×
1054
                        for (Obs groupMember : getGroupMembers()) {
×
1055
                                if (sb.length() > 0) {
×
1056
                                        sb.append(", ");
×
1057
                                }
1058
                                sb.append(groupMember.getValueAsString(locale));
×
1059
                        }
×
1060
                        return sb.toString();
×
1061
                }
1062
                
1063
                // returns the title portion of the valueComplex
1064
                // which is everything before the first bar '|' character.
1065
                if (getValueComplex() != null) {
×
1066
                        String[] valuesComplex = getValueComplex().split("\\|");
×
1067
                        for (String value : valuesComplex) {
×
1068
                                if (StringUtils.isNotEmpty(value)) {
×
1069
                                        return value.trim();
×
1070
                                }
1071
                        }
1072
                }
1073
                
1074
                return "";
×
1075
        }
1076
        
1077
        /**
1078
         * Sets the value for the obs from a string depending on the datatype of the question concept
1079
         *
1080
         * @param s the string to coerce to a boolean
1081
         * <strong>Should</strong> set value as boolean if the datatype of the question concept is boolean
1082
         * <strong>Should</strong> fail if the value of the string is null
1083
         * <strong>Should</strong> fail if the value of the string is empty
1084
         */
1085
        public void setValueAsString(String s) throws ParseException {
1086
                log.debug("getConcept() == {}", getConcept());
1 ✔
1087

1088
                if (getConcept() != null && s != null) {
1 ✔
1089
                        String abbrev = getConcept().getDatatype().getHl7Abbreviation();
1 ✔
1090
                        if ("ST".equals(abbrev)) {
1 ✔
1091
                                if (!StringUtils.isEmpty(s)) {
1 ✔
1092
                                        setValueText(s);
1 ✔
1093
                                }
1094
                                else {
1095
                                        throw new RuntimeException("Cannot set value to a empty string for concept: " + getConcept().getDisplayString());
1 ✔
1096
                                }
1097
                        } else if (!StringUtils.isBlank(s)) {
1 ✔
1098
                                s = s.trim();
1 ✔
1099
                                if ("BIT".equals(abbrev)) {
1 ✔
1100
                                        setValueBoolean(Boolean.valueOf(s));
×
1101
                                } else if ("CWE".equals(abbrev)) {
1 ✔
1102
                                        throw new RuntimeException("Not Yet Implemented");
×
1103
                                } else if ("NM".equals(abbrev) || "SN".equals(abbrev)) {
1 ✔
1104
                                        setValueNumeric(Double.valueOf(s));
×
1105
                                } else if ("DT".equals(abbrev)) {
1 ✔
UNCOV
1106
                                        DateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN);
×
NEW
1107
                                        setValueDatetime(dateFormat.parse(s));
×
1108
                                } else if ("TM".equals(abbrev)) {
1 ✔
UNCOV
1109
                                        DateFormat timeFormat = new SimpleDateFormat(TIME_PATTERN);
×
NEW
1110
                                        setValueDatetime(timeFormat.parse(s));
×
1111
                                } else if ("TS".equals(abbrev)) {
1 ✔
1112
                                        setValueDatetime(DateUtil.parseDatetimeString(s));
1 ✔
1113
                                }  else {
1114
                                        throw new RuntimeException("Don't know how to handle " + abbrev + " for concept: " + getConcept().getDisplayString());
×
1115
                                }
1116
                        } else {
1117
                                throw new RuntimeException("Cannot set value to a blank string for concept: " + getConcept().getDisplayString());
1 ✔
1118
                        }
1119
                } else {
1 ✔
1120
                        if (s == null) {
1 ✔
1121
                                throw new RuntimeException("cannot set value to null via setValueAsString()");
1 ✔
1122
                        } else {
1123
                                throw new RuntimeException("concept is null for " + this);
×
1124
                        }
1125
                }
1126
        }
1 ✔
1127
        
1128
        /**
1129
         * @see java.lang.Object#toString()
1130
         */
1131
        @Override
1132
        public String toString() {
1133
                if (obsId == null) {
1 ✔
1134
                        return "obs id is null";
1 ✔
1135
                }
1136
                
1137
                return "Obs #" + obsId.toString();
1 ✔
1138
        }
1139
        
1140
        /**
1141
         * @since 1.5
1142
         * @see org.openmrs.OpenmrsObject#getId()
1143
         */
1144
        @Override
1145
        public Integer getId() {
1146
                return getObsId();
1 ✔
1147
                
1148
        }
1149
        
1150
        /**
1151
         * @since 1.5
1152
         * @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
1153
         */
1154
        @Override
1155
        public void setId(Integer id) {
1156
                setObsId(id);
1 ✔
1157
                
1158
        }
1 ✔
1159
        
1160
        /**
1161
         * When ObsService updates an obs, it voids the old version, creates a new Obs with the updates,
1162
         * and adds a reference to the previousVersion in the new Obs. getPreviousVersion returns the
1163
         * last version of this Obs.
1164
         */
1165
        public Obs getPreviousVersion() {
1166
                return previousVersion;
1 ✔
1167
        }
1168
        
1169
        /**
1170
         * A previousVersion indicates that this Obs replaces an earlier one.
1171
         *
1172
         * @param previousVersion the Obs that this Obs superceeds
1173
         */
1174
        public void setPreviousVersion(Obs previousVersion) {
1175
                markAsDirty(this.previousVersion, previousVersion);
1 ✔
1176
                this.previousVersion = previousVersion;
1 ✔
1177
        }
1 ✔
1178
        
1179
        public Boolean hasPreviousVersion() {
1180
                return getPreviousVersion() != null;
1 ✔
1181
        }
1182
        
1183
        /**
1184
         * @param creator
1185
         * @see Auditable#setCreator(User)
1186
         */
1187
        @Override
1188
        public void setCreator(User creator) {
1189
                markAsDirty(getCreator(), creator);
1 ✔
1190
                super.setCreator(creator);
1 ✔
1191
        }
1 ✔
1192
        
1193
        /**
1194
         * @param dateCreated
1195
         * @see Auditable#setDateCreated(Date)
1196
         */
1197
        @Override
1198
        public void setDateCreated(Date dateCreated) {
1199
                markAsDirty(getDateCreated(), dateCreated);
1 ✔
1200
                super.setDateCreated(dateCreated);
1 ✔
1201
        }
1 ✔
1202
        
1203
        /**
1204
         * @see org.openmrs.FormRecordable#setFormField(String,String)
1205
         */
1206
        @Override
1207
        public void setFormField(String namespace, String formFieldPath) {
1208
                String oldValue = formNamespaceAndPath;
1 ✔
1209
                super.setFormField(namespace, formFieldPath);
1 ✔
1210
                markAsDirty(oldValue, formNamespaceAndPath);
1 ✔
1211
        }
1 ✔
1212
        
1213
        /**
1214
         * Returns true if any change has been made to an Obs instance. In general, the only time
1215
         * isDirty() is going to return false is when a new Obs has just been instantiated or loaded
1216
         * from the database and no method that modifies it internally has been invoked.
1217
         *
1218
         * @return true if not changed otherwise false
1219
         * @since 2.0
1220
         * <strong>Should</strong> return false when no change has been made
1221
         * <strong>Should</strong> return true when any immutable field has been changed
1222
         * <strong>Should</strong> return false when only mutable fields are changed
1223
         * <strong>Should</strong> return true when an immutable field is changed from a null to a non null value
1224
         * <strong>Should</strong> return true when an immutable field is changed from a non null to a null value
1225
         */
1226
        public boolean isDirty() {
1227
                return dirty;
1 ✔
1228
        }
1229
        
1230
        protected void markAsDirty(Object oldValue, Object newValue) {
1231
                //Should we ignore the case for Strings?
1232
                if (!isDirty() && obsId != null && !OpenmrsUtil.nullSafeEquals(oldValue, newValue)) {
1 ✔
1233
                        dirty = true;
1 ✔
1234
                }
1235
        }
1 ✔
1236
        
1237
        /**
1238
         * Similar to FHIR's Observation.interpretation. Supports a subset of FHIR's Observation
1239
         * Interpretation Codes. See https://www.hl7.org/fhir/valueset-observation-interpretation.html
1240
         * 
1241
         * @since 2.1.0
1242
         */
1243
        public Interpretation getInterpretation() {
1244
                return interpretation;
1 ✔
1245
        }
1246
        
1247
        /**
1248
         * @since 2.1.0
1249
         */
1250
        public void setInterpretation(Interpretation interpretation) {
1251
                markAsDirty(this.interpretation, interpretation);
1 ✔
1252
                this.interpretation = interpretation;
1 ✔
1253
        }
1 ✔
1254
        
1255
        /**
1256
         * Similar to FHIR's Observation.status. Supports a subset of FHIR's ObservationStatus values.
1257
         * At present OpenMRS does not support FHIR's REGISTERED and CANCELLED statuses, because we
1258
         * don't support obs with null values. See:
1259
         * https://www.hl7.org/fhir/valueset-observation-status.html
1260
         * 
1261
         * @since 2.1.0
1262
         */
1263
        public Status getStatus() {
1264
                return status;
1 ✔
1265
        }
1266
        
1267
        /**
1268
         * @since 2.1.0
1269
         */
1270
        public void setStatus(Status status) {
1271
                markAsDirty(this.status, status);
1 ✔
1272
                this.status = status;
1 ✔
1273
        }
1 ✔
1274

1275
        /**
1276
         * Returns the ObsReferenceRange
1277
         * @return obsReferenceRange.
1278
         * 
1279
         * @since 2.7.0
1280
         */
1281
        public ObsReferenceRange getReferenceRange() {
1282
                return referenceRange;
1 ✔
1283
        }
1284
        
1285
        /**
1286
         * Sets ObsReferenceRange
1287
         * 
1288
         * @param referenceRange ObsReferenceRange to set.
1289
         *               
1290
         * @since 2.7.0
1291
         */
1292
        public void setReferenceRange(ObsReferenceRange referenceRange) {
1293
                this.referenceRange = referenceRange;
1 ✔
1294
        }
1 ✔
1295
}
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