Search in sources :

Example 1 with UserSettingDto

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

the class UserSettingsServiceTest method createUserSettingsList.

private List<UserSettingDto> createUserSettingsList(int settingCount) {
    LinkedList<UserSettingDto> userSettings = new LinkedList<UserSettingDto>();
    for (int i = 0; i < settingCount; i++) {
        UserSettingDto userSettingDto = new UserSettingDto(i, KEY_PREFIX + i, VALUE_PREFIX + i, USER_NAME, ORG);
        userSettings.add(userSettingDto);
    }
    return userSettings;
}
Also used : UserSettingDto(org.opencastproject.adminui.usersettings.persistence.UserSettingDto) LinkedList(java.util.LinkedList)

Example 2 with UserSettingDto

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

the class UserSettingsServiceTest method addUserSettingInputNormalValuesExpectsSavedSetting.

@Test
public void addUserSettingInputNormalValuesExpectsSavedSetting() throws UserSettingsServiceException {
    String key = "newKey";
    String value = "newValue";
    Capture<UserSettingDto> userSettingDto = new Capture<UserSettingDto>();
    EntityTransaction tx = EasyMock.createNiceMock(EntityTransaction.class);
    EasyMock.replay(tx);
    EntityManager em = EasyMock.createNiceMock(EntityManager.class);
    EasyMock.expect(em.getTransaction()).andReturn(tx);
    em.persist(EasyMock.capture(userSettingDto));
    EasyMock.expectLastCall();
    EasyMock.replay(em);
    EntityManagerFactory emf = EasyMock.createNiceMock(EntityManagerFactory.class);
    EasyMock.expect(emf.createEntityManager()).andReturn(em);
    EasyMock.replay(emf);
    UserSettingsService userSettingsService = new UserSettingsService();
    userSettingsService.setSecurityService(securityService);
    userSettingsService.setEntityManagerFactory(emf);
    userSettingsService.setUserDirectoryService(userDirectoryService);
    userSettingsService.addUserSetting(key, value);
    assertEquals(userSettingDto.getValues().get(0).getKey(), key);
    assertEquals(userSettingDto.getValues().get(0).getValue(), value);
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) UserSettingDto(org.opencastproject.adminui.usersettings.persistence.UserSettingDto) Capture(org.easymock.Capture) Test(org.junit.Test)

Example 3 with UserSettingDto

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

the class UserSettingsServiceTest method deleteUserSettingInputNoIdExpectsSavedSetting.

@Test
public void deleteUserSettingInputNoIdExpectsSavedSetting() throws UserSettingsServiceException {
    long id = 8L;
    String key = "newKey";
    String oldValue = "oldValue";
    UserSettingDto userSettingDto = new UserSettingDto(id, key, oldValue, securityService.getUser().getUsername(), securityService.getOrganization().getId());
    LinkedList<UserSettingDto> userSettingDtos = new LinkedList<UserSettingDto>();
    userSettingDtos.add(userSettingDto);
    EntityTransaction tx = EasyMock.createNiceMock(EntityTransaction.class);
    EasyMock.replay(tx);
    Query query = EasyMock.createNiceMock(Query.class);
    EasyMock.expect(query.setParameter("key", key)).andReturn(query);
    EasyMock.expect(query.setParameter("username", securityService.getUser().getUsername())).andReturn(query);
    EasyMock.expect(query.setParameter("org", securityService.getOrganization().getId())).andReturn(query);
    EasyMock.expect(query.getResultList()).andReturn(userSettingDtos);
    EasyMock.replay(query);
    EntityManager em = EasyMock.createNiceMock(EntityManager.class);
    EasyMock.expect(em.getTransaction()).andReturn(tx);
    EasyMock.expect(em.find(UserSettingDto.class, id)).andReturn(userSettingDto);
    em.remove(userSettingDto);
    EasyMock.expectLastCall();
    EasyMock.replay(em);
    EntityManagerFactory emf = EasyMock.createNiceMock(EntityManagerFactory.class);
    EasyMock.expect(emf.createEntityManager()).andReturn(em).anyTimes();
    EasyMock.replay(emf);
    UserSettingsService userSettingsService = new UserSettingsService();
    userSettingsService.setSecurityService(securityService);
    userSettingsService.setEntityManagerFactory(emf);
    userSettingsService.setUserDirectoryService(userDirectoryService);
    userSettingsService.deleteUserSetting(id);
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) EntityManagerFactory(javax.persistence.EntityManagerFactory) UserSettingDto(org.opencastproject.adminui.usersettings.persistence.UserSettingDto) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with UserSettingDto

use of org.opencastproject.adminui.usersettings.persistence.UserSettingDto 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 5 with UserSettingDto

use of org.opencastproject.adminui.usersettings.persistence.UserSettingDto 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)

Aggregations

UserSettingDto (org.opencastproject.adminui.usersettings.persistence.UserSettingDto)10 EntityManager (javax.persistence.EntityManager)8 EntityTransaction (javax.persistence.EntityTransaction)6 UserSettingsServiceException (org.opencastproject.adminui.usersettings.persistence.UserSettingsServiceException)6 Query (javax.persistence.Query)4 LinkedList (java.util.LinkedList)3 EntityManagerFactory (javax.persistence.EntityManagerFactory)3 Test (org.junit.Test)3 Capture (org.easymock.Capture)1