Search in sources :

Example 1 with UserSettingsServiceException

use of org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException in project opencast by opencast.

the class UserSettingsService method addUserSetting.

/**
 * Create a new user setting key value pair.
 *
 * @param key
 *          The key to use for the current user setting.
 * @param value
 *          The value of the user setting.
 * @return A new user setting object
 * @throws UserSettingsServiceException
 */
public UserSetting addUserSetting(String key, String value) throws UserSettingsServiceException {
    EntityManager em = null;
    EntityTransaction tx = null;
    String orgId = "";
    String username = "";
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        orgId = securityService.getOrganization().getId();
        username = securityService.getUser().getUsername();
        UserSettingDto userSettingDto = new UserSettingDto();
        userSettingDto.setKey(key);
        userSettingDto.setValue(value);
        userSettingDto.setUsername(username);
        userSettingDto.setOrganization(orgId);
        em.persist(userSettingDto);
        tx.commit();
        return userSettingDto.toUserSetting();
    } catch (Exception e) {
        logger.error("Could not update user setting username '%s' org:'%s' key:'%s' value:'%s':%s", username, orgId, key, value, ExceptionUtils.getStackTrace(e));
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new UserSettingsServiceException(e);
    } finally {
        if (em != null) {
            em.close();
        }
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) UserSettingDto(org.opencastproject.adminui.usersettings.persistence.UserSettingDto) UserSettingsServiceException(org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException) UserSettingsServiceException(org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException)

Example 2 with UserSettingsServiceException

use of org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException in project opencast by opencast.

the class UserSettingsService method getUserSettingsTotal.

/**
 * @return Finds the total number of user settings for the current user.
 * @throws UserSettingsServiceException
 */
private int getUserSettingsTotal() throws UserSettingsServiceException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        String orgId = securityService.getOrganization().getId();
        String username = securityService.getUser().getUsername();
        Query q = em.createNamedQuery("UserSettings.countByUserName").setParameter("username", username).setParameter("org", orgId);
        Number countResult = (Number) q.getSingleResult();
        return countResult.intValue();
    } catch (Exception e) {
        logger.error("Could not count message signatures: %s", ExceptionUtils.getStackTrace(e));
        throw new UserSettingsServiceException(e);
    } finally {
        if (em != null) {
            em.close();
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) UserSettingsServiceException(org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException) UserSettingsServiceException(org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException)

Example 3 with UserSettingsServiceException

use of org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException in project opencast by opencast.

the class UserSettingsService method getUserSettingsByKey.

/**
 * Get all user settings based upon its key.
 * @param key The key to search for.
 * @return A {@link UserSettingDto} that matches the key.
 * @throws UserSettingsServiceException
 */
private List<UserSettingDto> getUserSettingsByKey(String key) throws UserSettingsServiceException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        String orgId = securityService.getOrganization().getId();
        String username = securityService.getUser().getUsername();
        logger.debug("Getting user settings for '%s' in org '%s'", username, orgId);
        Query q = em.createNamedQuery("UserSettings.findByKey").setParameter("key", key).setParameter("username", username).setParameter("org", orgId);
        List<UserSettingDto> result = q.getResultList();
        if (result.size() == 0) {
            logger.debug("Found no user settings.");
            return null;
        }
        return result;
    } catch (Exception e) {
        logger.error("Could not get user setting: {}", ExceptionUtils.getStackTrace(e));
        throw new UserSettingsServiceException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) UserSettingDto(org.opencastproject.adminui.usersettings.persistence.UserSettingDto) UserSettingsServiceException(org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException) UserSettingsServiceException(org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException)

Example 4 with UserSettingsServiceException

use of org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException in project opencast by opencast.

the class UserSettingsService method getUserSettings.

/**
 * @param offset
 *          The number of limits to page to.
 * @param limit
 *          The maximum number of settings to return.
 * @return Find all of the user settings for the current user.
 * @throws UserSettingsServiceException
 *           Thrown if there is a problem getting the user settings.
 */
private UserSettings getUserSettings(int limit, int offset) throws UserSettingsServiceException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        String orgId = securityService.getOrganization().getId();
        String username = securityService.getUser().getUsername();
        logger.debug("Getting user settings for '%s' in org '%s'", username, orgId);
        Query q = em.createNamedQuery("UserSettings.findByUserName").setParameter("username", username).setParameter("org", orgId).setMaxResults(limit).setFirstResult(offset);
        List<UserSettingDto> result = q.getResultList();
        if (result.size() == 0) {
            logger.debug("Found no user settings.");
        }
        UserSettings userSettings = new UserSettings();
        for (UserSettingDto userSettingsDto : result) {
            UserSetting userSetting = userSettingsDto.toUserSetting();
            logger.debug("Found user setting id: %d key: %s value: %s", userSetting.getId(), userSetting.getKey().toString(), userSetting.getValue().toString());
            userSettings.addUserSetting(userSetting);
        }
        return userSettings;
    } catch (Exception e) {
        logger.error("Could not get user settings: %s", ExceptionUtils.getStackTrace(e));
        throw new UserSettingsServiceException(e);
    } finally {
        if (em != null) {
            em.close();
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) UserSettingDto(org.opencastproject.adminui.usersettings.persistence.UserSettingDto) UserSettingsServiceException(org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException) UserSettingsServiceException(org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException)

Example 5 with UserSettingsServiceException

use of org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException in project opencast by opencast.

the class UserSettingsService method updateUserSetting.

/**
 * Update a user setting that currently exists using its key to find it.
 * @param key The key for the user setting.
 * @param value The new value to set for the user setting.
 * @return An updated {@link UserSetting}
 * @throws UserSettingsServiceException
 */
public UserSetting updateUserSetting(String key, String value, String oldValue) throws UserSettingsServiceException {
    UserSettingDto userSettingDto = null;
    List<UserSettingDto> userSettings = getUserSettingsByKey(key);
    for (UserSettingDto currentUserSetting : userSettings) {
        if (currentUserSetting.getKey().equalsIgnoreCase(key) && currentUserSetting.getValue().equalsIgnoreCase(oldValue)) {
            userSettingDto = currentUserSetting;
        }
    }
    if (userSettingDto == null) {
        throw new UserSettingsServiceException("Unable to find user setting with key " + key + " value " + value + " and old value " + oldValue);
    }
    return updateUserSetting(userSettingDto.getId(), key, value);
}
Also used : UserSettingDto(org.opencastproject.adminui.usersettings.persistence.UserSettingDto) UserSettingsServiceException(org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException)

Aggregations

UserSettingsServiceException (org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException)7 EntityManager (javax.persistence.EntityManager)6 UserSettingDto (org.opencastproject.adminui.usersettings.persistence.UserSettingDto)6 EntityTransaction (javax.persistence.EntityTransaction)3 Query (javax.persistence.Query)3