use of org.openmrs.api.PatientService in project openmrs-core by openmrs.
the class ObsServiceImpl method getObservations.
/**
* This implementation queries the obs table comparing the given <code>searchString</code> with
* the patient's identifier, encounterId, and obsId
*
* @see org.openmrs.api.ObsService#getObservations(java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public List<Obs> getObservations(String searchString) {
// search on patient identifier
PatientService ps = Context.getPatientService();
List<Patient> patients = ps.getPatients(searchString);
List<Person> persons = new ArrayList<>(patients);
// try to search on encounterId
EncounterService es = Context.getEncounterService();
List<Encounter> encounters = new ArrayList<>();
try {
Encounter e = es.getEncounter(Integer.valueOf(searchString));
if (e != null) {
encounters.add(e);
}
} catch (NumberFormatException e) {
// pass
}
List<Obs> returnList = new ArrayList<>();
if (!encounters.isEmpty() || !persons.isEmpty()) {
returnList = Context.getObsService().getObservations(persons, encounters, null, null, null, null, null, null, null, null, null, false);
}
// try to search on obsId
try {
Obs o = getObs(Integer.valueOf(searchString));
if (o != null) {
returnList.add(o);
}
} catch (NumberFormatException e) {
// pass
}
return returnList;
}
use of org.openmrs.api.PatientService in project openmrs-core by openmrs.
the class MigrationHelper method importRelationships.
/**
* Takes a list of Strings of the format RELATIONSHIP:<user last name>,<user first
* name>,<relationship type name>,<patient identifier type name>,<identifier> so if user hfraser
* if the cardiologist of the patient with patient_id 8039 in PIH's old emr, then:
* RELATIONSHIP:hfraser,Cardiologist,HIV-EMRV1,8039 (the "RELATIONSHIP:" is not actually
* necessary. Anything before and including the first : will be dropped If autoCreateUsers is
* true, and no user exists with the given username, one will be created. If autoAddRole is
* true, then whenever a user is auto-created, if a role exists with the same name as
* relationshipType.name, then the user will be added to that role
*/
public static int importRelationships(Collection<String> relationships, boolean autoCreateUsers, boolean autoAddRole) {
PatientService ps = Context.getPatientService();
UserService us = Context.getUserService();
PersonService personService = Context.getPersonService();
List<Relationship> relsToAdd = new ArrayList<>();
Random rand = new Random();
for (String s : relationships) {
if (s.contains(":")) {
s = s.substring(s.indexOf(":") + 1);
}
String[] ss = s.split(",");
if (ss.length < 5) {
throw new IllegalArgumentException("The line '" + s + "' is in the wrong format");
}
String userLastName = ss[0];
String userFirstName = ss[1];
String username = (userFirstName + userLastName).replaceAll(" ", "");
String relationshipType = ss[2];
String identifierType = ss[3];
String identifier = ss[4];
User user = null;
{
// first try looking for non-voided users
List<User> users = us.getUsersByName(userFirstName, userLastName, false);
if (users.size() == 1) {
user = users.get(0);
} else if (users.size() > 1) {
throw new IllegalArgumentException("Found " + users.size() + " users named '" + userLastName + ", " + userFirstName + "'");
}
}
if (user == null) {
// next try looking for voided users
List<User> users = us.getUsersByName(userFirstName, userLastName, false);
if (users.size() == 1) {
user = users.get(0);
} else if (users.size() > 1) {
throw new IllegalArgumentException("Found " + users.size() + " voided users named '" + userLastName + ", " + userFirstName + "'");
}
}
if (user == null && autoCreateUsers) {
user = new User();
user.setPerson(new Person());
PersonName pn = new PersonName(userFirstName, "", userLastName);
user.addName(pn);
user.setUsername(username);
// Generate a temporary password: 8-12 random characters
String pass;
{
int length = rand.nextInt(4) + 8;
char[] password = new char[length];
for (int x = 0; x < length; x++) {
int randDecimalAsciiVal = rand.nextInt(93) + 33;
password[x] = (char) randDecimalAsciiVal;
}
pass = new String(password);
}
if (autoAddRole) {
Role role = us.getRole(relationshipType);
if (role != null) {
user.addRole(role);
}
}
us.createUser(user, pass);
}
if (user == null) {
throw new IllegalArgumentException("Can't find user '" + userLastName + ", " + userFirstName + "'");
}
Person person = personService.getPerson(user.getUserId());
RelationshipType relationship = personService.getRelationshipTypeByName(relationshipType);
PatientIdentifierType pit = ps.getPatientIdentifierTypeByName(identifierType);
List<PatientIdentifier> found = ps.getPatientIdentifiers(identifier, Collections.singletonList(pit), null, null, null);
if (found.size() != 1) {
throw new IllegalArgumentException("Found " + found.size() + " patients with identifier '" + identifier + "' of type " + identifierType);
}
Person relative = personService.getPerson(found.get(0).getPatient().getPatientId());
Relationship rel = new Relationship();
rel.setPersonA(person);
rel.setRelationshipType(relationship);
rel.setPersonB(relative);
relsToAdd.add(rel);
}
int addedSoFar = 0;
for (Relationship rel : relsToAdd) {
personService.saveRelationship(rel);
++addedSoFar;
}
return addedSoFar;
}
use of org.openmrs.api.PatientService in project openmrs-module-mirebalais by PIH.
the class PaperRecordServiceIT method setUp.
@Before
public void setUp() {
paperRecordService = new PaperRecordServiceImpl();
patientService = mock(PatientService.class);
administrationService = mock(AdministrationService.class);
paperRecordProperties = mock(PaperRecordProperties.class);
paperRecordDAO = mock(PaperRecordDAO.class);
((PaperRecordServiceImpl) paperRecordService).setIdentifierSourceService(identifierSourceService);
((PaperRecordServiceImpl) paperRecordService).setPatientService(patientService);
((PaperRecordServiceImpl) paperRecordService).setPaperRecordProperties(paperRecordProperties);
((PaperRecordServiceImpl) paperRecordService).setPaperRecordDAO(paperRecordDAO);
// so we handle the hack in PaperRecordServiceImpl where internal methods are fetched via Context.getService
mockStatic(Context.class);
when(Context.getService(PaperRecordService.class)).thenReturn(paperRecordService);
}
use of org.openmrs.api.PatientService in project openmrs-module-pihcore by PIH.
the class PihCoreActivator method started.
// TODO test
@Override
public void started() {
try {
MetadataMappingService metadataMappingService = Context.getService(MetadataMappingService.class);
PatientService patientService = Context.getPatientService();
FormService formService = Context.getFormService();
LocationService locationService = Context.getLocationService();
EncounterService encounterService = Context.getEncounterService();
VisitService visitService = Context.getVisitService();
IdentifierSourceService identifierSourceService = Context.getService(IdentifierSourceService.class);
ConceptService conceptService = Context.getService(ConceptService.class);
if (config == null) {
// hack to allow injecting a mock config for testing
// currently only one of these
config = Context.getRegisteredComponents(Config.class).get(0);
}
setDispositionConfig(config);
installMetadataBundles(config);
setGlobalProperties(config);
setExtraIdentifierTypes(metadataMappingService, patientService, config);
MergeActionsSetup.registerMergeActions();
LocationTagSetup.setupLocationTags(locationService, config);
HtmlFormSetup.setupHtmlFormEntryTagHandlers();
MetadataMappingsSetup.setupGlobalMetadataMappings(metadataMappingService, locationService, encounterService, visitService);
MetadataMappingsSetup.setupPrimaryIdentifierTypeBasedOnCountry(metadataMappingService, patientService, config);
MetadataMappingsSetup.setupFormMetadataMappings(metadataMappingService);
PatientIdentifierSetup.setupIdentifierGeneratorsIfNecessary(identifierSourceService, locationService, config);
PacIntegrationSetup.setup(config);
AttachmentsSetup.migrateAttachmentsConceptsIfNecessary(conceptService);
// RetireProvidersSetup.setupRetireProvidersTask();
} catch (Exception e) {
Module mod = ModuleFactory.getModuleById("pihcore");
ModuleFactory.stopModule(mod);
throw new RuntimeException("failed to setup the required modules", e);
}
}
use of org.openmrs.api.PatientService in project openmrs-core by openmrs.
the class ContextTest method getService_shouldReturnTheSameObjectWhenCalledMultipleTimesForTheSameClass.
/**
* Prevents regression after patch from #2174:
* "Prevent duplicate proxies and AOP in context services"
*
* @see Context#getService(Class)
*/
@Test
public void getService_shouldReturnTheSameObjectWhenCalledMultipleTimesForTheSameClass() {
PatientService ps1 = Context.getService(PatientService.class);
PatientService ps2 = Context.getService(PatientService.class);
Assert.assertTrue(ps1 == ps2);
}
Aggregations