Search in sources :

Example 6 with PersonService

use of org.openmrs.api.PersonService in project openmrs-core by openmrs.

the class PatientServiceImpl method mergeRelationships.

private void mergeRelationships(Patient preferred, Patient notPreferred, PersonMergeLogData mergedData) {
    // copy all relationships
    PersonService personService = Context.getPersonService();
    Set<String> existingRelationships = new HashSet<>();
    // fill in the existing relationships with hashes
    for (Relationship rel : personService.getRelationshipsByPerson(preferred)) {
        existingRelationships.add(relationshipHash(rel, preferred));
    }
    // iterate over notPreferred's relationships and only copy them if they are needed
    for (Relationship rel : personService.getRelationshipsByPerson(notPreferred)) {
        if (!rel.getVoided()) {
            boolean personAisPreferred = rel.getPersonA().equals(preferred);
            boolean personAisNotPreferred = rel.getPersonA().equals(notPreferred);
            boolean personBisPreferred = rel.getPersonB().equals(preferred);
            boolean personBisNotPreferred = rel.getPersonB().equals(notPreferred);
            String relHash = relationshipHash(rel, notPreferred);
            if ((personAisPreferred && personBisNotPreferred) || (personBisPreferred && personAisNotPreferred)) {
                // void this relationship if it's between the preferred and notPreferred patients
                personService.voidRelationship(rel, "person " + (personAisNotPreferred ? "A" : "B") + " was merged to person " + (personAisPreferred ? "A" : "B"));
            } else if (existingRelationships.contains(relHash)) {
                // void this relationship if it already exists between preferred and the other side
                personService.voidRelationship(rel, "person " + (personAisNotPreferred ? "A" : "B") + " was merged and a relationship already exists");
            } else {
                // copy this relationship and replace notPreferred with preferred
                Relationship tmpRel = rel.copy();
                if (personAisNotPreferred) {
                    tmpRel.setPersonA(preferred);
                }
                if (personBisNotPreferred) {
                    tmpRel.setPersonB(preferred);
                }
                log.debug("Copying relationship " + rel.getRelationshipId() + " to " + preferred.getPatientId());
                Relationship persisted = personService.saveRelationship(tmpRel);
                mergedData.addCreatedRelationship(persisted.getUuid());
                // void the existing relationship to the notPreferred
                personService.voidRelationship(rel, "person " + (personAisNotPreferred ? "A" : "B") + " was merged, relationship copied to #" + tmpRel.getRelationshipId());
                // add the relationship hash to existing relationships
                existingRelationships.add(relHash);
            }
            mergedData.addVoidedRelationship(rel.getUuid());
        }
    }
}
Also used : PersonService(org.openmrs.api.PersonService) Relationship(org.openmrs.Relationship) HashSet(java.util.HashSet)

Example 7 with PersonService

use of org.openmrs.api.PersonService in project openmrs-core by openmrs.

the class PersonEditor method setAsText.

/**
 * @should set using id
 * @should set using uuid
 *
 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {
    PersonService ps = Context.getPersonService();
    if (StringUtils.hasText(text)) {
        try {
            Integer personId = Integer.valueOf(text);
            setValue(ps.getPerson(personId));
        } catch (NumberFormatException e) {
            Person person = ps.getPersonByUuid(text);
            setValue(person);
            if (person == null) {
                throw new IllegalArgumentException("Failed to find person for value [" + text + "]");
            }
        }
    } else {
        setValue(null);
    }
}
Also used : PersonService(org.openmrs.api.PersonService) Person(org.openmrs.Person)

Example 8 with PersonService

use of org.openmrs.api.PersonService in project openmrs-core by openmrs.

the class PersonAttributeTypeValidator method validate.

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null
 * @should fail validation if format is empty
 * @should fail validation if name already in use
 * @should pass validation if description is null or empty or whitespace
 * @should pass validation if all fields are correct
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object obj, Errors errors) {
    PersonAttributeType patObj = (PersonAttributeType) obj;
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "PersonAttributeType.error.nameEmpty");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "format", "PersonAttributeType.error.formatEmpty");
    PersonService ps = Context.getPersonService();
    PersonAttributeType pat = ps.getPersonAttributeTypeByName(patObj.getName());
    if (pat != null && !pat.getUuid().equals(patObj.getUuid())) {
        errors.rejectValue("name", "PersonAttributeType.error.nameAlreadyInUse");
    }
    ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "format", "retireReason");
}
Also used : PersonService(org.openmrs.api.PersonService) PersonAttributeType(org.openmrs.PersonAttributeType)

Aggregations

PersonService (org.openmrs.api.PersonService)8 Relationship (org.openmrs.Relationship)6 RelationshipType (org.openmrs.RelationshipType)5 Message (ca.uhn.hl7v2.model.Message)4 Test (org.junit.Test)4 Patient (org.openmrs.Patient)4 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)4 ORU_R01 (ca.uhn.hl7v2.model.v25.message.ORU_R01)3 NK1 (ca.uhn.hl7v2.model.v25.segment.NK1)3 Person (org.openmrs.Person)3 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Random (java.util.Random)1 PatientIdentifier (org.openmrs.PatientIdentifier)1 PatientIdentifierType (org.openmrs.PatientIdentifierType)1 PersonAttributeType (org.openmrs.PersonAttributeType)1 PersonName (org.openmrs.PersonName)1 Role (org.openmrs.Role)1 User (org.openmrs.User)1