Search in sources :

Example 66 with User

use of org.openmrs.User in project openmrs-core by openmrs.

the class ORUR01Handler method createEncounter.

/**
 * This method does not call the database to create the encounter row. The encounter is only
 * created after all obs have been attached to it Creates an encounter pojo to be attached
 * later. This method does not create an encounterId
 *
 * @param msh
 * @param patient
 * @param pv1
 * @param orc
 * @return
 * @throws HL7Exception
 */
private Encounter createEncounter(MSH msh, Patient patient, PV1 pv1, ORC orc) throws HL7Exception {
    // the encounter we will return
    Encounter encounter;
    // look for the encounter id in PV1-19
    CX visitNumber = pv1.getVisitNumber();
    Integer encounterId = null;
    try {
        encounterId = Integer.valueOf(visitNumber.getIDNumber().getValue());
    } catch (NumberFormatException e) {
    // pass
    }
    // the database
    if (encounterId != null) {
        encounter = Context.getEncounterService().getEncounter(encounterId);
    } else {
        // if no encounter_id was passed in, this is a new
        // encounter, create the object
        encounter = new Encounter();
        Date encounterDate = getEncounterDate(pv1);
        Provider provider = getProvider(pv1);
        Location location = getLocation(pv1);
        Form form = getForm(msh);
        EncounterType encounterType = getEncounterType(msh, form);
        User enterer = getEnterer(orc);
        // Date dateEntered = getDateEntered(orc); // ignore this since we have no place in the data model to store it
        encounter.setEncounterDatetime(encounterDate);
        if (unknownRole == null) {
            unknownRole = Context.getEncounterService().getEncounterRoleByUuid(EncounterRole.UNKNOWN_ENCOUNTER_ROLE_UUID);
        }
        encounter.setProvider(unknownRole, provider);
        encounter.setPatient(patient);
        encounter.setLocation(location);
        encounter.setForm(form);
        encounter.setEncounterType(encounterType);
        encounter.setCreator(enterer);
        encounter.setDateCreated(new Date());
    }
    return encounter;
}
Also used : User(org.openmrs.User) CX(ca.uhn.hl7v2.model.v25.datatype.CX) Form(org.openmrs.Form) Encounter(org.openmrs.Encounter) EncounterType(org.openmrs.EncounterType) Date(java.util.Date) Provider(org.openmrs.Provider) Location(org.openmrs.Location)

Example 67 with User

use of org.openmrs.User in project openmrs-core by openmrs.

the class ORUR01Handler method getEnterer.

private User getEnterer(ORC orc) throws HL7Exception {
    XCN hl7Enterer = orc.getEnteredBy(0);
    Integer entererId = Context.getHL7Service().resolveUserId(hl7Enterer);
    if (entererId == null) {
        throw new HL7Exception(Context.getMessageSourceService().getMessage("ORUR01.error.UnresolvedEnterer"));
    }
    User enterer = new User();
    enterer.setUserId(entererId);
    return enterer;
}
Also used : XCN(ca.uhn.hl7v2.model.v25.datatype.XCN) User(org.openmrs.User) HL7Exception(ca.uhn.hl7v2.HL7Exception)

Example 68 with User

use of org.openmrs.User in project openmrs-core by openmrs.

the class HL7ServiceImpl method resolveUserId.

/**
 * @param xcn HL7 component of data type XCN (extended composite ID number and name for persons)
 *            (see HL7 2.5 manual Ch.2A.86)
 * @return Internal ID # of the specified user, or null if that user can't be found or is
 *         ambiguous
 */
