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 SupplierListInfo extends ActionSupport implements SessionAware{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2342659923589029492L;

	private  HttpSession session;

	private String term;
	private Map<String, String> supplierList = 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
	        {
	        
	     	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 "
						+ "supplier_id,"
						+ "suplier_name,"
						+ "contact_number "
						+ "FROM "
						+ "shop_supplier_master "
						+ "where "
						+ "shop_id = ? "
						+ "AND "
						+ "shop_code = ? "
						+ "AND "
						+ "(suplier_name like '%"+term+"%' "
						+ "OR "
						+ "contact_number like '%"+term+"%')";
				
								
				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.getSupplierList().put(resultSet.getString(1)+"-"+resultSet.getString(2)+"-"+resultSet.getString(3), 
							resultSet.getString(1)+"-"+resultSet.getString(2)+"-"+resultSet.getString(3));
					
					}
				}
				else
				{
					this.getSupplierList().put("Supplier_Not_Found", "Supplier 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> getSupplierList() {
	return supplierList;
}

public void setSupplierList(Map<String, String> supplierList) {
	this.supplierList = supplierList;
}
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;  
}


}
