use of org.opencastproject.adminui.usersettings.persistence.UserSettingDto 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();
}
}
}
use of org.opencastproject.adminui.usersettings.persistence.UserSettingDto 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);
}
use of org.opencastproject.adminui.usersettings.persistence.UserSettingDto 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();
}
}
use of org.opencastproject.adminui.usersettings.persistence.UserSettingDto 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();
}
}
}
use of org.opencastproject.adminui.usersettings.persistence.UserSettingDto in project opencast by opencast.
the class UserSettingsServiceTest method updateUserSettingInputNoIdExpectsSavedSetting.
@Test
public void updateUserSettingInputNoIdExpectsSavedSetting() throws UserSettingsServiceException {
long id = 7L;
String key = "newKey";
String value = "newValue";
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.createNamedQuery("UserSettings.findByKey")).andReturn(query);
EasyMock.expectLastCall();
EasyMock.expect(em.getTransaction()).andReturn(tx);
EasyMock.expect(em.find(UserSettingDto.class, id)).andReturn(userSettingDto);
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);
UserSetting result = userSettingsService.updateUserSetting(key, value, oldValue);
assertEquals(result.getId(), id);
assertEquals(result.getKey(), key);
assertEquals(result.getValue(), value);
}
Aggregations