- Timestamp:
- 05/18/08 13:16:49 (8 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openmrs/trunk/src/api/org/openmrs/api/impl/PatientServiceImpl.java
r4095 r4245 27 27 import org.openmrs.Location; 28 28 import org.openmrs.Obs; 29 import org.openmrs.Order; 29 30 import org.openmrs.Patient; 30 31 import org.openmrs.PatientIdentifier; 31 32 import org.openmrs.PatientIdentifierType; 33 import org.openmrs.PatientProgram; 32 34 import org.openmrs.PersonAddress; 35 import org.openmrs.PersonAttribute; 33 36 import org.openmrs.PersonName; 37 import org.openmrs.Relationship; 34 38 import org.openmrs.Tribe; 35 39 import org.openmrs.api.APIAuthenticationException; … … 43 47 import org.openmrs.api.InvalidIdentifierFormatException; 44 48 import org.openmrs.api.MissingRequiredIdentifierException; 49 import org.openmrs.api.ObsService; 50 import org.openmrs.api.OrderService; 45 51 import org.openmrs.api.PatientIdentifierException; 46 52 import org.openmrs.api.PatientService; 53 import org.openmrs.api.PersonService; 54 import org.openmrs.api.ProgramWorkflowService; 47 55 import org.openmrs.api.context.Context; 48 56 import org.openmrs.api.db.PatientDAO; … … 258 266 List<PatientIdentifier> ids = getPatientIdentifiers(identifier, type); 259 267 for (PatientIdentifier id : ids) { 260 // Changed by CA on 10 Feb 2007 - not sure why you would only verify this for identifiers with checkdigits... 261 //if (id.getIdentifierType().hasCheckDigit() && (ignorePatient == null || !id.getPatient().equals(ignorePatient)) ) 268 // ignore identifiers that are assigned to voided patients 269 if (id.getPatient().isVoided()) 270 continue; 262 271 if ( ignorePatient == null || !id.getPatient().equals(ignorePatient)) 263 272 return id.getPatient(); … … 601 610 log.debug("Merging patients: (preferred)" + preferred.getPatientId() + ", (notPreferred) " + notPreferred.getPatientId()); 602 611 603 // change all encounters 612 // change all encounters. This will cascade to obs and orders contained in those encounters 613 // TODO: this should be a copy, not a move 604 614 EncounterService es = Context.getEncounterService(); 605 615 for (Encounter e : es.getEncounters(notPreferred)){ … … 631 641 tmpIdentifier.setVoidedBy(null); 632 642 tmpIdentifier.setVoidReason(null); 643 // we don't want to change the preferred identifier of the preferred patient 644 tmpIdentifier.setPreferred(false); 633 645 preferred.addIdentifier(tmpIdentifier); 634 646 log.debug("Merging identifier " + tmpIdentifier.getIdentifier() + " to " + preferred.getPatientId()); … … 657 669 tmpName.setVoidedBy(null); 658 670 tmpName.setVoidReason(null); 671 // we don't want to change the preferred name of the preferred patient 672 tmpName.setPreferred(false); 659 673 preferred.addName(tmpName); 660 674 log.debug("Merging name " + newName.getGivenName() + " to " + preferred.getPatientId()); … … 685 699 preferred.addAddress(tmpAddress); 686 700 log.debug("Merging address " + newAddress.getPersonAddressId() + " to " + preferred.getPatientId()); 701 } 702 } 703 704 // copy all program enrollments 705 ProgramWorkflowService programService = Context.getProgramWorkflowService(); 706 for (PatientProgram pp : programService.getPatientPrograms(notPreferred)) { 707 if (!pp.getVoided()) { 708 PatientProgram enroll = pp.copy(); 709 enroll.setPatient(preferred); 710 log.debug("Copying patientProgram " + pp.getPatientProgramId() + " to " + preferred.getPatientId()); 711 programService.createPatientProgram(enroll); 712 } 713 } 714 715 // copy all relationships 716 PersonService personService = Context.getPersonService(); 717 for (Relationship rel : personService.getRelationships(notPreferred)) { 718 if (!rel.isVoided()) { 719 Relationship tmpRel = rel.copy(); 720 if (tmpRel.getPersonA().equals(notPreferred)) 721 tmpRel.setPersonA(preferred); 722 if (tmpRel.getPersonB().equals(notPreferred)) 723 tmpRel.setPersonB(preferred); 724 log.debug("Copying relationship " + rel.getRelationshipId() + " to " + preferred.getPatientId()); 725 personService.createRelationship(tmpRel); 726 } 727 } 728 729 // move all obs that weren't contained in encounters 730 // TODO: this should be a copy, not a move 731 ObsService obsService = Context.getObsService(); 732 for (Obs obs : obsService.getObservations(notPreferred, false)) { 733 if (obs.getEncounter() == null && !obs.isVoided()) { 734 obs.setPerson(preferred); 735 obsService.updateObs(obs); 736 } 737 } 738 739 // copy all orders that weren't contained in encounters 740 OrderService os = Context.getOrderService(); 741 for (Order o : os.getOrdersByPatient(notPreferred)) { 742 if (o.getEncounter() == null && !o.getVoided()) { 743 Order tmpOrder = o.copy(); 744 tmpOrder.setPatient(preferred); 745 os.createOrder(tmpOrder); 746 } 747 } 748 749 // copy person attributes 750 for (PersonAttribute attr : notPreferred.getAttributes()) { 751 if (!attr.isVoided()) { 752 PersonAttribute tmpAttr = attr.copy(); 753 tmpAttr.setPerson(null); 754 preferred.addAttribute(tmpAttr); 687 755 } 688 756 }