Exemplo n.º 1
0
  public static Validation checkValidate(
      List<ValidatorConfigBean> valList, Map<String, String[]> paramMap, HttpServletRequest req) {

    Validation validation = new Validation();

    if (valList == null || valList.isEmpty()) return validation;

    // 启动验证器
    for (int i = valList.size() - 1; i > -1; i--) {
      ValidatorConfigBean val = valList.get(i);
      ValidatorIF validator = null;
      if (!"".equals(val.getName())) {
        // 根据name属性实例化相关的验证器,如果找不到,就根据class属性实例化
        validator = ValidatorFactory.getValidator(val.getName());
        if (validator == null)
          try {
            validator = (ValidatorIF) Class.forName(val.getClazz()).newInstance();
          } catch (Exception e) {
            log.error(StringUtil.getExceptionString(e));
          }
      }

      if (validator == null) continue;

      Map<String, Map<String, String>> err = validator.validate(val, paramMap, req).getErrors();
      for (Entry<String, Map<String, String>> en : err.entrySet()) {
        String key = en.getKey();
        Map<String, String> value = en.getValue();
        if (validation.getErrors().containsKey(key)) validation.getErrors().get(key).putAll(value);
        else validation.getErrors().put(key, value);
      }
    }

    return validation;
  }
Exemplo n.º 2
0
  private <T> List<T> query(String sql) {
    List<T> result = null;
    try {

      if (Map.class.isAssignableFrom(this.clazz)) {
        Connection con = ds.getConnection();
        if (args != null && args.size() > 0) {
          result =
              (List<T>) JdbcUtil.getListWithArgs(con, clazz, sql, args.toArray(new Object[] {}));
        } else {
          result = (List<T>) JdbcUtil.getList(con, clazz, sql);
        }

      } else {
        if (args != null && args.size() > 0) {
          result =
              (List<T>)
                  DAOFactory.getSelectDAO(dsName)
                      .selectBySQL(clazz, sql, args.toArray(new Object[] {}));
        } else {
          result = (List<T>) DAOFactory.getSelectDAO(dsName).selectBySQL(clazz, sql);
        }
      }

      // this.clear();
      if (result != null && result.size() > 0) {
        for (T t : result) {
          // ToOne relation class cascade select
          final String[] fields = ORMConfigBeanUtil.getToOneField(t.getClass());
          if (fields == null || fields.length == 0) continue;

          DAOFactory.getCascadeDAO(dsName).select(t, fields);
        }
      }

      return result;
    } catch (Exception e) {
      log.error("sql-->" + sql + "exception:" + StringUtil.getExceptionString(e));
      throw new DAOException(sql + " execute exception", e);
    }
  }
