// Register a custom AnnotationFieldHandler that implements the functionality of MyDataConverter.
jsoda.registerData1Handler(MyDataConverter.class, new AnnotationFieldHandler() {
// checkModel() is called when a model class is registered to see if the annotated fields confirm to this annotation's requirement.
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != String.class)
throw new ValidationException("The field must be String type. Field: " + field.getName());
}
// handle() is called when a model object is stored
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
String value = (String)field.get(object);
if (value != null) {
String trimValue = (value.length() > 4 ? value.substring(0, 4) : value);
field.set(object, trimValue);
}
}
});
// Register a custom AnnotationFieldHandler that implements the functionality of MyValidator
jsoda.registerValidationHandler(MyValidator.class, new AnnotationFieldHandler() {
// checkModel() is called when a model class is registered to see if the annotated fields confirm to this annotation's requirement.
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != String.class)
throw new ValidationException("The field must be String type. Field: " + field.getName());
}
// handle() is called when a model object is stored
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
String value = (String)field.get(object);
if (value != null) {
if (value.startsWith("foobar"))
throw new ValidationException("Field cannot start with foobar. Field: " + field.getName());
}
}
});
// Register a custom AnnotationFieldHandler that implements the functionality of MyDataJoiner
// Note it's registered as stage 2 handler, to run after the stage 1 handlers.
jsoda.registerData2Handler(MyDataJoiner.class, new AnnotationFieldHandler() {
// checkModel() is called when a model class is registered to see if the annotated fields confirm to this annotation's requirement.
public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
if (field.getType() != String.class)
throw new ValidationException("The field must be String type. Field: " + field.getName());
}
// handle() is called when a model object is stored
public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
// Join the values from field1, field2, and field3, and convert it to upper case.