Search in sources :

Example 1 with DAOException

use of org.openmrs.api.db.DAOException in project openmrs-core by openmrs.

the class HibernateSerializedObjectDAO method convertSerializedObject.

/**
 * @see SerializedObjectDAO#convertSerializedObject(Class, SerializedObject)
 */
@Override
@SuppressWarnings("unchecked")
public <T extends OpenmrsObject> T convertSerializedObject(Class<T> clazz, SerializedObject serializedObject) throws DAOException {
    if (serializedObject == null) {
        return null;
    }
    OpenmrsSerializer serializer = getSerializer(serializedObject);
    T obj;
    try {
        Class<?> subtype = Context.loadClass(serializedObject.getSubtype());
        obj = (T) serializer.deserialize(serializedObject.getSerializedData(), subtype);
    } catch (Exception e) {
        ExceptionUtil.rethrowAPIAuthenticationException(e);
        throw new DAOException("Unable to deserialize object: " + serializedObject, e);
    }
    if (obj == null) {
        // it's probably impossible to reach this code branch
        throw new DAOException("Unable to deserialize object: " + serializedObject);
    }
    obj.setId(serializedObject.getId());
    obj.setUuid(serializedObject.getUuid());
    return obj;
}
Also used : DAOException(org.openmrs.api.db.DAOException) SerializationException(org.openmrs.serialization.SerializationException) DAOException(org.openmrs.api.db.DAOException) OpenmrsSerializer(org.openmrs.serialization.OpenmrsSerializer)

Example 2 with DAOException

use of org.openmrs.api.db.DAOException in project openmrs-core by openmrs.

the class HibernateConceptDAO method getDefaultConceptMapType.

/**
 * @see org.openmrs.api.db.ConceptDAO#getDefaultConceptMapType()
 */
@Override
public ConceptMapType getDefaultConceptMapType() throws DAOException {
    FlushMode previousFlushMode = sessionFactory.getCurrentSession().getFlushMode();
    sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);
    try {
        // Defaults to same-as if the gp is not set.
        String defaultConceptMapType = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GP_DEFAULT_CONCEPT_MAP_TYPE);
        if (defaultConceptMapType == null) {
            throw new DAOException("The default concept map type is not set. You need to set the '" + OpenmrsConstants.GP_DEFAULT_CONCEPT_MAP_TYPE + "' global property.");
        }
        ConceptMapType conceptMapType = getConceptMapTypeByName(defaultConceptMapType);
        if (conceptMapType == null) {
            throw new DAOException("The default concept map type (name: " + defaultConceptMapType + ") does not exist! You need to set the '" + OpenmrsConstants.GP_DEFAULT_CONCEPT_MAP_TYPE + "' global property.");
        }
        return conceptMapType;
    } finally {
        sessionFactory.getCurrentSession().setFlushMode(previousFlushMode);
    }
}
Also used : DAOException(org.openmrs.api.db.DAOException) ConceptMapType(org.openmrs.ConceptMapType) FlushMode(org.hibernate.FlushMode)

Example 3 with DAOException

use of org.openmrs.api.db.DAOException in project openmrs-core by openmrs.

the class HibernateConceptDAO method saveConceptStopWord.

/**
 * @see org.openmrs.api.db.ConceptDAO#saveConceptStopWord(org.openmrs.ConceptStopWord)
 */
@Override
public ConceptStopWord saveConceptStopWord(ConceptStopWord conceptStopWord) throws DAOException {
    if (conceptStopWord != null) {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ConceptStopWord.class);
        criteria.add(Restrictions.eq("value", conceptStopWord.getValue()));
        criteria.add(Restrictions.eq("locale", conceptStopWord.getLocale()));
        List<ConceptStopWord> stopWordList = criteria.list();
        if (!stopWordList.isEmpty()) {
            throw new DAOException("Duplicate ConceptStopWord Entry");
        }
        sessionFactory.getCurrentSession().saveOrUpdate(conceptStopWord);
    }
    return conceptStopWord;
}
Also used : DAOException(org.openmrs.api.db.DAOException) ConceptStopWord(org.openmrs.ConceptStopWord) Criteria(org.hibernate.Criteria)

Example 4 with DAOException

use of org.openmrs.api.db.DAOException in project openmrs-core by openmrs.

the class HibernateUserDAO method updateUserPassword.

/**
 * @param newHashedPassword
 * @param salt
 * @param userId
 * @param date
 * @param userId2
 */
private void updateUserPassword(String newHashedPassword, String salt, Integer changedBy, Date dateChanged, Integer userIdToChange) {
    User changeForUser = getUser(userIdToChange);
    if (changeForUser == null) {
        throw new DAOException("Couldn't find user to set password for userId=" + userIdToChange);
    }
    User changedByUser = getUser(changedBy);
    LoginCredential credentials = getLoginCredential(changeForUser);
    credentials.setUserId(userIdToChange);
    credentials.setHashedPassword(newHashedPassword);
    credentials.setSalt(salt);
    credentials.setChangedBy(changedByUser);
    credentials.setDateChanged(dateChanged);
    credentials.setUuid(changeForUser.getUuid());
    sessionFactory.getCurrentSession().merge(credentials);
    // reset lockout
    changeForUser.setUserProperty(OpenmrsConstants.USER_PROPERTY_LOCKOUT_TIMESTAMP, "");
    changeForUser.setUserProperty(OpenmrsConstants.USER_PROPERTY_LOGIN_ATTEMPTS, "0");
    saveUser(changeForUser, null);
}
Also used : DAOException(org.openmrs.api.db.DAOException) User(org.openmrs.User) LoginCredential(org.openmrs.api.db.LoginCredential)

Example 5 with DAOException

use of org.openmrs.api.db.DAOException in project openmrs-core by openmrs.

the class HibernateUserDAO method changeQuestionAnswer.

/**
 * @see org.openmrs.api.UserService#changeQuestionAnswer(java.lang.String, java.lang.String,
 *      java.lang.String)
 */
@Override
public void changeQuestionAnswer(String pw, String question, String answer) throws DAOException {
    User u = Context.getAuthenticatedUser();
    LoginCredential credentials = getLoginCredential(u);
    if (!credentials.checkPassword(pw)) {
        log.error("Passwords don't match");
        throw new DAOException("Passwords don't match");
    }
    changeQuestionAnswer(u, question, answer);
}
Also used : DAOException(org.openmrs.api.db.DAOException) User(org.openmrs.User) LoginCredential(org.openmrs.api.db.LoginCredential)

Aggregations

DAOException (org.openmrs.api.db.DAOException)11 User (org.openmrs.User)3 LoginCredential (org.openmrs.api.db.LoginCredential)3 Date (java.util.Date)2 Criteria (org.hibernate.Criteria)2 ConceptMapType (org.openmrs.ConceptMapType)2 SerializationException (org.openmrs.serialization.SerializationException)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 FlushMode (org.hibernate.FlushMode)1 Auditable (org.openmrs.Auditable)1 ConceptStopWord (org.openmrs.ConceptStopWord)1 Drug (org.openmrs.Drug)1 OpenmrsData (org.openmrs.OpenmrsData)1 OpenmrsMetadata (org.openmrs.OpenmrsMetadata)1 OpenmrsObject (org.openmrs.OpenmrsObject)1 SerializedObject (org.openmrs.api.db.SerializedObject)1