/**
   * 마지막 실행했떤 쿼리 100개를 리턴합니다.
   *
   * @param user_seq
   * @param dbSeq
   * @return
   * @throws Exception
   */
  public static List<SQLHistoryDAO> getExecuteQueryHistory(int user_seq, int dbSeq, String filter)
      throws Exception {
    List<SQLHistoryDAO> returnSQLHistory = new ArrayList<SQLHistoryDAO>();

    Map<String, Object> queryMap = new HashMap<String, Object>();
    queryMap.put("user_seq", user_seq);
    queryMap.put("db_seq", dbSeq);
    queryMap.put("filter", "%" + filter + "%");
    queryMap.put("count", 50);

    SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
    List<java.util.Map> listResourceData =
        sqlClient.queryForList("getExecuteQueryHistory", queryMap);

    for (Map resultMap : listResourceData) {
      int seq = (Integer) resultMap.get("executed_sql_resource_seq");

      Long startdateexecute = 0l;
      // This case sqlite
      if (resultMap.get("startdateexecute") instanceof Long) {
        startdateexecute = (Long) resultMap.get("startdateexecute");
        // This case mysql
      } else {
        startdateexecute = ((Timestamp) resultMap.get("startdateexecute")).getTime();
      }
      String strSQLText = (String) resultMap.get("datas");
      Long enddateexecute = 0l;
      // This case sqlite
      if (resultMap.get("enddateexecute") instanceof Long) {
        enddateexecute = (Long) resultMap.get("enddateexecute");
        // This case mysql
      } else {
        enddateexecute = ((Timestamp) resultMap.get("enddateexecute")).getTime();
      }

      String strMessage = (String) resultMap.get("message");

      int row = (Integer) resultMap.get("row");
      String result = (String) resultMap.get("result");

      SQLHistoryDAO dao =
          new SQLHistoryDAO(
              new Timestamp(startdateexecute),
              strSQLText,
              new Timestamp(enddateexecute),
              row,
              result,
              strMessage);
      dao.setSeq(seq);
      returnSQLHistory.add(dao);
    }

    return returnSQLHistory;
  }
  /**
   * save sqlhistory
   *
   * @param user_seq
   * @param userDB
   * @param sqlType
   * @param sqlHistoryDAO
   */
  public static void saveExecuteSQUeryResource(
      int user_seq,
      UserDBDAO userDB,
      PublicTadpoleDefine.EXECUTE_SQL_TYPE sqlType,
      SQLHistoryDAO sqlHistoryDAO)
      throws Exception {
    if (PublicTadpoleDefine.YES_NO.YES.name().equals(userDB.getIs_profile())) {
      ExecutedSqlResourceDAO executeSQLResourceDao = new ExecutedSqlResourceDAO();
      executeSQLResourceDao.setDb_seq(userDB.getSeq());
      executeSQLResourceDao.setUser_seq(user_seq);
      executeSQLResourceDao.setTypes(sqlType.toString());

      executeSQLResourceDao.setStartDateExecute(sqlHistoryDAO.getStartDateExecute());
      executeSQLResourceDao.setEndDateExecute(sqlHistoryDAO.getEndDateExecute());
      long duration =
          sqlHistoryDAO.getEndDateExecute().getTime()
              - sqlHistoryDAO.getStartDateExecute().getTime();
      executeSQLResourceDao.setDuration(Integer.parseInt("" + duration));

      executeSQLResourceDao.setRow(sqlHistoryDAO.getRows());
      executeSQLResourceDao.setResult(sqlHistoryDAO.getResult());
      executeSQLResourceDao.setMessage(
          sqlHistoryDAO
              .getMesssage()); // ""+(sqlHistoryDAO.getEndDateExecute().getTime() -
                               // sqlHistoryDAO.getStartDateExecute().getTime()));
      executeSQLResourceDao.setIpAddress(sqlHistoryDAO.getIpAddress());
      // 기존에 등록 되어 있는지 검사한다
      SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
      ExecutedSqlResourceDAO executeSQL =
          (ExecutedSqlResourceDAO)
              sqlClient.insert(
                  "userExecuteSQLResourceInsert", executeSQLResourceDao); // $NON-NLS-1$

      insertResourceData(executeSQL, sqlHistoryDAO.getStrSQLText());
    }
  }
  /**
   * 쿼리 실행 히스토리 디테일 창을 얻습니다.
   *
   * @param strType
   * @param dbSeq
   * @param executeTime
   * @param durationLimit
   * @return
   * @throws Exception
   */
  public static List<SQLHistoryDAO> getExecuteQueryHistoryDetail(
      String strType,
      String dbSeq,
      long startTime,
      long endTime,
      int duringExecute,
      String strSearch)
      throws Exception {
    List<SQLHistoryDAO> returnSQLHistory = new ArrayList<SQLHistoryDAO>();

    Map<String, Object> queryMap = new HashMap<String, Object>();
    queryMap.put("db_seq", dbSeq);
    queryMap.put("type", strType);

    if (ApplicationArgumentUtils.isDBServer()) {
      Date date = new Date(startTime);
      DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
      queryMap.put("startTime", formatter.format(date));

      Date dateendTime = new Date(endTime);
      queryMap.put("endTime", formatter.format(dateendTime));
    } else {
      queryMap.put("startTime", startTime);
      queryMap.put("endTime", endTime);
    }

    queryMap.put("duration", duringExecute);
    queryMap.put("count", 1000);
    queryMap.put("strSearch", strSearch);

    SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
    List<java.util.Map> listResourceData = new ArrayList<Map>();
    if (PublicTadpoleDefine.EXECUTE_SQL_TYPE.API.name().endsWith(strType)) {
      listResourceData = sqlClient.queryForList("getExecuteQueryHistoryAPIDetail", queryMap);
    } else {
      listResourceData = sqlClient.queryForList("getExecuteQueryHistoryDetail", queryMap);
    }

    for (Map resultMap : listResourceData) {
      int seq = (Integer) resultMap.get("executed_sql_resource_seq");

      Long startdateexecute = 0l;
      String strSQLText = (String) resultMap.get("datas");
      Long enddateexecute = 0l;

      if (ApplicationArgumentUtils.isDBServer()) {
        startdateexecute = ((Timestamp) resultMap.get("startdateexecute")).getTime();
        enddateexecute = ((Timestamp) resultMap.get("enddateexecute")).getTime();
      } else {
        startdateexecute = (Long) resultMap.get("startdateexecute");
        enddateexecute = (Long) resultMap.get("enddateexecute");
      }

      int row = (Integer) resultMap.get("row");
      String result = (String) resultMap.get("result");

      String userName = resultMap.get("name") == null ? "" : (String) resultMap.get("name");
      String dbName = (String) resultMap.get("display_name");

      String ipAddress = (String) resultMap.get("ipaddress");
      int dbSeq2 = (Integer) resultMap.get("dbseq");

      String strMessage = (String) resultMap.get("message");

      SQLHistoryDAO dao =
          new SQLHistoryDAO(
              userName,
              dbName,
              new Timestamp(startdateexecute),
              strSQLText,
              new Timestamp(enddateexecute),
              row,
              result,
              strMessage,
              ipAddress,
              dbSeq2);
      dao.setSeq(seq);
      if (PublicTadpoleDefine.EXECUTE_SQL_TYPE.API.name().endsWith(strType)) {
        dao.setEXECUSTE_SQL_TYPE(PublicTadpoleDefine.EXECUTE_SQL_TYPE.API);
      } else {
        dao.setEXECUSTE_SQL_TYPE(PublicTadpoleDefine.EXECUTE_SQL_TYPE.EDITOR);
      }

      returnSQLHistory.add(dao);
    }

    return returnSQLHistory;
  }