TabbedTargetController extends TabbedController { @Override protected ModelAndView processSave(Tab currentTab, HttpServletRequest req, HttpServletResponse res, Object comm, BindException errors) { TargetEditorContext ctx = getEditorContext(req); Target target = ctx.getTarget(); TargetSchedulesCommand cmd = null; if(comm instanceof TargetSchedulesCommand) { cmd = (TargetSchedulesCommand)comm; } if(!targetManager.isNameOk(target)) { errors.reject("target.errors.duplicatename"); } if(target.getProfile()==null) { errors.reject("target.errors.profile.missing"); } if( ( target.getState() == Target.STATE_APPROVED || target.getState() == Target.STATE_COMPLETED ) && !target.isApprovable()) { errors.reject("target.errors.notapprovable"); } if(cmd != null && cmd.isHarvestNowSet() && target.getState() != Target.STATE_APPROVED && target.getState() != Target.STATE_COMPLETED) { errors.reject("target.error.notapproved"); } if(cmd != null && cmd.isHarvestNowSet() && target.getState()==Target.STATE_COMPLETED) { if(!authorityManager.hasPrivilege(target, Privilege.APPROVE_TARGET) || !authorityManager.hasPrivilege(target, Privilege.REINSTATE_TARGET)) { errors.reject("target.error.approve.denied"); } } if(errors.hasErrors()) { TabbedModelAndView tmav = currentTab.getTabHandler().preProcessNextTab(this, currentTab, req, res, comm ,errors); tmav.getTabStatus().setCurrentTab(currentTab); tmav.addObject(Constants.GBL_CMD_DATA, comm); tmav.addObject(Constants.GBL_ERRORS, errors); return tmav; } else { if(cmd != null) { if (cmd.isHarvestNowSet()) { target.setHarvestNow(true); if (target.getState() == Target.STATE_COMPLETED) { target.changeState(Target.STATE_APPROVED); } } target.setAllowOptimize(cmd.isAllowOptimize()); } int beforeSaveState = target.getState(); targetManager.save(target, ctx.getParents()); int afterSaveState = target.getState(); if(comm instanceof TargetAnnotationCommand) { TargetAnnotationCommand tac = (TargetAnnotationCommand)comm; if(tac.isAction(TargetAnnotationCommand.ACTION_ADD_NOTE)) { TabbedModelAndView tmav = currentTab.getTabHandler().preProcessNextTab(this, currentTab, req, res, comm ,errors); tmav.getTabStatus().setCurrentTab(currentTab); tmav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("target.saved", new Object[] { target.getName() }, Locale.getDefault())); tmav.addObject(Constants.GBL_CMD_DATA, comm); ctx.refreshCachedSchedules(); return tmav; } } ModelAndView mav = searchController.prepareSearchView(req, res, errors); if( beforeSaveState == afterSaveState) { if (target.isHarvestNow() && afterSaveState == Target.STATE_APPROVED) { mav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("target.saved.schedulednow", new Object[] { target.getName() }, Locale.getDefault())); } else { mav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("target.saved", new Object[] { target.getName() }, Locale.getDefault())); } } else if(afterSaveState == Target.STATE_NOMINATED) { mav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("target.saved.nominated", new Object[] { target.getName() }, Locale.getDefault())); } else if(afterSaveState == Target.STATE_REINSTATED) { mav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("target.saved.reinstated", new Object[] { target.getName() }, Locale.getDefault())); } unbindEditorContext(req); return mav; } }  void bindEditorContext(HttpServletRequest req, TargetEditorContext context); void unbindEditorContext(HttpServletRequest req); TargetEditorContext getEditorContext(HttpServletRequest req); void setTargetManager(TargetManager targetManager); void setBusinessObjectFactory(BusinessObjectFactory businessObjectFactory); void setSearchController(TargetSearchController searchController); void setMessageSource(MessageSource messageSource); void setAuthorityManager(AuthorityManager authorityManager); static final String EDITOR_CONTEXT; }
@Test public final void testProcessSave() { try { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); Target target = manager.load(4000L); TargetEditorContext context = new TargetEditorContext(manager, target, true); request.getSession().setAttribute(TabbedTargetController.EDITOR_CONTEXT, context); TargetDefaultCommand comm = new TargetDefaultCommand(); comm.setMode(TargetDefaultCommand.MODE_EDIT); comm.setTargetOid(4000L); Tab currTab = testInstance.getTabConfig().getTabs().get(0); assertTrue(currTab != null); BindException aError = new BindException(new TargetDefaultCommand(), null); testInstance.showForm(request, response, comm, aError); context = testInstance.getEditorContext(request); context.getTarget().setName("Test Target"); context.setParents(new ArrayList<GroupMemberDTO>()); ModelAndView mav = testInstance.processSave(currTab, request, response, comm, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("target-search")); assertTrue(mav.getModel().get("page_message") != null); assertTrue(((String)mav.getModel().get("page_message")).startsWith("target.saved")); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }