Search in sources :

Example 1 with PatientService

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;
}
Also used : Obs(org.openmrs.Obs) PatientService(org.openmrs.api.PatientService) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) Encounter(org.openmrs.Encounter) Person(org.openmrs.Person) EncounterService(org.openmrs.api.EncounterService) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with PatientService

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:&lt;user last name&gt;,&lt;user first
 * name&gt;,&lt;relationship type name&gt;,&lt;patient identifier type name&gt;,&lt;identifier&gt; 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;
}
Also used : PersonName(org.openmrs.PersonName) User(org.openmrs.User) UserService(org.openmrs.api.UserService) PersonService(org.openmrs.api.PersonService) ArrayList(java.util.ArrayList) RelationshipType(org.openmrs.RelationshipType) PatientIdentifier(org.openmrs.PatientIdentifier) Role(org.openmrs.Role) Random(java.util.Random) PatientService(org.openmrs.api.PatientService) Relationship(org.openmrs.Relationship) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) Person(org.openmrs.Person) PatientIdentifierType(org.openmrs.PatientIdentifierType)

Example 3 with PatientService

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);
}
Also used : PaperRecordDAO(org.openmrs.module.paperrecord.db.PaperRecordDAO) AdministrationService(org.openmrs.api.AdministrationService) PatientService(org.openmrs.api.PatientService) PaperRecordProperties(org.openmrs.module.paperrecord.PaperRecordProperties) PaperRecordServiceImpl(org.openmrs.module.paperrecord.PaperRecordServiceImpl) Before(org.junit.Before)

Example 4 with PatientService

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);
    }
}
Also used : LocationService(org.openmrs.api.LocationService) VisitService(org.openmrs.api.VisitService) PatientService(org.openmrs.api.PatientService) FormService(org.openmrs.api.FormService) IdentifierSourceService(org.openmrs.module.idgen.service.IdentifierSourceService) Module(org.openmrs.module.Module) ConceptService(org.openmrs.api.ConceptService) EncounterService(org.openmrs.api.EncounterService) MetadataMappingService(org.openmrs.module.metadatamapping.api.MetadataMappingService)

Example 5 with PatientService

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);
}
Also used : PatientService(org.openmrs.api.PatientService) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

PatientService (org.openmrs.api.PatientService)8 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 PatientIdentifier (org.openmrs.PatientIdentifier)3 PatientIdentifierType (org.openmrs.PatientIdentifierType)3 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)3 Patient (org.openmrs.Patient)2 Person (org.openmrs.Person)2 EncounterService (org.openmrs.api.EncounterService)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Random (java.util.Random)1 Before (org.junit.Before)1 Cohort (org.openmrs.Cohort)1 CohortMembership (org.openmrs.CohortMembership)1 Encounter (org.openmrs.Encounter)1 Obs (org.openmrs.Obs)1 PatientProgram (org.openmrs.PatientProgram)1 PatientState (org.openmrs.PatientState)1