Java Code Examples for org.apache.commons.validator.util.ValidatorUtils#getValueAsString()

The following examples show how to use org.apache.commons.validator.util.ValidatorUtils#getValueAsString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValidateTwoFields.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean validate(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request, ServletContext application) {

    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    String sProperty2 = field.getVarValue("secondProperty");
    String value2 = ValidatorUtils.getValueAsString(bean, sProperty2);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            if (!value.equals(value2)) {
                errors.add(field.getKey(), new ActionMessage("errors.different.passwords"));
                return false;
            }
        } catch (Exception e) {
            errors.add(field.getKey(), new ActionMessage("errors.different.passwords"));
            return false;
        }
    }

    return true;
}
 
Example 2
Source File: ValidateCompareTwoFields.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Compares the two fields using the given comparator
 * 
 * @param bean
 * @param va
 * @param field
 * @param errors
 * @param request
 * @param comparator
 * @return
 */
private static boolean validate(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request, Comparator comparator) {
    String greaterInputString = ValidatorUtils.getValueAsString(bean, field.getProperty());
    String secondProperty = field.getVarValue("secondProperty");
    String lowerInputString = ValidatorUtils.getValueAsString(bean, secondProperty);

    if (!GenericValidator.isBlankOrNull(lowerInputString) && !GenericValidator.isBlankOrNull(greaterInputString)) {
        try {
            Double lowerInput = new Double(lowerInputString);
            Double greaterInput = new Double(greaterInputString);
            // if comparator result != VALUE then the condition is false
            if (comparator.compare(lowerInput, greaterInput) != VALUE) {
                errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
                return false;
            }
            return true;
        } catch (NumberFormatException e) {
            errors.add(field.getKey(), new ActionMessage(va.getMsg()));
            return false;
        }
    }
    return true;
}
 
Example 3
Source File: IntegerValidator.java    From hop with Apache License 2.0 6 votes vote down vote up
public boolean validate( ICheckResultSource source, String propertyName,
                         List<ICheckResult> remarks, ValidatorContext context ) {

  Object result = null;
  String value = null;

  value = ValidatorUtils.getValueAsString( source, propertyName );

  if ( GenericValidator.isBlankOrNull( value ) ) {
    return true;
  }

  result = GenericTypeValidator.formatInt( value );

  if ( result == null ) {
    ActionValidatorUtils.addFailureRemark( source, propertyName, VALIDATOR_NAME, remarks,
      ActionValidatorUtils.getLevelOnFail( context, VALIDATOR_NAME ) );
    return false;
  }
  return true;

}
 
Example 4
Source File: JobEntryValidatorUtils.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Fails if a field's value does not match the given mask.
 */
public static boolean validateMask( CheckResultSourceInterface source, String propertyName, int levelOnFail,
  List<CheckResultInterface> remarks, String mask ) {
  final String VALIDATOR_NAME = "matches";
  String value = null;

  value = ValidatorUtils.getValueAsString( source, propertyName );

  try {
    if ( null == mask ) {
      addGeneralRemark(
        source, propertyName, VALIDATOR_NAME, remarks, "errors.missingVar",
        CheckResultInterface.TYPE_RESULT_ERROR );
      return false;
    }

    if ( StringUtils.isNotBlank( value ) && !GenericValidator.matchRegexp( value, mask ) ) {
      addFailureRemark( source, propertyName, VALIDATOR_NAME, remarks, levelOnFail );
      return false;
    } else {
      return true;
    }
  } catch ( Exception e ) {
    addExceptionRemark( source, propertyName, VALIDATOR_NAME, remarks, e );
    return false;
  }
}
 
Example 5
Source File: EmailValidator.java    From hop with Apache License 2.0 5 votes vote down vote up
public boolean validate( ICheckResultSource source, String propertyName,
                         List<ICheckResult> remarks, ValidatorContext context ) {
  String value = null;

  value = ValidatorUtils.getValueAsString( source, propertyName );

  if ( !GenericValidator.isBlankOrNull( value ) && !GenericValidator.isEmail( value ) ) {
    ActionValidatorUtils.addFailureRemark(
      source, propertyName, VALIDATOR_NAME, remarks, ActionValidatorUtils.getLevelOnFail(
        context, VALIDATOR_NAME ) );
    return false;
  } else {
    return true;
  }
}
 
