@Test public void prepareDashboardStringBasedOnUserRolesTest() throws Exception { String dashboardConfigString = "{\"title\":\"Dashboard\",\"structure\":\"6-6\",\"rows\":[{\"columns\":[{\"styleClass\":\"col-md-6\",\"widgets\":[{\"type\":\"myTasks\",\"config\":{},\"title\":\"My Tasks\",\"titleTemplateUrl\":\"../src/templates/widget-title.html\",\"wid\":3}],\"cid\":5},{\"styleClass\":\"col-md-6\",\"widgets\":[{\"type\":\"newComplaints\",\"config\":{},\"title\":\"New Complaints\",\"titleTemplateUrl\":\"../src/templates/widget-title.html\",\"wid\":10},{\"type\":\"myComplaints\",\"config\":{},\"title\":\"My Complaints\",\"titleTemplateUrl\":\"../src/templates/widget-title.html\",\"wid\":9},{\"type\":\"myCases\",\"config\":{},\"title\":\"My Cases\",\"titleTemplateUrl\":\"../src/templates/widget-title.html\",\"wid\":4}],\"cid\":6}]}],\"titleTemplateUrl\":\"modules/dashboard/views/dashboard-title.client.view.html\"}"; String expectedDashboardConfigStringAfterRemovingWidgets = "{\"titleTemplateUrl\":\"modules/dashboard/views/dashboard-title.client.view.html\",\"title\":\"Dashboard\",\"rows\":[{\"columns\":[{\"styleClass\":\"col-md-6\",\"widgets\":[],\"cid\":5},{\"styleClass\":\"col-md-6\",\"widgets\":[],\"cid\":6}]}],\"structure\":\"6-6\"}"; AcmUser user = new AcmUser(); user.setUserId("marst"); Dashboard dashboard = new Dashboard(); dashboard.setDashboardConfig(dashboardConfigString); dashboard.setDashboardOwner(user); dashboard.setCollapsed(false); Dashboard expectedDashboard = new Dashboard(); expectedDashboard.setDashboardConfig(expectedDashboardConfigStringAfterRemovingWidgets); expectedDashboard.setDashboardOwner(user); expectedDashboard.setCollapsed(false); DashboardDto expectedDashboardDto = new DashboardDto(); expectedDashboardDto.setDashboardConfig(expectedDashboard.getDashboardConfig()); expectedDashboardDto.setCollapsed(false); expectedDashboardDto.setModule("DASHBOARD"); expectedDashboardDto.setUserId(user.getUserId()); Set<String> roles = new HashSet<>(Arrays.asList("ACM_MARST")); Widget news = new Widget(); news.setWidgetId(1201L); news.setWidgetName("news"); Widget teamWorkload = new Widget(); teamWorkload.setWidgetId(1212L); teamWorkload.setWidgetName("teamWorkload"); Widget casesByQueue = new Widget(); casesByQueue.setWidgetId(1209L); casesByQueue.setWidgetName("casesByQueue"); List<Widget> widgetsByUserRoles = new ArrayList<>(); widgetsByUserRoles.add(news); widgetsByUserRoles.add(teamWorkload); widgetsByUserRoles.add(casesByQueue); List<Widget> listOfDashboardWidgetOnly = new ArrayList<>(); listOfDashboardWidgetOnly.add(news); listOfDashboardWidgetOnly.add(teamWorkload); listOfDashboardWidgetOnly.add(casesByQueue); expect(mockUserDao.findByUserId(user.getUserId())).andReturn(user); expect(mockDashboardDao.getDashboardConfigForUserAndModuleName(user, "DASHBOARD")).andReturn(dashboard).anyTimes(); expect(mockUserRoleService.getUserRoles(user.getUserId())).andReturn(roles); expect(mockWidgetDao.getAllWidgetsByRoles(roles)).andReturn(widgetsByUserRoles); expect(mockDashboardPropertyReader.getDashboardWidgetsOnly()).andReturn(listOfDashboardWidgetOnly); expect(mockDashboardDao.setDashboardConfigForUserAndModule(isA(AcmUser.class), isA(DashboardDto.class), isA(String.class))) .andReturn(1); replayAll(); Dashboard result = unit.prepareDashboardStringBasedOnUserRoles(user.getUserId(), "DASHBOARD"); verifyAll(); assertNotNull(result); assertEquals(result.getDashboardConfig(), expectedDashboardConfigStringAfterRemovingWidgets); }
public Dashboard prepareDashboardStringBasedOnUserRoles(String userId, String moduleName) throws AcmObjectNotFoundException { AcmUser user = userDao.findByUserId(userId); Dashboard dashboard; try { dashboard = dashboardDao.getDashboardConfigForUserAndModuleName(user, moduleName); } catch (AcmObjectNotFoundException e) { throw e; } String dashboardModifiedString = dashboard.getDashboardConfig(); Set<String> roles = userRoleService.getUserRoles(userId); try { List<Widget> result = onlyUniqueValues(widgetDao.getAllWidgetsByRoles(roles)); List<Widget> listOfDashboardWidgetsOnly = dashboardPropertyReader.getDashboardWidgetsOnly(); List<Widget> dashboardWidgetsOnly = result.stream().filter(w -> listOfDashboardWidgetsOnly.contains(w)) .collect(Collectors.toList()); JSONObject dashboardJSONObject = new JSONObject(dashboard.getDashboardConfig()); dashboardModifiedString = removeNotAuthorizedWidgets(dashboardJSONObject, dashboardWidgetsOnly); } catch (AcmObjectNotFoundException e) { log.info("There are no widgets associated with roles of the user: {}", userId); } dashboard.setDashboardConfig(dashboardModifiedString); DashboardDto dashboardDto = prepareDashboardDto(dashboard, false, moduleName); int retval = setDashboardConfigForUserAndModule(user, dashboardDto, moduleName); if (retval != 1) { log.error("Unable to update dashboard config because dashboard for user: [{}] is not found", userId); throw new AcmObjectNotFoundException("dashboard", null, "Object not found", null); } return dashboardDao.getDashboardConfigForUserAndModuleName(user, moduleName); }
DashboardService { public Dashboard prepareDashboardStringBasedOnUserRoles(String userId, String moduleName) throws AcmObjectNotFoundException { AcmUser user = userDao.findByUserId(userId); Dashboard dashboard; try { dashboard = dashboardDao.getDashboardConfigForUserAndModuleName(user, moduleName); } catch (AcmObjectNotFoundException e) { throw e; } String dashboardModifiedString = dashboard.getDashboardConfig(); Set<String> roles = userRoleService.getUserRoles(userId); try { List<Widget> result = onlyUniqueValues(widgetDao.getAllWidgetsByRoles(roles)); List<Widget> listOfDashboardWidgetsOnly = dashboardPropertyReader.getDashboardWidgetsOnly(); List<Widget> dashboardWidgetsOnly = result.stream().filter(w -> listOfDashboardWidgetsOnly.contains(w)) .collect(Collectors.toList()); JSONObject dashboardJSONObject = new JSONObject(dashboard.getDashboardConfig()); dashboardModifiedString = removeNotAuthorizedWidgets(dashboardJSONObject, dashboardWidgetsOnly); } catch (AcmObjectNotFoundException e) { log.info("There are no widgets associated with roles of the user: {}", userId); } dashboard.setDashboardConfig(dashboardModifiedString); DashboardDto dashboardDto = prepareDashboardDto(dashboard, false, moduleName); int retval = setDashboardConfigForUserAndModule(user, dashboardDto, moduleName); if (retval != 1) { log.error("Unable to update dashboard config because dashboard for user: [{}] is not found", userId); throw new AcmObjectNotFoundException("dashboard", null, "Object not found", null); } return dashboardDao.getDashboardConfigForUserAndModuleName(user, moduleName); } }
DashboardService { public Dashboard prepareDashboardStringBasedOnUserRoles(String userId, String moduleName) throws AcmObjectNotFoundException { AcmUser user = userDao.findByUserId(userId); Dashboard dashboard; try { dashboard = dashboardDao.getDashboardConfigForUserAndModuleName(user, moduleName); } catch (AcmObjectNotFoundException e) { throw e; } String dashboardModifiedString = dashboard.getDashboardConfig(); Set<String> roles = userRoleService.getUserRoles(userId); try { List<Widget> result = onlyUniqueValues(widgetDao.getAllWidgetsByRoles(roles)); List<Widget> listOfDashboardWidgetsOnly = dashboardPropertyReader.getDashboardWidgetsOnly(); List<Widget> dashboardWidgetsOnly = result.stream().filter(w -> listOfDashboardWidgetsOnly.contains(w)) .collect(Collectors.toList()); JSONObject dashboardJSONObject = new JSONObject(dashboard.getDashboardConfig()); dashboardModifiedString = removeNotAuthorizedWidgets(dashboardJSONObject, dashboardWidgetsOnly); } catch (AcmObjectNotFoundException e) { log.info("There are no widgets associated with roles of the user: {}", userId); } dashboard.setDashboardConfig(dashboardModifiedString); DashboardDto dashboardDto = prepareDashboardDto(dashboard, false, moduleName); int retval = setDashboardConfigForUserAndModule(user, dashboardDto, moduleName); if (retval != 1) { log.error("Unable to update dashboard config because dashboard for user: [{}] is not found", userId); throw new AcmObjectNotFoundException("dashboard", null, "Object not found", null); } return dashboardDao.getDashboardConfigForUserAndModuleName(user, moduleName); }  }
DashboardService { public Dashboard prepareDashboardStringBasedOnUserRoles(String userId, String moduleName) throws AcmObjectNotFoundException { AcmUser user = userDao.findByUserId(userId); Dashboard dashboard; try { dashboard = dashboardDao.getDashboardConfigForUserAndModuleName(user, moduleName); } catch (AcmObjectNotFoundException e) { throw e; } String dashboardModifiedString = dashboard.getDashboardConfig(); Set<String> roles = userRoleService.getUserRoles(userId); try { List<Widget> result = onlyUniqueValues(widgetDao.getAllWidgetsByRoles(roles)); List<Widget> listOfDashboardWidgetsOnly = dashboardPropertyReader.getDashboardWidgetsOnly(); List<Widget> dashboardWidgetsOnly = result.stream().filter(w -> listOfDashboardWidgetsOnly.contains(w)) .collect(Collectors.toList()); JSONObject dashboardJSONObject = new JSONObject(dashboard.getDashboardConfig()); dashboardModifiedString = removeNotAuthorizedWidgets(dashboardJSONObject, dashboardWidgetsOnly); } catch (AcmObjectNotFoundException e) { log.info("There are no widgets associated with roles of the user: {}", userId); } dashboard.setDashboardConfig(dashboardModifiedString); DashboardDto dashboardDto = prepareDashboardDto(dashboard, false, moduleName); int retval = setDashboardConfigForUserAndModule(user, dashboardDto, moduleName); if (retval != 1) { log.error("Unable to update dashboard config because dashboard for user: [{}] is not found", userId); throw new AcmObjectNotFoundException("dashboard", null, "Object not found", null); } return dashboardDao.getDashboardConfigForUserAndModuleName(user, moduleName); }  static String removeWidgetsFromJson(JSONObject dashboardJson, Set<String> widgetNames); Dashboard getDashboardConfigForUserAndModuleName(AcmUser owner, String moduleName); int setDashboardConfigForUserAndModule(AcmUser user, DashboardDto updateDashboardDto, String moduleName); DashboardDto prepareDashboardDto(Dashboard dashboard, boolean inserted, String module); AcmUser getUserByUserId(String userId); Dashboard createDefaultModuleDashboard(AcmUser owner, String moduleName); Dashboard prepareDashboardStringBasedOnUserRoles(String userId, String moduleName); List<Widget> onlyUniqueValues(List<Widget> widgets); void raiseGetEvent(Authentication authentication, HttpSession session, List<RolesGroupByWidgetDto> rolesPerWidgets,
            boolean succeeded); List<RolesGroupByWidgetDto> addNotAuthorizedRolesPerWidget(List<RolesGroupByWidgetDto> rolesPerWidget); List<WidgetRoleName> getRolesByWidgetPaged(String widgetName, String sortBy, String sortDirection, Integer startRow,
            Integer maxRows,
            Boolean authorized, List<RolesGroupByWidgetDto> rolesGroupsPerWidget); List<WidgetRoleName> getRolesByWidget(String widgetName, String sortBy, String sortDirection, Integer startRow,
            Integer maxRows, String filterName, Boolean authorized, List<RolesGroupByWidgetDto> rolesGroupsPerWidget); List<WidgetRoleName> getRolesGroupsPaged(List<RolesGroupByWidgetDto> rolesGroupsPerWidget, String widgetName,
            String sortBy,
            String sortDirection, Integer startRow, Integer maxRows, Boolean authorized, String filterName); DashboardDao getDashboardDao(); void setDashboardDao(DashboardDao dashboardDao); UserDao getUserDao(); void setUserDao(UserDao userDao); WidgetDao getWidgetDao(); void setWidgetDao(WidgetDao widgetDao); DashboardPropertyReader getDashboardPropertyReader(); void setDashboardPropertyReader(DashboardPropertyReader dashboardPropertyReader); void setUserRoleService(AcmUserRoleService userRoleService); WidgetEventPublisher getEventPublisher(); void setEventPublisher(WidgetEventPublisher eventPublisher); DashboardConfig getDashboardConfig(); void setDashboardConfig(DashboardConfig dashboardConfig); }
