package com.reallinkbilling.ajax;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.reallinkbilling.db.GetDBConnection;
import com.opensymphony.xwork2.ActionSupport;

public class EmployeeListInfo extends ActionSupport implements SessionAware{

	
/**
	 * 
	 */
	private static final long serialVersionUID = 8950399200516125555L;
private String term;
private Map<String, String> employeeList = new HashMap<String, String>();
private SessionMap<String,Object> sessionMap;  


public String execute() {
	
	String result = "login";
	     if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
        	addActionMessage("Please Login First !!!");
        	result= "login";  
        }  
        else
        {
        
    	Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		String sql = "";
		
		String db = (String) this.getSessionMap().get("db");
		
		try {
			
			String shopId = (String) this.getSessionMap().get("shopId");
    		String shopCode = (String) this.getSessionMap().get("shopCode");
         	
			connection = GetDBConnection.getDBConnection(db);
			
			sql = "SELECT "
					+ "employee_id,"
					+ "employee_name,"
					+ "employee_mobile_number,"
					+ "employee_address "
					+ "FROM "
					+ "shop_employee_master "
					+ "WHERE "
					+ "shop_id = ? "
					+ "AND "
					+ "shop_code = ? "
					+ "AND "
					+ "(employee_name like '%"+term.trim()+"%' "
					+ "OR "
					+ "employee_mobile_number like '%"+term.trim()+"%') ";
			
			
			
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, shopId);
			preparedStatement.setString(2, shopCode);
					
			resultSet = preparedStatement.executeQuery();
			resultSet.last();
			int rows=resultSet.getRow();
			resultSet.beforeFirst();
			
			if(rows>0)
			{
				while(resultSet.next())
				{
					this.getEmployeeList().put(resultSet.getString(1)+"-"+resultSet.getString(2)+"-"+resultSet.getString(3), 
						resultSet.getString(1)+"-"+resultSet.getString(2)+"-"+resultSet.getString(3));
				
				}
			}
			else
			{
				this.getEmployeeList().put("Employee_Not_Found", "Employee Not Found");
			}
			
			}catch (SQLException e) {
				//e.printStackTrace();
				
			} catch (IOException e) {
			
				//e.printStackTrace();
			}finally
			{
				if(resultSet != null)
   				{
   					try {resultSet.close(); resultSet = null;}catch(SQLException sqe){}   					
   				}
				if(preparedStatement != null)
				{
					try {
						preparedStatement.close(); preparedStatement = null;
					} catch (SQLException e) {
						
					}
				}
				
				if(connection != null)
				{
					try {
						connection.close(); connection = null;
					} catch (SQLException e) {
						
					}
				}
				
				
			}
		result = SUCCESS;
        }
		return result;
	     
}    


public String getTerm() {
return term;
}


public void setTerm(String term) {
this.term = term;
}



public Map<String, String> getEmployeeList() {
	return employeeList;
}


public void setEmployeeList(Map<String, String> employeeList) {
	this.employeeList = employeeList;
}


public static long getSerialversionuid() {
return serialVersionUID;
}


public SessionMap<String, Object> getSessionMap() {
return sessionMap;
}

public void setSessionMap(SessionMap<String, Object> sessionMap) {
this.sessionMap = sessionMap;
}


public void setSession(Map<String, Object> map) {  
sessionMap=(SessionMap<String, Object>)map;  
}



}