@Override
@Transactional(readOnly = true)
public Integer resolveUserId(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 {
            Integer userId = Integer.valueOf(idNumber);
            User user = Context.getUserService().getUser(userId);
            return user.getUserId();
        } catch (Exception e) {
            log.error("Invalid user ID '" + idNumber + "'", e);
            return null;
        }
    } else {
        try {
            List<User> users = Context.getUserService().getUsersByName(givenName, familyName, true);
            if (users.size() == 1) {
                return users.get(0).getUserId();
            } else if (users.size() > 1) {
                // Return null if that user ambiguous
                log.error(getFindingUserErrorMessage(idNumber, familyName, givenName) + ": Found " + users.size() + " ambiguous users.");
                return null;
            } else {
                // legacy behavior is looking up by username
                StringBuilder username = new StringBuilder();
                if (familyName != null) {
                    username.append(familyName);
                }
                if (givenName != null) {
                    if (username.length() > 0) {
                        // separate names with a space
                        username.append(" ");
                    }
                    username.append(givenName);
                }
                User user = Context.getUserService().getUserByUsername(username.toString());
                if (user == null) {
                    log.error(getFindingUserErrorMessage(idNumber, familyName, givenName) + ": User not found");
                    return null;
                }
                return user.getUserId();
            }
        } catch (Exception e) {
            log.error(getFindingUserErrorMessage(idNumber, familyName, givenName), e);
            return null;
        }
    }
}
Also used : User(org.openmrs.User) URISyntaxException(java.net.URISyntaxException) DAOException(org.openmrs.api.db.DAOException) FileNotFoundException(java.io.FileNotFoundException) APIException(org.openmrs.api.APIException) HL7Exception(ca.uhn.hl7v2.HL7Exception) EncodingNotSupportedException(ca.uhn.hl7v2.parser.EncodingNotSupportedException) IOException(java.io.IOException) PatientIdentifierException(org.openmrs.api.PatientIdentifierException) ApplicationException(ca.uhn.hl7v2.app.ApplicationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 69 with User

use of org.openmrs.User in project openmrs-core by openmrs.

the class UserServiceImpl method changePassword.

/**
 * @see org.openmrs.api.UserService#changePassword(java.lang.String, java.lang.String)
 */
@Override
public void changePassword(String pw, String pw2) throws APIException {
    User user = Context.getAuthenticatedUser();
    changePassword(user, pw, pw2);
}
Also used : User(org.openmrs.User)

Example 70 with User

use of org.openmrs.User in project openmrs-core by openmrs.

the class MigrationHelper method importUsers.

/**
 * Takes XML like: &lt;something&gt; &lt;user date_changed="2001-03-06 08:46:53.0"
 * date_created="2001-03-06 08:46:53.0" username="hamish@mit.edu" first_name="Hamish"
 * last_name="Fraser" user_id="2001"/&gt; &lt;/something&gt; Returns the number of users added
 */
public static int importUsers(Document document) throws ParseException {
    int ret = 0;
    Random rand = new Random();
    UserService us = Context.getUserService();
    List<Node> toAdd = new ArrayList<>();
    findNodesNamed(document, "user", toAdd);
    for (Node node : toAdd) {
        Element e = (Element) node;
        String username = e.getAttribute("username");
        if (username == null || username.length() == 0) {
            throw new IllegalArgumentException("each <user /> element must define a user_name attribute");
        }
        if (us.getUserByUsername(username) != null) {
            continue;
        }
        User user = new User();
        user.setPerson(new Person());
        PersonName pn = new PersonName(e.getAttribute("first_name"), "", e.getAttribute("last_name"));
        user.addName(pn);
        user.setUsername(username);
        user.setDateCreated(parseDate(e.getAttribute("date_created")));
        user.setDateChanged(parseDate(e.getAttribute("date_changed")));
        // 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);
        }
        us.createUser(user, pass);
        ++ret;
    }
    return ret;
}
Also used : PersonName(org.openmrs.PersonName) User(org.openmrs.User) Random(java.util.Random) UserService(org.openmrs.api.UserService) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Person(org.openmrs.Person)

Aggregations

User (org.openmrs.User)201 Test (org.junit.Test)150 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)132 Date (java.util.Date)38 Person (org.openmrs.Person)33 Encounter (org.openmrs.Encounter)21 Patient (org.openmrs.Patient)18 PersonName (org.openmrs.PersonName)17 Role (org.openmrs.Role)13 GlobalProperty (org.openmrs.GlobalProperty)11 Location (org.openmrs.Location)11 ArrayList (java.util.ArrayList)10 EncounterType (org.openmrs.EncounterType)10 Locale (java.util.Locale)7 UserService (org.openmrs.api.UserService)7 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)7 BindException (org.springframework.validation.BindException)7 Errors (org.springframework.validation.Errors)7 EncounterRole (org.openmrs.EncounterRole)6 PatientIdentifier (org.openmrs.PatientIdentifier)6