Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
05/18/08 13:16:49 (8 months ago)
Author:
djazayeri
Message:

Fixing #746 - Merge Patients should merge orders, program enrollment, person attributes, and relationship
Also allowing identifiers for voided patients to be reassigned to non-voided patients

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs/trunk/src/api/org/openmrs/api/impl/PatientServiceImpl.java

    r4095 r4245  
    2727import org.openmrs.Location; 
    2828import org.openmrs.Obs; 
     29import org.openmrs.Order; 
    2930import org.openmrs.Patient; 
    3031import org.openmrs.PatientIdentifier; 
    3132import org.openmrs.PatientIdentifierType; 
     33import org.openmrs.PatientProgram; 
    3234import org.openmrs.PersonAddress; 
     35import org.openmrs.PersonAttribute; 
    3336import org.openmrs.PersonName; 
     37import org.openmrs.Relationship; 
    3438import org.openmrs.Tribe; 
    3539import org.openmrs.api.APIAuthenticationException; 
     
    4347import org.openmrs.api.InvalidIdentifierFormatException; 
    4448import org.openmrs.api.MissingRequiredIdentifierException; 
     49import org.openmrs.api.ObsService; 
     50import org.openmrs.api.OrderService; 
    4551import org.openmrs.api.PatientIdentifierException; 
    4652import org.openmrs.api.PatientService; 
     53import org.openmrs.api.PersonService; 
     54import org.openmrs.api.ProgramWorkflowService; 
    4755import org.openmrs.api.context.Context; 
    4856import org.openmrs.api.db.PatientDAO; 
     
    258266                List<PatientIdentifier> ids = getPatientIdentifiers(identifier, type); 
    259267                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; 
    262271                        if ( ignorePatient == null || !id.getPatient().equals(ignorePatient)) 
    263272                                return id.getPatient(); 
     
    601610                log.debug("Merging patients: (preferred)" + preferred.getPatientId() + ", (notPreferred) " + notPreferred.getPatientId()); 
    602611                 
    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 
    604614                EncounterService es = Context.getEncounterService(); 
    605615                for (Encounter e : es.getEncounters(notPreferred)){ 
     
    631641                                tmpIdentifier.setVoidedBy(null); 
    632642                                tmpIdentifier.setVoidReason(null); 
     643                                // we don't want to change the preferred identifier of the preferred patient 
     644                                tmpIdentifier.setPreferred(false); 
    633645                                preferred.addIdentifier(tmpIdentifier); 
    634646                                log.debug("Merging identifier " + tmpIdentifier.getIdentifier() + " to " + preferred.getPatientId()); 
     
    657669                                tmpName.setVoidedBy(null); 
    658670                                tmpName.setVoidReason(null); 
     671                                // we don't want to change the preferred name of the preferred patient 
     672                                tmpName.setPreferred(false); 
    659673                                preferred.addName(tmpName); 
    660674                                log.debug("Merging name " + newName.getGivenName() + " to " + preferred.getPatientId()); 
     
    685699                                preferred.addAddress(tmpAddress); 
    686700                                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); 
    687755                        } 
    688756                }