Skip to content

Commit

Permalink
SAK-44466 Ensure we get a non null locale from preferences (sakaiproj…
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianfish authored Oct 15, 2020
1 parent aaba9ea commit 33cd5d2
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,8 @@ public String getReviewReportRedirectUrl(String contentId, String assignmentRef,
data.put(VIEWER_PERMISSIONS, viewerPermissionsOverride);
}

// Check user preference for locale
// If user has no preference set - get the system default
Locale locale = Optional.ofNullable(preferencesService.getLocale(userId))
.orElse(Locale.getDefault());

// Set locale, getLanguage removes locale region
data.put("locale", locale.getLanguage());
data.put("locale", preferencesService.getLocale(userId).getLanguage());

HashMap<String, Object> response = makeHttpCall("GET",
getNormalizedServiceUrl() + "submissions/" + item.getExternalId() + "/viewer-url",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public RenderedTemplate getRenderedTemplate(String key, Locale locale, Map<Strin
public RenderedTemplate getRenderedTemplateForUser(String key, String userReference, Map<String, String> replacementValues) {
log.debug("getRenderedTemplateForUser(" + key + ", " +userReference);
String userId = developerHelperService.getUserIdFromRef(userReference);
Locale loc = getUserLocale(userId);
Locale loc = preferencesService.getLocale(userId);
return getRenderedTemplate(key,loc,replacementValues);
}

Expand Down Expand Up @@ -274,18 +274,6 @@ public void updateTemplate(EmailTemplate template) {
log.info("updated template: " + template.getId());
}

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;
}


protected String processText(String text, Map<String, String> values, String templateName) {
return TextTemplateLogicUtils.processTextTemplate(text, values, templateName);
}
Expand Down Expand Up @@ -336,7 +324,7 @@ public Map<EmailTemplateLocaleUsers, RenderedTemplate> getRenderedTemplates(
for (int i = 0; i < userReferences.size(); i++) {
String userReference = userReferences.get(i);
String userId = developerHelperService.getUserIdFromRef(userReference);
Locale loc = getUserLocale(userId);
Locale loc = preferencesService.getLocale(userId);
//have we found this locale?
if (! foundLocales.contains(loc)) {
//create a new EmailTemplateLocalUser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,14 +751,7 @@ public Object doInTransaction(TransactionStatus status) {
* Returns the user locale
*/
private Locale getSelectedLocale() {

Locale loc = preferencesService.getLocale(userDirectoryService.getCurrentUser().getId());
if (loc != null)
{
return loc;
} else {
return Locale.getDefault();
}
return preferencesService.getLocale(userDirectoryService.getCurrentUser().getId());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,9 @@ public interface PreferencesService extends EntityProducer
* The PreferencesEdit object to remove.
*/
void remove(PreferencesEdit edit);



/**
* Get user's preferred locale (or null if not set)
* Get user's preferred locale (or the default locale if not set)
* @param userId
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,7 @@ protected BasePreferences findPreferences(String id)

return prefs;
}



/**
** Get user's preferred locale (or null if not set)
***/
Expand All @@ -450,15 +449,17 @@ public Locale getLocale(String userId)
String localeString = locProps.getProperty(Preferences.FIELD_LOCALE);

// Parse user locale preference if set
if (localeString != null)
{
if (localeString != null) {
String[] locValues = localeString.split("_");
if (locValues.length > 2)
if (locValues.length > 2) {
loc = new Locale(locValues[0], locValues[1], locValues[2]); // language, country, variant
else if (locValues.length == 2)
} else if (locValues.length == 2) {
loc = new Locale(locValues[0], locValues[1]); // language, country
else if (locValues.length == 1)
} else if (locValues.length == 1) {
loc = new Locale(locValues[0]); // just language
}
} else {
loc = Locale.getDefault();
}

return loc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ else if (validationLogic.isTokenExpired(account))
String creatorId = entry.getKey();
try {
User creator = userDirectoryService.getUser(creatorId);
Locale locale = getUserLocale(creatorId);
Locale locale = preferencesService.getLocale(creatorId);
List<String> users = entry.getValue();
StringBuilder userText = new StringBuilder();
for (int i = 0; i < users.size(); i++) {
Expand Down Expand Up @@ -282,24 +282,4 @@ private Map<String, List<String>> buildAddedMap(List<String> oldAccounts) {

return ret;
}

protected Locale getUserLocale(String userId) {
Locale loc = preferencesService.getLocale(userId);
//the user has no preference set - get the system default
if (loc == null ) {
String lang = System.getProperty("user.language");
String region = System.getProperty("user.region");

if (region != null) {
log.debug("getting system locale for: " + lang + "_" + region);
loc = new Locale(lang,region);
} else {
log.debug("getting system locale for: " + lang );
loc = new Locale(lang);
}
}

return loc;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@

package org.sakaiproject.tool.assessment.ui.listener.delivery;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private void assessmentSubmittedHelper (Map<String,
// Format dates
DateFormat dfIn = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
DateFormat dfIn2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
DateFormat dfOut = new SimpleDateFormat("yyyy-MMM-dd hh:mm a", getUserLocale(user.getId()));
DateFormat dfOut = new SimpleDateFormat("yyyy-MMM-dd hh:mm a", preferencesService.getLocale(user.getId()));
String formattedDueDate = (pubAssFac.getDueDate() == null) ? "" : dfOut.format(pubAssFac.getDueDate());
Date submissionDate = null;
String inSubmissionDateStr = (notificationValues.get("submissionDate") == null) ? "" : notificationValues.get("submissionDate").toString();
Expand Down Expand Up @@ -423,16 +423,6 @@ private List<String> getHeaders (RenderedTem
return headers;
}

private Locale getUserLocale (final String userId) {
Locale locale = preferencesService.getLocale(userId);

if(locale == null) {
locale = Locale.getDefault();
}

return locale;
}

@Setter
private ServerConfigurationService serverConfigurationService;

Expand Down

0 comments on commit 33cd5d2

Please sign in to comment.