Example 6
Source File: JobEntryValidatorUtils.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static void addOkRemark( CheckResultSourceInterface source, String propertyName,
  List<CheckResultInterface> remarks ) {
  final int SUBSTRING_LENGTH = 20;
  String value = ValidatorUtils.getValueAsString( source, propertyName );
  String substr = null;
  if ( value != null ) {
    substr = value.substring( 0, Math.min( SUBSTRING_LENGTH, value.length() ) );
    if ( value.length() > SUBSTRING_LENGTH ) {
      substr += "...";
    }
  }
  remarks.add( new CheckResult( CheckResultInterface.TYPE_RESULT_OK, ValidatorMessages.getString(
    "messages.passed", propertyName, substr ), source ) );
}
 
Example 7
Source File: ActionValidatorUtils.java    From hop with Apache License 2.0 5 votes vote down vote up
/**
 * Fails if a field's value does not match the given mask.
 */
public static boolean validateMask( ICheckResultSource source, String propertyName, int levelOnFail,
                                    List<ICheckResult> remarks, String mask ) {
  final String VALIDATOR_NAME = "matches";
  String value = null;

  value = ValidatorUtils.getValueAsString( source, propertyName );

  try {
    if ( null == mask ) {
      addGeneralRemark(
        source, propertyName, VALIDATOR_NAME, remarks, "errors.missingVar",
        ICheckResult.TYPE_RESULT_ERROR );
      return false;
    }

    if ( StringUtils.isNotBlank( value ) && !GenericValidator.matchRegexp( value, mask ) ) {
      addFailureRemark( source, propertyName, VALIDATOR_NAME, remarks, levelOnFail );
      return false;
    } else {
      return true;
    }
  } catch ( Exception e ) {
    addExceptionRemark( source, propertyName, VALIDATOR_NAME, remarks, e );
    return false;
  }
}
 
Example 8
Source File: FileDoesNotExistValidator.java    From hop with Apache License 2.0 5 votes vote down vote up
public boolean validate( ICheckResultSource source, String propertyName,
                         List<ICheckResult> remarks, ValidatorContext context ) {

  String filename = ValidatorUtils.getValueAsString( source, propertyName );
  IVariables variables = getVariableSpace( source, propertyName, remarks, context );
  boolean failIfExists = getFailIfExists( source, propertyName, remarks, context );

  if ( null == variables ) {
    return false;
  }

  String realFileName = variables.environmentSubstitute( filename );
  FileObject fileObject = null;
  try {
    fileObject = HopVfs.getFileObject( realFileName );

    if ( fileObject.exists() && failIfExists ) {
      ActionValidatorUtils.addFailureRemark(
        source, propertyName, VALIDATOR_NAME, remarks, ActionValidatorUtils.getLevelOnFail(
          context, VALIDATOR_NAME ) );
      return false;
    }
    try {
      fileObject.close(); // Just being paranoid
    } catch ( IOException ignored ) {
      // Ignore close errors
    }
  } catch ( Exception e ) {
    ActionValidatorUtils.addExceptionRemark( source, propertyName, VALIDATOR_NAME, remarks, e );
    return false;
  }
  return true;
}
 
Example 9
Source File: FileExistsValidator.java    From hop with Apache License 2.0 5 votes vote down vote up
public boolean validate( ICheckResultSource source, String propertyName,
                         List<ICheckResult> remarks, ValidatorContext context ) {

  String filename = ValidatorUtils.getValueAsString( source, propertyName );
  IVariables variables = getVariableSpace( source, propertyName, remarks, context );
  boolean failIfDoesNotExist = getFailIfDoesNotExist( source, propertyName, remarks, context );

  if ( null == variables ) {
    return false;
  }

  String realFileName = variables.environmentSubstitute( filename );
  FileObject fileObject = null;
  try {
    fileObject = HopVfs.getFileObject( realFileName );
    if ( fileObject == null || ( fileObject != null && !fileObject.exists() && failIfDoesNotExist ) ) {
      ActionValidatorUtils.addFailureRemark(
        source, propertyName, VALIDATOR_NAME, remarks, ActionValidatorUtils.getLevelOnFail(
          context, VALIDATOR_NAME ) );
      return false;
    }
    try {
      fileObject.close(); // Just being paranoid
    } catch ( IOException ignored ) {
      // Ignore close errors
    }
  } catch ( Exception e ) {
    ActionValidatorUtils.addExceptionRemark( source, propertyName, VALIDATOR_NAME, remarks, e );
    return false;
  }
  return true;
}
 
