TargetSeedsHandler extends AbstractTargetTabHandler { public ModelAndView processOther(TabbedController tc, Tab currentTab, HttpServletRequest req, HttpServletResponse res, Object comm, BindException errors) { SeedsCommand command = (SeedsCommand) comm; TargetEditorContext ctx = getEditorContext(req); if(authorityManager.hasAtLeastOnePrivilege(ctx.getTarget(), Privilege.MODIFY_TARGET, Privilege.CREATE_TARGET )) { if(command.isAction(SeedsCommand.ACTION_ADD)) { if(errors.hasErrors()) { TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.addObject(Constants.GBL_CMD_DATA, command); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } else { Seed seed = businessObjectFactory.newSeed(ctx.getTarget()); seed.setSeed( command.getSeed().trim() ); seed.setPrimary(ctx.getTarget().getSeeds().size() == 0); long option = command.getPermissionMappingOption().longValue(); boolean addedExclusions = false; try { addedExclusions = mapSeed(ctx, seed, option); ctx.putObject(seed); ctx.getTarget().addSeed(seed); } catch(SeedLinkWrongAgencyException ex) { errors.reject("target.seeds.link.wrong_agency", new Object[] { }, "One of the selected seeds cannot be linked because it belongs to another agency."); } TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); if(addedExclusions) { tmav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("target.linkseeds.exclusions", new Object[] {}, Locale.getDefault())); } return tmav; } } if(command.isAction(SeedsCommand.ACTION_REMOVE)) { Seed seedToRemove = (Seed) ctx.getObject(Seed.class, command.getSelectedSeed()); ctx.getTarget().removeSeed(seedToRemove); TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if(command.isAction(SeedsCommand.ACTION_REMOVE_SELECTED)) { Set<Seed> seeds = ctx.getTarget().getSeeds(); Set<Seed> seedsToRemove = new HashSet<Seed>(); Iterator<Seed> it = seeds.iterator(); while(it.hasNext()) { Seed seed = it.next(); if(isSelected(req, seed)) { seedsToRemove.add(seed); } } it = seedsToRemove.iterator(); while(it.hasNext()) { Seed seedToRemove = it.next(); ctx.getTarget().removeSeed(seedToRemove); } TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if( command.isAction(SeedsCommand.ACTION_TOGGLE_PRIMARY)) { Seed seed = (Seed) ctx.getObject(Seed.class, command.getSelectedSeed()); if(targetManager.getAllowMultiplePrimarySeeds()) { seed.setPrimary(!seed.isPrimary()); } else { List<Seed> seeds = ctx.getSortedSeeds(); Iterator<Seed> it = seeds.iterator(); while(it.hasNext()) { Seed listItem = it.next(); listItem.setPrimary(false); } seed.setPrimary(true); } TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if( command.isAction(SeedsCommand.ACTION_UNLINK)) { Seed seed = (Seed) ctx.getObject(Seed.class, command.getSelectedSeed()); Permission permission = (Permission) ctx.getObject(Permission.class, command.getSelectedPermission()); seed.removePermission(permission); TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if(command.isAction(SeedsCommand.ACTION_UNLINK_SELECTED)) { Set<Seed> seeds = ctx.getTarget().getSeeds(); Iterator<Seed> it = seeds.iterator(); while(it.hasNext()) { Seed seed = it.next(); if(isSelected(req, seed)) { Set<Permission> permissions = seed.getPermissions(); permissions.clear(); seed.getTarget().setDirty(true); } } TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if( command.isAction(SeedsCommand.ACTION_LINK_NEW)) { SeedsCommand newCommand = new SeedsCommand(); Seed selectedSeed = (Seed) ctx.getObject(Seed.class, command.getSelectedSeed()); newCommand.setSelectedSeed(command.getSelectedSeed()); newCommand.setSearchType(SeedsCommand.SEARCH_URL); newCommand.setUrlSearchCriteria(selectedSeed.getSeed()); boolean doSearch = validator.validateLinkSearch(command, errors); return processLinkSearch(tc, ctx.getTarget(), newCommand, doSearch, errors); } if(command.isAction(SeedsCommand.ACTION_LINK_SELECTED)) { Set<Seed> seeds = ctx.getTarget().getSeeds(); Set<Seed> selectedSeeds = new HashSet<Seed>(); String seedList = ""; Iterator<Seed> it = seeds.iterator(); while(it.hasNext()) { Seed seed = it.next(); if(isSelected(req, seed)) { selectedSeeds.add(seed); if(seedList.isEmpty()) { seedList = seed.getIdentity(); } else { seedList += ","+seed.getIdentity(); } } } if(selectedSeeds.size() > 0) { SeedsCommand newCommand = new SeedsCommand(); newCommand.setSelectedSeed(seedList); newCommand.setSearchType(SeedsCommand.SEARCH_SITES); newCommand.setSiteSearchCriteria(""); ctx.putAllObjects(selectedSeeds); return processLinkSearch(tc, ctx.getTarget(), newCommand, false, errors); } else { TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } } if( command.isAction(SeedsCommand.ACTION_LINK_NEW_SEARCH)) { if(errors.hasErrors()) { return processLinkSearch(tc, ctx.getTarget(), command, false, errors); } else { SeedsCommand newCommand = new SeedsCommand(); newCommand.setSelectedSeed(command.getSelectedSeed()); newCommand.setSearchType(command.getSearchType()); newCommand.setSiteSearchCriteria(command.getSiteSearchCriteria()); newCommand.setUrlSearchCriteria(command.getUrlSearchCriteria()); newCommand.setPageNumber(command.getPageNumber()); return processLinkSearch(tc, ctx.getTarget(), newCommand, true, errors); } } if( command.isAction(SeedsCommand.ACTION_LINK_NEW_CONFIRM) ) { if(errors.hasErrors()) { boolean doSearch = validator.validateLinkSearch(command, errors); return processLinkSearch(tc, ctx.getTarget(), command, doSearch, errors); } else { TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); String[] seedList = command.getSelectedSeed().split(","); for(int s = 0; s < seedList.length; s++) { Seed theSeed = (Seed) ctx.getObject(Seed.class, seedList[s]); String[] perms = command.getLinkPermIdentity(); Set<Permission> toLink = new HashSet<Permission>(); boolean wrongAgencyPermission = false; for(int i=0; i<perms.length && !wrongAgencyPermission; i++) { Permission linkPerm = targetManager.loadPermission(ctx, perms[i]); toLink.add(linkPerm); if(!linkPerm.getOwningAgency().equals(ctx.getTarget().getOwner().getAgency())) { wrongAgencyPermission = true; } } try { boolean addedExclusions = linkSeed(ctx.getTarget(), theSeed, toLink); if(addedExclusions) { tmav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("target.linkseeds.exclusions", new Object[] {}, Locale.getDefault())); } } catch(SeedLinkWrongAgencyException ex) { errors.reject("target.seeds.link.wrong_agency", new Object[] { }, "One of the selected seeds cannot be linked because it belongs to another agency."); boolean doSearch = validator.validateLinkSearch(command, errors); return processLinkSearch(tc, ctx.getTarget(), command, doSearch, errors); } } return tmav; } } if( command.isAction(SeedsCommand.ACTION_LINK_NEW_CANCEL) ) { TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if( command.isAction(SeedsCommand.ACTION_START_IMPORT) ) { return getImportSeedsTabModel(tc, ctx); } if( command.isAction(SeedsCommand.ACTION_DO_IMPORT) ) { BufferedReader reader = null; MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req; CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("seedsFile"); if(command.getSeedsFile().length == 0 || file.getOriginalFilename() == null || "".equals(file.getOriginalFilename().trim())) { errors.reject("target.seeds.import.nofile"); TabbedModelAndView tmav = getImportSeedsTabModel(tc, ctx); tmav.addObject(Constants.GBL_ERRORS, errors); return tmav; } if(!file.getOriginalFilename().endsWith(".txt") && !"text/plain".equals(file.getContentType())) { errors.reject("target.seeds.import.filetype"); TabbedModelAndView tmav = getImportSeedsTabModel(tc, ctx); tmav.addObject(Constants.GBL_ERRORS, errors); return tmav; } else { try { reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(command.getSeedsFile()))); List<Seed> validSeeds = new LinkedList<Seed>(); boolean success = true; int lineNumber = 1; String line = reader.readLine(); SeedsCommand importCommand = new SeedsCommand(); while(success && line != null) { if( !line.startsWith("#") && !"".equals(line.trim())) { Seed seed = businessObjectFactory.newSeed(ctx.getTarget()); importCommand.setSeed(line); seed.setSeed(importCommand.getSeed()); if(UrlUtils.isUrl(seed.getSeed())) { mapSeed(ctx, seed, command.getPermissionMappingOption()); ctx.putObject(seed); validSeeds.add(seed); } else { success = false; } } if(success) { line = reader.readLine(); lineNumber++; } } if(success) { for(Seed seed: validSeeds) { ctx.getTarget().addSeed(seed); } } else { errors.reject("target.seeds.import.badline", new Object[] { lineNumber }, "Bad seed detected on line: " + lineNumber); } } catch(SeedLinkWrongAgencyException ex) { errors.reject("target.seeds.link.wrong_agency", new Object[] { }, "One of the selected seeds cannot be linked because it belongs to another agency."); } catch(IOException ex) { errors.reject("target.seeds.import.ioerror"); log.error("Failed to import seeds", ex); } finally { try { reader.close(); } catch(Exception ex) { log.debug("Failed to close uploaded seeds file", ex); } } if(errors.hasErrors()) { TabbedModelAndView tmav = getImportSeedsTabModel(tc, ctx); tmav.addObject(Constants.GBL_ERRORS, errors); return tmav; } else { TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } } } } if(command.isAction(SeedsCommand.ACTION_SET_NAME)) { String id = command.getUpdatedNameSeedId(); if(id!=null) { for(Seed seed:ctx.getSortedSeeds()) { if(seed.getOid().equals(Long.valueOf(id))) { String value = command.getUpdatedNameSeedValue(); if(value.trim().equals("")) { errors.reject("target.seeds.name.edit.required"); TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.addObject(Constants.GBL_CMD_DATA, command); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if(UrlUtils.isUrl(seed.getSeed())) { seed.setSeed(UrlUtils.fixUrl(value)); ctx.putObject(seed); } else { errors.reject("target.seeds.name.edit.invalid"); TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.addObject(Constants.GBL_CMD_DATA, command); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } System.out.println("Found seed " + id); } } } } TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; }  @Override 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); ModelAndView processOther(TabbedController tc, Tab currentTab,
			HttpServletRequest req, HttpServletResponse res, Object comm,
			BindException errors); ModelAndView processLinkSearch(TabbedController tc, Target aTarget, SeedsCommand command, boolean doSearch, BindException errors); void setBusinessObjectFactory(BusinessObjectFactory businessObjectFactory); void setTargetManager(TargetManager targetManager); void setValidator(TargetSeedsValidator validator); void setAuthorityManager(AuthorityManager authorityManager); void setMessageSource(MessageSource messageSource);  }
