Fix sonar issues in UsageListController 48/82648/1
authorburdziak <olaf.burdziakowski@nokia.com>
Tue, 19 Mar 2019 09:06:38 +0000 (10:06 +0100)
committerburdziak <olaf.burdziakowski@nokia.com>
Tue, 19 Mar 2019 09:06:38 +0000 (10:06 +0100)
Change-Id: Id7bc48db31e9b8eb7a6fed1d8edcfdc34f60c655
Issue-ID: PORTAL-523
Signed-off-by: burdziak <olaf.burdziakowski@nokia.com>
ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/UsageListController.java

index 681702b..04c1f2b 100644 (file)
@@ -65,6 +65,31 @@ public class UsageListController extends RestrictedBaseController {
 
        private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UsageListController.class);
 
+       private void addUsers2jsonArray(JSONArray ja,HashMap activeUsers,String httpSessionId)
+  {
+    List<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
+               for (UserRowBean userRowBean : rows)
+                 ja.put(userRowBean2json(userRowBean,httpSessionId));
+  }
+
+       private JSONObject userRowBean2json(UserRowBean userRowBean,String httpSessionId)
+  {
+    JSONObject jo = new JSONObject();
+    jo.put("id", userRowBean.getId());
+    jo.put("lastName", userRowBean.getLastName());
+    jo.put("email", userRowBean.getEmail());
+    jo.put("lastAccess", userRowBean.getLastAccess());
+    jo.put("remaining", userRowBean.getRemaining());
+    jo.put("sessionId", userRowBean.getSessionId());
+    if (!httpSessionId.equals(userRowBean.getSessionId())) {
+      jo.put("delete", "yes");
+    } else {
+      jo.put("delete", "no");
+    }
+
+    return jo;
+  }
+
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @RequestMapping(value = { "/usage_list" }, method = RequestMethod.GET)
        public ModelAndView usageList(HttpServletRequest request) {
@@ -76,24 +101,10 @@ public class UsageListController extends RestrictedBaseController {
                        activeUsers.put(httpSession.getId(), httpSession);
                        httpSession.getServletContext().setAttribute("activeUsers", activeUsers);
                }
-               List<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
+
                JSONArray ja = new JSONArray();
                try {
-                       for (UserRowBean userRowBean : rows) {
-                               JSONObject jo = new JSONObject();
-                               jo.put("id", userRowBean.getId());
-                               jo.put("lastName", userRowBean.getLastName());
-                               jo.put("email", userRowBean.getEmail());
-                               jo.put("lastAccess", userRowBean.getLastAccess());
-                               jo.put("remaining", userRowBean.getRemaining());
-                               jo.put("sessionId", userRowBean.getSessionId());
-                               if (!(httpSession.getId().equals(userRowBean.getSessionId()))) {
-                                       jo.put("delete", "yes");
-                               } else {
-                                       jo.put("delete", "no");
-                               }
-                               ja.put(jo);
-                       }
+      addUsers2jsonArray(ja,activeUsers,httpSession.getId());
                } catch (Exception e) {
                        logger.error(EELFLoggerDelegate.errorLogger, "usageList failed", e);
                }
@@ -113,24 +124,9 @@ public class UsageListController extends RestrictedBaseController {
                        activeUsers.put(httpSession.getId(), httpSession);
                        httpSession.getServletContext().setAttribute("activeUsers", activeUsers);
                }
-               List<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
                JSONArray ja = new JSONArray();
                try {
-                       for (UserRowBean userRowBean : rows) {
-                               JSONObject jo = new JSONObject();
-                               jo.put("id", userRowBean.getId());
-                               jo.put("lastName", userRowBean.getLastName());
-                               jo.put("email", userRowBean.getEmail());
-                               jo.put("lastAccess", userRowBean.getLastAccess());
-                               jo.put("remaining", userRowBean.getRemaining());
-                               jo.put("sessionId", userRowBean.getSessionId());
-                               if (!(httpSession.getId().equals(userRowBean.getSessionId()))) {
-                                       jo.put("delete", "yes");
-                               } else {
-                                       jo.put("delete", "no");
-                               }
-                               ja.put(jo);
-                       }
+      addUsers2jsonArray(ja,activeUsers,httpSession.getId());
                } catch (Exception e) {
                        logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed", e);
                }
@@ -154,24 +150,9 @@ public class UsageListController extends RestrictedBaseController {
                UsageUtils.getActiveUsersAfterDelete(activeUsers, data);
 
                HttpSession httpSession = request.getSession();
-               List<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
                JSONArray ja = new JSONArray();
                try {
-                       for (UserRowBean userRowBean : rows) {
-                               JSONObject jo = new JSONObject();
-                               jo.put("id", userRowBean.getId());
-                               jo.put("lastName", userRowBean.getLastName());
-                               jo.put("email", userRowBean.getEmail());
-                               jo.put("lastAccess", userRowBean.getLastAccess());
-                               jo.put("remaining", userRowBean.getRemaining());
-                               jo.put("sessionId", userRowBean.getSessionId());
-                               if (!(httpSession.getId().equals(userRowBean.getSessionId()))) {
-                                       jo.put("delete", "yes");
-                               } else {
-                                       jo.put("delete", "no");
-                               }
-                               ja.put(jo);
-                       }
+      addUsers2jsonArray(ja,activeUsers,httpSession.getId());
                } catch (Exception e) {
                        logger.error(EELFLoggerDelegate.errorLogger, "removeSession failed", e);
                }