Example 10
Source File: MailingModelChecks.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean validateMaildropStatus(Object bean, Field field, ValidatorResults results, ValidatorAction action) {
	String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

	boolean valid;
	try {
		MaildropStatus.fromName(value.toLowerCase());
		valid = true;
	} catch (Exception e) {
		valid = false;
	}
	results.add(field, action.getName(), valid, value);
	return valid;
}
 
Example 11
Source File: GenericModelChecks.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Checks if the field is required.
 *
 * @return boolean If the field isn't <code>null</code> and
 * has a length greater than zero, <code>true</code> is returned.  
 * Otherwise <code>false</code>.
 */
public static boolean validateRequired(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
   String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

   boolean valid = !GenericValidator.isBlankOrNull(value);
   results.add(field, action.getName(), valid, value);
   return valid;
}
 
Example 12
Source File: MailingModelChecks.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean validateOnePixel(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
   String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

   boolean valid = MailingModel.onePixelMap.containsKey(value.toLowerCase());
   results.add(field, action.getName(), valid, value);
   return valid;
}
 
Example 13
Source File: NotBlankValidator.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public boolean validate( CheckResultSourceInterface source, String propertyName,
  List<CheckResultInterface> remarks, ValidatorContext context ) {
  String value = ValidatorUtils.getValueAsString( source, propertyName );
  if ( GenericValidator.isBlankOrNull( value ) ) {
    JobEntryValidatorUtils.addFailureRemark(
      source, propertyName, VALIDATOR_NAME, remarks, JobEntryValidatorUtils.getLevelOnFail(
        context, VALIDATOR_NAME ) );
    return false;
  } else {
    return true;
  }
}
 
Example 14
Source File: MailingModelChecks.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean validateMailingType(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
   String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

   boolean valid = MailingModel.mailingTypeMap.containsKey(value.toLowerCase());
   results.add(field, action.getName(), valid, value);
   return valid;
}
 
Example 15
Source File: GenericModelChecks.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Checks if field is not negative assuming it is an integer
 * 
 * @param    value       The value validation is being performed on.
 * @param    field       Description of the field to be evaluated
 * @return   boolean     If the integer field is greater than or equals zero, returns
 *                        true, otherwise returns false.
 */
public static boolean validatePositiveOrZero(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
   String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

   boolean valid = GenericTypeValidator.formatInt(value).intValue() >= 0;
   results.add(field, action.getName(), valid, value);
   return valid;
}
 
Example 16
Source File: GenericModelChecks.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Checks if the field value is in email format or if it is null
 *
 * @return if the field is null or has a valid email format - true is returned (false otherwise)
 */
public static boolean validateEmailOrNull(Object bean, Field field,
        ValidatorResults results, ValidatorAction action) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

    boolean valid = value == null || AgnitasEmailValidator.getInstance().isValid(value);
    results.add(field, action.getName(), valid, value);
    return valid;
}
 
Example 17
Source File: FileExistsValidator.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public boolean validate( CheckResultSourceInterface source, String propertyName,
  List<CheckResultInterface> remarks, ValidatorContext context ) {

  String filename = ValidatorUtils.getValueAsString( source, propertyName );
  VariableSpace variableSpace = getVariableSpace( source, propertyName, remarks, context );
  boolean failIfDoesNotExist = getFailIfDoesNotExist( source, propertyName, remarks, context );

  if ( null == variableSpace ) {
    return false;
  }

  String realFileName = variableSpace.environmentSubstitute( filename );
  FileObject fileObject = null;
  try {
    fileObject = KettleVFS.getFileObject( realFileName, variableSpace );
    if ( fileObject == null || ( fileObject != null && !fileObject.exists() && failIfDoesNotExist ) ) {
      JobEntryValidatorUtils.addFailureRemark(
        source, propertyName, VALIDATOR_NAME, remarks, JobEntryValidatorUtils.getLevelOnFail(
          context, VALIDATOR_NAME ) );
      return false;
    }
    try {
      fileObject.close(); // Just being paranoid
    } catch ( IOException ignored ) {
      // Ignore close errors
    }
  } catch ( Exception e ) {
    JobEntryValidatorUtils.addExceptionRemark( source, propertyName, VALIDATOR_NAME, remarks, e );
    return false;
  }
  return true;
}
 
