List of usage examples for org.springframework.validation BindException reject
@Override
public void reject(String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage)
From source file:org.openmrs.web.controller.person.PersonFormController.java
/** * Updates person addresses based on request parameters * /*from w w w . jav a 2 s .c o m*/ * @param request * @param person * @param errors * @throws ParseException */ protected void updatePersonAddresses(HttpServletRequest request, Person person, BindException errors) throws ParseException { String[] add1s = ServletRequestUtils.getStringParameters(request, "address1"); String[] add2s = ServletRequestUtils.getStringParameters(request, "address2"); String[] cities = ServletRequestUtils.getStringParameters(request, "cityVillage"); String[] states = ServletRequestUtils.getStringParameters(request, "stateProvince"); String[] countries = ServletRequestUtils.getStringParameters(request, "country"); String[] lats = ServletRequestUtils.getStringParameters(request, "latitude"); String[] longs = ServletRequestUtils.getStringParameters(request, "longitude"); String[] pCodes = ServletRequestUtils.getStringParameters(request, "postalCode"); String[] counties = ServletRequestUtils.getStringParameters(request, "countyDistrict"); String[] add3s = ServletRequestUtils.getStringParameters(request, "address3"); String[] addPrefStatus = ServletRequestUtils.getStringParameters(request, "preferred"); String[] add6s = ServletRequestUtils.getStringParameters(request, "address6"); String[] add5s = ServletRequestUtils.getStringParameters(request, "address5"); String[] add4s = ServletRequestUtils.getStringParameters(request, "address4"); String[] startDates = ServletRequestUtils.getStringParameters(request, "startDate"); String[] endDates = ServletRequestUtils.getStringParameters(request, "endDate"); if (add1s != null || add2s != null || cities != null || states != null || countries != null || lats != null || longs != null || pCodes != null || counties != null || add3s != null || add6s != null || add5s != null || add4s != null || startDates != null || endDates != null) { int maxAddrs = 0; if (add1s != null && add1s.length > maxAddrs) { maxAddrs = add1s.length; } if (add2s != null && add2s.length > maxAddrs) { maxAddrs = add2s.length; } if (cities != null && cities.length > maxAddrs) { maxAddrs = cities.length; } if (states != null && states.length > maxAddrs) { maxAddrs = states.length; } if (countries != null && countries.length > maxAddrs) { maxAddrs = countries.length; } if (lats != null && lats.length > maxAddrs) { maxAddrs = lats.length; } if (longs != null && longs.length > maxAddrs) { maxAddrs = longs.length; } if (pCodes != null && pCodes.length > maxAddrs) { maxAddrs = pCodes.length; } if (counties != null && counties.length > maxAddrs) { maxAddrs = counties.length; } if (add3s != null && add3s.length > maxAddrs) { maxAddrs = add3s.length; } if (add6s != null && add6s.length > maxAddrs) { maxAddrs = add6s.length; } if (add5s != null && add5s.length > maxAddrs) { maxAddrs = add5s.length; } if (add4s != null && add4s.length > maxAddrs) { maxAddrs = add4s.length; } if (startDates != null && startDates.length > maxAddrs) { maxAddrs = startDates.length; } if (endDates != null && endDates.length > maxAddrs) { maxAddrs = endDates.length; } log.debug("There appears to be " + maxAddrs + " addresses that need to be saved"); for (int i = 0; i < maxAddrs; i++) { PersonAddress pa = new PersonAddress(); if (add1s.length >= i + 1) { pa.setAddress1(add1s[i]); } if (add2s.length >= i + 1) { pa.setAddress2(add2s[i]); } if (cities.length >= i + 1) { pa.setCityVillage(cities[i]); } if (states.length >= i + 1) { pa.setStateProvince(states[i]); } if (countries.length >= i + 1) { pa.setCountry(countries[i]); } if (lats.length >= i + 1) { pa.setLatitude(lats[i]); } if (longs.length >= i + 1) { pa.setLongitude(longs[i]); } if (pCodes.length >= i + 1) { pa.setPostalCode(pCodes[i]); } if (counties.length >= i + 1) { pa.setCountyDistrict(counties[i]); } if (add3s.length >= i + 1) { pa.setAddress3(add3s[i]); } if (addPrefStatus != null && addPrefStatus.length > i) { pa.setPreferred(new Boolean(addPrefStatus[i])); } if (add6s.length >= i + 1) { pa.setAddress6(add6s[i]); } if (add5s.length >= i + 1) { pa.setAddress5(add5s[i]); } if (add4s.length >= i + 1) { pa.setAddress4(add4s[i]); } if (startDates.length >= i + 1 && StringUtils.isNotBlank(startDates[i])) { pa.setStartDate(Context.getDateFormat().parse(startDates[i])); } if (endDates.length >= i + 1 && StringUtils.isNotBlank(endDates[i])) { pa.setEndDate(Context.getDateFormat().parse(endDates[i])); } //check if all required addres fields are filled Errors addressErrors = new BindException(pa, "personAddress"); new PersonAddressValidator().validate(pa, addressErrors); if (addressErrors.hasErrors()) { for (ObjectError error : addressErrors.getAllErrors()) { errors.reject(error.getCode(), error.getArguments(), ""); } } if (errors.hasErrors()) { return; } person.addAddress(pa); } Iterator<PersonAddress> addresses = person.getAddresses().iterator(); PersonAddress currentAddress = null; PersonAddress preferredAddress = null; while (addresses.hasNext()) { currentAddress = addresses.next(); //check if all required addres fields are filled Errors addressErrors = new BindException(currentAddress, "personAddress"); new PersonAddressValidator().validate(currentAddress, addressErrors); if (addressErrors.hasErrors()) { for (ObjectError error : addressErrors.getAllErrors()) { errors.reject(error.getCode(), error.getArguments(), ""); } } if (errors.hasErrors()) { return; } if (currentAddress.isPreferred()) { if (preferredAddress != null) { // if there's a preferred address already exists, make it preferred=false preferredAddress.setPreferred(false); } preferredAddress = currentAddress; } } if ((preferredAddress == null) && (currentAddress != null)) { // No preferred address. Make the last address entry as preferred. currentAddress.setPreferred(true); } } }
From source file:gov.nih.nci.cabig.caaers.web.user.ChangePasswordController.java
@Override protected ModelAndView onSubmit(Object command, BindException errors) throws Exception { ModelAndView modelAndView = new ModelAndView(getFormView(), errors.getModel()); ChangePasswordCommand cmd = (ChangePasswordCommand) command; try {//from w ww. j a v a 2s . c o m passwordManagerService.setPassword(cmd.getUserName(), cmd.confirmedPassword(), cmd.getToken()); return modelAndView.addObject("updated", true); } catch (PasswordCreationPolicyException e) { for (ValidationError vError : e.getErrors().getErrors()) { errors.reject(vError.getCode(), vError.getReplacementVariables(), vError.getMessage()); } return modelAndView.addObject("change_pwd_error", e.getErrors()); } catch (CaaersNoSuchUserException e) { errors.rejectValue("userName", "USR_015", new Object[] { cmd.getUserName() }, "Username is invalid."); return modelAndView; } catch (CaaersSystemException e) { errors.reject("USR_016", "Invalid token."); return modelAndView; } }
From source file:org.webcurator.ui.site.controller.SiteController.java
private void checkAuthAgencyNamesUnique(HttpServletRequest request, BindException errors) { SiteEditorContext ctx = getEditorContext(request); for (AuthorisingAgent agent : ctx.getSite().getAuthorisingAgents()) { if (!siteManager.isAuthAgencyNameUnique(agent.isNew() ? null : agent.getOid(), agent.getName())) { errors.reject("site.errors.authagent.duplicatename", new Object[] { agent.getName() }, ""); }/*from w w w. j av a2s .c om*/ } }
From source file:org.webcurator.ui.site.controller.SiteController.java
public void checkSave(HttpServletRequest req, BindException errors) { if (!errors.hasErrors() && !siteManager.isSiteTitleUnique(getEditorContext(req).getSite())) { errors.reject("site.errors.duplicatename", new Object[] { getEditorContext(req).getSite().getTitle() }, ""); }/*from w ww .j av a 2 s . com*/ checkAuthAgencyNamesUnique(req, errors); }
From source file:org.webcurator.ui.site.controller.SiteUrlHandler.java
public ModelAndView processOther(TabbedController tc, Tab currentTab, HttpServletRequest req, HttpServletResponse res, Object comm, BindException errors) { SiteEditorContext ctx = getEditorContext(req); UrlCommand command = (UrlCommand) comm; if (errors.hasErrors()) { Tab tab = tc.getTabConfig().getTabByID("URLS"); TabbedModelAndView tmav = tab.getTabHandler().preProcessNextTab(tc, tab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(tab); tmav.addObject(Constants.GBL_CMD_DATA, command); tmav.addObject(Constants.GBL_ERRORS, errors); return tmav; }//from w w w . j a v a2 s. c om if (command.isAction(UrlCommand.ACTION_REMOVE_URL)) { log.info("In the URLS tab - REMOVE URL: " + command.getUrlId()); UrlPattern patternToRemove = (UrlPattern) ctx.getObject(UrlPattern.class, command.getUrlId()); if (patternToRemove.getPermissions().size() > 0) { errors.reject("sitecontroller.error.url_in_use", new Object[] { patternToRemove.getPattern() }, "URL Pattern is linked to a permission."); } else { ctx.getSite().removeUrlPattern(patternToRemove); } TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if (command.isAction(UrlCommand.ACTION_ADD_URL)) { log.info("In the URLS tab - ADD URL: " + command.getUrl()); if (!"".equals(command.getUrl())) { try { new URL(command.getUrl()); } catch (MalformedURLException ex) { throw new WCTRuntimeException("The URL " + command.getUrl() + " is invaid."); } UrlPattern url = businessObjectFactory.newUrlPattern(ctx.getSite()); url.setPattern(command.getUrl()); ctx.putObject(url); ctx.getSite().getUrlPatterns().add(url); } TabbedModelAndView tmav = preProcessNextTab(tc, tc.getTabConfig().getTabByID("URLS"), req, res, comm, errors); tmav.getTabStatus().setCurrentTab(tc.getTabConfig().getTabByID("URLS")); return tmav; } TabbedModelAndView tmav = preProcessNextTab(tc, tc.getTabConfig().getTabByID("URLS"), req, res, comm, errors); tmav.getTabStatus().setCurrentTab(tc.getTabConfig().getTabByID("URLS")); return tmav; }
From source file:org.webcurator.ui.target.controller.TargetSeedsHandler.java
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)) {/* w ww. ja v a2s . co m*/ if (command.isAction(SeedsCommand.ACTION_ADD)) { if (errors.hasErrors()) { // Go to the Seeds tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.addObject(Constants.GBL_CMD_DATA, command); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } else { // Create the seed business object. Seed seed = businessObjectFactory.newSeed(ctx.getTarget()); seed.setSeed(command.getSeed().trim()); //remove any trailing spaces // Set the first seed to be primary, others to be secondary. seed.setPrimary(ctx.getTarget().getSeeds().size() == 0); // Associate with the correct permissions. long option = command.getPermissionMappingOption().longValue(); boolean addedExclusions = false; try { addedExclusions = mapSeed(ctx, seed, option); ctx.putObject(seed); ctx.getTarget().addSeed(seed); } // The seed we've tried to link to is from a different // agency. 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."); } // Go to the Seeds tab. 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)) { // Remove the URL. Seed seedToRemove = (Seed) ctx.getObject(Seed.class, command.getSelectedSeed()); ctx.getTarget().removeSeed(seedToRemove); // Go back to the URLs tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if (command.isAction(SeedsCommand.ACTION_REMOVE_SELECTED)) { // Remove the selected URLs. 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); } // Go back to the URLs tab. 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()) { //toggle the selected seed seed.setPrimary(!seed.isPrimary()); } else { //Reset all seeds to not primary List<Seed> seeds = ctx.getSortedSeeds(); Iterator<Seed> it = seeds.iterator(); while (it.hasNext()) { Seed listItem = it.next(); listItem.setPrimary(false); } //Set the selected seed to primary seed.setPrimary(true); } // Go back to the URLs tab. 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); // Go back to the URLs tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); return tmav; } if (command.isAction(SeedsCommand.ACTION_UNLINK_SELECTED)) { //Unlink all permissions from the selected seeds 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); } } // Go back to the URLs tab. 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)) { //Unlink all permissions from the selected seeds 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 { // Go back to the URLs tab. 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 { // Go back to the URLs tab. TabbedModelAndView tmav = preProcessNextTab(tc, currentTab, req, res, comm, errors); tmav.getTabStatus().setCurrentTab(currentTab); // Get the selected seed(s). String[] seedList = command.getSelectedSeed().split(","); for (int s = 0; s < seedList.length; s++) { Seed theSeed = (Seed) ctx.getObject(Seed.class, seedList[s]); // Link all the selected permissions. 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)) { // Go back to the URLs tab. 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()); // Update the target. 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 { // Go back to the URLs tab. 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; }