com.hris.serviceprovider.UserServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.hris.serviceprovider.UserServiceImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hris.serviceprovider;

import com.hris.common.CommonUtil;
import com.hris.connection.DBConnection;
import com.hris.connection.ErrorLoggedNotification;
import com.hris.model.User;
import com.hris.service.UserService;
import com.vaadin.server.VaadinSession;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jetdario
 */
public class UserServiceImpl implements UserService {

    @Override
    public boolean loggedIn(String username, String password) {
        Connection conn = DBConnection.connect();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        boolean isResult = false;

        try {
            pstmt = conn.prepareStatement("SELECT COUNT(*) AS LoginValue, " + "u.id AS id, "
                    + "u.username_ AS username, " + "u.userRole AS userRole " + "FROM user_ u "
                    + "WHERE u.username_ = ? AND u.password_ = ? " + "AND u.UserStatus = 0 " + "GROUP BY u.id ");
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                //                SystemLog sl = new SystemLog();
                //                sl.setUserId(CommonUtil.convertStringToInteger(rs.getString("UserID")));
                //                sl.setEntryDateTime(new Date());

                if (!rs.getString("LoginValue").equals("0")) {
                    //                    sl.setActivity(SystemLogDefaults.LOGGED_IN);                    
                    //                    sls.insert(sl);

                    VaadinSession.getCurrent().setAttribute("username", rs.getString("username"));
                    VaadinSession.getCurrent().setAttribute("userId", rs.getString("id"));
                    VaadinSession.getCurrent().setAttribute("userRole", rs.getString("userRole"));

                    isResult = true;
                }
                //                else {
                //                    sl.setActivity(SystemLogDefaults.UNSUCCESSFUL_LOGGED_IN+" with USERNAME: "+username);                    
                //                    sls.insert(sl);
                //                }
            }
        } catch (SQLException ex) {
            ErrorLoggedNotification.showErrorLoggedOnWindow(ex.getMessage(), this.getClass().getName());
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (conn != null || !conn.isClosed()) {
                    pstmt.close();
                    rs.close();
                    conn.close();
                }
            } catch (SQLException ex) {
                ErrorLoggedNotification.showErrorLoggedOnWindow(ex.getMessage(), this.getClass().getName());
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
            }
        }

        return isResult;
    }

    @Override
    public void loggedOut() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<User> findAll() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public User findUserByUserId(int userId) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public User findUserByEmployeeId(int employeeId) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean insert(User u) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean update(User u) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean delete(int userId) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean isPasswordMatched(String pass1, String pass2) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String getEncryptedPassword(String password) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean changePassword(String password, int userId) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String findEmployeeIdByUserId(int userId) {
        Connection conn = DBConnection.connect();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String employeeId = null;

        try {
            pstmt = conn.prepareStatement("SELECT employeeId FROM user_ " + "WHERE id = ?");
            pstmt.setInt(1, userId);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                employeeId = rs.getString("employeeId");
            }
        } catch (SQLException ex) {
            ErrorLoggedNotification.showErrorLoggedOnWindow(ex.toString(), this.getClass().getName());
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                pstmt.close();
                rs.close();
                conn.close();
            } catch (SQLException ex) {
                ErrorLoggedNotification.showErrorLoggedOnWindow(ex.toString(), this.getClass().getName());
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
            }
        }

        return employeeId;
    }

}