Skip to content

Commit

Permalink
SAK-50728 Mailsender optimize calls around users, roles, sections (sa…
Browse files Browse the repository at this point in the history
  • Loading branch information
ottenhoff authored Dec 3, 2024
1 parent 2efb65f commit e7f6951
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,8 @@ public String idFromReference(String reference) {

@Override
public boolean isRoleViewType(String id) {
if (id != null) {
String userId = Optional.ofNullable(m_storage.checkMapForId(id)).orElse(id);
User user = m_storage.getById(userId);
if (user != null) return ROLEVIEW_USER_TYPE.equals(user.getType());
}
return false;
if (id == null) return false;
return getOptionalUser(id).map(u -> ROLEVIEW_USER_TYPE.equals(u.getType())).orElse(false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
Expand All @@ -46,7 +44,6 @@
import org.sakaiproject.tool.api.ToolManager;
import org.sakaiproject.user.api.User;
import org.sakaiproject.user.api.UserDirectoryService;
import org.sakaiproject.user.api.UserNotDefinedException;
import org.sakaiproject.util.comparator.UserSortNameComparator;

import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -78,42 +75,35 @@ public List<EmailRole> getEmailRoles() throws GroupNotDefinedException
String realmId = externalLogic.getSiteRealmID();
AuthzGroup arole = authzGroupService.getAuthzGroup(realmId);

for (Iterator<?> i = arole.getRoles().iterator(); i.hasNext();)
{
Role r = (Role) i.next();
String rolename = r.getId();
if (includeRole(rolename)) {
String singular = null;
String plural = null;

EmailRole configRole = findConfigRole(realmId, rolename, configRoles);
// check first for an override from config
if (configRole != null)
{
singular = configRole.getRoleSingular();
plural = configRole.getRolePlural();
}
// default case
else
{
singular = rolename;
plural = rolename;
}
// create email role and add to list
EmailRole emailrole = null;
if (getGroupAwareRole().equals(rolename))
{
emailrole = new EmailRole(realmId, rolename, singular, plural, EmailRole.Type.ROLE,
true);
}
else
{
emailrole = new EmailRole(realmId, rolename, singular, plural, EmailRole.Type.ROLE);
}
theRoles.add(emailrole);
}
}
Collections.sort(theRoles, new EmailRoleComparator(EmailRoleComparator.SORT_BY.PLURAL));
for (Role r : arole.getRoles()) {
String rolename = r.getId();
if (includeRole(rolename)) {
String singular = null;
String plural = null;

EmailRole configRole = findConfigRole(realmId, rolename, configRoles);
// check first for an override from config
if (configRole != null) {
singular = configRole.getRoleSingular();
plural = configRole.getRolePlural();
}
// default case
else {
singular = rolename;
plural = rolename;
}
// create email role and add to list
EmailRole emailrole = null;
if (getGroupAwareRole().equals(rolename)) {
emailrole = new EmailRole(realmId, rolename, singular, plural, EmailRole.Type.ROLE,
true);
} else {
emailrole = new EmailRole(realmId, rolename, singular, plural, EmailRole.Type.ROLE);
}
theRoles.add(emailrole);
}
}
theRoles.sort(new EmailRoleComparator(EmailRoleComparator.SORT_BY.PLURAL));
return theRoles;
}

Expand All @@ -129,8 +119,6 @@ protected boolean includeRole(String rolename) {

/**
* Get the config roles defined in the tool configuration
*
* @return
*/
private List<EmailRole> getConfigRoles()
{
Expand Down Expand Up @@ -191,7 +179,7 @@ public List<EmailRole> getEmailGroups() throws IdUnusedException
}
}
}
Collections.sort(roles, new EmailRoleComparator(EmailRoleComparator.SORT_BY.PLURAL));
roles.sort(new EmailRoleComparator(EmailRoleComparator.SORT_BY.PLURAL));
return roles;
}

Expand Down Expand Up @@ -222,7 +210,7 @@ public List<EmailRole> getEmailSections() throws IdUnusedException
}
}
}
Collections.sort(roles, new EmailRoleComparator(EmailRoleComparator.SORT_BY.PLURAL));
roles.sort(new EmailRoleComparator(EmailRoleComparator.SORT_BY.PLURAL));
return roles;
}

