@Test public final void testHandleCancel() { try { testSetAuthorityManager(); testSetTargetManager(); testSetGroupsController(); HttpServletRequest request = new MockHttpServletRequest(); HttpServletResponse response = new MockHttpServletResponse(); AddMembersCommand command = new AddMembersCommand(); BindException errors = new BindException(command, "AddMembersCommand"); bindEditorContext(request, 15001L); command.setActionCmd(null); long[] oids = {4000L, 15002L}; command.setMemberOids(oids); List<AddMembersController.MemberSelection> selections = (List<AddMembersController.MemberSelection>)request.getSession().getAttribute(AddMembersCommand.SESSION_SELECTIONS); assertNull(selections); ModelAndView mav = testInstance.handle(request, response, command, errors); assertNotNull(mav); assertEquals(mav.getViewName(), "group-add-members"); selections = (List<AddMembersController.MemberSelection>)request.getSession().getAttribute(AddMembersCommand.SESSION_SELECTIONS); assertNotNull(selections); assertTrue(selections.size() == 2); assertEquals(selections.get(0).getOid(), new Long(4000)); assertEquals(selections.get(1).getOid(), new Long(15002)); command = new AddMembersCommand(); errors = new BindException(command, "AddMembersCommand"); bindEditorContext(request, 15001L); command.setActionCmd(AddMembersCommand.ACTION_CANCEL); mav = testInstance.handle(request, response, command, errors); assertNotNull(mav); assertEquals(mav.getViewName(), "groups"); selections = (List<AddMembersController.MemberSelection>)request.getSession().getAttribute(AddMembersCommand.SESSION_SELECTIONS); assertNull(selections); } catch(Exception e) { fail(e.getMessage()); } }
@Override protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object comm, BindException errors) throws Exception { AddMembersCommand command = (AddMembersCommand) comm; if( AddMembersCommand.ACTION_ADD_MEMBERS.equals(command.getActionCmd())) { TargetGroup group = getEditorContext(request).getTargetGroup(); long[] memberOids = command.getMemberOids(); for(int i=0;i<memberOids.length;i++) { long nmo = memberOids[i]; addSelection(request, nmo); } Set<Long> ancestorOids = targetManager.getAncestorOids(group); List<MemberSelection> newMembers = getSelections(request); if(newMembers != null && newMembers.size() > 0 ) { Iterator<MemberSelection> it = newMembers.iterator(); while(it.hasNext()) { MemberSelection newMember = it.next(); if(ancestorOids.contains(newMember)) { String name = newMember.getName(); errors.reject("groups.error.loops", new Object[] { name }, "Adding one of these members would cause an inclusion loop"); } else if(group.getOid() != null && newMember.equals(group.getOid())) { String name = newMember.getName(); errors.reject("groups.error.selfref", new Object[] { name }, "Cannot create self referencing groups"); } else if( targetManager.isDuplicateMember(group, newMember.getOid())) { String name = newMember.getName(); errors.reject("groups.errors.duplicate", new Object[] { name }, "Already a member of this group"); } } } if(errors.hasErrors()) { return doSearch(request, response, comm, errors); } else { if(newMembers != null && newMembers.size() > 0 && authorityManager.hasPrivilege(group, Privilege.ADD_TARGET_TO_GROUP)) { Iterator<MemberSelection> it = newMembers.iterator(); while(it.hasNext()) { MemberSelection newMember = it.next(); GroupMemberDTO dto = targetManager.createGroupMemberDTO(group, newMember.getOid()); group.getNewChildren().add(dto); } } clearSelections(request); Tab membersTab = groupsController.getTabConfig().getTabByID("MEMBERS"); TabbedModelAndView tmav = membersTab.getTabHandler().preProcessNextTab(groupsController, membersTab, request, response, command, errors); tmav.getTabStatus().setCurrentTab(membersTab); return tmav; } } else if( AddMembersCommand.ACTION_CANCEL.equals(command.getActionCmd())) { clearSelections(request); Tab membersTab = groupsController.getTabConfig().getTabByID("MEMBERS"); TabbedModelAndView tmav = membersTab.getTabHandler().preProcessNextTab(groupsController, membersTab, request, response, command, errors); tmav.getTabStatus().setCurrentTab(membersTab); return tmav; } else if( AddMembersCommand.ACTION_REMOVE.equals(command.getActionCmd())) { getSelections(request).remove(command.getMemberIndex()); return doSearch(request, response, comm, errors); } else { long[] memberOids = command.getMemberOids(); for(int i=0;i<memberOids.length;i++) { long nmo = memberOids[i]; addSelection(request, nmo); } return doSearch(request, response, comm, errors); } }
AddMembersController extends AbstractCommandController { @Override protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object comm, BindException errors) throws Exception { AddMembersCommand command = (AddMembersCommand) comm; if( AddMembersCommand.ACTION_ADD_MEMBERS.equals(command.getActionCmd())) { TargetGroup group = getEditorContext(request).getTargetGroup(); long[] memberOids = command.getMemberOids(); for(int i=0;i<memberOids.length;i++) { long nmo = memberOids[i]; addSelection(request, nmo); } Set<Long> ancestorOids = targetManager.getAncestorOids(group); List<MemberSelection> newMembers = getSelections(request); if(newMembers != null && newMembers.size() > 0 ) { Iterator<MemberSelection> it = newMembers.iterator(); while(it.hasNext()) { MemberSelection newMember = it.next(); if(ancestorOids.contains(newMember)) { String name = newMember.getName(); errors.reject("groups.error.loops", new Object[] { name }, "Adding one of these members would cause an inclusion loop"); } else if(group.getOid() != null && newMember.equals(group.getOid())) { String name = newMember.getName(); errors.reject("groups.error.selfref", new Object[] { name }, "Cannot create self referencing groups"); } else if( targetManager.isDuplicateMember(group, newMember.getOid())) { String name = newMember.getName(); errors.reject("groups.errors.duplicate", new Object[] { name }, "Already a member of this group"); } } } if(errors.hasErrors()) { return doSearch(request, response, comm, errors); } else { if(newMembers != null && newMembers.size() > 0 && authorityManager.hasPrivilege(group, Privilege.ADD_TARGET_TO_GROUP)) { Iterator<MemberSelection> it = newMembers.iterator(); while(it.hasNext()) { MemberSelection newMember = it.next(); GroupMemberDTO dto = targetManager.createGroupMemberDTO(group, newMember.getOid()); group.getNewChildren().add(dto); } } clearSelections(request); Tab membersTab = groupsController.getTabConfig().getTabByID("MEMBERS"); TabbedModelAndView tmav = membersTab.getTabHandler().preProcessNextTab(groupsController, membersTab, request, response, command, errors); tmav.getTabStatus().setCurrentTab(membersTab); return tmav; } } else if( AddMembersCommand.ACTION_CANCEL.equals(command.getActionCmd())) { clearSelections(request); Tab membersTab = groupsController.getTabConfig().getTabByID("MEMBERS"); TabbedModelAndView tmav = membersTab.getTabHandler().preProcessNextTab(groupsController, membersTab, request, response, command, errors); tmav.getTabStatus().setCurrentTab(membersTab); return tmav; } else if( AddMembersCommand.ACTION_REMOVE.equals(command.getActionCmd())) { getSelections(request).remove(command.getMemberIndex()); return doSearch(request, response, comm, errors); } else { long[] memberOids = command.getMemberOids(); for(int i=0;i<memberOids.length;i++) { long nmo = memberOids[i]; addSelection(request, nmo); } return doSearch(request, response, comm, errors); } } }
AddMembersController extends AbstractCommandController { @Override protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object comm, BindException errors) throws Exception { AddMembersCommand command = (AddMembersCommand) comm; if( AddMembersCommand.ACTION_ADD_MEMBERS.equals(command.getActionCmd())) { TargetGroup group = getEditorContext(request).getTargetGroup(); long[] memberOids = command.getMemberOids(); for(int i=0;i<memberOids.length;i++) { long nmo = memberOids[i]; addSelection(request, nmo); } Set<Long> ancestorOids = targetManager.getAncestorOids(group); List<MemberSelection> newMembers = getSelections(request); if(newMembers != null && newMembers.size() > 0 ) { Iterator<MemberSelection> it = newMembers.iterator(); while(it.hasNext()) { MemberSelection newMember = it.next(); if(ancestorOids.contains(newMember)) { String name = newMember.getName(); errors.reject("groups.error.loops", new Object[] { name }, "Adding one of these members would cause an inclusion loop"); } else if(group.getOid() != null && newMember.equals(group.getOid())) { String name = newMember.getName(); errors.reject("groups.error.selfref", new Object[] { name }, "Cannot create self referencing groups"); } else if( targetManager.isDuplicateMember(group, newMember.getOid())) { String name = newMember.getName(); errors.reject("groups.errors.duplicate", new Object[] { name }, "Already a member of this group"); } } } if(errors.hasErrors()) { return doSearch(request, response, comm, errors); } else { if(newMembers != null && newMembers.size() > 0 && authorityManager.hasPrivilege(group, Privilege.ADD_TARGET_TO_GROUP)) { Iterator<MemberSelection> it = newMembers.iterator(); while(it.hasNext()) { MemberSelection newMember = it.next(); GroupMemberDTO dto = targetManager.createGroupMemberDTO(group, newMember.getOid()); group.getNewChildren().add(dto); } } clearSelections(request); Tab membersTab = groupsController.getTabConfig().getTabByID("MEMBERS"); TabbedModelAndView tmav = membersTab.getTabHandler().preProcessNextTab(groupsController, membersTab, request, response, command, errors); tmav.getTabStatus().setCurrentTab(membersTab); return tmav; } } else if( AddMembersCommand.ACTION_CANCEL.equals(command.getActionCmd())) { clearSelections(request); Tab membersTab = groupsController.getTabConfig().getTabByID("MEMBERS"); TabbedModelAndView tmav = membersTab.getTabHandler().preProcessNextTab(groupsController, membersTab, request, response, command, errors); tmav.getTabStatus().setCurrentTab(membersTab); return tmav; } else if( AddMembersCommand.ACTION_REMOVE.equals(command.getActionCmd())) { getSelections(request).remove(command.getMemberIndex()); return doSearch(request, response, comm, errors); } else { long[] memberOids = command.getMemberOids(); for(int i=0;i<memberOids.length;i++) { long nmo = memberOids[i]; addSelection(request, nmo); } return doSearch(request, response, comm, errors); } } AddMembersController(); }
AddMembersController extends AbstractCommandController { @Override protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object comm, BindException errors) throws Exception { AddMembersCommand command = (AddMembersCommand) comm; if( AddMembersCommand.ACTION_ADD_MEMBERS.equals(command.getActionCmd())) { TargetGroup group = getEditorContext(request).getTargetGroup(); long[] memberOids = command.getMemberOids(); for(int i=0;i<memberOids.length;i++) { long nmo = memberOids[i]; addSelection(request, nmo); } Set<Long> ancestorOids = targetManager.getAncestorOids(group); List<MemberSelection> newMembers = getSelections(request); if(newMembers != null && newMembers.size() > 0 ) { Iterator<MemberSelection> it = newMembers.iterator(); while(it.hasNext()) { MemberSelection newMember = it.next(); if(ancestorOids.contains(newMember)) { String name = newMember.getName(); errors.reject("groups.error.loops", new Object[] { name }, "Adding one of these members would cause an inclusion loop"); } else if(group.getOid() != null && newMember.equals(group.getOid())) { String name = newMember.getName(); errors.reject("groups.error.selfref", new Object[] { name }, "Cannot create self referencing groups"); } else if( targetManager.isDuplicateMember(group, newMember.getOid())) { String name = newMember.getName(); errors.reject("groups.errors.duplicate", new Object[] { name }, "Already a member of this group"); } } } if(errors.hasErrors()) { return doSearch(request, response, comm, errors); } else { if(newMembers != null && newMembers.size() > 0 && authorityManager.hasPrivilege(group, Privilege.ADD_TARGET_TO_GROUP)) { Iterator<MemberSelection> it = newMembers.iterator(); while(it.hasNext()) { MemberSelection newMember = it.next(); GroupMemberDTO dto = targetManager.createGroupMemberDTO(group, newMember.getOid()); group.getNewChildren().add(dto); } } clearSelections(request); Tab membersTab = groupsController.getTabConfig().getTabByID("MEMBERS"); TabbedModelAndView tmav = membersTab.getTabHandler().preProcessNextTab(groupsController, membersTab, request, response, command, errors); tmav.getTabStatus().setCurrentTab(membersTab); return tmav; } } else if( AddMembersCommand.ACTION_CANCEL.equals(command.getActionCmd())) { clearSelections(request); Tab membersTab = groupsController.getTabConfig().getTabByID("MEMBERS"); TabbedModelAndView tmav = membersTab.getTabHandler().preProcessNextTab(groupsController, membersTab, request, response, command, errors); tmav.getTabStatus().setCurrentTab(membersTab); return tmav; } else if( AddMembersCommand.ACTION_REMOVE.equals(command.getActionCmd())) { getSelections(request).remove(command.getMemberIndex()); return doSearch(request, response, comm, errors); } else { long[] memberOids = command.getMemberOids(); for(int i=0;i<memberOids.length;i++) { long nmo = memberOids[i]; addSelection(request, nmo); } return doSearch(request, response, comm, errors); } } AddMembersController(); GroupsEditorContext getEditorContext(HttpServletRequest req); void setGroupsController(TabbedGroupController groupsController); void setTargetManager(TargetManager targetManager); void setAuthorityManager(AuthorityManager authorityManager); }
AddMembersController extends AbstractCommandController { @Override protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object comm, BindException errors) throws Exception { AddMembersCommand command = (AddMembersCommand) comm; if( AddMembersCommand.ACTION_ADD_MEMBERS.equals(command.getActionCmd())) { TargetGroup group = getEditorContext(request).getTargetGroup(); long[] memberOids = command.getMemberOids(); for(int i=0;i<memberOids.length;i++) { long nmo = memberOids[i]; addSelection(request, nmo); } Set<Long> ancestorOids = targetManager.getAncestorOids(group); List<MemberSelection> newMembers = getSelections(request); if(newMembers != null && newMembers.size() > 0 ) { Iterator<MemberSelection> it = newMembers.iterator(); while(it.hasNext()) { MemberSelection newMember = it.next(); if(ancestorOids.contains(newMember)) { String name = newMember.getName(); errors.reject("groups.error.loops", new Object[] { name }, "Adding one of these members would cause an inclusion loop"); } else if(group.getOid() != null && newMember.equals(group.getOid())) { String name = newMember.getName(); errors.reject("groups.error.selfref", new Object[] { name }, "Cannot create self referencing groups"); } else if( targetManager.isDuplicateMember(group, newMember.getOid())) { String name = newMember.getName(); errors.reject("groups.errors.duplicate", new Object[] { name }, "Already a member of this group"); } } } if(errors.hasErrors()) { return doSearch(request, response, comm, errors); } else { if(newMembers != null && newMembers.size() > 0 && authorityManager.hasPrivilege(group, Privilege.ADD_TARGET_TO_GROUP)) { Iterator<MemberSelection> it = newMembers.iterator(); while(it.hasNext()) { MemberSelection newMember = it.next(); GroupMemberDTO dto = targetManager.createGroupMemberDTO(group, newMember.getOid()); group.getNewChildren().add(dto); } } clearSelections(request); Tab membersTab = groupsController.getTabConfig().getTabByID("MEMBERS"); TabbedModelAndView tmav = membersTab.getTabHandler().preProcessNextTab(groupsController, membersTab, request, response, command, errors); tmav.getTabStatus().setCurrentTab(membersTab); return tmav; } } else if( AddMembersCommand.ACTION_CANCEL.equals(command.getActionCmd())) { clearSelections(request); Tab membersTab = groupsController.getTabConfig().getTabByID("MEMBERS"); TabbedModelAndView tmav = membersTab.getTabHandler().preProcessNextTab(groupsController, membersTab, request, response, command, errors); tmav.getTabStatus().setCurrentTab(membersTab); return tmav; } else if( AddMembersCommand.ACTION_REMOVE.equals(command.getActionCmd())) { getSelections(request).remove(command.getMemberIndex()); return doSearch(request, response, comm, errors); } else { long[] memberOids = command.getMemberOids(); for(int i=0;i<memberOids.length;i++) { long nmo = memberOids[i]; addSelection(request, nmo); } return doSearch(request, response, comm, errors); } } AddMembersController(); GroupsEditorContext getEditorContext(HttpServletRequest req); void setGroupsController(TabbedGroupController groupsController); void setTargetManager(TargetManager targetManager); void setAuthorityManager(AuthorityManager authorityManager);  }