Пример #1
0
  private static <T> T mapToClazz(Map<String, String> map, Class<T> clazz, String formatStr)
      throws InstantiationException, IllegalAccessException, InvocationTargetException {
    Map<String, String> mapdata = getCaseInsensitveMap(map);
    T paypalClassicResponseModel;
    paypalClassicResponseModel = clazz.newInstance();
    for (Field field : FieldUtils.getAllFields(clazz)) {
      if (String.class.isAssignableFrom(field.getType())) {
        BeanUtils.setProperty(
            paypalClassicResponseModel,
            field.getName(),
            mapdata.get(getFormatedKeyName(formatStr, field.getName(), null)));
      }

      if (List.class.isAssignableFrom(field.getType())) {
        PaypalCollection paypalCollection = field.getAnnotation(PaypalCollection.class);
        ParameterizedType ty = ((ParameterizedType) field.getGenericType());
        if (ty.getActualTypeArguments()[0] instanceof Class) {
          Class subClazz = (Class<?>) ty.getActualTypeArguments()[0];
          if (String.class.isAssignableFrom(subClazz)) {
            if (paypalCollection != null && StringUtils.isNotEmpty(paypalCollection.format())) {
              List<String> values =
                  fetchPartialKey(
                      mapdata,
                      getFormatedKeyName(paypalCollection.format(), field.getName(), null));
              BeanUtils.setProperty(paypalClassicResponseModel, field.getName(), values);
            } else {
              List<String> values = fetchPartialKey(mapdata, field.getName().toUpperCase());
              BeanUtils.setProperty(paypalClassicResponseModel, field.getName(), values);
            }
          }
          if (PaypalClassicModel.class.isAssignableFrom(subClazz)) {
            if (paypalCollection != null && StringUtils.isNotEmpty(paypalCollection.format())) {
              BeanUtils.setProperty(
                  paypalClassicResponseModel,
                  field.getName(),
                  mapToClazzList(mapdata, subClazz, paypalCollection.format()));
            }
          }
        }
      }
      if (PaypalClassicModel.class.isAssignableFrom(field.getType())) {
        BeanUtils.setProperty(
            paypalClassicResponseModel, field.getName(), mapToClazz(mapdata, field.getType()));
      }
    }
    return paypalClassicResponseModel;
  }
Пример #2
0
  public static Map<String, String> toRequestMap(
      PaypalClassicModel paypalClassicModel, String format) {
    Map<String, String> returnMap = new HashMap<>();
    if (paypalClassicModel == null) {
      return returnMap;
    }
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    //        ValidatorFactory factory = Validation.byDefaultProvider()
    //                .configure()
    //                .messageInterpolator( new MyMessageInterpolator() )
    //                .buildValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<PaypalClassicModel>> violations =
        validator.validate(paypalClassicModel);
    if (violations.size() == 0) {
      try {
        for (Field field : FieldUtils.getAllFields(paypalClassicModel.getClass())) {
          if (field.getType().isAssignableFrom(String.class)) {
            if (BeanUtils.getProperty(paypalClassicModel, field.getName()) != null) {
              if (StringUtils.isNumeric(format)) {
                returnMap.put(
                    field.getName().toUpperCase() + format,
                    BeanUtils.getProperty(paypalClassicModel, field.getName()));
              } else {
                returnMap.put(
                    getFormatedKeyName(format, field.getName(), null),
                    BeanUtils.getProperty(paypalClassicModel, field.getName()));
              }
            }
          }
          if (PaypalClassicModel.class.isAssignableFrom(field.getType())) {

            if (PropertyUtils.getProperty(paypalClassicModel, field.getName()) != null) {
              returnMap.putAll(
                  toRequestMap(
                      (PaypalClassicModel)
                          PropertyUtils.getProperty(paypalClassicModel, field.getName()),
                      format));
            }
          }
          if (List.class.isAssignableFrom(field.getType())) {
            List listItem = (List) PropertyUtils.getProperty(paypalClassicModel, field.getName());
            if (listItem != null) {
              for (int i = 0; i < listItem.size(); i++) {
                if (listItem.get(i) instanceof PaypalClassicModel) {
                  PaypalCollection paypalCollection = field.getAnnotation(PaypalCollection.class);
                  if (paypalCollection != null
                      && StringUtils.isNotEmpty(paypalCollection.format())) {
                    String formatStr = field.getAnnotation(PaypalCollection.class).format();
                    returnMap.putAll(
                        toRequestMap(
                            (PaypalClassicModel) listItem.get(i),
                            getFormatedKeyName(formatStr, null, i)));
                  } else {
                    if (StringUtils.isNoneEmpty(format)) {
                      returnMap.putAll(
                          toRequestMap(
                              (PaypalClassicModel) listItem.get(i), format + String.valueOf(i)));
                    } else {
                      returnMap.putAll(
                          toRequestMap((PaypalClassicModel) listItem.get(i), String.valueOf(i)));
                    }
                  }
                }
                if (listItem.get(i) instanceof List) {
                  PaypalCollection paypalCollection = field.getAnnotation(PaypalCollection.class);
                  if (paypalCollection != null
                      && StringUtils.isNotEmpty(paypalCollection.format())) {
                    String formatStr = field.getAnnotation(PaypalCollection.class).format();
                    returnMap.putAll(
                        toRequestMap(
                            (List) listItem.get(i), getFormatedKeyName(formatStr, null, i)));
                  } else {
                    if (StringUtils.isNoneEmpty(format)) {
                      returnMap.putAll(
                          toRequestMap((List) listItem.get(i), format + String.valueOf(i)));
                    } else {
                      returnMap.putAll(toRequestMap((List) listItem.get(i), String.valueOf(i)));
                    }
                  }
                }
                if (listItem.get(i) instanceof String) {
                  PaypalCollection paypalCollection = field.getAnnotation(PaypalCollection.class);
                  if (paypalCollection != null
                      && StringUtils.isNotEmpty(paypalCollection.format())) {
                    String formatStr = paypalCollection.format();
                    formatStr = getFormatedKeyName(formatStr, field.getName(), i);
                    returnMap.put(
                        getFormatedKeyName(format, formatStr, null), listItem.get(i).toString());
                  } else {
                    returnMap.put(
                        getFormatedKeyName(format, field.getName(), null) + i,
                        listItem.get(i).toString());
                  }
                }
              }
            }
          }
        }
      } catch (IllegalAccessException e) {
        throw new ValidationException(e.getMessage());
      } catch (InvocationTargetException e) {
        throw new ValidationException(e.getMessage());
      } catch (NoSuchMethodException e) {
        throw new ValidationException(e.getMessage());
      }
    } else {
      StringBuffer buf = new StringBuffer();
      for (ConstraintViolation<PaypalClassicModel> violation : violations) {
        buf.append(violation.getMessage() + "\n");
      }
      throw new ValidationException(buf.toString());
    }
    return returnMap;
  }