Expand All @@ -241,21 +229,17 @@ public String getGroupAwareRole()
String realmId = externalLogic.getSiteRealmID();
AuthzGroup arole = authzGroupService.getAuthzGroup(realmId);

for (Iterator<?> i = arole.getRoles().iterator(); i.hasNext();)
{
Role r = (Role) i.next();
String rolename = r.getId();
for (int t = 0; t < gartokens.length; t++)
{
if (gartokens[t].trim().equals(rolename.trim()))
{
retval = rolename;
break;
}
}
if (retval != null)
break;
}
for (Role r : arole.getRoles()) {
String rolename = r.getId();
for (String gartoken : gartokens) {
if (gartoken.trim().equals(rolename.trim())) {
retval = rolename;
break;
}
}
if (retval != null)
break;
}
}
catch (GroupNotDefinedException e)
{
Expand Down Expand Up @@ -287,12 +271,11 @@ public String getGroupAwareRoleDefault()
return defaultRole;
}

public List<User> getUsers() throws IdUnusedException
@Override
public List<User> getUsers() throws IdUnusedException
{
ArrayList<User> users = new ArrayList<User>();
Set<String> userIds = getUserIds();
compileUsers(users, userIds);
return users;
return getSortedUsers(userIds);
}

protected Set<String> getUserIds() throws IdUnusedException
Expand All @@ -311,12 +294,11 @@ protected Set<String> getUserIds() throws IdUnusedException
*
* @see org.sakaiproject.mailsender.logic.ComposeLogic#getUsersByRole(String)
*/
public List<User> getUsersByRole(String role) throws IdUnusedException
@Override
public List<User> getUsersByRole(String role) throws IdUnusedException
{
ArrayList<User> users = new ArrayList<User>();
Set<String> userIds = getUserIdsByRole(role);
compileUsers(users, userIds);
return users;
return getSortedUsers(userIds);
}

protected Set<String> getUserIdsByRole(String role) throws IdUnusedException
Expand Down Expand Up @@ -355,12 +337,11 @@ public int countUsersByRole(String role)
*
* @see org.sakaiproject.mailsender.logic.ComposeLogic#getUsersByGroup(String)
*/
public List<User> getUsersByGroup(String groupId) throws IdUnusedException
@Override
public List<User> getUsersByGroup(String groupId) throws IdUnusedException
{
ArrayList<User> users = new ArrayList<User>();
Set<String> userIds = getUserIdsByGroup(groupId);
compileUsers(users, userIds);
return users;
return getSortedUsers(userIds);
}

protected Set<String> getUserIdsByGroup(String groupId) throws IdUnusedException
Expand Down Expand Up @@ -407,7 +388,7 @@ public int countUsersByGroup(String groupId)

/**
* Dependency injection method
*
*
* @param ss
*/
public void setSiteService(SiteService ss)
Expand Down Expand Up @@ -499,30 +480,17 @@ public void setIgnoreRoles(String ignoreRoles)
protected Site currentSite() throws IdUnusedException
{
String siteId = externalLogic.getSiteID();
Site currentSite = siteService.getSite(siteId);
return currentSite;
return siteService.getSite(siteId);
}

/**
* Compile a list of users based on user IDs. Does not include the current user.
*
* @param users
* @param userIds
* Compile a list of users based on user IDs. Invalid users are not included.
*/
private void compileUsers(ArrayList<User> users, Set<String> userIds)
private List<User> getSortedUsers(Set<String> userIds)
{
for (String userId : userIds)
{
try
{
users.add(userDirectoryService.getUser(userId));
}
catch (UserNotDefinedException e)
{
log.warn("Unable to retrieve user: " + userId);
}
}
Collections.sort(users, new UserSortNameComparator());
List<User> users = this.userDirectoryService.getUsers(userIds);
users.sort(new UserSortNameComparator());
return users;
}

/**
Expand Down
Loading

0 comments on commit e7f6951

Please sign in to comment.