use of org.akaza.openclinica.bean.core.NumericComparisonOperator in project OpenClinica by OpenClinica.
the class Validator method validate.
protected HashMap validate(String fieldName, Validation v) {
switch(v.getType()) {
case NO_BLANKS:
if (isBlank(fieldName)) {
addError(fieldName, v);
}
break;
case IS_A_NUMBER:
if (!isNumber(fieldName)) {
addError(fieldName, v);
}
break;
case IS_IN_RANGE:
float lowerBound = v.getFloat(0);
float upperBound = v.getFloat(1);
if (!isInRange(fieldName, lowerBound, upperBound)) {
addError(fieldName, v);
}
break;
case IS_A_DATE:
if (!isDate(fieldName)) {
addError(fieldName, v);
}
break;
case IS_DATE_TIME:
if (!isDateTime(fieldName)) {
addError(fieldName, v);
}
break;
case CHECK_SAME:
String compareField = v.getString(0);
if (!isSame(fieldName, compareField)) {
addError(fieldName, v);
}
break;
case IS_A_EMAIL:
if (!isEmail(fieldName)) {
addError(fieldName, v);
}
break;
case IS_A_PHONE_NUMBER:
if (!isPhoneNumber(fieldName)) {
addError(fieldName, v);
}
break;
case ENTITY_EXISTS:
EntityDAO edao = (EntityDAO) v.getArg(0);
if (!entityExists(fieldName, edao)) {
addError(fieldName, v);
}
break;
case ENTITY_EXISTS_IN_STUDY:
AuditableEntityDAO dao = (AuditableEntityDAO) v.getArg(0);
StudyBean study = (StudyBean) v.getArg(1);
if (!entityExistsInStudy(fieldName, dao, study)) {
addError(fieldName, v);
}
break;
case USERNAME_UNIQUE:
UserAccountDAO udao = (UserAccountDAO) v.getArg(0);
if (!usernameUnique(fieldName, udao)) {
addError(fieldName, v);
}
break;
case IS_AN_INTEGER:
if (!isInteger(fieldName)) {
addError(fieldName, v);
}
break;
// break;
case IS_IN_SET:
ArrayList set = (ArrayList) v.getArg(0);
if (!isInSet(fieldName, set)) {
addError(fieldName, v);
}
break;
case IS_A_PASSWORD:
if (!lengthComparesToStaticValue(fieldName, NumericComparisonOperator.GREATER_THAN_OR_EQUAL_TO, PASSWORD_MINIMUM_LENGTH)) {
addError(fieldName, v);
}
break;
case IS_A_USERNAME:
if (!isUsername(fieldName)) {
addError(fieldName, v);
}
break;
case IS_VALID_TERM:
TermType termType = (TermType) v.getArg(0);
if (!isValidTerm(fieldName, termType)) {
addError(fieldName, v);
}
break;
case COMPARES_TO_STATIC_VALUE:
NumericComparisonOperator operator = (NumericComparisonOperator) v.getArg(0);
float compareTo = v.getFloat(1);
if (!comparesToStaticValue(fieldName, operator, compareTo)) {
addError(fieldName, v);
}
break;
case LENGTH_NUMERIC_COMPARISON:
NumericComparisonOperator operator2 = (NumericComparisonOperator) v.getArg(0);
int compareTo2 = v.getInt(1);
if (!lengthComparesToStaticValue(fieldName, operator2, compareTo2)) {
addError(fieldName, v);
}
break;
case DATE_IS_AFTER_OR_EQUAL:
String earlierDateFieldName = v.getString(0);
if (!isDateAfterOrEqual(fieldName, earlierDateFieldName)) {
addError(fieldName, v);
}
break;
case NO_BLANKS_SET:
if (isSetBlank(fieldName)) {
addError(fieldName, v);
}
break;
case IN_RESPONSE_SET:
ResponseSetBean rsb = (ResponseSetBean) v.getArg(0);
if (!isInResponseSet(fieldName, rsb, true)) {
addError(fieldName, v);
}
break;
case IN_RESPONSE_SET_COMMA_SEPERATED:
ResponseSetBean rsbs = (ResponseSetBean) v.getArg(0);
if (!isInResponseSetCommaSeperated(fieldName, rsbs, true)) {
addError(fieldName, v);
}
break;
case IN_RESPONSE_SET_SINGLE_VALUE:
ResponseSetBean rsbSingle = (ResponseSetBean) v.getArg(0);
if (!isInResponseSet(fieldName, rsbSingle, false)) {
addError(fieldName, v);
}
break;
case MATCHES_INITIAL_DATA_ENTRY_VALUE:
String oldValue = v.getString(0);
boolean isMultiple = v.getBoolean(1);
if (!valueMatchesInitialValue(fieldName, oldValue, isMultiple)) {
addError(fieldName, v);
}
break;
case DIFFERENT_NUMBER_OF_GROUPS_IN_DDE:
addError(fieldName, v);
break;
case IS_REQUIRED:
if (isBlank(fieldName)) {
addError(fieldName, v);
}
break;
case DATE_IN_PAST:
if (!isDateInPast(fieldName)) {
addError(fieldName, v);
}
break;
case MATCHES_REGULAR_EXPRESSION:
if (!matchesRegex(fieldName, v.getString(0))) {
addError(fieldName, v);
}
break;
case CHECK_DIFFERENT:
String old = v.getString(0);
if (isSame(fieldName, old)) {
addError(fieldName, v);
}
break;
case IS_A_DATE_WITHOUT_REQUIRED_CHECK:
if (!isDateWithoutRequiredCheck(fieldName)) {
addError(fieldName, v);
}
break;
case CALCULATION_FAILED:
addError(fieldName, v);
break;
case IS_AN_RULE:
ArrayList<String> messages = (ArrayList<String>) v.getArg(0);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < messages.size(); i++) {
sb.append(messages.get(i));
if (i != messages.size() - 1)
sb.append(" , ");
logger.debug(messages.get(i));
}
v.setErrorMessage(sb.toString());
addError(fieldName, v);
break;
case IS_PARTIAL_DATE:
boolean isPDate = Boolean.FALSE;
String fieldValue = getFieldValue(fieldName);
if (fieldValue != null) {
// resformat.getString("date_format_string"))) {
if (StringUtil.isFormatDate(fieldValue, resformat.getString("date_format_string")) || StringUtil.isPartialYear(fieldValue, "yyyy") || StringUtil.isPartialYearMonth(fieldValue, resformat.getString("date_format_year_month"))) {
isPDate = true;
}
}
if (!isPDate) {
addError(fieldName, v);
}
break;
case IS_VALID_WIDTH_DECIMAL:
ArrayList<String> params = (ArrayList<String>) v.getArg(0);
String dataType = params.get(0);
String widthDecimal = params.get(1);
StringBuffer error = this.validateFieldWidthDecimal(fieldName, dataType, widthDecimal);
if (error.length() > 0) {
addError(fieldName, error.toString());
}
break;
case BARCODE_EAN_13:
EanCheckDigit eanChk = new EanCheckDigit();
if (!eanChk.isValid(getFieldValue(fieldName))) {
addError(fieldName, v);
}
break;
case TO_HIDE_CONDITIONAL_DISPLAY:
addError(fieldName, v);
break;
}
return errors;
}
use of org.akaza.openclinica.bean.core.NumericComparisonOperator in project OpenClinica by OpenClinica.
the class Validator method addError.
protected void addError(String fieldName, Validation v) {
// locale = request.getLocale(); htaycher : set in constructor
resexception = ResourceBundleProvider.getExceptionsBundle(locale);
resword = ResourceBundleProvider.getWordsBundle(locale);
String errorMessage = "";
if (v.isErrorMessageSet()) {
errorMessage = v.getErrorMessage();
} else {
switch(v.getType()) {
case NO_BLANKS:
errorMessage = resexception.getString("field_not_blank");
break;
case IS_A_NUMBER:
errorMessage = resexception.getString("field_should_number");
break;
case IS_IN_RANGE:
float lowerBound = v.getFloat(0);
float upperBound = v.getFloat(1);
errorMessage = resexception.getString("input_should_be_between") + new Float(lowerBound).intValue() + " " + resword.getString("and") + new Float(upperBound).intValue() + ".";
break;
case IS_A_DATE:
errorMessage = resexception.getString("input_not_valid_date") + getDateRegEx().getDescription() + " " + resexception.getString("format1") + ".";
break;
case IS_DATE_TIME:
errorMessage = resexception.getString("input_not_valid_date_time") + getDateTimeRegEx().getDescription() + " " + resexception.getString("format2") + ".";
break;
case CHECK_SAME:
errorMessage = resexception.getString("anwer_not_match");
break;
case IS_A_EMAIL:
errorMessage = resexception.getString("input_not_valid_email") + EMAIL.getDescription() + " " + resexception.getString("format3") + ".";
break;
case IS_A_PHONE_NUMBER:
errorMessage = resexception.getString("input_not_valid_phone") + getPhoneRegEx().getDescription() + " " + resexception.getString("format4") + ".";
break;
case ENTITY_EXISTS:
errorMessage = resexception.getString("not_select_valid_entity");
break;
case ENTITY_EXISTS_IN_STUDY:
errorMessage = resexception.getString("not_select_valid_entity_current_study");
break;
case USERNAME_UNIQUE:
errorMessage = resexception.getString("username_already_exists");
break;
case IS_AN_INTEGER:
errorMessage = resexception.getString("input_not_integer");
break;
// break;
case IS_IN_SET:
errorMessage = resexception.getString("input_not_acceptable_option");
break;
case IS_A_PASSWORD:
errorMessage = resexception.getString("password_must_be_at_least") + PASSWORD_MINIMUM_LENGTH + " " + resword.getString("characters_long") + ".";
break;
case IS_A_USERNAME:
errorMessage = resexception.getString("input_not_valid_username") + USERNAME.getDescription() + " " + resexception.getString("format5") + ".";
break;
case IS_VALID_TERM:
errorMessage = resexception.getString("input_invalid");
break;
case COMPARES_TO_STATIC_VALUE:
NumericComparisonOperator operator = (NumericComparisonOperator) v.getArg(0);
float compareTo = v.getFloat(1);
errorMessage = resexception.getString("input_provided_is_not") + operator.getDescription() + " " + new Float(compareTo).intValue() + ".";
break;
case LENGTH_NUMERIC_COMPARISON:
NumericComparisonOperator operator2 = (NumericComparisonOperator) v.getArg(0);
int compareTo2 = v.getInt(1);
errorMessage = resexception.getString("input_provided_is_not") + operator2.getDescription() + " " + compareTo2 + " " + resword.getString("characters_long") + ".";
break;
case DATE_IS_AFTER_OR_EQUAL:
String earlierDateFieldName = v.getString(0);
String earlierDateValue = getFieldValue(earlierDateFieldName);
if (earlierDateValue == null || earlierDateValue.equals("")) {
errorMessage = resexception.getString("input_provided_not_precede_earlier");
} else {
errorMessage = resexception.getString("input_provided_not_precede") + earlierDateValue + ".";
}
break;
case NO_BLANKS_SET:
errorMessage = resexception.getString("must_choose_leat_one_value");
break;
case IN_RESPONSE_SET:
errorMessage = resexception.getString("all_values_must_from_specified");
break;
case IN_RESPONSE_SET_COMMA_SEPERATED:
errorMessage = resexception.getString("all_values_must_from_specified");
break;
case IN_RESPONSE_SET_SINGLE_VALUE:
errorMessage = resexception.getString("values_must_from_valid");
break;
case DIFFERENT_NUMBER_OF_GROUPS_IN_DDE:
errorMessage = resexception.getString("different_number_of_groups");
break;
case MATCHES_INITIAL_DATA_ENTRY_VALUE:
String value = v.getString(0);
// errorMessage = v.getErrorMessage();//should be set at the DDE
// stage, tbh 112007
errorMessage = resexception.getString("value_not_match") + " : " + value;
break;
case IS_REQUIRED:
errorMessage = resexception.getString("input_is_required");
break;
case DATE_IN_PAST:
errorMessage = resexception.getString("date_provided_not_past");
break;
case MATCHES_REGULAR_EXPRESSION:
errorMessage = resexception.getString("input_not_match_regular_expression") + v.getString(0) + ".";
break;
case IS_A_DATE_WITHOUT_REQUIRED_CHECK:
errorMessage = resexception.getString("input_not_valid_date") + getDateRegEx().getDescription() + " " + resexception.getString("format1") + ".";
break;
case IS_AN_RULE:
errorMessage = resexception.getString("input_not_integer");
break;
case IS_PARTIAL_DATE:
errorMessage = resexception.getString("input_not_partial_date") + " (" + resformat.getString("date_format_year") + ", or " + resformat.getString("date_format_year_month") + ", or " + resformat.getString("date_format_string");
break;
case BARCODE_EAN_13:
errorMessage = resexception.getString("input_not_barcode");
break;
case TO_HIDE_CONDITIONAL_DISPLAY:
errorMessage = v.getErrorMessage();
}
}
// logger.info("<<<error added: "+errorMessage+" to "+fieldName);
addError(fieldName, errorMessage);
}
use of org.akaza.openclinica.bean.core.NumericComparisonOperator in project OpenClinica by OpenClinica.
the class Validator method processCRFValidationFunction.
public static Validation processCRFValidationFunction(String inputFunction) throws Exception {
ResourceBundle resexception = ResourceBundleProvider.getExceptionsBundle();
Validation v = null;
if (inputFunction.equals("func: barcode(EAN-13)")) {
v = new Validation(BARCODE_EAN_13);
return v;
}
String fname;
ArrayList args;
HashMap numArgsByFunction = new HashMap();
numArgsByFunction.put("range", new Integer(2));
numArgsByFunction.put("gt", new Integer(1));
numArgsByFunction.put("lt", new Integer(1));
numArgsByFunction.put("gte", new Integer(1));
numArgsByFunction.put("lte", new Integer(1));
numArgsByFunction.put("ne", new Integer(1));
numArgsByFunction.put("eq", new Integer(1));
numArgsByFunction.put("getExternalValue", new Integer(3));
HashMap valTypeByFunction = new HashMap();
valTypeByFunction.put("range", new Integer(Validator.IS_IN_RANGE));
valTypeByFunction.put("gt", new Integer(Validator.COMPARES_TO_STATIC_VALUE));
valTypeByFunction.put("lt", new Integer(Validator.COMPARES_TO_STATIC_VALUE));
valTypeByFunction.put("gte", new Integer(Validator.COMPARES_TO_STATIC_VALUE));
valTypeByFunction.put("lte", new Integer(Validator.COMPARES_TO_STATIC_VALUE));
valTypeByFunction.put("ne", new Integer(Validator.COMPARES_TO_STATIC_VALUE));
valTypeByFunction.put("eq", new Integer(Validator.COMPARES_TO_STATIC_VALUE));
HashMap compareOpByFunction = new HashMap();
compareOpByFunction.put("gt", NumericComparisonOperator.GREATER_THAN);
compareOpByFunction.put("lt", NumericComparisonOperator.LESS_THAN);
compareOpByFunction.put("gte", NumericComparisonOperator.GREATER_THAN_OR_EQUAL_TO);
compareOpByFunction.put("lte", NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO);
compareOpByFunction.put("ne", NumericComparisonOperator.NOT_EQUALS);
compareOpByFunction.put("eq", NumericComparisonOperator.EQUALS);
// if(!inputFunction.equals("func:barcode(EAN-13)")){
// this is a regular expression for the pattern:
// func: fname(arg1 [, arg2] ... [, argn])
// if it matches then:
// fname is group 1
// arg1 is group 2
// "," + arg2 is group 3
// ...
// "," + argn is group n + 1
Pattern funcPattern = Pattern.compile("func:\\s*([A-Za-z]+)\\(([^,]*)?(,[^,]*)*\\)");
Matcher funcMatcher = funcPattern.matcher(inputFunction);
if (!funcMatcher.matches()) {
throw new Exception(resexception.getString("syntax_incorrect"));
// error: the syntax is incorrect, should be func:
// fname(arg1,...,argn)
}
int numGroups = funcMatcher.groupCount();
// note that numGroups must be > 1
fname = funcMatcher.group(1);
args = new ArrayList();
for (int i = 2; i <= funcMatcher.groupCount(); i++) {
String arg = funcMatcher.group(i);
if (StringUtil.isBlank(arg)) {
continue;
}
// this means we need to get rid of the preceding ,
if (i >= 3) {
arg = arg.substring(1);
}
arg = arg.trim();
args.add(arg);
}
if (!fname.equalsIgnoreCase("range") && !fname.equalsIgnoreCase("gt") && !fname.equalsIgnoreCase("lt") && !fname.equalsIgnoreCase("gte") && !fname.equalsIgnoreCase("lte") && !fname.equalsIgnoreCase("eq") && !fname.equalsIgnoreCase("ne") && !fname.equalsIgnoreCase("getexternalvalue")) {
throw new Exception(resexception.getString("validation_column_invalid_function"));
}
// test that the right number of arguments have been provdided; complain
// if not
Integer numProperArgsInFunction = (Integer) numArgsByFunction.get(fname);
if (args.size() != numProperArgsInFunction.intValue()) {
throw new Exception(resexception.getString("validation_column_invalid_function") + ": " + resexception.getString("number_of_arguments_incorrect"));
}
// test that each argument is a number; complain if not
for (int i = 0; i < args.size(); i++) {
int ord = i + 1;
try {
float f = Float.parseFloat((String) args.get(i));
} catch (Exception e) {
throw new Exception(resexception.getString("validation_column_invalid_function") + ": " + resexception.getString("argument") + ord + " " + resexception.getString("is_not_a_number"));
}
}
// success - all tests have been passed
// now we compose the validation object created by this function
Integer valType = (Integer) valTypeByFunction.get(fname);
v = new Validation(valType.intValue());
if (!fname.equalsIgnoreCase("range")) {
NumericComparisonOperator operator = (NumericComparisonOperator) compareOpByFunction.get(fname);
v.addArgument(operator);
}
for (int i = 0; i < args.size(); i++) {
float f = Float.parseFloat((String) args.get(i));
v.addArgument(f);
}
// }
return v;
}
Aggregations