コード例 #1
0
  /**
   * returns public keys based on sort order defined
   *
   * @param sortedSet object that defines sort order
   * @param userId user id
   * @return sorted script list
   */
  public static SortedSet getPublicKeySet(SortedSet sortedSet, Long userId) {

    ArrayList<PublicKey> publicKeysList = new ArrayList<PublicKey>();

    String orderBy = "";
    if (sortedSet.getOrderByField() != null && !sortedSet.getOrderByField().trim().equals("")) {
      orderBy = "order by " + sortedSet.getOrderByField() + " " + sortedSet.getOrderByDirection();
    }
    String sql = "select * from public_keys where user_id = ? and enabled=true " + orderBy;

    Connection con = null;
    try {
      con = DBUtils.getConn();
      PreparedStatement stmt = con.prepareStatement(sql);
      stmt.setLong(1, userId);
      ResultSet rs = stmt.executeQuery();

      while (rs.next()) {
        PublicKey publicKey = new PublicKey();
        publicKey.setId(rs.getLong("id"));
        publicKey.setKeyNm(rs.getString("key_nm"));
        publicKey.setPublicKey(rs.getString("public_key"));
        publicKey.setProfile(ProfileDB.getProfile(con, rs.getLong("profile_id")));
        publicKey.setType(SSHUtil.getKeyType(publicKey.getPublicKey()));
        publicKey.setFingerprint(SSHUtil.getFingerprint(publicKey.getPublicKey()));
        publicKey.setCreateDt(rs.getTimestamp("create_dt"));
        publicKeysList.add(publicKey);
      }
      DBUtils.closeRs(rs);
      DBUtils.closeStmt(stmt);

    } catch (Exception e) {
      log.error(e.toString(), e);
    }
    DBUtils.closeConn(con);

    sortedSet.setItemList(publicKeysList);
    return sortedSet;
  }
コード例 #2
0
ファイル: ScriptDB.java プロジェクト: tobiasquinteiro/KeyBox
  /**
   * returns scripts based on sort order defined
   *
   * @param sortedSet object that defines sort order
   * @param userId user id
   * @return sorted script list
   */
  public static SortedSet getScriptSet(SortedSet sortedSet, Long userId) {

    ArrayList<Script> scriptList = new ArrayList<Script>();

    String orderBy = "";
    if (sortedSet.getOrderByField() != null && !sortedSet.getOrderByField().trim().equals("")) {
      orderBy = "order by " + sortedSet.getOrderByField() + " " + sortedSet.getOrderByDirection();
    }
    String sql = "select * from scripts where user_id=? " + orderBy;

    Connection con = null;
    try {
      con = DBUtils.getConn();
      PreparedStatement stmt = con.prepareStatement(sql);
      stmt.setLong(1, userId);
      ResultSet rs = stmt.executeQuery();

      while (rs.next()) {
        Script script = new Script();
        script.setId(rs.getLong("id"));
        script.setDisplayNm(rs.getString("display_nm"));
        script.setScript(rs.getString("script"));

        scriptList.add(script);
      }
      DBUtils.closeRs(rs);
      DBUtils.closeStmt(stmt);

    } catch (Exception e) {
      log.error(e.toString(), e);
    }
    DBUtils.closeConn(con);

    sortedSet.setItemList(scriptList);
    return sortedSet;
  }
コード例 #3
0
  /**
   * returns public keys based on sort order defined
   *
   * @param sortedSet object that defines sort order
   * @return sorted script list
   */
  public static SortedSet getPublicKeySet(SortedSet sortedSet) {

    ArrayList<PublicKey> publicKeysList = new ArrayList<PublicKey>();

    String orderBy = "";
    if (sortedSet.getOrderByField() != null && !sortedSet.getOrderByField().trim().equals("")) {
      orderBy = " order by " + sortedSet.getOrderByField() + " " + sortedSet.getOrderByDirection();
    }
    String sql = "select p.*, u.username from public_keys p, users u where u.id=p.user_id  ";

    sql +=
        StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_USER_ID))
            ? " and p.user_id=? "
            : "";
    sql +=
        StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_PROFILE_ID))
            ? " and p.profile_id=? "
            : "";
    sql +=
        StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_ENABLED))
            ? " and p.enabled=? "
            : " and p.enabled=true";
    sql = sql + orderBy;

    Connection con = null;
    try {
      con = DBUtils.getConn();
      PreparedStatement stmt = con.prepareStatement(sql);
      int i = 1;
      // set filters in prepared statement
      if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_USER_ID))) {
        stmt.setLong(i++, Long.valueOf(sortedSet.getFilterMap().get(FILTER_BY_USER_ID)));
      }
      if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_PROFILE_ID))) {
        stmt.setLong(i++, Long.valueOf(sortedSet.getFilterMap().get(FILTER_BY_PROFILE_ID)));
      }
      if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_ENABLED))) {
        stmt.setBoolean(i++, Boolean.valueOf(sortedSet.getFilterMap().get(FILTER_BY_ENABLED)));
      }
      ResultSet rs = stmt.executeQuery();

      while (rs.next()) {
        PublicKey publicKey = new PublicKey();
        publicKey.setId(rs.getLong("id"));
        publicKey.setKeyNm(rs.getString("key_nm"));
        publicKey.setPublicKey(rs.getString("public_key"));
        publicKey.setProfile(ProfileDB.getProfile(con, rs.getLong("profile_id")));
        publicKey.setType(SSHUtil.getKeyType(publicKey.getPublicKey()));
        publicKey.setFingerprint(SSHUtil.getFingerprint(publicKey.getPublicKey()));
        publicKey.setCreateDt(rs.getTimestamp("create_dt"));
        publicKey.setUsername(rs.getString("username"));
        publicKey.setUserId(rs.getLong("user_id"));
        publicKey.setEnabled(rs.getBoolean("enabled"));
        publicKeysList.add(publicKey);
      }
      DBUtils.closeRs(rs);
      DBUtils.closeStmt(stmt);

    } catch (Exception e) {
      log.error(e.toString(), e);
    }
    DBUtils.closeConn(con);

    sortedSet.setItemList(publicKeysList);
    return sortedSet;
  }