List of usage examples for org.apache.commons.validator GenericValidator isEmail
public static boolean isEmail(String value)
Checks if a field has a valid e-mail address.
From source file:com.core.validators.CommonValidator.java
/** * Checks if the field is an e-mail address. * * @param value The value validation is being performed on. * @return boolean If the field is an e-mail address * <code>true</code> is returned. * Otherwise <code>false</code>. */// w w w . j ava2s .c om public static boolean validateEmail(Object bean, Field field) { String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); return GenericValidator.isEmail(value); }
From source file:gov.nih.nci.cabig.caaers.web.admin.UserTab.java
private void editPersonValidation(final UserCommand command, final BeanWrapper commandBean, final Map<String, InputFieldGroup> fieldGroups, final Errors errors) { String em = command.getEmailAddress(); Person existingPerson = command.getPerson(); ResearchStaffQuery rsQuery = new ResearchStaffQuery(); rsQuery.setFiltered(true);// w ww. j a v a2 s .c o m rsQuery.filterByEmailAddress(em); if (existingPerson instanceof ResearchStaff) rsQuery.excludeHavingId(command.getPerson().getId()); List<ResearchStaff> existingStaffs = personRepository.searchLocalResearchStaff(rsQuery); if (CollectionUtils.isNotEmpty(existingStaffs)) { errors.rejectValue("emailAddress", "USR_010"); } InvestigatorQuery invQuery = new InvestigatorQuery(); invQuery.setFiltered(true); invQuery.filterByEmailAddress(em); if (existingPerson instanceof Investigator) invQuery.excludeHavingId(command.getPerson().getId()); List<Investigator> existingInvs = personRepository.searchLocalInvestigator(invQuery); if (CollectionUtils.isNotEmpty(existingInvs)) { errors.rejectValue("emailAddress", "USR_010"); } if (StringUtils.isNotEmpty(command.getNciIdentifier())) { rsQuery = new ResearchStaffQuery(); rsQuery.setFiltered(true); rsQuery.filterByNciIdentifier(command.getNciIdentifier()); if (existingPerson instanceof ResearchStaff) rsQuery.excludeHavingId(command.getPerson().getId()); existingStaffs = personRepository.searchLocalResearchStaff(rsQuery); if (CollectionUtils.isNotEmpty(existingStaffs)) { errors.rejectValue("nciIdentifier", "USR_018"); } invQuery = new InvestigatorQuery(); invQuery.setFiltered(true); invQuery.filterByNciIdentifier(command.getNciIdentifier()); if (existingPerson instanceof Investigator) invQuery.excludeHavingId(command.getPerson().getId()); existingInvs = personRepository.searchLocalInvestigator(invQuery); if (CollectionUtils.isNotEmpty(existingInvs)) { errors.rejectValue("nciIdentifier", "USR_018"); } } List<SitePerson> sitePersonnel = command.getSitePersonnel(); if (CollectionUtils.isEmpty(sitePersonnel)) { errors.reject("USR_005", "Provide at least one organization"); } for (int i = 0; i < sitePersonnel.size(); i++) { if ((sitePersonnel.get(i).getOrganization() == null || sitePersonnel.get(i).getOrganization().getId() == null)) { errors.reject("USR_004", new Object[] { new Integer(i) }, "Provide the organization"); } String email = sitePersonnel.get(i).getEmailAddress(); if (email != null && !email.trim().equals("") && !GenericValidator.isEmail(email)) { errors.rejectValue(String.format("sitePersonnel[%d].emailAddress", i), "USR_006", "Invalid email"); } } }
From source file:gov.nih.nci.cabig.caaers.web.admin.UserTab.java
@Override protected void validate(final UserCommand command, final BeanWrapper commandBean, final Map<String, InputFieldGroup> fieldGroups, final Errors errors) { String em = command.getEmailAddress(); if (em != null && !em.trim().equals("") && !GenericValidator.isEmail(em)) { errors.rejectValue("emailAddress", "USR_006", "Invalid email"); }/*from w w w . j a va 2 s .c om*/ if (command.getCreateMode()) { if (!command.getCreateAsPerson() && !command.getCreateAsUser()) { errors.reject("USER_PERSON_001", "Either Create as Person or Create as User or both must be checked"); } //another person should not be present. if (command.getCreateAsPerson() && em != null) { createNewPersonValidation(command, commandBean, fieldGroups, errors); } if (command.getCreateAsUser()) { if (StringUtils.isEmpty(command.getUserName())) { errors.rejectValue("userName", "USR_014"); } User user = userRepository.getUserByLoginName(command.getUserName()); if (user != null && user.getCsmUser() != null) { errors.rejectValue("userName", "USR_001", "Username already taken"); } } } else if (command.getEditMode()) { //there should not be another person with the same details. if (command.getCreateAsPerson()) { Person person = command.getPerson(); if (person != null) { editPersonValidation(command, commandBean, fieldGroups, errors); } else { //creating new person createNewPersonValidation(command, commandBean, fieldGroups, errors); } } //only do user validation if already user don't exist. if (command.getCreateAsUser() && (command.getUser() == null || command.getUser().getCsmUser().getUserId() == null)) { if (StringUtils.isEmpty(command.getUserName())) { errors.rejectValue("userName", "USR_014"); } User user = userRepository.getUserByLoginName(command.getUserName()); if (user != null && user.getCsmUser() != null) { errors.rejectValue("userName", "USR_001", "Username already taken"); } } } }
From source file:de.knurt.fam.core.persistence.dao.ibatis.UserDao4ibatis.java
/** {@inheritDoc} */ @Override//from w w w. jav a 2 s . c o m public List<User> getUsersWithEMail(String email) throws InvalidParameterException { if (GenericValidator.isEmail(email)) { String where = String.format("mail = \"%s\"", email); return this.getWhere(where); } else { throw new InvalidParameterException(email + " is not an email"); } }
From source file:jp.co.ctc_g.jse.core.validation.util.Validators.java
/** * ????????????/*from w ww . j av a 2s . c om*/ * ??{@link GenericValidator#isEmail(String)}????? * @param suspect * @return GenericValidator#isEmail(String)?? */ public static boolean isEmail(CharSequence suspect) { return GenericValidator.isEmail(suspect.toString()); }
From source file:it.eng.spagobi.tools.scheduler.dispatcher.MailDocumentDispatchChannel.java
private static String[] findRecipients(DispatchContext info, BIObject biobj, IDataStore dataStore) { logger.debug("IN"); String[] toReturn = null;/* w ww .j ava2 s . c o m*/ List<String> recipients = new ArrayList(); try { recipients.addAll(findRecipientsFromFixedList(info)); } catch (Exception e) { logger.error(e); } try { recipients.addAll(findRecipientsFromDataSet(info, biobj, dataStore)); } catch (Exception e) { logger.error(e); } try { recipients.addAll(findRecipientsFromExpression(info, biobj)); } catch (Exception e) { logger.error(e); } // validates addresses List<String> validRecipients = new ArrayList(); Iterator it = recipients.iterator(); while (it.hasNext()) { String recipient = (String) it.next(); if (GenericValidator.isBlankOrNull(recipient) || !GenericValidator.isEmail(recipient)) { logger.error("[" + recipient + "] is not a valid email address."); continue; } if (validRecipients.contains(recipient)) continue; validRecipients.add(recipient); } toReturn = validRecipients.toArray(new String[0]); logger.debug("OUT: returning " + toReturn); return toReturn; }
From source file:com.aoindustries.creditcards.CreditCard.java
/** * Trims and sets the card holder's email address. * * @throws IllegalArgumentException if the address does is not in the proper format *//*from w ww . j av a 2 s.c om*/ public void setEmail(String email) { if (email == null || email.length() == 0) { this.email = null; } else { email = email.trim(); if (!GenericValidator.isEmail(email)) throw new LocalizedIllegalArgumentException(accessor, "CreditCard.setEmail.email.invalid"); this.email = email; } }
From source file:com.aoindustries.creditcards.TransactionRequest.java
/** * Trims and sets the merchant email address that will be emailed a copy of the customer email directly * by the payment gateway.// ww w . j av a 2 s .c o m * * @throws IllegalArgumentException if the address does is not in the proper format */ public void setMerchantEmail(String merchantEmail) { if (merchantEmail == null) { this.merchantEmail = null; } else { merchantEmail = merchantEmail.trim(); if (!GenericValidator.isEmail(merchantEmail)) throw new LocalizedIllegalArgumentException(accessor, "TransactionRequest.setMerchantEmail.merchantEmail.invalid"); this.merchantEmail = merchantEmail; } }
From source file:it.eng.spagobi.commons.validation.SpagoBIValidationImpl.java
public static EMFValidationError validateField(String fieldName, String fieldLabel, String value, String validatorName, String arg0, String arg1, String arg2) throws Exception { List params = null;// w w w . j a v a 2 s. c om if (validatorName.equalsIgnoreCase("MANDATORY")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the MANDATORY VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); if (GenericValidator.isBlankOrNull(value)) { params = new ArrayList(); params.add(fieldLabel); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_MANDATORY, params); } } else if (validatorName.equalsIgnoreCase("URL")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the URL VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); UrlValidator urlValidator = new SpagoURLValidator(); if (!GenericValidator.isBlankOrNull(value) && !urlValidator.isValid(value)) { params = new ArrayList(); params.add(fieldLabel); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_URL, params); } } else if (validatorName.equalsIgnoreCase("LETTERSTRING")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the LETTERSTRING VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.matchRegexp(value, LETTER_STRING_REGEXP)) { params = new ArrayList(); params.add(fieldLabel); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_LETTERSTRING, params); } } else if (validatorName.equalsIgnoreCase("ALFANUMERIC")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the ALFANUMERIC VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.matchRegexp(value, ALPHANUMERIC_STRING_REGEXP)) { params = new ArrayList(); params.add(fieldLabel); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_ALFANUMERIC, params); } } else if (validatorName.equalsIgnoreCase("NUMERIC")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the NUMERIC VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); if (!GenericValidator.isBlankOrNull(value) && (!(GenericValidator.isInt(value) || GenericValidator.isFloat(value) || GenericValidator.isDouble(value) || GenericValidator.isShort(value) || GenericValidator.isLong(value)))) { // The string is not a integer, not a float, not double, not short, not long // so is not a number params = new ArrayList(); params.add(fieldLabel); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_NUMERIC, params); } } else if (validatorName.equalsIgnoreCase("EMAIL")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the EMAIL VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.isEmail(value)) { // Generate errors params = new ArrayList(); params.add(fieldLabel); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_EMAIL, params); } } else if (validatorName.equalsIgnoreCase("BOOLEAN")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the MANDATORY VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); if (!GenericValidator.isBlankOrNull(value) && !value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false")) { params = new ArrayList(); params.add(fieldLabel); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_BOOLEAN, params); } } else if (validatorName.equalsIgnoreCase("FISCALCODE")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the FISCALCODE VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.matchRegexp(value, FISCAL_CODE_REGEXP)) { // Generate errors params = new ArrayList(); params.add(fieldLabel); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_FISCALCODE, params); } } else if (validatorName.equalsIgnoreCase("DECIMALS")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the DECIMALS VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); int maxNumberOfDecimalDigit = Integer.valueOf(arg0).intValue(); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Max Numbers of decimals is [" + maxNumberOfDecimalDigit + "]"); String decimalSeparator = arg1; if (GenericValidator.isBlankOrNull(decimalSeparator)) { decimalSeparator = "."; } int pos = value.indexOf(decimalSeparator); String decimalCharacters = ""; if (pos != -1) decimalCharacters = value.substring(pos + 1); if (decimalCharacters.length() > maxNumberOfDecimalDigit) { // Generate errors params = new ArrayList(); params.add(fieldLabel); params.add(String.valueOf(maxNumberOfDecimalDigit)); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_DECIMALS, params); } } } else if (validatorName.equalsIgnoreCase("NUMERICRANGE")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the NUMERICRANGE VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); String firstValueStr = arg0; String secondValueStr = arg1; SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Range is [" + firstValueStr + "< x <" + secondValueStr + "]"); boolean syntaxCorrect = true; if (!GenericValidator.isDouble(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", " CANNOT APPLY THE NUMERICRANGE VALIDATOR value [" + value + "] is not a Number"); syntaxCorrect = false; } if (!GenericValidator.isDouble(firstValueStr)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", " CANNOT APPLY THE NUMERICRANGE VALIDATOR first value of range [" + firstValueStr + "] is not a Number"); syntaxCorrect = false; } if (!GenericValidator.isDouble(secondValueStr)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", " CANNOT APPLY THE NUMERICRANGE VALIDATOR second value of range [" + secondValueStr + "] is not a Number"); syntaxCorrect = false; } if (syntaxCorrect) { double firstValue = Double.valueOf(firstValueStr).doubleValue(); double secondValue = Double.valueOf(secondValueStr).doubleValue(); double valueToCheckDouble = Double.valueOf(value).doubleValue(); if (!(GenericValidator.isInRange(valueToCheckDouble, firstValue, secondValue))) { params = new ArrayList(); params.add(fieldLabel); params.add(firstValueStr); params.add(secondValueStr); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_RANGE, params); } } else { return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_GENERIC); } } } else if (validatorName.equalsIgnoreCase("DATERANGE")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the DATERANGE VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); String firstValueStr = arg0; String secondValueStr = arg1; String dateFormat = arg2; SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Range is [" + firstValueStr + "< x <" + secondValueStr + "]"); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Date Format is [" + dateFormat + "]"); // //boolean syntaxCorrect = false; boolean syntaxCorrect = true; //if (!GenericValidator.isDate(value,dateFormat,true)){ if (!GenericValidator.isDate(value, dateFormat, true)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", " CANNOT APPLY THE DATERANGE VALIDATOR value [" + value + "] is not a is not valid Date according to [" + dateFormat + "]"); syntaxCorrect = false; } //if (!GenericValidator.isDate(firstValueStr,dateFormat,true)){ if (!GenericValidator.isDate(firstValueStr, dateFormat, true)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", " CANNOT APPLY THE DATERANGE VALIDATOR first value of range [" + firstValueStr + "] is not valid Date according to [" + dateFormat + "]"); syntaxCorrect = false; } //if (!GenericValidator.isDate(secondValueStr,dateFormat, true)){ if (!GenericValidator.isDate(secondValueStr, dateFormat, true)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", " CANNOT APPLY THE DATERANGE VALIDATOR second value of range [" + secondValueStr + "] is not a valid Date according to [" + dateFormat + "]"); syntaxCorrect = false; } if (syntaxCorrect) { DateFormat df = new SimpleDateFormat(dateFormat); Date firstValueDate = df.parse(firstValueStr); Date secondValueDate = df.parse(secondValueStr); Date theValueDate = df.parse(value); if ((theValueDate.getTime() < firstValueDate.getTime()) || (theValueDate.getTime() > secondValueDate.getTime())) { params = new ArrayList(); params.add(fieldLabel); params.add(firstValueStr); params.add(secondValueStr); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_RANGE, params); } } else { return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_GENERIC); } } } else if (validatorName.equalsIgnoreCase("STRINGRANGE")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the STRINGRANGE VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); String firstValueStr = arg0; String secondValueStr = arg1; SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Range is [" + firstValueStr + "< x <" + secondValueStr + "]"); //if (firstValueStr.compareTo(secondValueStr) > 0){ if ((value.compareTo(firstValueStr) < 0) || (value.compareTo(secondValueStr) > 0)) { params = new ArrayList(); params.add(fieldLabel); params.add(firstValueStr); params.add(secondValueStr); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_RANGE, params); } } } else if (validatorName.equalsIgnoreCase("MAXLENGTH")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the MAXLENGTH VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); int maxLength = Integer.valueOf(arg0).intValue(); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "maxLength is [" + maxLength + "]"); if (!GenericValidator.maxLength(value, maxLength)) { params = new ArrayList(); params.add(fieldLabel); params.add(String.valueOf(maxLength)); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_MAXLENGTH, params); } } } else if (validatorName.equalsIgnoreCase("MINLENGTH")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the MINLENGTH VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); int minLength = Integer.valueOf(arg0).intValue(); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "minLength is [" + minLength + "]"); if (!GenericValidator.minLength(value, minLength)) { // Generate Errors params = new ArrayList(); params.add(fieldLabel); params.add(String.valueOf(minLength)); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_MINLENGTH, params); } } } else if (validatorName.equalsIgnoreCase("REGEXP")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the REGEXP VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); String regexp = arg0; SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "regexp is [" + regexp + "]"); if (!(GenericValidator.matchRegexp(value, regexp))) { // Generate Errors params = new ArrayList(); params.add(fieldLabel); params.add(regexp); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_REGEXP, params); } } } else if (validatorName.equalsIgnoreCase("XSS")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the XSS VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); String toVerify = value.toUpperCase(); if (toVerify.contains("<A") || toVerify.contains("<LINK") || toVerify.contains("<IMG") || toVerify.contains("<SCRIPT") || toVerify.contains("<A") || toVerify.contains("<LINK") || toVerify.contains("<IMG") || toVerify.contains("<SCRIPT")) { // Generate Errors params = new ArrayList(); params.add(fieldLabel); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_XSS, params); } } } else if (validatorName.equalsIgnoreCase("DATE")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the DATE VALIDATOR to field [" + fieldName + "] with value [" + value + "]"); String dateFormat = arg0; SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "dateFormat is [" + dateFormat + "]"); //if (!GenericValidator.isDate(value, dateFormat, true)){ if (!GenericValidator.isDate(value, dateFormat, true)) { //Generate Errors params = new ArrayList(); params.add(fieldLabel); params.add(dateFormat); return new EMFValidationError(EMFErrorSeverity.ERROR, fieldName, ERROR_DATE, params); } } } // all checks had positive result (no errors) return null; }
From source file:it.eng.spagobi.commons.utilities.BIObjectValidator.java
/** * For each input field type (Numeric, URL, extc:), this method applies validation. * Every time a validation fails, an error is added to the <code>errorHandler</code> * errors stack./* w w w . ja v a 2 s. com*/ * The field label to be displayed is defined in file validation.xml for each * validation: if it is not defined it is set with the field name; if it starts with * "#" it is interpreted as a key and the message is recovered by * PortletUtilities.getMessage(key, "messages") method, else it remains unchanged. * * @param serviceRequest The request Source Bean * @param errorHandler The errors Stack * @throws Exception If any exception occurs. */ private void automaticValidation(SourceBean serviceRequest, EMFErrorHandler errorHandler) throws Exception { // Reperisco l'elenco di tutti gli attributi che mi aspetto di trovare // nella richiesta List fields = _validationStructure.getAttributeAsList("FIELDS.FIELD"); for (Iterator iter = fields.iterator(); iter.hasNext();) { String value = null; List validators = null; SourceBean currentValidator = null; String validatorName = null; Iterator itValidators = null; try { SourceBean field = (SourceBean) iter.next(); String fieldName = (String) field.getAttribute("name"); value = (String) serviceRequest.getAttribute(fieldName); //******************************************** String fieldLabel = (String) field.getAttribute("label"); if (fieldLabel != null && fieldLabel.startsWith("#")) { String key = fieldLabel.substring(1); String fieldDescription = PortletUtilities.getMessage(key, "messages"); if (fieldDescription != null && !fieldDescription.trim().equals("")) fieldLabel = fieldDescription; } if (fieldLabel == null || fieldLabel.trim().equals("")) fieldLabel = fieldName; //******************************************** validators = field.getAttributeAsList("VALIDATOR"); itValidators = validators.iterator(); Vector params = new Vector(); while (itValidators.hasNext()) { currentValidator = (SourceBean) itValidators.next(); validatorName = (String) currentValidator.getAttribute("validatorName"); if (validatorName.equalsIgnoreCase("MANDATORY")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the MANDATORY VALIDATOR to field [" + field + "] with value [" + value + "]"); if (GenericValidator.isBlankOrNull(value)) { params = new Vector(); params.add(fieldLabel); errorHandler .addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_MANDATORY, params)); } } else if (validatorName.equalsIgnoreCase("URL")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the URL VALIDATOR to field [" + field + "] with value [" + value + "]"); UrlValidator urlValidator = new SpagoURLValidator(); if (!GenericValidator.isBlankOrNull(value) && !urlValidator.isValid(value)) { params = new Vector(); params.add(fieldLabel); errorHandler.addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_URL, params)); } } else if (validatorName.equalsIgnoreCase("LETTERSTRING")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the LETTERSTRING VALIDATOR to field [" + field + "] with value [" + value + "]"); if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.matchRegexp(value, LETTER_STRING_REGEXP)) { params = new Vector(); params.add(fieldLabel); errorHandler .addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_LETTERSTRING, params)); } } else if (validatorName.equalsIgnoreCase("ALFANUMERIC")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the ALFANUMERIC VALIDATOR to field [" + field + "] with value [" + value + "]"); if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.matchRegexp(value, ALPHANUMERIC_STRING_REGEXP)) { params = new Vector(); params.add(fieldLabel); errorHandler .addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_ALFANUMERIC, params)); } } else if (validatorName.equalsIgnoreCase("NUMERIC")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the NUMERIC VALIDATOR to field [" + field + "] with value [" + value + "]"); if (!GenericValidator.isBlankOrNull(value) && (!(GenericValidator.isInt(value) || GenericValidator.isFloat(value) || GenericValidator.isDouble(value) || GenericValidator.isShort(value) || GenericValidator.isLong(value)))) { // The string is not a integer, not a float, not double, not short, not long // so is not a number params = new Vector(); params.add(fieldLabel); errorHandler.addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_NUMERIC, params)); } } else if (validatorName.equalsIgnoreCase("EMAIL")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the EMAIL VALIDATOR to field [" + field + "] with value [" + value + "]"); if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.isEmail(value)) { // Generate errors params = new Vector(); params.add(fieldLabel); errorHandler.addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_EMAIL, params)); } } else if (validatorName.equalsIgnoreCase("FISCALCODE")) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the FISCALCODE VALIDATOR to field [" + field + "] with value [" + value + "]"); if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.matchRegexp(value, FISCAL_CODE_REGEXP)) { // Generate errors params = new Vector(); params.add(fieldLabel); errorHandler .addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_FISCALCODE, params)); } } else if (validatorName.equalsIgnoreCase("DECIMALS")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the DECIMALS VALIDATOR to field [" + field + "] with value [" + value + "]"); int maxNumberOfDecimalDigit = Integer .valueOf((String) currentValidator.getAttribute("arg0")).intValue(); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Max Numbers of decimals is [" + maxNumberOfDecimalDigit + "]"); String decimalSeparator = (String) currentValidator.getAttribute("arg1"); if (GenericValidator.isBlankOrNull(decimalSeparator)) { decimalSeparator = "."; } int pos = value.indexOf(decimalSeparator); String decimalCharacters = ""; if (pos != -1) decimalCharacters = value.substring(pos + 1); if (decimalCharacters.length() > maxNumberOfDecimalDigit) { // Generate errors params = new Vector(); params.add(fieldLabel); params.add(String.valueOf(maxNumberOfDecimalDigit)); errorHandler .addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_DECIMALS, params)); } } } else if (validatorName.equalsIgnoreCase("NUMERICRANGE")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the NUMERICRANGE VALIDATOR to field [" + field + "] with value [" + value + "]"); String firstValueStr = (String) currentValidator.getAttribute("arg0"); String secondValueStr = (String) currentValidator.getAttribute("arg1"); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Range is [" + firstValueStr + "< x <" + secondValueStr + "]"); boolean syntaxCorrect = true; if (!GenericValidator.isDouble(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", " CANNOT APPLY THE NUMERICRANGE VALIDATOR value [" + value + "] is not a Number"); syntaxCorrect = false; } if (!GenericValidator.isDouble(firstValueStr)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", " CANNOT APPLY THE NUMERICRANGE VALIDATOR first value of range [" + firstValueStr + "] is not a Number"); syntaxCorrect = false; } if (!GenericValidator.isDouble(secondValueStr)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", " CANNOT APPLY THE NUMERICRANGE VALIDATOR second value of range [" + secondValueStr + "] is not a Number"); syntaxCorrect = false; } if (syntaxCorrect) { double firstValue = Double.valueOf(firstValueStr).doubleValue(); double secondValue = Double.valueOf(secondValueStr).doubleValue(); double valueToCheckDouble = Double.valueOf(value).doubleValue(); if (!(GenericValidator.isInRange(valueToCheckDouble, firstValue, secondValue))) { params = new Vector(); params.add(fieldLabel); params.add(firstValueStr); params.add(secondValueStr); errorHandler.addError( new EMFUserError(EMFErrorSeverity.ERROR, ERROR_RANGE, params)); } } else { errorHandler.addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_GENERIC)); } } } else if (validatorName.equalsIgnoreCase("DATERANGE")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the DATERANGE VALIDATOR to field [" + field + "] with value [" + value + "]"); String firstValueStr = (String) currentValidator.getAttribute("arg0"); String secondValueStr = (String) currentValidator.getAttribute("arg1"); String dateFormat = (String) currentValidator.getAttribute("arg2"); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Range is [" + firstValueStr + "< x <" + secondValueStr + "]"); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Date Format is [" + dateFormat + "]"); boolean syntaxCorrect = false; if (!GenericValidator.isDate(value, dateFormat, true)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", " CANNOT APPLY THE DATERANGE VALIDATOR value [" + value + "] is not a is not valid Date according to [" + dateFormat + "]"); syntaxCorrect = false; } if (!GenericValidator.isDate(firstValueStr, dateFormat, true)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", " CANNOT APPLY THE DATERANGE VALIDATOR first value of range [" + firstValueStr + "] is not valid Date according to [" + dateFormat + "]"); syntaxCorrect = false; } if (!GenericValidator.isDate(secondValueStr, dateFormat, true)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", " CANNOT APPLY THE DATERANGE VALIDATOR second value of range [" + secondValueStr + "] is not a valid Date according to [" + dateFormat + "]"); syntaxCorrect = false; } if (syntaxCorrect) { DateFormat df = new SimpleDateFormat(dateFormat); Date firstValueDate = df.parse(firstValueStr); Date secondValueDate = df.parse(secondValueStr); Date theValueDate = df.parse(value); if ((theValueDate.getTime() < firstValueDate.getTime()) || (theValueDate.getTime() > secondValueDate.getTime())) { params = new Vector(); params.add(fieldLabel); params.add(firstValueStr); params.add(secondValueStr); errorHandler.addError( new EMFUserError(EMFErrorSeverity.ERROR, ERROR_RANGE, params)); } } else { errorHandler.addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_GENERIC)); } } } else if (validatorName.equalsIgnoreCase("STRINGRANGE")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the STRINGRANGE VALIDATOR to field [" + field + "] with value [" + value + "]"); String firstValueStr = (String) currentValidator.getAttribute("arg0"); String secondValueStr = (String) currentValidator.getAttribute("arg1"); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Range is [" + firstValueStr + "< x <" + secondValueStr + "]"); if (firstValueStr.compareTo(secondValueStr) > 0) { params = new Vector(); params.add(fieldLabel); params.add(firstValueStr); params.add(secondValueStr); errorHandler .addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_RANGE, params)); } } } else if (validatorName.equalsIgnoreCase("MAXLENGTH")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the MAXLENGTH VALIDATOR to field [" + field + "] with value [" + value + "]"); int maxLength = Integer.valueOf((String) currentValidator.getAttribute("arg0")) .intValue(); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "maxLength is [" + maxLength + "]"); if (!GenericValidator.maxLength(value, maxLength)) { params = new Vector(); params.add(fieldLabel); params.add(String.valueOf(maxLength)); errorHandler.addError( new EMFUserError(EMFErrorSeverity.ERROR, ERROR_MAXLENGTH, params)); } } } else if (validatorName.equalsIgnoreCase("MINLENGTH")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the MINLENGTH VALIDATOR to field [" + field + "] with value [" + value + "]"); int minLength = Integer.valueOf((String) currentValidator.getAttribute("arg0")) .intValue(); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "minLength is [" + minLength + "]"); if (!GenericValidator.minLength(value, minLength)) { // Generate Errors params = new Vector(); params.add(fieldLabel); params.add(String.valueOf(minLength)); errorHandler.addError( new EMFUserError(EMFErrorSeverity.ERROR, ERROR_MINLENGTH, params)); } } } else if (validatorName.equalsIgnoreCase("REGEXP")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the REGEXP VALIDATOR to field [" + field + "] with value [" + value + "]"); String regexp = (String) currentValidator.getAttribute("arg0"); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "regexp is [" + regexp + "]"); if (!(GenericValidator.matchRegexp(value, regexp))) { // Generate Errors params = new Vector(); params.add(fieldLabel); params.add(regexp); errorHandler .addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_REGEXP, params)); } } } else if (validatorName.equalsIgnoreCase("DATE")) { if (!GenericValidator.isBlankOrNull(value)) { SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "Apply the DATE VALIDATOR to field [" + field + "] with value [" + value + "]"); String dateFormat = (String) currentValidator.getAttribute("arg0"); SpagoBITracer.info("SpagoBI", "Validator", "automaticValidator", "dateFormat is [" + dateFormat + "]"); if (!GenericValidator.isDate(value, dateFormat, true)) { //Generate Errors params = new Vector(); params.add(fieldLabel); params.add(dateFormat); errorHandler.addError(new EMFUserError(EMFErrorSeverity.ERROR, ERROR_DATE, params)); } } } } //while (itValidators.hasNext()) } catch (Exception ex) { TracerSingleton.log(Constants.NOME_MODULO, TracerSingleton.INFORMATION, "ValidationModule::automaticValidation", ex); } } }