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;
}
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);
}
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);
}
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();
}
}
}
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();
}
}
Aggregations