Example #1
0
  @Override
  public Object parse(String jsonStr, Class<?> clazz, Type genericType) {
    Object bean = null;

    if (clazz == null) {

      throw new AJAXParserException("No specific class type.");
    }

    ValidationException validationException = new ValidationException();
    try {
      bean = dispatcher(jsonStr, clazz, genericType, validationException);
    } catch (Exception e) {
      if (e instanceof ValidationException) {
        throw (ValidationException) e;
      } else {
        throw new AJAXParserException(e.getMessage());
      }
    }

    if (validationException.size() > 0) {
      throw validationException;
    }

    return bean;
  }
Example #2
0
  protected boolean validateValue(
      Class<?> beanClazz, String name, JSONObject jsonObj, ValidationException validationException)
      throws JSONException {
    boolean rightValue = true;

    try {
      Field field = beanClazz.getDeclaredField(name);
      Annotation[] annotations = field.getDeclaredAnnotations();

      ArrayList<Annotation> validators = new ArrayList<Annotation>();
      boolean inputRequired = false;
      for (Annotation annotation : annotations) {

        if (AnnotationValidator.isValidationAnnotation(annotation)) {
          validators.add(annotation);
        }

        if (!inputRequired) {
          if (annotation instanceof com.maxiaohua.genealogy.fw.core.validator.type.NotNull
              || annotation instanceof com.maxiaohua.genealogy.fw.core.validator.type.NotEmpty) {
            inputRequired = true;
          } else if (annotation instanceof MultiField) {
            MultiField multiField = (MultiField) annotation;
            if (multiField
                .validator()
                .equals(com.maxiaohua.genealogy.fw.core.validator.type.NotAllEmpty.class)) {
              inputRequired = true;
            }
          }
        }
      }

      boolean sitei = jsonObj.containsKey(name);

      if (inputRequired || (sitei && jsonObj.get(name) != null)) {

        Collections.sort(
            validators,
            new Comparator<Annotation>() {
              public int compare(Annotation arg0, Annotation arg1) {
                int result = -1;
                try {
                  Integer junban0 =
                      (Integer) arg0.getClass().getDeclaredMethod(KEY_JUNBAN).invoke(arg0);
                  Integer junban2 =
                      (Integer) arg1.getClass().getDeclaredMethod(KEY_JUNBAN).invoke(arg1);
                  result = junban0.compareTo(junban2);
                } catch (NoSuchMethodException nsme) {
                  errorLogger.writeErrorLog(nsme.getMessage());
                  appLogger.error(nsme.getMessage(), nsme);
                  debugLogger.error(nsme.getMessage(), nsme);
                } catch (InvocationTargetException ite) {
                  errorLogger.writeErrorLog(ite.getMessage());
                  appLogger.error(ite.getMessage(), ite);
                  debugLogger.error(ite.getMessage(), ite);

                } catch (IllegalAccessException iae) {
                  errorLogger.writeErrorLog(iae.getMessage());
                  appLogger.error(iae.getMessage(), iae);
                  debugLogger.error(iae.getMessage(), iae);
                }
                return result;
              }
            });

        for (Annotation annotation : validators) {

          if (annotation instanceof MultiField) {
            MultiField multiField = (MultiField) annotation;
            String[] fieldNames = multiField.others();
            if (fieldNames != null) {

              Object[] values = new Object[fieldNames.length + 1];
              if (sitei) {
                values[0] = jsonObj.get(name);
              }
              for (int i = 0; i < fieldNames.length; i++) {
                if (jsonObj.containsKey(fieldNames[i])) {
                  values[i + 1] = jsonObj.get(fieldNames[i]);
                }
              }

              AnnotationValidator validator = AnnotationValidationUtil.validate(values, annotation);

              if (validator != null) {

                String errorCode = validator.getErrorCode();
                String fieldBungen = beanClazz.getName() + "." + name;
                Alias alias = (Alias) field.getAnnotation(Alias.class);
                if (alias != null) {
                  fieldBungen = alias.value();
                }
                String[] parameters = null;
                String[] ps = validator.getMsgParameters();
                if (ps == null) {
                  parameters = new String[2];
                  parameters[0] = fieldBungen;
                  StringBuffer othersBungen = new StringBuffer();
                  for (int i = 0; i < fieldNames.length; i++) {
                    try {
                      String bungen = beanClazz.getName() + "." + name;
                      Field f = beanClazz.getDeclaredField(fieldNames[i]);
                      Alias a = (Alias) f.getAnnotation(Alias.class);
                      if (a != null) {
                        bungen = a.value();
                      }
                      if (i == 0) {
                        othersBungen.append(bungen);
                      } else {
                        othersBungen.append(",").append(bungen);
                      }
                    } catch (NoSuchFieldException nsfe) {
                      nsfe.printStackTrace();
                    }
                  }
                  parameters[1] = othersBungen.toString();
                } else {
                  parameters = new String[ps.length + 1];
                  parameters[0] = fieldBungen;
                  for (int i = 0; i < ps.length; i++) {
                    parameters[i + 1] = ps[i];
                  }
                }

                validationException.addValidationException(errorCode, parameters, null);
                rightValue = false;
              }
            }
          } else {
            Object value = null;
            if (sitei) {
              value = jsonObj.get(name);
            }

            AnnotationValidator validator = AnnotationValidationUtil.validate(value, annotation);

            if (validator != null) {

              String errorCode = validator.getErrorCode();
              String fieldBungen = beanClazz.getName() + "." + name;
              Alias alias = (Alias) field.getAnnotation(Alias.class);
              if (alias != null) {
                fieldBungen = alias.value();
              }
              String[] parameters = null;
              String[] ps = validator.getMsgParameters();
              if (ps == null) {
                parameters = new String[] {fieldBungen};
              } else {
                parameters = new String[ps.length + 1];
                parameters[0] = fieldBungen;
                for (int i = 0; i < ps.length; i++) {
                  parameters[i + 1] = ps[i];
                }
              }

              validationException.addValidationException(errorCode, parameters, null);
              rightValue = false;
            }
          }

          if (!rightValue) {
            break;
          }
        }
      }
    } catch (NoSuchFieldException nsfe) {

    }

    return rightValue;
  }