/src-pda/com/openbravo/pos/pda/dao/LoginDAO.java

https://code.google.com/p/openbravoposru/ · Java · 94 lines · 58 code · 9 blank · 27 comment · 10 complexity · 828fbe3a3cce3d61d3f3baa0f7f6e7b6 MD5 · raw file

  1. // Openbravo POS is a point of sales application designed for touch screens.
  2. // Copyright (C) 2007-2009 Openbravo, S.L.
  3. // http://www.openbravo.com/product/pos
  4. //
  5. // This file is part of Openbravo POS.
  6. //
  7. // Openbravo POS is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // Openbravo POS is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with Openbravo POS. If not, see <http://www.gnu.org/licenses/>.
  19. package com.openbravo.pos.pda.dao;
  20. import com.openbravo.pos.pda.util.StringUtils;
  21. import com.openbravo.pos.ticket.UserInfo;
  22. import java.sql.Connection;
  23. import java.sql.PreparedStatement;
  24. import java.sql.ResultSet;
  25. import java.sql.SQLException;
  26. /**
  27. *
  28. * @author jaroslawwozniak
  29. */
  30. public class LoginDAO extends BaseJdbcDAO {
  31. public UserInfo findUser(String login, String password) {
  32. Connection con = null;
  33. PreparedStatement ps = null;
  34. ResultSet rs = null;
  35. UserInfo user = null;
  36. String sqlStr = "SELECT NAME, APPPASSWORD FROM PEOPLE WHERE NAME = ? AND APPPASSWORD ";
  37. String end = "";
  38. if (password.equals("")) {
  39. end = "IS NULL";
  40. } else {
  41. end = " = ?";
  42. }
  43. try {
  44. //get connection
  45. con = getConnection();
  46. //prepare statement
  47. ps = con.prepareStatement(sqlStr + end);
  48. ps.setString(1, login);
  49. if (!password.equals("")) {
  50. ps.setString(2, StringUtils.hashString(password));
  51. }
  52. //execute
  53. rs = ps.executeQuery();
  54. //transform to VO
  55. user = map2VO(rs);
  56. } catch (Exception ex) {
  57. ex.printStackTrace();
  58. } finally {
  59. try {
  60. // close the resources
  61. if (ps != null) {
  62. ps.close();
  63. }
  64. if (con != null) {
  65. con.close();
  66. }
  67. } catch (SQLException sqlee) {
  68. sqlee.printStackTrace();
  69. }
  70. }
  71. return user;
  72. }
  73. @Override
  74. protected UserInfo map2VO(ResultSet rs) throws SQLException {
  75. UserInfo user = new UserInfo();
  76. rs.next();
  77. user.setLogin(rs.getString("name"));
  78. if (rs.getString("apppassword") == null) {
  79. user.setPassword("");
  80. } else {
  81. user.setPassword(rs.getString("apppassword"));
  82. }
  83. return user;
  84. }
  85. }