@Test public final void testProcessOther() { HttpServletRequest aReq = new MockHttpServletRequest(); MockTargetManager targetManager = new MockTargetManager(testFile); testInstance.setTargetManager(targetManager); testSetBusinessObjectFactory(); testSetValidator(); testSetAuthorityManager(); testSetMessageSource(); Target target = targetManager.load(4000L); TargetEditorContext targetEditorContext = new TargetEditorContext(targetManager,target,true); aReq.getSession().setAttribute(TabbedTargetController.EDITOR_CONTEXT, targetEditorContext); HttpServletResponse aResp = new MockHttpServletResponse(); SeedsCommand aCmd = new SeedsCommand(); TabbedController tc = new TabbedTargetController(); TabConfig tabConfig = new TabConfig(); tabConfig.setViewName("target"); List<Tab> tabs = getTabList(targetManager); tabConfig.setTabs(tabs); tc.setTabConfig(tabConfig); tc.setDefaultCommandClass(org.webcurator.ui.target.command.TargetDefaultCommand.class); Tab currentTab = tabs.get(1); BindException aErrors = new BindException(aCmd, "SeedsCommand"); targetManager.setAllowMultiplePrimarySeeds(true); aCmd.setActionCmd(SeedsCommand.ACTION_TOGGLE_PRIMARY); aCmd.setSelectedSeed("6001"); ModelAndView mav = testInstance.processOther(tc, currentTab, aReq, aResp, aCmd, aErrors); assertNotNull(mav); List<Seed> seeds = (List<Seed>)mav.getModel().get("seeds"); Iterator<Seed> it = seeds.iterator(); int primaryCount = 0; while(it.hasNext()) { primaryCount += (it.next().isPrimary()?1:0); } assertTrue(primaryCount > 1); aCmd = new SeedsCommand(); targetManager.setAllowMultiplePrimarySeeds(false); aCmd.setActionCmd(SeedsCommand.ACTION_TOGGLE_PRIMARY); aCmd.setSelectedSeed("6002"); mav = testInstance.processOther(tc, currentTab, aReq, aResp, aCmd, aErrors); assertNotNull(mav); seeds = (List<Seed>)mav.getModel().get("seeds"); it = seeds.iterator(); primaryCount = 0; while(it.hasNext()) { primaryCount += (it.next().isPrimary()?1:0); } assertTrue(primaryCount == 1); aCmd = new SeedsCommand(); targetEditorContext.getTarget().setSeeds(new HashSet<Seed>()); String testSeed = "http: String expectedSeed = "http: aCmd.setActionCmd(SeedsCommand.ACTION_ADD); aCmd.setSeed(testSeed); aCmd.setPermissionMappingOption(SeedsCommand.PERM_MAPPING_NONE); mav = testInstance.processOther(tc, currentTab, aReq, aResp, aCmd, aErrors); assertNotNull(mav); assertEquals(targetEditorContext.getTarget().getSeeds().size(), 1); assertEquals(((Seed)targetEditorContext.getTarget().getSeeds().toArray()[0]).getSeed(),expectedSeed); }