use of org.openmrs.Person in project openmrs-module-coreapps by openmrs.
the class VisitTypeHelper method setEncounterBasedOnVisitType.
/**
* Create encounters based on visit type
*
* @param visit
* @param loginLocation
* @param previousType
*/
public void setEncounterBasedOnVisitType(Visit visit, Location loginLocation, VisitType previousType) {
// all types are transfer type
boolean isTransferType = true;
if (visit.getVisitType() == previousType) {
// visit type is not changed: do nothing
return;
}
EncounterService es = Context.getEncounterService();
VisitService vs = Context.getVisitService();
Patient patient = visit.getPatient();
Person person = Context.getUserContext().getAuthenticatedUser().getPerson();
Encounter encounter = new Encounter();
setTransferEncounter(visit, vs, es, encounter, patient, person, loginLocation, previousType, isTransferType);
}
use of org.openmrs.Person in project openmrs-module-pihcore by PIH.
the class UpdateProviderRetiredStatesBasedOnAssociatedUserAccounts method execute.
@Override
public void execute() {
UserService userService = Context.getUserService();
ProviderService providerService = Context.getProviderService();
Set<Person> personsToIncludeWhenRetiringProviderAccts = new HashSet<Person>();
Set<Person> personsToExcludeWhenRetiringProviderAccts = new HashSet<Person>();
for (User user : userService.getUsers(null, null, true)) {
// the user account has been retired over a month ago
if (user.isRetired() && user.getDateRetired() != null && user.getDateRetired().before(new DateTime().minusMonths(1).toDate())) {
personsToIncludeWhenRetiringProviderAccts.add(user.getPerson());
} else {
personsToExcludeWhenRetiringProviderAccts.add(user.getPerson());
/* if (!user.isRetired()) {
personsToMakeSureHaveActiveProviderAcct.add(user.getPerson());
}*/
}
}
personsToIncludeWhenRetiringProviderAccts.removeAll(personsToExcludeWhenRetiringProviderAccts);
// retire all provider accts associated with persons with retired user accts
for (Person person : personsToIncludeWhenRetiringProviderAccts) {
for (Provider provider : providerService.getProvidersByPerson(person)) {
if (!provider.isRetired()) {
providerService.retireProvider(provider, "user account retired over 1 month ago");
}
}
}
/* // check all persons to include and make sure, if they have provider accts, that at least one is unretired
for (Person person : personsToMakeSureHaveActiveProviderAcct) {
Collection<Provider> providers = providerService.getProvidersByPerson(person);
if (providers.size() == 1 && providers.iterator().next().isRetired()) {
providerService.unretireProvider(providers.iterator().next());
}
else if (providers.size() > 1) {
Provider mostRecentlyRetiredProvider = null;
Boolean noUnretiredProviders = true;
Iterator<Provider> i = providers.iterator();
while (i.hasNext()) {
Provider provider = i.next();
if (!provider.isRetired()) {
noUnretiredProviders = false;
break;
}
else if (mostRecentlyRetiredProvider == null || mostRecentlyRetiredProvider.getDateRetired().before(provider.getDateRetired())) {
mostRecentlyRetiredProvider = provider;
}
}
if (noUnretiredProviders) {
providerService.unretireProvider(mostRecentlyRetiredProvider);
}
}
}*/
}
use of org.openmrs.Person in project openmrs-core by openmrs.
the class ORUR01Handler method processNK1.
/**
* process an NK1 segment and add relationships if needed
*
* @param patient
* @param nk1
* @throws HL7Exception
* @should create a relationship from a NK1 segment
* @should not create a relationship if one exists
* @should create a person if the relative is not found
* @should fail if the coding system is not 99REL
* @should fail if the relationship identifier is formatted improperly
* @should fail if the relationship type is not found
*/
protected void processNK1(Patient patient, NK1 nk1) throws HL7Exception {
// guarantee we are working with our custom coding system
String relCodingSystem = nk1.getRelationship().getNameOfCodingSystem().getValue();
if (!relCodingSystem.equals(HL7Constants.HL7_LOCAL_RELATIONSHIP)) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.relationshipCoding", new Object[] { relCodingSystem }, null));
}
// get the relationship type identifier
String relIdentifier = nk1.getRelationship().getIdentifier().getValue();
// validate the format of the relationship identifier
if (!Pattern.matches("[0-9]+[AB]", relIdentifier)) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.relationshipType", new Object[] { relIdentifier }, null));
}
// get the type ID
Integer relTypeId;
try {
relTypeId = Integer.parseInt(relIdentifier.substring(0, relIdentifier.length() - 1));
} catch (NumberFormatException e) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.relationshipType", new Object[] { relIdentifier }, null));
}
// find the relationship type
RelationshipType relType = Context.getPersonService().getRelationshipType(relTypeId);
if (relType == null) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.relationshipTypeNotFound", new Object[] { relTypeId }, null));
}
// find the relative
Person relative = getRelative(nk1);
// determine if the patient is person A or B; the relIdentifier indicates
// the relative's side of the relationship, so the patient is the inverse
boolean patientIsPersonA = relIdentifier.endsWith("B");
boolean patientCanBeEitherPerson = relType.getbIsToA().equals(relType.getaIsToB());
// look at existing relationships to determine if a new one is needed
Set<Relationship> rels = new HashSet<>();
if (relative != null) {
if (patientCanBeEitherPerson || patientIsPersonA) {
rels.addAll(Context.getPersonService().getRelationships(patient, relative, relType));
}
if (patientCanBeEitherPerson || !patientIsPersonA) {
rels.addAll(Context.getPersonService().getRelationships(relative, patient, relType));
}
}
// create a relationship if none is found
if (rels.isEmpty()) {
// check the relative's existence
if (relative == null) {
// create one based on NK1 information
relative = Context.getHL7Service().createPersonFromNK1(nk1);
if (relative == null) {
throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.relativeNotCreated"));
}
}
// create the relationship
Relationship relation = new Relationship();
if (patientCanBeEitherPerson || patientIsPersonA) {
relation.setPersonA(patient);
relation.setPersonB(relative);
} else {
relation.setPersonA(relative);
relation.setPersonB(patient);
}
relation.setRelationshipType(relType);
Context.getPersonService().saveRelationship(relation);
}
}
use of org.openmrs.Person in project openmrs-core by openmrs.
the class HL7ServiceImpl method resolvePersonId.
/**
* @see org.openmrs.hl7.HL7Service#resolvePersonId(ca.uhn.hl7v2.model.v25.datatype.XCN)
*/
@Override
@Transactional(readOnly = true)
public Integer resolvePersonId(XCN xcn) throws HL7Exception {
String idNumber = xcn.getIDNumber().getValue();
String familyName = xcn.getFamilyName().getSurname().getValue();
String givenName = xcn.getGivenName().getValue();
if (idNumber != null && idNumber.length() > 0) {
try {
Person person = Context.getPersonService().getPerson(Integer.valueOf(idNumber));
return person.getPersonId();
} catch (Exception e) {
log.error("Invalid person ID '" + idNumber + "'", e);
return null;
}
} else {
List<Person> persons = Context.getPersonService().getPeople(givenName + " " + familyName, null);
if (persons.size() == 1) {
return persons.get(0).getPersonId();
} else if (persons.isEmpty()) {
log.error("Couldn't find a person named " + givenName + " " + familyName);
return null;
} else {
log.error("Found more than one person named " + givenName + " " + familyName);
return null;
}
}
}
use of org.openmrs.Person in project openmrs-core by openmrs.
the class PersonServiceImpl method getRelationshipMap.
/**
* @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;
}
Aggregations