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 ProductBarcodeListInfo extends ActionSupport implements SessionAware{


	/**
	 * 
	 */
	private static final long serialVersionUID = -3613661633658227196L;

	private  HttpSession session;

	private String term;
	private Map<String, String> productList = 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
	        {
	        
	        	
	        	String shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
	    		
	    	Connection connection = null;
			PreparedStatement preparedStatement = null;
			ResultSet resultSet = null;
			String sql = "";
			
			String db = (String) this.getSessionMap().get("db");
			
			try {
				
				connection = GetDBConnection.getDBConnection(db);
				
				sql = "SELECT "
						+ "product_id,"
						+ "product_alias,"
						+ "product_name_eng,"
						+ "product_name_mar,"
						+ "product_short_code "
						+ "FROM "
						+ "shop_product_master "
						+ "WHERE "
						+ "(product_name_eng LIKE '%"+term.trim()+"%' "
						+ "OR product_alias LIKE '"+term.trim().toUpperCase()+"%') "
						+ "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.getProductList().put(resultSet.getString(5), 
							resultSet.getString(3)+" | "+resultSet.getString(4)+" | "+resultSet.getString(2));
					
					}
				}
				else
				{
					this.getProductList().put("Product_Not_Found", "Product 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> getProductList() {
	return productList;
}

public void setProductList(Map<String, String> productList) {
	this.productList = productList;
}


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;  
}



}


