From c58ff1cc532b267c901d7f573bc1091695d6b70d Mon Sep 17 00:00:00 2001 From: HerbertYiga Date: Mon, 1 Mar 2021 22:33:56 +0300 Subject: [PATCH] RA-552:Adding the View Logged in Users functionality to core --- .../java/org/openmrs/util/CurrentUsers.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 api/src/main/java/org/openmrs/util/CurrentUsers.java 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..ba9b37e3c367 --- /dev/null +++ b/api/src/main/java/org/openmrs/util/CurrentUsers.java @@ -0,0 +1,67 @@ +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; + } + + +}