/
PersonServiceImpl.java
1001 lines (869 loc) · 31 KB
/
PersonServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.api.impl;
import org.apache.commons.lang3.StringUtils;
import org.openmrs.GlobalProperty;
import org.openmrs.Person;
import org.openmrs.PersonAddress;
import org.openmrs.PersonAttribute;
import org.openmrs.PersonAttributeType;
import org.openmrs.PersonName;
import org.openmrs.Relationship;
import org.openmrs.RelationshipType;
import org.openmrs.api.APIException;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.PersonAttributeTypeLockedException;
import org.openmrs.api.PersonService;
import org.openmrs.api.context.Context;
import org.openmrs.api.db.PersonDAO;
import org.openmrs.person.PersonMergeLog;
import org.openmrs.person.PersonMergeLogData;
import org.openmrs.serialization.SerializationException;
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.OpenmrsConstants.PERSON_TYPE;
import org.openmrs.validator.ValidateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MarkerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Default implementation of the PersonService
* <p>
* Which implementation to use is determined by Spring. See the spring application context file in
* /metadata/api/spring/applicatContext-service.xml
*
* @see PersonService
* @see org.openmrs.api.context.Context
*/
@Transactional
public class PersonServiceImpl extends BaseOpenmrsService implements PersonService {
private static final Logger log = LoggerFactory.getLogger(PersonServiceImpl.class);
private PersonDAO dao;
/**
* @see org.openmrs.api.PersonService#setPersonDAO(org.openmrs.api.db.PersonDAO)
*/
@Override
public void setPersonDAO(PersonDAO dao) {
this.dao = dao;
}
/**
* @see org.openmrs.api.PersonService#getSimilarPeople(java.lang.String, java.lang.Integer,
* java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public Set<Person> getSimilarPeople(String name, Integer birthyear, String gender) throws APIException {
return dao.getSimilarPeople(name, birthyear, gender);
}
/**
* @see org.openmrs.api.PersonService#getPeople(String, Boolean)
*/
@Override
@Transactional(readOnly = true)
public List<Person> getPeople(String searchPhrase, Boolean dead) throws APIException {
return dao.getPeople(searchPhrase, dead);
}
@Override
public List<Person> getPeople(String searchPhrase, Boolean dead, Boolean voided) throws APIException {
return dao.getPeople(searchPhrase, dead, voided);
}
/**
* @see org.openmrs.api.PersonService#getAllPersonAttributeTypes()
*/
@Override
@Transactional(readOnly = true)
public List<PersonAttributeType> getAllPersonAttributeTypes() throws APIException {
return Context.getPersonService().getAllPersonAttributeTypes(true);
}
/**
* @see org.openmrs.api.PersonService#getAllPersonAttributeTypes(boolean)
*/
@Override
@Transactional(readOnly = true)
public List<PersonAttributeType> getAllPersonAttributeTypes(boolean includeRetired) throws APIException {
return dao.getAllPersonAttributeTypes(includeRetired);
}
/**
* @see org.openmrs.api.PersonService#getPersonAttributeTypeByName(java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public PersonAttributeType getPersonAttributeTypeByName(String typeName) throws APIException {
List<PersonAttributeType> types = Context.getPersonService().getPersonAttributeTypes(typeName, null, null, null);
if (types.isEmpty()) {
return null;
} else {
return types.get(0);
}
}
/**
* @see org.openmrs.api.PersonService#purgePersonAttributeType(org.openmrs.PersonAttributeType)
*/
@Override
public void purgePersonAttributeType(PersonAttributeType type) throws APIException {
checkIfPersonAttributeTypesAreLocked();
dao.deletePersonAttributeType(type);
}
/**
* @see org.openmrs.api.PersonService#savePersonAttributeType(org.openmrs.PersonAttributeType)
*/
@Override
public PersonAttributeType savePersonAttributeType(PersonAttributeType type) throws APIException {
checkIfPersonAttributeTypesAreLocked();
if (type.getSortWeight() == null) {
List<PersonAttributeType> allTypes = Context.getPersonService().getAllPersonAttributeTypes();
if (!allTypes.isEmpty()) {
type.setSortWeight(allTypes.get(allTypes.size() - 1).getSortWeight() + 1);
} else {
type.setSortWeight(1.0);
}
}
boolean updateExisting = false;
if (type.getId() != null) {
updateExisting = true;
String oldTypeName = dao.getSavedPersonAttributeTypeName(type);
String newTypeName = type.getName();
if (!oldTypeName.equals(newTypeName)) {
List<GlobalProperty> props = new ArrayList<>();
AdministrationService as = Context.getAdministrationService();
for (String propName : OpenmrsConstants.GLOBAL_PROPERTIES_OF_PERSON_ATTRIBUTES) {
props.add(as.getGlobalPropertyObject(propName));
}
for (GlobalProperty prop : props) {
if (prop != null) {
String propVal = prop.getPropertyValue();
if (propVal != null && propVal.contains(oldTypeName)) {
prop.setPropertyValue(propVal.replaceFirst(oldTypeName, newTypeName));
as.saveGlobalProperty(prop);
}
}
}
}
}
PersonAttributeType attributeType = dao.savePersonAttributeType(type);
if (updateExisting ) {
Boolean oldSearchable = dao.getSavedPersonAttributeTypeSearchable(type);
if (oldSearchable == null || !oldSearchable.equals(type.getSearchable())) {
//we need to update index searchable property has changed
Context.updateSearchIndexForType(PersonAttribute.class);
}
}
return attributeType;
}
/**
* @see org.openmrs.api.PersonService#retirePersonAttributeType(PersonAttributeType, String)
*/
@Override
public PersonAttributeType retirePersonAttributeType(PersonAttributeType type, String retiredReason)
throws APIException {
checkIfPersonAttributeTypesAreLocked();
if (retiredReason == null || retiredReason.length() < 1) {
throw new APIException("Person.retiring.reason.required", (Object[]) null);
}
type.setRetired(true);
type.setRetiredBy(Context.getAuthenticatedUser());
type.setRetireReason(retiredReason);
type.setDateRetired(new Date());
return dao.savePersonAttributeType(type);
}
/**
* @see org.openmrs.api.PersonService#getPersonAttributeTypes(java.lang.String,
* java.lang.String, java.lang.Integer, java.lang.Boolean)
*/
@Override
@Transactional(readOnly = true)
public List<PersonAttributeType> getPersonAttributeTypes(String exactName, String format, Integer foreignKey,
Boolean searchable) throws APIException {
return dao.getPersonAttributeTypes(exactName, format, foreignKey, searchable);
}
@Override
public void unretirePersonAttributeType(PersonAttributeType type) throws APIException {
checkIfPersonAttributeTypesAreLocked();
type.setRetired(false);
type.setDateRetired(null);
type.setRetiredBy(null);
type.setRetireReason(null);
Context.getPersonService().savePersonAttributeType(type);
}
/**
* @see org.openmrs.api.PersonService#getPersonAttributeType(java.lang.Integer)
*/
@Override
@Transactional(readOnly = true)
public PersonAttributeType getPersonAttributeType(Integer typeId) {
return dao.getPersonAttributeType(typeId);
}
/**
* @see org.openmrs.api.PersonService#getPersonAttribute(java.lang.Integer)
*/
@Override
@Transactional(readOnly = true)
public PersonAttribute getPersonAttribute(Integer id) {
return dao.getPersonAttribute(id);
}
/**
* @see org.openmrs.api.PersonService#getRelationship(java.lang.Integer)
*/
@Override
@Transactional(readOnly = true)
public Relationship getRelationship(Integer relationshipId) throws APIException {
return dao.getRelationship(relationshipId);
}
/**
* @see org.openmrs.api.PersonService#getRelationshipType(java.lang.Integer)
*/
@Override
@Transactional(readOnly = true)
public RelationshipType getRelationshipType(Integer relationshipTypeId) throws APIException {
return dao.getRelationshipType(relationshipTypeId);
}
/**
* @see org.openmrs.api.PersonService#getRelationshipTypeByName(java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public RelationshipType getRelationshipTypeByName(String relationshipTypeName) throws APIException {
List<RelationshipType> types = dao.getRelationshipTypes(relationshipTypeName, null);
if (types.isEmpty()) {
return null;
} else {
return types.get(0);
}
}
/**
* @see org.openmrs.api.PersonService#purgePerson(org.openmrs.Person)
*/
@Override
public void purgePerson(Person person) throws APIException {
dao.deletePerson(person);
}
/**
* @see org.openmrs.api.PersonService#savePerson(org.openmrs.Person)
*/
@Override
public Person savePerson(Person person) throws APIException {
setPreferredPersonName(person);
setPreferredPersonAddress(person);
return dao.savePerson(person);
}
private void setPreferredPersonName(Person person) {
PersonName preferredName = null;
PersonName possiblePreferredName = person.getPersonName();
if (possiblePreferredName != null && possiblePreferredName.getPreferred() && !possiblePreferredName.getVoided()) {
preferredName = possiblePreferredName;
}
for (PersonName name : person.getNames()) {
if (preferredName == null && !name.getVoided()) {
name.setPreferred(true);
preferredName = name;
continue;
}
if (!name.equals(preferredName)) {
name.setPreferred(false);
}
}
}
private void setPreferredPersonAddress(Person person) {
PersonAddress preferredAddress = null;
PersonAddress possiblePreferredAddress = person.getPersonAddress();
if (possiblePreferredAddress != null && possiblePreferredAddress.getPreferred()
&& !possiblePreferredAddress.getVoided()) {
preferredAddress = possiblePreferredAddress;
}
for (PersonAddress address : person.getAddresses()) {
if (preferredAddress == null && !address.getVoided()) {
address.setPreferred(true);
preferredAddress = address;
continue;
}
if (!address.equals(preferredAddress)) {
address.setPreferred(false);
}
}
}
/**
* @see org.openmrs.api.PersonService#voidPerson(org.openmrs.Person, java.lang.String)
*/
@Override
public Person voidPerson(Person person, String reason) throws APIException {
if (person == null) {
return null;
}
return dao.savePerson(person);
}
/**
* @see org.openmrs.api.PersonService#unvoidPerson(org.openmrs.Person)
*/
@Override
public Person unvoidPerson(Person person) throws APIException {
if (person == null) {
return null;
}
return Context.getPersonService().savePerson(person);
}
/**
* @see org.openmrs.api.PersonService#getPerson(java.lang.Integer)
*/
@Override
@Transactional(readOnly = true)
public Person getPerson(Integer personId) throws APIException {
if (personId == null) {
return null;
}
return dao.getPerson(personId);
}
/**
* @see org.openmrs.api.PersonService#getAllRelationships()
*/
@Override
@Transactional(readOnly = true)
public List<Relationship> getAllRelationships() throws APIException {
return Context.getPersonService().getAllRelationships(false);
}
/**
* @see org.openmrs.api.PersonService#getAllRelationships(boolean)
*/
@Override
@Transactional(readOnly = true)
public List<Relationship> getAllRelationships(boolean includeVoided) throws APIException {
return dao.getAllRelationships(includeVoided);
}
/**
* @see org.openmrs.api.PersonService#getRelationships(org.openmrs.Person, org.openmrs.Person,
* org.openmrs.RelationshipType)
*/
@Override
@Transactional(readOnly = true)
public List<Relationship> getRelationships(Person fromPerson, Person toPerson, RelationshipType relType)
throws APIException {
return dao.getRelationships(fromPerson, toPerson, relType);
}
/**
* @see org.openmrs.api.PersonService#getRelationships(org.openmrs.Person, org.openmrs.Person,
* org.openmrs.RelationshipType, java.util.Date)
*/
@Override
@Transactional(readOnly = true)
public List<Relationship> getRelationships(Person fromPerson, Person toPerson, RelationshipType relType,
Date effectiveDate) throws APIException {
return dao.getRelationships(fromPerson, toPerson, relType, effectiveDate, null);
}
/**
* @see org.openmrs.api.PersonService#getRelationships(org.openmrs.Person, org.openmrs.Person,
* org.openmrs.RelationshipType, java.util.Date, java.util.Date)
*/
@Override
@Transactional(readOnly = true)
public List<Relationship> getRelationships(Person fromPerson, Person toPerson, RelationshipType relType,
Date startEffectiveDate, Date endEffectiveDate) throws APIException {
return dao.getRelationships(fromPerson, toPerson, relType, startEffectiveDate, endEffectiveDate);
}
/**
* @see org.openmrs.api.PersonService#getRelationshipsByPerson(org.openmrs.Person)
*/
@Override
@Transactional(readOnly = true)
public List<Relationship> getRelationshipsByPerson(Person p) throws APIException {
// search both the left side and the right side of the relationship
// for this person
List<Relationship> rels = Context.getPersonService().getRelationships(p, null, null);
rels.addAll(Context.getPersonService().getRelationships(null, p, null));
return rels;
}
/**
* @see org.openmrs.api.PersonService#getRelationshipsByPerson(org.openmrs.Person,
* java.util.Date)
*/
@Override
@Transactional(readOnly = true)
public List<Relationship> getRelationshipsByPerson(Person p, Date effectiveDate) throws APIException {
// search both the left side and the right side of the relationship
// for this person
List<Relationship> rels = Context.getPersonService().getRelationships(p, null, null, effectiveDate);
rels.addAll(Context.getPersonService().getRelationships(null, p, null, effectiveDate));
return rels;
}
/**
* @see org.openmrs.api.PersonService#purgeRelationship(org.openmrs.Relationship)
*/
@Override
public void purgeRelationship(Relationship relationship) throws APIException {
dao.deleteRelationship(relationship);
}
/**
* @see org.openmrs.api.PersonService#saveRelationship(org.openmrs.Relationship)
*/
@Override
public Relationship saveRelationship(Relationship relationship) throws APIException {
if (relationship.getPersonA().equals(relationship.getPersonB())) {
throw new APIException("Person.cannot.same", (Object[]) null);
}
return dao.saveRelationship(relationship);
}
/**
* @see org.openmrs.api.PersonService#voidRelationship(org.openmrs.Relationship,
* java.lang.String)
*/
@Override
public Relationship voidRelationship(Relationship relationship, String voidReason) throws APIException {
if(relationship == null) {
return null;
}
return Context.getPersonService().saveRelationship(relationship);
}
/**
* @see org.openmrs.api.PersonService#unvoidRelationship(org.openmrs.Relationship)
*/
@Override
public Relationship unvoidRelationship(Relationship relationship) throws APIException {
relationship.setVoided(false);
relationship.setVoidedBy(null);
relationship.setDateVoided(null);
relationship.setVoidReason(null);
return Context.getPersonService().saveRelationship(relationship);
}
/**
* @see org.openmrs.api.PersonService#getAllRelationshipTypes()
*/
@Override
@Transactional(readOnly = true)
public List<RelationshipType> getAllRelationshipTypes() throws APIException {
return Context.getPersonService().getAllRelationshipTypes(false);
}
/**
* @see org.openmrs.api.PersonService#getRelationshipTypes(java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public List<RelationshipType> getRelationshipTypes(String searchString) throws APIException {
return Context.getPersonService().getRelationshipTypes(searchString, null);
}
/**
* @see org.openmrs.api.PersonService#getRelationshipTypes(java.lang.String, java.lang.Boolean)
*/
@Override
@Transactional(readOnly = true)
public List<RelationshipType> getRelationshipTypes(String relationshipTypeName, Boolean preferred) throws APIException {
Assert.hasText(relationshipTypeName, "The search string cannot be empty");
return dao.getRelationshipTypes(relationshipTypeName, preferred);
}
/**
* @see org.openmrs.api.PersonService#purgeRelationshipType(org.openmrs.RelationshipType)
*/
@Override
public void purgeRelationshipType(RelationshipType relationshipType) throws APIException {
dao.deleteRelationshipType(relationshipType);
}
/**
* @see org.openmrs.api.PersonService#saveRelationshipType(org.openmrs.RelationshipType)
*/
@Override
public RelationshipType saveRelationshipType(RelationshipType relationshipType) throws APIException {
if (StringUtils.isBlank(relationshipType.getDescription())) {
throw new APIException("error.required",
new Object[] { Context.getMessageSourceService().getMessage("general.description") });
}
return dao.saveRelationshipType(relationshipType);
}
/**
* @see org.openmrs.api.PersonService#getPersonAttributeTypes(org.openmrs.util.OpenmrsConstants.PERSON_TYPE,
* org.openmrs.api.PersonService.ATTR_VIEW_TYPE)
*/
@Override
@Transactional(readOnly = true)
public List<PersonAttributeType> getPersonAttributeTypes(PERSON_TYPE personType, ATTR_VIEW_TYPE viewType)
throws APIException {
if (viewType == null) {
return Context.getPersonService().getAllPersonAttributeTypes();
}
List<String> attrNames = getAttributeTypesFromGlobalProperties(viewType, personType);
List<PersonAttributeType> result = new ArrayList<>();
for (String nameOrId : attrNames) {
if (nameOrId.matches("\\d")) {
result.add(getPersonAttributeType(Integer.valueOf(nameOrId)));
} else {
result.add(getPersonAttributeTypeByName(nameOrId));
}
}
return result;
}
private List<String> getAttributeTypesFromGlobalProperties(ATTR_VIEW_TYPE viewType, PERSON_TYPE personType) {
List<String> result = new ArrayList<>();
if (viewType == ATTR_VIEW_TYPE.LISTING) {
result = combineAttributes(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_LISTING_ATTRIBUTES, OpenmrsConstants.GLOBAL_PROPERTY_USER_LISTING_ATTRIBUTES, personType);
} else if (viewType == ATTR_VIEW_TYPE.VIEWING) {
result = combineAttributes(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_VIEWING_ATTRIBUTES, OpenmrsConstants.GLOBAL_PROPERTY_USER_VIEWING_ATTRIBUTES, personType);
} else if (viewType == ATTR_VIEW_TYPE.HEADER) {
result = combineAttributes(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_HEADER_ATTRIBUTES, OpenmrsConstants.GLOBAL_PROPERTY_USER_HEADER_ATTRIBUTES, personType);
} else {
log.error(MarkerFactory.getMarker("FATAL"), "Should not be here.");
}
List<String> attrTypes = new ArrayList<>();
for (String res : result) {
for (String attrType : res.split(",")) {
if (attrType != null) {
attrType = attrType.trim();
if (!attrType.isEmpty()) {
attrTypes.add(attrType);
}
}
}
}
return attrTypes;
}
private List<String> combineAttributes(String patientAttributeProperty, String userAttributeProperty, PERSON_TYPE personType) {
List<String> result = new ArrayList<>();
if (personType == null || personType == PERSON_TYPE.PERSON) {
result = getGlobalProperties(patientAttributeProperty, userAttributeProperty);
} else if (personType == PERSON_TYPE.PATIENT) {
result = getGlobalProperties(patientAttributeProperty);
} else if (personType == PERSON_TYPE.USER) {
result = getGlobalProperties(userAttributeProperty);
} else {
log.error(MarkerFactory.getMarker("FATAL"), "Should not be here.");
}
return result;
}
private List<String> getGlobalProperties(String... properties) {
List<String> result = new ArrayList<>();
AdministrationService as = Context.getAdministrationService();
for (String p : properties) {
String id = as.getGlobalProperty(p, "");
if (StringUtils.isNotBlank(id)) {
result.add(id.trim());
}
}
return result;
}
/**
* @see org.openmrs.api.PersonService#parsePersonName(java.lang.String)
*/
@Override
public PersonName parsePersonName(String name) throws APIException {
// strip beginning/ending whitespace
name = name.trim();
// trim off all trailing commas
while (name.endsWith(",")) {
name = name.substring(0, name.length() - 1);
}
String firstName = name;
String middleName = "";
String lastName = "";
String lastName2 = null;
if (name.contains(",")) {
String[] names = name.split(",");
// trim whitespace on each part of the name
for (int x = 0; x < names.length; x++) {
names[x] = names[x].trim();
}
String[] firstNames = names[1].split(" ");
if (firstNames.length == 2) {
// user entered "Smith, John Adam"
lastName = names[0];
firstName = firstNames[0];
middleName = firstNames[1];
} else {
// user entered "Smith, John"
firstName = names[1];
lastName = names[0];
}
} else if (name.contains(" ")) {
String[] names = name.split(" ");
if (names.length == 4) {
// user entered "John Adam Smith"
firstName = names[0];
middleName = names[1];
lastName = names[2];
lastName2 = names[3];
} else if (names.length == 3) {
// user entered "John Adam Smith"
firstName = names[0];
middleName = names[1];
lastName = names[2];
} else {
// user entered "John Smith"
firstName = names[0];
lastName = names[1];
}
}
PersonName pn = new PersonName(firstName, middleName, lastName);
pn.setFamilyName2(lastName2);
return pn;
}
/**
* @see org.openmrs.api.PersonService#voidPersonName(org.openmrs.PersonName, String)
*/
@Override
public PersonName voidPersonName(PersonName personName, String voidReason) throws APIException {
return Context.getPersonService().savePersonName(personName);
}
/**
* @see org.openmrs.api.PersonService#unvoidPersonName(org.openmrs.PersonName)
*/
@Override
public PersonName unvoidPersonName(PersonName personName) throws APIException {
return Context.getPersonService().savePersonName(personName);
}
/**
* @see org.openmrs.api.PersonService#savePersonName(org.openmrs.PersonName)
*/
@Override
public PersonName savePersonName(PersonName personName) throws APIException {
ValidateUtil.validate(personName.getPerson());
return dao.savePersonName(personName);
}
/**
* @see org.openmrs.api.PersonService#getRelationshipMap(org.openmrs.RelationshipType)
*/
@Override
@Transactional(readOnly = true)
public Map<Person, List<Person>> getRelationshipMap(RelationshipType relType) throws APIException {
// get all relationships with this type
List<Relationship> relationships = Context.getPersonService().getRelationships(null, null, relType);
// the map to return
Map<Person, List<Person>> ret = new HashMap<>();
if (relationships != null) {
for (Relationship rel : relationships) {
Person from = rel.getPersonA();
Person to = rel.getPersonB();
List<Person> relList = ret.get(from);
if (relList == null) {
relList = new ArrayList<>();
}
relList.add(to);
ret.put(from, relList);
}
}
return ret;
}
/**
* @see org.openmrs.api.PersonService#getPersonAttributeTypeByUuid(java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public PersonAttributeType getPersonAttributeTypeByUuid(String uuid) {
return dao.getPersonAttributeTypeByUuid(uuid);
}
/**
* @see org.openmrs.api.PersonService#getPersonByUuid(java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public Person getPersonByUuid(String uuid) throws APIException {
return dao.getPersonByUuid(uuid);
}
@Override
@Transactional(readOnly = true)
public PersonAddress getPersonAddressByUuid(String uuid) throws APIException {
return dao.getPersonAddressByUuid(uuid);
}
@Override
@Transactional(readOnly = true)
public PersonAttribute getPersonAttributeByUuid(String uuid) throws APIException {
return dao.getPersonAttributeByUuid(uuid);
}
/**
* @see org.openmrs.api.PersonService#getPersonName(java.lang.Integer)
*/
@Override
@Transactional(readOnly = true)
public PersonName getPersonName(Integer personNameId) {
return dao.getPersonName(personNameId);
}
/**
* @see org.openmrs.api.PersonService#getPersonNameByUuid(java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public PersonName getPersonNameByUuid(String uuid) throws APIException {
return dao.getPersonNameByUuid(uuid);
}
/**
* @see org.openmrs.api.PersonService#savePersonMergeLog(PersonMergeLog)
*/
@Override
public PersonMergeLog savePersonMergeLog(PersonMergeLog personMergeLog) throws SerializationException, APIException {
//verify required fields
if (Context.getSerializationService().getDefaultSerializer() == null) {
throw new APIException("serializer.default.not.found", (Object[]) null);
}
log.debug("Auditing merging of non-preferred person " + personMergeLog.getLoser().getUuid()
+ " with preferred person " + personMergeLog.getWinner().getId());
//populate the mergedData XML from the PersonMergeLogData object
String serialized = Context.getSerializationService().getDefaultSerializer()
.serialize(personMergeLog.getPersonMergeLogData());
personMergeLog.setSerializedMergedData(serialized);
log.debug(serialized);
//save the bean to the database
return dao.savePersonMergeLog(personMergeLog);
}
/**
* @see org.openmrs.api.PersonService#getPersonMergeLogByUuid(String, boolean)
*/
@Override
@Transactional(readOnly = true)
public PersonMergeLog getPersonMergeLogByUuid(String uuid, boolean deserialize)
throws SerializationException, APIException {
if (uuid == null) {
throw new APIException("uuid.cannot.null", (Object[]) null);
}
PersonMergeLog personMergeLog = dao.getPersonMergeLogByUuid(uuid);
//deserialize if requested
if (deserialize) {
deserialize(personMergeLog);
}
return personMergeLog;
}
/**
* Deserializes a List of <code>PersonMErgeLog</code> objects
*
* @param lst the List of <code> PersonMergeLog</code> objects to deserialize
* @throws SerializationException
*/
private void deserializeList(List<PersonMergeLog> lst) throws SerializationException {
for (PersonMergeLog personMergeLog : lst) {
deserialize(personMergeLog);
}
}
/**
* Deserializes a <code>PersonMErgeLog</code> object
*
* @param personMergeLog the <code> PersonMergeLog</code> object to deserialize
* @throws SerializationException
*/
private void deserialize(PersonMergeLog personMergeLog) throws SerializationException {
PersonMergeLogData data = Context.getSerializationService().getDefaultSerializer()
.deserialize(personMergeLog.getSerializedMergedData(), PersonMergeLogData.class);
personMergeLog.setPersonMergeLogData(data);
}
/**
* @see org.openmrs.api.PersonService#getAllPersonMergeLogs(boolean)
*/
@Override
@Transactional(readOnly = true)
public List<PersonMergeLog> getAllPersonMergeLogs(boolean deserialize) throws SerializationException {
List<PersonMergeLog> lst = dao.getAllPersonMergeLogs();
//deserialize if requested
if (deserialize) {
deserializeList(lst);
}
return lst;
}
/**
* @see org.openmrs.api.PersonService#getWinningPersonMergeLogs(Person, boolean)
*/
@Override
@Transactional(readOnly = true)
public List<PersonMergeLog> getWinningPersonMergeLogs(Person person, boolean deserialize) throws SerializationException {
List<PersonMergeLog> lst = dao.getWinningPersonMergeLogs(person);
if (deserialize) {
deserializeList(lst);
}
return lst;
}
/**
* @see org.openmrs.api.PersonService#getLosingPersonMergeLog(Person, boolean)
*/
@Override
@Transactional(readOnly = true)
public PersonMergeLog getLosingPersonMergeLog(Person person, boolean deserialize) throws SerializationException {
PersonMergeLog personMergeLog = dao.getLosingPersonMergeLogs(person);
if (deserialize) {
deserialize(personMergeLog);
}
return personMergeLog;
}
/**
* @see org.openmrs.api.PersonService#getRelationshipByUuid(java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public Relationship getRelationshipByUuid(String uuid) throws APIException {
return dao.getRelationshipByUuid(uuid);
}
/**
* @see org.openmrs.api.PersonService#getRelationshipTypeByUuid(java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public RelationshipType getRelationshipTypeByUuid(String uuid) throws APIException {
return dao.getRelationshipTypeByUuid(uuid);
}
/**
* @see org.openmrs.api.PersonService#getAllRelationshipTypes(boolean)
*/
@Override
@Transactional(readOnly = true)
public List<RelationshipType> getAllRelationshipTypes(boolean includeRetired) throws APIException {
return dao.getAllRelationshipTypes(includeRetired);
}
/**
* @see org.openmrs.api.PersonService#retireRelationshipType(org.openmrs.RelationshipType,
* java.lang.String)
*/
@Override
public RelationshipType retireRelationshipType(RelationshipType type, String retiredReason) throws APIException {
if (retiredReason == null || retiredReason.length() < 1) {
throw new APIException("Relationship.retiring.reason.required", (Object[]) null);
}
type.setRetired(true);
type.setRetiredBy(Context.getAuthenticatedUser());
type.setDateRetired(new Date());
type.setRetireReason(retiredReason);
return Context.getPersonService().saveRelationshipType(type);
}
/**
* @see org.openmrs.api.PersonService#unretireRelationshipType(org.openmrs.RelationshipType)
*/
@Override
public RelationshipType unretireRelationshipType(RelationshipType relationshipType) {
relationshipType.setRetired(false);
relationshipType.setRetiredBy(null);
relationshipType.setDateRetired(null);
relationshipType.setRetireReason(null);
return Context.getPersonService().saveRelationshipType(relationshipType);
}
/**
* @see org.openmrs.api.PersonService#voidPersonAddress(org.openmrs.PersonAddress, String)
*/
@Override
public PersonAddress voidPersonAddress(PersonAddress personAddress, String voidReason) {
return Context.getPersonService().savePersonAddress(personAddress);
}
/**
* @see org.openmrs.api.PersonService#unvoidPersonAddress(org.openmrs.PersonAddress)
*/
@Override
public PersonAddress unvoidPersonAddress(PersonAddress personAddress) throws APIException {
return Context.getPersonService().savePersonAddress(personAddress);
}
/**
* @see org.openmrs.api.PersonService#savePersonAddress(org.openmrs.PersonAddress)
*/
@Override
public PersonAddress savePersonAddress(PersonAddress personAddress) {
return dao.savePersonAddress(personAddress);
}
@Override
public void checkIfPersonAttributeTypesAreLocked() {
String locked = Context.getAdministrationService()
.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PERSON_ATRIBUTE_TYPES_LOCKED, "false");
if (Boolean.valueOf(locked)) {
throw new PersonAttributeTypeLockedException();
}
}