TargetInstanceResultHandler extends TabHandler { public ModelAndView processOther(TabbedController tc, Tab currentTab, HttpServletRequest req, HttpServletResponse res, Object comm, BindException errors) { TargetInstanceCommand cmd = (TargetInstanceCommand) comm; if (cmd.getCmd().equals(TargetInstanceCommand.ACTION_HARVEST)) { ModelAndView mav = new ModelAndView(); HashMap agents = harvestCoordinator.getHarvestAgents(); mav.addObject(Constants.GBL_CMD_DATA, cmd); mav.addObject(TargetInstanceCommand.MDL_AGENTS, agents); mav.setViewName(Constants.VIEW_HARVEST_NOW); return mav; } else if (cmd.getCmd().equals(TargetInstanceCommand.ACTION_ENDORSE)) { TargetInstance ti = (TargetInstance) req.getSession().getAttribute(TargetInstanceCommand.SESSION_TI); ti.setState(TargetInstance.STATE_ENDORSED); for (HarvestResult hr : ti.getHarvestResults()) { if (hr.getOid().equals(cmd.getHarvestResultId())) { hr.setState(HarvestResult.STATE_ENDORSED); } else { if(hr.getState() != HarvestResult.STATE_REJECTED) { hr.setState(HarvestResult.STATE_REJECTED); harvestCoordinator.removeIndexes(hr); } } targetInstanceManager.save(hr); } targetInstanceManager.save(ti); req.getSession().setAttribute(TargetInstanceCommand.SESSION_TI, ti); TabbedModelAndView tmav = buildResultsModel(tc, ti, true, errors); tmav.getTabStatus().setCurrentTab(currentTab); buildCustomDepositFormDetails(req, errors, ti, tmav); return tmav; } else if (cmd.getCmd().equals(TargetInstanceCommand.ACTION_UNENDORSE)) { TargetInstance ti = (TargetInstance) req.getSession().getAttribute(TargetInstanceCommand.SESSION_TI); ti.setState(TargetInstance.STATE_HARVESTED); for (HarvestResult hr : ti.getHarvestResults()) { hr.setState(0); targetInstanceManager.save(hr); } targetInstanceManager.save(ti); req.getSession().setAttribute(TargetInstanceCommand.SESSION_TI, ti); TabbedModelAndView tmav = buildResultsModel(tc, ti, true, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } else if (cmd.getCmd().equals(TargetInstanceCommand.ACTION_REJECT)) { TargetInstance ti = (TargetInstance) req.getSession().getAttribute(TargetInstanceCommand.SESSION_TI); for (HarvestResult hr : ti.getHarvestResults()) { if (hr.getOid().equals(cmd.getHarvestResultId())) { if(hr.getState() != HarvestResult.STATE_REJECTED) { Long rejReasonId = cmd.getRejReasonId(); if(rejReasonId==null) { String[] codes = {"result.rejection.missing"}; Object[] args = new Object[0]; if (errors == null) { errors = new BindException(cmd, "command"); } errors.addError(new ObjectError("command",codes,args,"Unable to reject harvest, no rejection reasons have been created.")); req.getSession().setAttribute(TargetInstanceCommand.SESSION_TI, ti); TabbedModelAndView tmav = buildResultsModel(tc, ti, true, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } hr.setState(HarvestResult.STATE_REJECTED); RejReason rejReason = agencyUserManager.getRejReasonByOid(rejReasonId); hr.setRejReason(rejReason); harvestCoordinator.removeIndexes(hr); } targetInstanceManager.save(hr); } } boolean allRejected = true; for (HarvestResult hr : ti.getHarvestResults()) { if ((HarvestResult.STATE_REJECTED != hr.getState()) && (HarvestResult.STATE_ABORTED != hr.getState())) { allRejected = false; break; } } if (allRejected) { ti.setState(TargetInstance.STATE_REJECTED); ti.setArchivedTime(new Date()); } targetInstanceManager.save(ti); req.getSession().setAttribute(TargetInstanceCommand.SESSION_TI, ti); TabbedModelAndView tmav = buildResultsModel(tc, ti, true, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } else if (cmd.getCmd().equals(TargetInstanceCommand.ACTION_REINDEX)) { Boolean reIndexSuccessful = null; TargetInstance ti = (TargetInstance) req.getSession().getAttribute(TargetInstanceCommand.SESSION_TI); ti = targetInstanceManager.getTargetInstance(ti.getOid()); for (HarvestResult hr : ti.getHarvestResults()) { if (hr.getOid().equals(cmd.getHarvestResultId()) && hr.getState() == HarvestResult.STATE_INDEXING) { reIndexSuccessful = harvestCoordinator.reIndexHarvestResult(hr); break; } } if(reIndexSuccessful != null && reIndexSuccessful == false) { String[] codes = {"result.reindex.fail"}; Object[] args = new Object[0]; if (errors == null) { errors = new BindException(cmd, "command"); } errors.addError(new ObjectError("command",codes,args,"Reindex did not occur as HarvestResult is still indexing")); } req.getSession().setAttribute(TargetInstanceCommand.SESSION_TI, ti); TabbedModelAndView tmav = buildResultsModel(tc, ti, true, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } else if (cmd.getCmd().equals(TargetInstanceCommand.ACTION_ARCHIVE)) { throw new WCTRuntimeException("Archive command processing is not implemented yet."); } else { throw new WCTRuntimeException("Unknown command " + cmd.getCmd() + " recieved."); } }  void initBinder(HttpServletRequest request, ServletRequestDataBinder binder); void processTab(TabbedController tc, Tab currentTab,
            HttpServletRequest req, HttpServletResponse res, Object comm,
            BindException errors); TabbedModelAndView preProcessNextTab(TabbedController tc,
            Tab nextTabID, HttpServletRequest req, HttpServletResponse res,
            Object comm, BindException errors); TabbedModelAndView buildResultsModel(TabbedController tc, TargetInstance ti, boolean editMode, BindException errors); ModelAndView processOther(TabbedController tc, Tab currentTab,
            HttpServletRequest req, HttpServletResponse res, Object comm,
            BindException errors); void setHarvestCoordinator(HarvestCoordinator harvestCoordinator); void setTargetInstanceManager(TargetInstanceManager targetInstanceManager); void setDigitalAssetStore(DigitalAssetStore digitalAssetStore); void setAgencyUserManager(AgencyUserManager agencyUserManager);  }
@Test public final void testProcessOther() { HttpServletRequest aReq = new MockHttpServletRequest(); MockTargetInstanceManager targetInstanceManager = new MockTargetInstanceManager(testFile); MockHarvestCoordinator coordinator = new MockHarvestCoordinator(); coordinator.setTargetInstanceDao(targetInstanceManager.getTargetInstanceDAO()); MockDigitalAssetStore digitalAssetStore = new MockDigitalAssetStore() { public CustomDepositFormResultDTO getCustomDepositFormDetails(CustomDepositFormCriteriaDTO criteria) throws DigitalAssetStoreException { CustomDepositFormResultDTO result = new CustomDepositFormResultDTO(); result.setCustomDepositFormRequired(true); result.setHTMLForCustomDepositForm("an html"); result.setUrlForCustomDepositForm("http: return result; } }; testInstance.setTargetInstanceManager(targetInstanceManager); testInstance.setHarvestCoordinator(coordinator); testInstance.setDigitalAssetStore(digitalAssetStore); testInstance.setAgencyUserManager(new MockAgencyUserManagerImpl(testFile)); TargetInstance targetInstance = targetInstanceManager.getTargetInstance(5000L); List<HarvestResult> results = targetInstanceManager.getHarvestResults(targetInstance.getOid()); HarvestResult result = results.get(0); assertNotNull(result); aReq.getSession().setAttribute(TargetInstanceCommand.SESSION_TI, targetInstance); HttpServletResponse aResp = new MockHttpServletResponse(); TabbedController tc = new TabbedTargetInstanceController(); TabConfig tabConfig = new TabConfig(); tabConfig.setViewName("targetInstance"); List<Tab> tabs = getTabList(targetInstanceManager); tabConfig.setTabs(tabs); tc.setTabConfig(tabConfig); tc.setDefaultCommandClass(org.webcurator.ui.target.command.TargetInstanceCommand.class); Tab currentTab = tabs.get(1); TargetInstanceCommand aCmd = new TargetInstanceCommand(); aCmd.setCmd(TargetInstanceCommand.ACTION_ENDORSE); aCmd.setHarvestResultId(result.getOid()); BindException aErrors = new BindException(aCmd, aCmd.getCmd()); ModelAndView mav = testInstance.processOther(tc, currentTab, aReq, aResp, aCmd, aErrors); assertTrue(mav != null); assertTrue(mav.getViewName().equals("targetInstance")); assertTrue(((TabStatus)mav.getModel().get("tabStatus")).getCurrentTab().getPageId().equals("RESULTS")); assertEquals(targetInstance.getState(), TargetInstance.STATE_ENDORSED); assertEquals(result.getState(), HarvestResult.STATE_ENDORSED); assertNull(targetInstance.getArchivedTime()); assertEquals(true, mav.getModel().get("customDepositFormRequired")); assertEquals("http: assertEquals("an html", aReq.getSession().getAttribute("customDepositFormHTMLContent")); List<HarvestResult> removedIndexes = coordinator.getRemovedIndexes(); assertEquals(2,removedIndexes.size()); removedIndexes.clear(); aCmd = new TargetInstanceCommand(); aCmd.setCmd(TargetInstanceCommand.ACTION_UNENDORSE); aCmd.setHarvestResultId(result.getOid()); aErrors = new BindException(aCmd, aCmd.getCmd()); mav = testInstance.processOther(tc, currentTab, aReq, aResp, aCmd, aErrors); assertTrue(mav != null); assertTrue(mav.getViewName().equals("targetInstance")); assertTrue(((TabStatus)mav.getModel().get("tabStatus")).getCurrentTab().getPageId().equals("RESULTS")); assertEquals(targetInstance.getState(),TargetInstance.STATE_HARVESTED); assertEquals(result.getState(), 0); assertNull(targetInstance.getArchivedTime()); removedIndexes = coordinator.getRemovedIndexes(); assertEquals(0,removedIndexes.size()); removedIndexes.clear(); }