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());
}
}
}
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);
}
}
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");
}
Aggregations