Example 18
Source File: GenericModelChecks.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean validateIntRange(Object bean, Field field, ValidatorResults results, ValidatorAction action) {
	String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
	if (value == null) {
		return true;
	}
	int intValue = Integer.parseInt(value);
	int min = Integer.parseInt(field.getVarValue("min"));
	int max = Integer.parseInt(field.getVarValue("max"));

	boolean valid = GenericValidator.isInRange(intValue, min, max);
	results.add(field, action.getName(), valid, value);
	return valid;
}
 
Example 19
Source File: GenericModelChecks.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean validateMaxLength(Object bean, Field field, ValidatorResults results, ValidatorAction action) {
	String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
	if (value == null) {
		return true;
	}
	int max = Integer.parseInt(field.getVarValue("maxlength"));

	boolean valid = GenericValidator.maxLength(value, max);
	results.add(field, action.getName(), valid, value);
	return valid;
}
 
Example 20
Source File: HcFtpToFileSystemConfigAction.java    From MicroCommunity with Apache License 2.0 4 votes vote down vote up
/**
 * 根据任务名称或任务ID模糊查询
 *
 * @return
 */
public String searchTaskByNameOrId(HttpServletRequest request) {

	String ftpItemJson = request.getParameter("ftpItemJson");
	JSONObject ftpItemJsonObj = null;
	try {
		// 校验格式是否正确
		// {"taskName":"经办人照片同步处理"}
		ftpItemJsonObj = JSONObject.parseObject(ftpItemJson);
	} catch (Exception e) {
		logger.error("传入参数格式不正确:" + ftpItemJson, e);
		resultMsg = createResultMsg("1999", "传入参数格式不正确:" + ftpItemJson, "");
		return "addFtpItem";
	}

	// 将ftpItemJson装为Map保存操作
	Map paramIn = JSONObject.parseObject(ftpItemJsonObj.toJSONString(), Map.class);

	String taskNameOrTaskId = paramIn.get("taskName") == null ? "1" : paramIn.get("taskName").toString();

	taskNameOrTaskId = ValidatorUtils.getValueAsString(paramIn, "taskName");

	// 规则校验
	JSONObject data = new JSONObject();
	data.put("total", 1); // 搜索不进行分页处理
	data.put("currentPage", 1);
	List<Map> ftpItems = null;
	// 说明是taskId
	if (GenericValidator.isInt(taskNameOrTaskId) || GenericValidator.isLong(taskNameOrTaskId)) {
		// 根据taskId 查询记录
		paramIn.put("taskId", taskNameOrTaskId);
		Map ftpItem = iHcFtpFileDAO.queryFtpItemByTaskId(paramIn);
		if (ftpItem != null && ftpItem.containsKey("FTP_ITEM_ATTRS")) {
			ftpItem.remove("FTP_ITEM_ATTRS");// 前台暂时用不到,所以这里将属性移除

			ftpItems = new ArrayList<Map>();
			ftpItems.add(ftpItem);
		}
	} else {
		ftpItems = iHcFtpFileDAO.searchFtpItemByTaskName(paramIn);
	}

	JSONArray rows = new JSONArray();
	if (ftpItems != null && ftpItems.size() > 0) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		for (Map ftpItemMap : ftpItems) {

			// 处理时间显示和界面显示传输类型
			ftpItemMap.put("U_OR_D_NAME", ftpItemMap.get("U_OR_D"));// 暂且写死,最终还是读取配置
			ftpItemMap.put("CREATE_DATE", df.format(ftpItemMap.get("CREATE_DATE")));// 暂且写死,最终还是读取配置
			rows.add(JSONObject.parseObject(JSONObject.toJSONString(ftpItemMap)));
		}
	}
	data.put("rows", rows);
	resultMsg = data;
	return "searchTaskByNameOrId";
}