Search in sources :

Example 6 with UserSettingsServiceException

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

the class UserSettingsService method deleteUserSetting.

/**
 * Delete a user setting by using a unique id to find it.
 *
 * @param id
 *          The unique id for the user setting.
 * @throws UserSettingsServiceException
 */
public void deleteUserSetting(long id) throws UserSettingsServiceException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        UserSettingDto userSettingsDto = em.find(UserSettingDto.class, id);
        tx = em.getTransaction();
        tx.begin();
        em.remove(userSettingsDto);
        tx.commit();
    } catch (Exception e) {
        logger.error("Could not delete user setting '%d': %s", id, 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 7 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 unique id to find it.
 * @param id The id for the user setting.
 * @param key The key for the user setting.
 * @param value The value for the user setting.
 * @return The updated {@link UserSetting}.
 * @throws UserSettingsServiceException
 */
public UserSetting updateUserSetting(long id, String key, String value) throws UserSettingsServiceException {
    EntityManager em = null;
    EntityTransaction tx = null;
    String orgId = "";
    String username = "";
    logger.debug("Updating user setting id: %d key: %s value: %s", id, key, value);
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        orgId = securityService.getOrganization().getId();
        username = securityService.getUser().getUsername();
        UserSettingDto userSettingDto = em.find(UserSettingDto.class, id);
        em.persist(userSettingDto);
        userSettingDto.setKey(key);
        userSettingDto.setValue(value);
        tx.commit();
        return userSettingDto.toUserSetting();
    } catch (Exception e) {
        logger.error("Could not update user setting username '%s' org:'%s' id:'%d' key:'%s' value:'%s':\n%s", username, orgId, id, 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)

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