DashboardService { public Dashboard prepareDashboardStringBasedOnUserRoles(String userId, String moduleName) throws AcmObjectNotFoundException { AcmUser user = userDao.findByUserId(userId); Dashboard dashboard; try { dashboard = dashboardDao.getDashboardConfigForUserAndModuleName(user, moduleName); } catch (AcmObjectNotFoundException e) { throw e; } String dashboardModifiedString = dashboard.getDashboardConfig(); Set<String> roles = userRoleService.getUserRoles(userId); try { List<Widget> result = onlyUniqueValues(widgetDao.getAllWidgetsByRoles(roles)); List<Widget> listOfDashboardWidgetsOnly = dashboardPropertyReader.getDashboardWidgetsOnly(); List<Widget> dashboardWidgetsOnly = result.stream().filter(w -> listOfDashboardWidgetsOnly.contains(w)) .collect(Collectors.toList()); JSONObject dashboardJSONObject = new JSONObject(dashboard.getDashboardConfig()); dashboardModifiedString = removeNotAuthorizedWidgets(dashboardJSONObject, dashboardWidgetsOnly); } catch (AcmObjectNotFoundException e) { log.info("There are no widgets associated with roles of the user: {}", userId); } dashboard.setDashboardConfig(dashboardModifiedString); DashboardDto dashboardDto = prepareDashboardDto(dashboard, false, moduleName); int retval = setDashboardConfigForUserAndModule(user, dashboardDto, moduleName); if (retval != 1) { log.error("Unable to update dashboard config because dashboard for user: [{}] is not found", userId); throw new AcmObjectNotFoundException("dashboard", null, "Object not found", null); } return dashboardDao.getDashboardConfigForUserAndModuleName(user, moduleName); }  static String removeWidgetsFromJson(JSONObject dashboardJson, Set<String> widgetNames); Dashboard getDashboardConfigForUserAndModuleName(AcmUser owner, String moduleName); int setDashboardConfigForUserAndModule(AcmUser user, DashboardDto updateDashboardDto, String moduleName); DashboardDto prepareDashboardDto(Dashboard dashboard, boolean inserted, String module); AcmUser getUserByUserId(String userId); Dashboard createDefaultModuleDashboard(AcmUser owner, String moduleName); Dashboard prepareDashboardStringBasedOnUserRoles(String userId, String moduleName); List<Widget> onlyUniqueValues(List<Widget> widgets); void raiseGetEvent(Authentication authentication, HttpSession session, List<RolesGroupByWidgetDto> rolesPerWidgets,
            boolean succeeded); List<RolesGroupByWidgetDto> addNotAuthorizedRolesPerWidget(List<RolesGroupByWidgetDto> rolesPerWidget); List<WidgetRoleName> getRolesByWidgetPaged(String widgetName, String sortBy, String sortDirection, Integer startRow,
            Integer maxRows,
            Boolean authorized, List<RolesGroupByWidgetDto> rolesGroupsPerWidget); List<WidgetRoleName> getRolesByWidget(String widgetName, String sortBy, String sortDirection, Integer startRow,
            Integer maxRows, String filterName, Boolean authorized, List<RolesGroupByWidgetDto> rolesGroupsPerWidget); List<WidgetRoleName> getRolesGroupsPaged(List<RolesGroupByWidgetDto> rolesGroupsPerWidget, String widgetName,
            String sortBy,
            String sortDirection, Integer startRow, Integer maxRows, Boolean authorized, String filterName); DashboardDao getDashboardDao(); void setDashboardDao(DashboardDao dashboardDao); UserDao getUserDao(); void setUserDao(UserDao userDao); WidgetDao getWidgetDao(); void setWidgetDao(WidgetDao widgetDao); DashboardPropertyReader getDashboardPropertyReader(); void setDashboardPropertyReader(DashboardPropertyReader dashboardPropertyReader); void setUserRoleService(AcmUserRoleService userRoleService); WidgetEventPublisher getEventPublisher(); void setEventPublisher(WidgetEventPublisher eventPublisher); DashboardConfig getDashboardConfig(); void setDashboardConfig(DashboardConfig dashboardConfig);  }