Esempio n. 1
0
  /**
   * returns user base on id
   *
   * @param con DB connection
   * @param userId user id
   * @return user object
   */
  public static User getUser(Connection con, Long userId) {

    User user = null;
    try {
      PreparedStatement stmt = con.prepareStatement("select * from  users where id=?");
      stmt.setLong(1, userId);
      ResultSet rs = stmt.executeQuery();

      while (rs.next()) {
        user = new User();
        user.setId(rs.getLong("id"));
        user.setFirstNm(rs.getString(FIRST_NM));
        user.setLastNm(rs.getString(LAST_NM));
        user.setEmail(rs.getString(EMAIL));
        user.setUsername(rs.getString(USERNAME));
        user.setPassword(rs.getString("password"));
        user.setUserType(rs.getString(USER_TYPE));
        user.setSalt(rs.getString("salt"));
        user.setProfileList(UserProfileDB.getProfilesByUser(con, userId));
      }
      DBUtils.closeRs(rs);
      DBUtils.closeStmt(stmt);

    } catch (Exception e) {
      e.printStackTrace();
    }

    return user;
  }
Esempio n. 2
0
  /**
   * inserts new user
   *
   * @param user user object
   */
  public static void insertUser(User user) {

    Connection con = null;
    try {
      con = DBUtils.getConn();
      String salt = EncryptionUtil.generateSalt();
      PreparedStatement stmt =
          con.prepareStatement(
              "insert into users (first_nm, last_nm, email, username, user_type, password, salt) values (?,?,?,?,?,?,?)");
      stmt.setString(1, user.getFirstNm());
      stmt.setString(2, user.getLastNm());
      stmt.setString(3, user.getEmail());
      stmt.setString(4, user.getUsername());
      stmt.setString(5, user.getUserType());
      stmt.setString(6, EncryptionUtil.hash(user.getPassword() + salt));
      stmt.setString(7, salt);
      stmt.execute();
      DBUtils.closeStmt(stmt);

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DBUtils.closeConn(con);
    }
  }
Esempio n. 3
0
  /**
   * returns users based on sort order defined
   *
   * @param sortedSet object that defines sort order
   * @return sorted user list
   */
  public static SortedSet getUserSet(SortedSet sortedSet) {

    ArrayList<User> userList = new ArrayList<>();

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

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

      while (rs.next()) {
        User user = new User();
        user.setId(rs.getLong("id"));
        user.setFirstNm(rs.getString(FIRST_NM));
        user.setLastNm(rs.getString(LAST_NM));
        user.setEmail(rs.getString(EMAIL));
        user.setUsername(rs.getString(USERNAME));
        user.setPassword(rs.getString("password"));
        user.setUserType(rs.getString(USER_TYPE));
        userList.add(user);
      }
      DBUtils.closeRs(rs);
      DBUtils.closeStmt(stmt);

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DBUtils.closeConn(con);
    }

    sortedSet.setItemList(userList);
    return sortedSet;
  }
Esempio n. 4
0
 /**
  * updates existing user
  *
  * @param user user object
  */
 public static void updateUserCredentials(User user) {
   Connection con = null;
   try {
     con = DBUtils.getConn();
     String salt = EncryptionUtil.generateSalt();
     PreparedStatement stmt =
         con.prepareStatement(
             "update users set first_nm=?, last_nm=?, email=?, username=?, user_type=?, password=?, salt=? where id=?");
     stmt.setString(1, user.getFirstNm());
     stmt.setString(2, user.getLastNm());
     stmt.setString(3, user.getEmail());
     stmt.setString(4, user.getUsername());
     stmt.setString(5, user.getUserType());
     stmt.setString(6, EncryptionUtil.hash(user.getPassword() + salt));
     stmt.setString(7, salt);
     stmt.setLong(8, user.getId());
     stmt.execute();
     DBUtils.closeStmt(stmt);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     DBUtils.closeConn(con);
   }
 }