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 javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.db.GetDBConnection;


public class CustromerListInfo extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5866515510478343589L;
	private  HttpSession session;

	private String term;
	private Map<String, String> customerList = new HashMap<String, String>();
	private SessionMap<String,Object> sessionMap;  
	
	public String execute() {
		
		String result = "login";
		  session=ServletActionContext.getRequest().getSession(false);  
	        if(session==null || session.getAttribute("login")==null){ 
	        	addActionMessage("Please Login First !!!");
	        	result= "login";  
	        }  
	        else
	        {
	        
	        session=ServletActionContext.getRequest().getSession(false);  
			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 "
				        + "customer_id,"
				        + "customer_name,"
				        + "mobile_number,"
				        + "customer_type "
				        + "FROM "
				        + "shop_customer_master "
				        + "WHERE "
				        + "("
				        + "UPPER(customer_name) LIKE '%" + this.getTerm().toUpperCase() + "%' "
				        + "OR "
				        + "CAST(customer_id AS CHAR) = '" + this.getTerm().trim() + "' "
				        + ") "
				        + "AND "
				        + "shop_id = ? "
				        + "AND "
				        + "shop_code = ? ";
				
				
				
				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.getCustomerList().put(resultSet.getString(1)+"-"+resultSet.getString(2)+"-"+resultSet.getString(3)+"-"+resultSet.getString(4), 
							resultSet.getString(1)+"-"+resultSet.getString(2)+"-"+resultSet.getString(3)+"-"+resultSet.getString(4));
					
					}
				}
				else
				{
					this.getCustomerList().put("Customer_Not_Found", "Customer Not Found");
				}
				
			} catch (SQLException e) {
				//e.printStackTrace();
				
			} catch (IOException e) {
			
				//e.printStackTrace();
			}finally
			{
				if(resultSet != null)
   				{
   					try {resultSet.close();}catch(SQLException sqe){}   					
   				}
				if(preparedStatement != null)
				{
					try {
						preparedStatement.close();
					} catch (SQLException e) {
						
					}
				}
				
				if(connection != null)
				{
					try {
						connection.close();
					} catch (SQLException e) {
						
					}
				}
				
				
			}
			
			result = "success";
	        }
	        
	        return result;
}

public String getTerm() {
	return term;
}

public void setTerm(String term) {
	this.term = term;
}

public Map<String, String> getCustomerList() {
	return customerList;
}

public void setCustomerList(Map<String, String> customerList) {
	this.customerList = customerList;
}


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;  
}




}

