/** * When a correction criterion is about to be added to a group, this will check if it is valid, * meaning that the field name is not blank * * @param correctionCriteria validated correction criteria * @return true if correction criteria is valid for adding */ public static boolean validCorrectionCriteriaForAdding(CorrectionCriteria correctionCriteria) { String fieldName = correctionCriteria.getCorrectionFieldName(); if (StringUtils.isBlank(fieldName)) { return false; } return true; }
/** * Compares string data * * @param cc criteria * @param fieldTestValue test value * @param fieldActualValueString actual value * @return flag true if matches with criteria */ public static boolean compareStringData( CorrectionCriteria cc, String fieldTestValue, String fieldActualValueString) { if ("eq".equals(cc.getCorrectionOperatorCode())) { return fieldActualValueString.equals(fieldTestValue); } else if ("ne".equals(cc.getCorrectionOperatorCode())) { return (!fieldActualValueString.equals(fieldTestValue)); } else if ("sw".equals(cc.getCorrectionOperatorCode())) { return fieldActualValueString.startsWith(fieldTestValue); } else if ("ew".equals(cc.getCorrectionOperatorCode())) { return fieldActualValueString.endsWith(fieldTestValue); } else if ("ct".equals(cc.getCorrectionOperatorCode())) { return (fieldActualValueString.indexOf(fieldTestValue) > -1); } else if ("lt".equals(cc.getCorrectionOperatorCode())) { return (fieldActualValueString.compareTo(fieldTestValue) < 0); } else if ("le".equals(cc.getCorrectionOperatorCode())) { return (fieldActualValueString.compareTo(fieldTestValue) <= 0); } else if ("gt".equals(cc.getCorrectionOperatorCode())) { return (fieldActualValueString.compareTo(fieldTestValue) > 0); } else if ("ge".equals(cc.getCorrectionOperatorCode())) { return (fieldActualValueString.compareTo(fieldTestValue) >= 0); } throw new IllegalArgumentException("Unknown operator: " + cc.getCorrectionOperatorCode()); }
/** * Returns whether an origin entry matches the passed in criteria. If both the criteria and actual * value are both String types and are empty, null, or whitespace only, then they will match. * * @param cc correction criteria to test against origin entry * @param oe origin entry to test * @return true if origin entry matches the passed in criteria */ public static boolean entryMatchesCriteria(CorrectionCriteria cc, OriginEntryFull oe) { OriginEntryFieldFinder oeff = new OriginEntryFieldFinder(); Object fieldActualValue = oe.getFieldValue(cc.getCorrectionFieldName()); String fieldTestValue = StringUtils.isBlank(cc.getCorrectionFieldValue()) ? "" : cc.getCorrectionFieldValue(); String fieldType = oeff.getFieldType(cc.getCorrectionFieldName()); String fieldActualValueString = convertToString(fieldActualValue, fieldType); if ("String".equals(fieldType) || "sw".equals(cc.getCorrectionOperatorCode()) || "ew".equals(cc.getCorrectionOperatorCode()) || "ct".equals(cc.getCorrectionOperatorCode())) { return compareStringData(cc, fieldTestValue, fieldActualValueString); } int compareTo = 0; try { if (fieldActualValue == null) { return false; } if ("Integer".equals(fieldType)) { compareTo = ((Integer) fieldActualValue).compareTo(Integer.parseInt(fieldTestValue)); } if ("KualiDecimal".equals(fieldType)) { compareTo = ((KualiDecimal) fieldActualValue) .compareTo(new KualiDecimal(Double.parseDouble(fieldTestValue))); } if ("BigDecimal".equals(fieldType)) { compareTo = ((BigDecimal) fieldActualValue) .compareTo(new BigDecimal(Double.parseDouble(fieldTestValue))); } if ("Date".equals(fieldType)) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); compareTo = ((Date) fieldActualValue).compareTo(df.parse(fieldTestValue)); } } catch (Exception e) { // any exception while parsing data return false return false; } return compareTo(compareTo, cc.getCorrectionOperatorCode()); }
/** * When a document is about to be saved, this will check if it is valid, meaning that the field * name and value are both blank * * @param correctionCriteria validated correction criteria * @return true if correction criteria is valid for saving */ public static boolean validCorrectionCriteriaForSaving(CorrectionCriteria correctionCriteria) { return correctionCriteria == null || (StringUtils.isBlank(correctionCriteria.getCorrectionFieldName()) && StringUtils.isBlank(correctionCriteria.getCorrectionFieldValue())); }