Exemplo n.º 3
0
  public int execute() {
    int id = -1;
    String sql =
        this.sql.toString().replace("${_where_}", this.condition.toString()).replace("'?'", "?");
    DataSource ds = DataSourceWrapCache.get(dsName);
    try {
      int rs = 0;
      if (args != null && args.size() > 0) {
        rs =
            (Integer)
                JdbcUtil.updateWithArgs(ds.getConnection(), sql, args.toArray(new Object[] {}));
      } else {
        rs = (Integer) JdbcUtil.update(ds.getConnection(), sql);
      }

      if (rs > 0 && sql.contains("INSERT INTO")) {
        if (Map.class.isAssignableFrom(clazz)) {
          if (map == null) {
            map = new HashMap<String, Object>();
            map.put("idColumn", "id");
            map.put("table", this.table);
          } else if (map.get("idColumn") == null) {
            map.put("idColumn", "id");
          }

          id = (Integer) DAOUtil.selectMaxId(map, ds.getConnection(), dbType);
        } else {
          id = (Integer) DAOUtil.selectMaxId(clazz, ds.getConnection(), dbType);
        }
      }

    } catch (SQLException e) {
      log.error("sql-->" + sql + "exception:" + StringUtil.getExceptionString(e));
      throw new DAOException(sql + " execute exception", e);
    }

    // this.clear();
    return id;
  }
  private Object[] assemParams(Class<?>[] paramTypes, Annotation[][] paramAnns) throws Exception {
    Object[] params = new Object[paramTypes.length];
    int pathParamIndex = 0;
    for (int i = 0; i < paramTypes.length; ++i) {
      Annotation[] anns = paramAnns[i];
      Class<?> paramClass = paramTypes[i];
      String[] paramValue = null;
      // ------------------------------------------------------
      // 通过给定class 获取对应的ActionContextObj
      if (Context.class.isAssignableFrom(paramClass)) {
        params[i] = this.context;
        continue;
      }

      if (HttpServletRequest.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getRequest();
        continue;
      }

      if (HttpServletResponse.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getResponse();
        continue;
      }

      if (PrintWriter.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getWriter();
        continue;
      }

      if (ServletOutputStream.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getOut();
        continue;
      }

      if (HttpSession.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getSession();
        continue;
      }

      if (ActionProp.class.isAssignableFrom(paramClass)) {
        if (this.context.getActionProp() == null)
          this.context.setActionProp(new ActionProp(this.actionObject.getClass().getName()));

        params[i] = this.context.getActionProp();
        continue;
      }

      if (Validation.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getValidation();
        continue;
      }

      if (QueryParams.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getQueryParams();
        continue;
      }

      if (DAO.class.isAssignableFrom(paramClass)) {
        params[i] = new DAOImpl("");
        continue;
      }

      if (InsertDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getInsertDAO();
        continue;
      }

      if (DeleteDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getDeleteDAO();
        continue;
      }

      if (UpdateDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getUpdateDAO();
        continue;
      }

      if (SelectDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getSelectDAO();
        continue;
      }

      if (DivPageDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getDivPageDAO();
        continue;
      }

      if (SearchDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getSearchDAO();
        continue;
      }

      if (CascadeDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getCascadeDAO();
        continue;
      }

      PathParam pathParamAnn = this.getPathParamAnn(anns);
      if (pathParamAnn != null) {
        paramValue = this.getPathParamValue(pathParamAnn.value());
        params[i] = ClassUtil.getParamVal(paramClass, paramValue[0]);
        continue;
      }

      QueryParam queryParamAnn = this.getQueryParamAnn(anns);

      // 视图模型
      if (queryParamAnn == null && Map.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getModel();
        continue;
      }

      if (queryParamAnn != null) {
        final String fieldName = queryParamAnn.value();
        if (File.class.isAssignableFrom(paramClass)) {
          if (!this.context.getUploadMap().containsKey(fieldName)) continue;
          List<UploadFile> list = this.context.getUploadMap().get(fieldName);
          if (list == null || list.isEmpty()) continue;

          UploadFile uploadFile = list.get(0);
          File file = uploadFile.getTmpFile();
          params[i] = file;

          continue;
        }

        if (File[].class.isAssignableFrom(paramClass)) {
          if (!this.context.getUploadMap().containsKey(fieldName)) continue;
          List<UploadFile> list = this.context.getUploadMap().get(fieldName);
          if (list == null || list.isEmpty()) continue;
          File[] files = new File[list.size()];
          for (int j = 0; j < files.length; j++) files[j] = list.get(j).getTmpFile();

          params[i] = files;
        }

        if (UploadFile.class.isAssignableFrom(paramClass)) {
          if (!this.context.getUploadMap().containsKey(fieldName)) continue;
          List<UploadFile> list = this.context.getUploadMap().get(fieldName);
          if (list == null || list.isEmpty()) continue;

          UploadFile uploadFile = list.get(0);
          params[i] = uploadFile;

          continue;
        }

        if (UploadFile[].class.isAssignableFrom(paramClass)) {
          if (!this.context.getUploadMap().containsKey(fieldName)) continue;
          List<UploadFile> list = this.context.getUploadMap().get(fieldName);
          if (list == null || list.isEmpty()) continue;

          params[i] = list.toArray(new UploadFile[] {});
        }

        String defaultValue = null;
        DefaultValue defaultValueAnn = this.getDefaultValueAnn(anns);
        if (defaultValueAnn != null) defaultValue = defaultValueAnn.value();

        paramValue = this.getQueryParamValue(fieldName, defaultValue);

        if (java.util.Date.class.isAssignableFrom(paramClass)) {
          params[i] = this.getDateParam(anns, paramValue[0]);
          continue;
        }

        String startName = fieldName;
        if (ClassUtil.isPojo(paramClass)) {
          params[i] = this.injectParam2Pojo(paramClass, startName);
          continue;
        }

        if (Map.class.isAssignableFrom(paramClass)) {
          params[i] = this.injectParam2Map(startName);
          continue;
        }

        if (paramClass.isArray()) params[i] = ClassUtil.getParamVals(paramClass, paramValue);
        else params[i] = ClassUtil.getParamVal(paramClass, paramValue[0]);
      } else {
        // 如果是基本数据类型,则按照排序进行注入
        String[] pathParams = this.context.getActionConfigBean().getPathParams();
        if (pathParams == null) {
          log.warn("QueryParam not found and PathParam not found too");
          continue;
        }

        paramValue = this.getPathParamValue(pathParams[pathParamIndex]);
        params[i] = ClassUtil.getParamVal(paramClass, paramValue[0]);
        pathParamIndex++;
        continue;
      }
    }

    return params;
  }
  private void exeActionLog() {
    StringBuilder sb = new StringBuilder();
    sb.append("execute action -> ").append(this.context.getActionConfigBean()).append("; ");

    log.debug(sb.toString());
  }