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.ArrayList;
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.DAO.ProductInfoDAO;
import com.reallinkbilling.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;


public class SearchProductInfo extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8486455428421982535L;
	
	private String key;
	
	private  HttpSession session;
	private ArrayList<ProductInfoDAO> searchProductInfo =  new ArrayList<ProductInfoDAO>();
	private SessionMap<String,Object> sessionMap;  
	
	
	
	@Override
	public String execute() throws Exception {

	 	String result = "login";
		session=ServletActionContext.getRequest().getSession(false);  
        
		if(session==null || session.getAttribute("login")==null){ 
        	addActionMessage("Please Login First !!!");
            return "login";  
        }  
        else
        {
        	String shopId = (String) this.getSessionMap().get("shopId");
    		String shopCode = (String) this.getSessionMap().get("shopCode");
    		
        	//System.out.println("hello");
        	
        	Connection connection = null;
       		PreparedStatement preparedStatement= null;
       		ResultSet resultSet = null;
       		
       		
       		String sql = "";
       		String db = (String) this.getSessionMap().get("db");
       		try {
   				
       			
       			sql = "SELECT "
       					+ "DATE_FORMAT(add_date, '%d-%m-%Y'),"
       					+ "product_name_eng,"
       					+ "product_name_mar,"
       					+ "product_alias,"
       					+ "product_short_code,"
       					+ "customer_sales_price,"
       					+ "dealer_sales_price,"
       					+ "retailer_sales_price,"
       					+ "tax_percentage,"
       					+ "product_id,"
       					+ "in_stock_quantity,"
       					+ "rack_number,"
       					+ "product_short_code "
       					+ "FROM "
       					+ "shop_product_master "
       					+ "WHERE "
       					+ "shop_id = ? "
       					+ "AND "
       					+ "shop_code= ? "
       					+ "AND "
       					+ "(product_name_eng "
       					+ "LIKE '%"+this.getKey().trim().toUpperCase()+"%' "
       					+ "OR "
       					+ "product_alias "
       					+ "LIKE '%"+this.getKey().trim().toUpperCase()+"%' "
       					+ "OR "
       					+ "product_short_code "
       					+ "LIKE '%"+this.getKey().trim().toUpperCase()+"%')";
       			
       			
				
				connection = GetDBConnection.getDBConnection(db);
				preparedStatement = connection.prepareStatement(sql);
				preparedStatement.setString(1, shopId);
				preparedStatement.setString(2, shopCode);
				
				resultSet = preparedStatement.executeQuery();
				resultSet.last();
				int rowCount = resultSet.getRow();
				resultSet.beforeFirst();
				
				if(rowCount>0)
				{
					while(resultSet.next())
					{
						result =  "success";
						
						
						ProductInfoDAO productInfoDAO = new ProductInfoDAO();
						
						productInfoDAO.setAddDate(resultSet.getString(1));
						productInfoDAO.setProductName(resultSet.getString(2));
						productInfoDAO.setMarathiName(resultSet.getString(3));
						productInfoDAO.setAlias(resultSet.getString(4));
						productInfoDAO.setProductCode(resultSet.getString(5));
						productInfoDAO.setCustomerSalesRate(resultSet.getString(6));
						productInfoDAO.setDealerSalesRate(resultSet.getString(7));
						productInfoDAO.setRetailerSaleRate(resultSet.getString(8));
						productInfoDAO.setTaxPercentage(resultSet.getString(9));
						productInfoDAO.setProductId(EncryptionDecryption.encrypt(resultSet.getString(10)));
						productInfoDAO.setInStockQuantity(resultSet.getString(11));
						productInfoDAO.setRackNumber(resultSet.getString(12));
						
						this.getSearchProductInfo().add(productInfoDAO);
						
					}
					
					
				}
				else
				{
					addActionMessage("Records Not Found !!!");
				}
       		
			} catch (SQLException e) {
   				//e.printStackTrace();
   				addActionMessage(e.getErrorCode()+"||"+e.getMessage()+"|| SOME ERROR");
			
			} catch (IOException e) {
				
				addActionMessage(e.getMessage()+"|| SOME ERROR");
			}	
   			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) {}
   				}
   				
   			}
   		}
    	
    	
    	return result;
	}
	
	
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public ArrayList<ProductInfoDAO> getSearchProductInfo() {
		return searchProductInfo;
	}
	public void setSearchProductInfo(ArrayList<ProductInfoDAO> searchProductInfo) {
		this.searchProductInfo = searchProductInfo;
	}
	
	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;  
	}
	
	
	

}
