diff --git a/api/src/main/java/org/openmrs/util/CurrentUsers.java b/api/src/main/java/org/openmrs/util/CurrentUsers.java new file mode 100644 index 000000000000..1fdb161024a7 --- /dev/null +++ b/api/src/main/java/org/openmrs/util/CurrentUsers.java @@ -0,0 +1,69 @@ +package org.openmrs.util; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import javax.servlet.http.HttpSession; + +public class CurrentUsers { + + + /** + * Initialize the current users list. + * + * @param servletContext + */ + + public static Map init(ServletContext servletContext){ + Map currentUserMap = Collections.synchronizedMap(new TreeMap()); + servletContext.setAtrribute(WebConstants.CURRENT_USERS,currentUserMap); + return currentUserMap; + } + + /** + * Get the current list of map of users stored in the session + * + * @param httpSession the current session + * @return map of users logged in + */ + private static Map getCurrentUsers(HttpSession httpSession){ + + Map currentUsers = (Map ) httpSession.getServletContext().getAttribute(WebConstants.CURRENT_USERS); + if(currentUsers == null) { + + currentUsers = init(httpSession.getServletContext()); + } + + return currentUsers; + } + + /** + * Get sorted user names list. + * + * @param httpSession + * @return sorted user names + */ + + public static List getCurrentUsernames(HttpSession httpSession){ + + Map currentUsers = getCurrentUsers(httpSession); + + List userNames = new ArrayList(); + synchronized(currentUsers){ + + for(String value:currentUsers.values()) { + + userNames.add(value); + } + + } + + Collections.sort(userNames); + return userNames; + } + + +}