private static void setPreferenceList(String name, Collection values) throws Exception { PreferencesEdit prefsEdit = null; String userId = M_sm.getCurrentSessionUserId(); try { prefsEdit = M_ps.edit(userId); } catch (IdUnusedException e) { prefsEdit = M_ps.add(userId); } try { ResourcePropertiesEdit props = prefsEdit.getPropertiesEdit(PREFS_KEY); if (values == null) { props.removeProperty(name); } else { List existing = props.getPropertyList(name); Iterator it = values.iterator(); while (it.hasNext()) { String value = (String) it.next(); if (existing == null || !existing.contains(value)) props.addPropertyToList(name, value.toString()); } } } catch (Exception e) { if (prefsEdit != null) M_ps.cancel(prefsEdit); M_ps.cancel(prefsEdit); throw e; } M_ps.commit(prefsEdit); }
@Override public void setDefaultPrivacyState(String userId, String visibility) { if (userId == null) { log.warn("Cannot set priavacy status for a null userId"); return; } if (visibility == null) { visibility = PrivacyManager.VISIBLE; } PreferencesEdit editPref; try { editPref = preferencesService.edit(userId); ResourcePropertiesEdit props = editPref.getPropertiesEdit(PRIVACY_PREFS); props.addProperty(PrivacyManager.DEFAULT_PRIVACY_KEY, visibility); preferencesService.commit(editPref); } catch (PermissionException e) { log.warn( "You do not have the appropriate permissions to edit preferences for user: "******". " + e.getMessage()); } catch (InUseException e) { log.warn( "Preferences for user: "******" are currently being edited. " + e.getMessage()); } catch (IdUnusedException e) { try { editPref = preferencesService.add(userId); ResourcePropertiesEdit props = editPref.getPropertiesEdit(PRIVACY_PREFS); props.addProperty(PrivacyManager.DEFAULT_PRIVACY_KEY, visibility); preferencesService.commit(editPref); } catch (PermissionException e1) { // TODO Auto-generated catch block log.warn( "You do not have the appropriate permissions to edit preferences for user: "******". " + e1.getMessage()); } catch (IdUsedException e1) { log.warn( "No preferences for user: "******" found intially, attempted to add new preferences. " + e1.getMessage()); } } }
private static void clearPreferenceList(String name) throws Exception { PreferencesEdit prefsEdit = null; try { prefsEdit = M_ps.edit(M_sm.getCurrentSessionUserId()); ResourcePropertiesEdit props = prefsEdit.getPropertiesEdit(PREFS_KEY); props.removeProperty(name); } catch (Exception e) { M_ps.cancel(prefsEdit); throw e; } M_ps.commit(prefsEdit); }
/** * Return current user locale. * * @return user's Locale object */ private Locale getCurrentUserLocale() { Locale loc = null; try { // check if locale is requested for specific user String userId = M_sm.getCurrentSessionUserId(); if (userId != null) { Preferences prefs = M_ps.getPreferences(userId); ResourceProperties locProps = prefs.getProperties(ResourceLoader.APPLICATION_ID); String localeString = locProps.getProperty(ResourceLoader.LOCALE_KEY); // Parse user locale preference if set if (localeString != null) { String[] locValues = localeString.split("_"); if (locValues.length > 1) // language, country loc = new Locale(locValues[0], locValues[1]); else if (locValues.length == 1) // language loc = new Locale(locValues[0]); } if (loc == null) loc = Locale.getDefault(); } else { loc = (Locale) M_sm.getCurrentSession() .getAttribute(ResourceLoader.LOCALE_KEY + M_sm.getCurrentSessionUserId()); } } catch (NullPointerException e) { loc = Locale.getDefault(); } return loc; }
protected Locale getUserLocale(String userId) { Locale loc = preferencesService.getLocale(userId); // the user has no preference set - get the system default if (loc == null) { loc = Locale.getDefault(); } return loc; }
private static void setPreferenceString(String name, String value) throws Exception { PreferencesEdit prefsEdit = null; String userId = M_sm.getCurrentSessionUserId(); try { prefsEdit = M_ps.edit(userId); } catch (IdUnusedException e) { prefsEdit = M_ps.add(userId); } try { ResourcePropertiesEdit props = prefsEdit.getPropertiesEdit(PREFS_KEY); if (value == null) { props.removeProperty(name); } else { props.addProperty(name, value.toString()); } } catch (Exception e) { if (prefsEdit != null) M_ps.cancel(prefsEdit); throw e; } M_ps.commit(prefsEdit); }
@Override public String getDefaultPrivacyState(String userId) { String privacy = null; if (userId != null) { Preferences prefs = preferencesService.getPreferences(userId); ResourceProperties props = prefs.getProperties(PRIVACY_PREFS); privacy = props.getProperty(PrivacyManager.DEFAULT_PRIVACY_KEY); } if (privacy == null) { // default privacy is visible privacy = PrivacyManagerImpl.VISIBLE; } return privacy; }
/** * Get the current user preference list value. First attempt Preferences, then defaults from * sakai.properties. * * @param name The property name. * @return The preference list value or null if not set. */ private static List getPreferenceList(String name) { Preferences prefs = M_ps.getPreferences(M_sm.getCurrentSessionUserId()); ResourceProperties rp = prefs.getProperties(PREFS_KEY); List l = rp.getPropertyList(name); return l; }
/** * Get the current user preference value. First attempt Preferences, then defaults from * sakai.properties. * * @param name The property name. * @return The preference value or null if not set. */ private static String getPreferenceString(String name) { Preferences prefs = M_ps.getPreferences(M_sm.getCurrentSessionUserId()); ResourceProperties rp = prefs.getProperties(PREFS_KEY); String value = rp.getProperty(name); return value; }