test
Форк от lirfrnk/test
1package com.bittercode.service.impl;
2
3import java.sql.Connection;
4import java.sql.PreparedStatement;
5import java.sql.ResultSet;
6import java.sql.SQLException;
7
8import javax.servlet.http.HttpSession;
9
10import com.bittercode.constant.ResponseCode;
11import com.bittercode.constant.db.UsersDBConstants;
12import com.bittercode.model.StoreException;
13import com.bittercode.model.User;
14import com.bittercode.model.UserRole;
15import com.bittercode.service.UserService;
16import com.bittercode.util.DBUtil;
17
18public class UserServiceImpl implements UserService {
19
20private static final String registerUserQuery = "INSERT INTO " + UsersDBConstants.TABLE_USERS
21+ " VALUES(?,?,?,?,?,?,?,?)";
22
23private static final String loginUserQuery = "SELECT * FROM " + UsersDBConstants.TABLE_USERS + " WHERE "
24+ UsersDBConstants.COLUMN_USERNAME + "=? AND " + UsersDBConstants.COLUMN_PASSWORD + "=? AND "
25+ UsersDBConstants.COLUMN_USERTYPE + "=?";
26
27@Override
28public User login(UserRole role, String email, String password, HttpSession session) throws StoreException {
29Connection con = DBUtil.getConnection();
30PreparedStatement ps;
31User user = null;
32try {
33String userType = UserRole.SELLER.equals(role) ? "1" : "2";
34ps = con.prepareStatement(loginUserQuery);
35ps.setString(1, email);
36ps.setString(2, password);
37ps.setString(3, userType);
38ResultSet rs = ps.executeQuery();
39if (rs.next()) {
40user = new User();
41user.setFirstName(rs.getString("firstName"));
42user.setLastName(rs.getString("lastName"));
43user.setPhone(rs.getLong("phone"));
44user.setEmailId(email);
45user.setPassword(password);
46session.setAttribute(role.toString(), user.getEmailId());
47}
48} catch (SQLException e) {
49e.printStackTrace();
50}
51return user;
52}
53
54@Override
55public boolean isLoggedIn(UserRole role, HttpSession session) {
56if (role == null)
57role = UserRole.CUSTOMER;
58return session.getAttribute(role.toString()) != null;
59}
60
61@Override
62public boolean logout(HttpSession session) {
63session.removeAttribute(UserRole.CUSTOMER.toString());
64session.removeAttribute(UserRole.SELLER.toString());
65session.invalidate();
66return true;
67}
68
69@Override
70public String register(UserRole role, User user) throws StoreException {
71String responseMessage = ResponseCode.FAILURE.name();
72Connection con = DBUtil.getConnection();
73try {
74PreparedStatement ps = con.prepareStatement(registerUserQuery);
75ps.setString(1, user.getEmailId());
76ps.setString(2, user.getPassword());
77ps.setString(3, user.getFirstName());
78ps.setString(4, user.getLastName());
79ps.setString(5, user.getAddress());
80ps.setLong(6, user.getPhone());
81ps.setString(7, user.getEmailId());
82int userType = UserRole.SELLER.equals(role) ? 1 : 2;
83ps.setInt(8, userType);
84int k = ps.executeUpdate();
85if (k == 1) {
86responseMessage = ResponseCode.SUCCESS.name();
87;
88}
89} catch (Exception e) {
90responseMessage += " : " + e.getMessage();
91if (responseMessage.contains("Duplicate"))
92responseMessage = "User already registered with this email !!";
93e.printStackTrace();
94}
95return responseMessage;
96}
97
98}
99