private static void setupBuiltinPreStore1Handlers(AnnotationRegistry registry) {
registry.register( DefaultGUID.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != String.class)
throw new ValidationException("The @DefaultGUID field must be String type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
Object value = field.get(object);
if (value == null || value.toString().length() == 0) {
boolean isShort = ReflectUtil.getAnnoValue(fieldAnnotation, "isShort", false);
String uuidStr = isShort ? BaseXUtil.uuid8() : BaseXUtil.uuid16();
field.set(object, uuidStr);
}
}
});
registry.register( ModifiedTime.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != java.util.Date.class)
throw new ValidationException("The @ModifiedTime field must be java.util.Date type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
field.set(object, new Date());
}
});
registry.register( VersionLocking.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != Integer.class && field.getType() != int.class)
throw new ValidationException("The @VersionLocking field must be int type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
ReflectUtil.incrementField(object, field, 1);
}
});
registry.register( ToUpper.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != String.class)
throw new ValidationException("The @ToUpper field must be String type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
String value = (String)field.get(object);
if (value != null) {
field.set(object, value.toUpperCase());
}
}
});
registry.register( ToLower.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != String.class)
throw new ValidationException("The @ToLower field must be String type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
String value = (String)field.get(object);
if (value != null) {
field.set(object, value.toLowerCase());
}
}
});
registry.register( Trim.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != String.class)
throw new ValidationException("The @Trim field must be String type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
String value = (String)field.get(object);
if (value != null) {
field.set(object, value.trim());
}
}
});
registry.register( RemoveChar.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != String.class)
throw new ValidationException("The @RemoveChar field must be String type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
char charToRemove = ReflectUtil.getAnnoValue(fieldAnnotation, "charToRemove", ' ');
String value = (String)field.get(object);
if (value != null) {
field.set(object, StringUtils.remove(value, charToRemove));
}
}
});
registry.register( RemoveAlphaDigits.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != String.class)
throw new ValidationException("The @RemoveAlphaDigits field must be String type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
boolean removeDigits = ReflectUtil.getAnnoValue(fieldAnnotation, "removeDigits", false);
String value = (String)field.get(object);
if (value != null) {
if (removeDigits)
field.set(object, value.replaceAll("[\\d]", "")); // remove all digits
else
field.set(object, value.replaceAll("[^\\d]", "")); // remove all alphas (non-digits)
}
}
});
registry.register( MaxValue.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != Integer.class && field.getType() != int.class &&
field.getType() != Long.class && field.getType() != long.class &&
field.getType() != Short.class && field.getType() != short.class &&
field.getType() != Float.class && field.getType() != float.class &&
field.getType() != Double.class && field.getType() != double.class)
throw new ValidationException("The @MaxValue field must be number type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
Object maxValueObj = ReflectUtil.getAnnoValue(fieldAnnotation, "value", (Object)null);
double maxValue = ((Double)ConvertUtils.convert(maxValueObj, Double.class)).doubleValue();
Object valueObj = field.get(object);
double value = ((Double)ConvertUtils.convert(valueObj, Double.class)).doubleValue();
value = (value > maxValue ? maxValue : value);
field.set(object, ConvertUtils.convert(value, field.getType()));
}
});
registry.register( MinValue.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != Integer.class && field.getType() != int.class &&
field.getType() != Long.class && field.getType() != long.class &&
field.getType() != Short.class && field.getType() != short.class &&
field.getType() != Float.class && field.getType() != float.class &&
field.getType() != Double.class && field.getType() != double.class)
throw new ValidationException("The @MinValue field must be number type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
Object minValueObj = ReflectUtil.getAnnoValue(fieldAnnotation, "value", (Object)null);
double minValue = ((Double)ConvertUtils.convert(minValueObj, Double.class)).doubleValue();
Object valueObj = field.get(object);
double value = ((Double)ConvertUtils.convert(valueObj, Double.class)).doubleValue();
value = (value < minValue ? minValue : value);
field.set(object, ConvertUtils.convert(value, field.getType()));
}
});
registry.register( AbsValue.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != Integer.class && field.getType() != int.class &&
field.getType() != Long.class && field.getType() != long.class &&
field.getType() != Short.class && field.getType() != short.class &&
field.getType() != Float.class && field.getType() != float.class &&
field.getType() != Double.class && field.getType() != double.class)
throw new ValidationException("The @AbsValue field must be number type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
Object valueObj = field.get(object);
double value = ((Double)ConvertUtils.convert(valueObj, Double.class)).doubleValue();
value = Math.abs(value);
field.set(object, ConvertUtils.convert(value, field.getType()));
}
});
registry.register( CeilValue.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != Integer.class && field.getType() != int.class &&
field.getType() != Long.class && field.getType() != long.class &&
field.getType() != Short.class && field.getType() != short.class &&
field.getType() != Float.class && field.getType() != float.class &&
field.getType() != Double.class && field.getType() != double.class)
throw new ValidationException("The @CeilValue field must be number type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
Object valueObj = field.get(object);
double value = ((Double)ConvertUtils.convert(valueObj, Double.class)).doubleValue();
value = Math.ceil(value);
field.set(object, ConvertUtils.convert(value, field.getType()));
}
});
registry.register( FloorValue.class, new AnnotationFieldHandler() {
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != Integer.class && field.getType() != int.class &&
field.getType() != Long.class && field.getType() != long.class &&
field.getType() != Short.class && field.getType() != short.class &&
field.getType() != Float.class && field.getType() != float.class &&
field.getType() != Double.class && field.getType() != double.class)
throw new ValidationException("The @FloorValue field must be number type. Field: " + field.getName());
}
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
Object valueObj = field.get(object);
double value = ((Double)ConvertUtils.convert(valueObj, Double.class)).doubleValue();