Package wwutil.model

Examples of wwutil.model.AnnotationFieldHandler


        throws Exception
    {
        Jsoda       jsoda = new Jsoda(new BasicAWSCredentials(key, secret));

        // 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());
            }
View Full Code Here


    // Stage 1 data handlers
    ////////////////////////////////////////////////////////////////////////////

    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 &&
View Full Code Here

    // Stage 2 data handlers
    ////////////////////////////////////////////////////////////////////////////

    private static void setupBuiltinPreStore2Handlers(AnnotationRegistry registry) {

        registry.register( DefaultComposite.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 @DefaultComposite field must be String type.  Field: " + field.getName());
            }

            public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
                fillDefaultComposite(field, object, allFieldMap);
            }
        });

        registry.register( FormatMsg.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 @FormatMsg field must be String type.  Field: " + field.getName());
            }
View Full Code Here

    // Validation handlers
    ////////////////////////////////////////////////////////////////////////////

    private static void setupBuiltinValidationHandlers(AnnotationRegistry registry) {

        registry.register( Required.class, new AnnotationFieldHandler() {
            public void checkModel(Annotation fieldAnnotation, Field field, Map<String, Field> allFieldMap) throws ValidationException {
            }

            public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
                if (field.get(object) == null)
                    throw new ValidationException("@Required field cannot be null.  Field: " + field.getName());
            }
        });

        registry.register( MaxSize.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 &&
                    field.getType() != String.class)
                    throw new ValidationException("The @MaxSize field must be number type or String type.  Field: " + field.getName());
            }

            public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
                Object  annValueObj = ReflectUtil.getAnnoValue(fieldAnnotation, "value", (Object)null);
                double  annValue = ((Double)ConvertUtils.convert(annValueObj, Double.class)).doubleValue();
                if (annValue != 0) {
                    Object  valueObj = field.get(object);
                    double  value;
                    if (valueObj instanceof String)
                        value = ((String)valueObj).length();
                    else
                        value = ((Double)ConvertUtils.convert(valueObj, Double.class)).doubleValue();
                    if (value > annValue)
                        throw new ValidationException("Field value " + valueObj + " exceeds MaxSize " + annValueObj + ".  Field: " + field.getName());
                }
            }
        });
       
        registry.register( MinSize.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 &&
                    field.getType() != String.class)
                    throw new ValidationException("The @MinSize field must be number type or String type.  Field: " + field.getName());
            }

            public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
                Object  annValueObj = ReflectUtil.getAnnoValue(fieldAnnotation, "value", (Object)null);
                double  annValue = ((Double)ConvertUtils.convert(annValueObj, Double.class)).doubleValue();
                if (annValue != 0) {
                    Object  valueObj = field.get(object);
                    double  value;
                    if (valueObj instanceof String)
                        value = ((String)valueObj).length();
                    else
                        value = ((Double)ConvertUtils.convert(valueObj, Double.class)).doubleValue();
                    if (value < annValue)
                        throw new ValidationException("Field value " + valueObj + " is less than MinSize " + annValueObj + ".  Field: " + field.getName());
                }
            }
        });

        registry.register( StartsWith.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 @StartsWith field must be String type.  Field: " + field.getName());
            }

            public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
                String  annValue = ReflectUtil.getAnnoValue(fieldAnnotation, "value", "");
                String  value = (String)field.get(object);
                if (value != null && !value.startsWith(annValue))
                    throw new ValidationException("Field value " + value + " does not start with " + annValue + ".  Field: " + field.getName());
            }
        });

        registry.register( EndsWith.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 @EndsWith field must be String type.  Field: " + field.getName());
            }

            public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
                String  annValue = ReflectUtil.getAnnoValue(fieldAnnotation, "value", "");
                String  value = (String)field.get(object);
                if (value != null && !value.endsWith(annValue))
                    throw new ValidationException("Field value " + value + " does not end with " + annValue + ".  Field: " + field.getName());
            }
        });

        registry.register( Contains.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 @Contains field must be String type.  Field: " + field.getName());
            }

            public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
                String  annValue = ReflectUtil.getAnnoValue(fieldAnnotation, "value", "");
                String  value = (String)field.get(object);
                if (value != null && !value.contains(annValue))
                    throw new ValidationException("Field value " + value + " does not contain " + annValue + ".  Field: " + field.getName());
            }
        });

        registry.register( NotContains.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 @NotContains field must be String type.  Field: " + field.getName());
            }

            public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
                String  annValue = ReflectUtil.getAnnoValue(fieldAnnotation, "value", "");
                String  value = (String)field.get(object);
                if (value != null && value.contains(annValue))
                    throw new ValidationException("Field value " + value + " contains " + annValue + ".  Field: " + field.getName());
            }
        });

        registry.register( RegexMatch.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 @RegexMatch field must be String type.  Field: " + field.getName());
            }

            public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
                String  annValue = ReflectUtil.getAnnoValue(fieldAnnotation, "value", "");
                String  value = (String)field.get(object);
                if (value != null && !Pattern.matches(annValue, value))
                    throw new ValidationException("Field value " + value + " does not match the regex " + annValue + ".  Field: " + field.getName());
            }
        });

        registry.register( EmailMatch.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 @EmailMatch 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) {
                    if (!sEmailPattern.matcher(value.toUpperCase()).matches())
                        throw new ValidationException("Field value " + value + " is not an email.  Field: " + field.getName());
                }
            }
        });



        registry.register( MaskMatch.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 @MaskMatch field must be String type.  Field: " + field.getName());
            }

            public void handle(Annotation fieldAnnotation, Object object, Field field, Map<String, Field> allFieldMap) throws Exception {
                char    digitMask = ReflectUtil.getAnnoValue(fieldAnnotation, "digitMask", '#');
                char    letterMask = ReflectUtil.getAnnoValue(fieldAnnotation, "letterMask", '@');
                char    anyMask = ReflectUtil.getAnnoValue(fieldAnnotation, "anyMask", '*');
                String  pattern = ReflectUtil.getAnnoValue(fieldAnnotation, "pattern", "");
                String  value = (String)field.get(object);
                if (value != null) {
                    MaskMatcher matcher = new MaskMatcher(pattern, digitMask, letterMask, anyMask);
                    if (!matcher.matches(value))
                        throw new ValidationException("Field value " + value + " does not match the mask pattern " + pattern + ".  Field: " + field.getName());
                }
            }
        });

        registry.register( OneOf.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 @OneOf field must be String type.  Field: " + field.getName());
            }

View Full Code Here

    // Stage 2 data handlers
    ////////////////////////////////////////////////////////////////////////////

    private static void setupBuiltinPostLoadHandlers(AnnotationRegistry registry) {

        registry.register( FormatMsg.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 @FormatMsg field must be String type.  Field: " + field.getName());
            }
View Full Code Here

TOP

Related Classes of wwutil.model.AnnotationFieldHandler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.