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.List;
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.ProductSerialBatchIdDAO;
import com.reallinkbilling.db.GetDBConnection;

public class GetSerialBatchIdList extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2215511971668055519L;
	
	private SessionMap<String,Object> sessionMap;  

	private HttpSession session;	
	private String productName;
	
	private List<ProductSerialBatchIdDAO> serialBatchIdList = new ArrayList<ProductSerialBatchIdDAO>();
	
	public String execute() throws Exception {

	 	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 shopId = (String) this.getSessionMap().get("shopId");
    		String shopCode = (String) this.getSessionMap().get("shopCode");
        
       		
       		String sql ="";
       		String db = (String) session.getAttribute("db");
       		try {
       			
       			connection = GetDBConnection.getDBConnection(db);
       			
       			String productDetails [] = this.getProductName().split("-");
       			
       			sql = "SELECT "
       					+ "serial_no_batch_id "
       					+ "FROM "
       					+ "shop_purchase_product_info_master "
       					+ "WHERE "
       					+ "shop_id = ? "
       					+ "AND "
       					+ "shop_code = ? "
       					+ "AND "
       					+ "product_id = ? "
       					+ "AND "
       					+ "puroduct_quantity > 0 "
       					+ "AND "
       					+ "serial_no_batch_id <> 'NA' ";
       			
       			
       			preparedStatement = connection.prepareStatement(sql);
       			preparedStatement.setString(1, shopId);
       			preparedStatement.setString(2, shopCode);
       			preparedStatement.setString(3, productDetails[0]);
       			
       			resultSet = preparedStatement.executeQuery();
       			ProductSerialBatchIdDAO dao1 = new ProductSerialBatchIdDAO();
       			dao1.setProductSerailBatchIdValue("-----  SELECT SERIAL / BATCH ------");
       			dao1.setProductSerailBatchId("");
       			this.getSerialBatchIdList().add(dao1);
       			
       			while(resultSet.next())
       			{
       				ProductSerialBatchIdDAO dao = new ProductSerialBatchIdDAO();
       				dao.setProductSerailBatchId(resultSet.getString(1));
       				dao.setProductSerailBatchIdValue(resultSet.getString(1));
       				
       				this.getSerialBatchIdList().add(dao);
       			}
       			if(this.getSerialBatchIdList().size() == 1)
       			{
       				ProductSerialBatchIdDAO dao = new ProductSerialBatchIdDAO();
       				dao.setProductSerailBatchId("NA");
       				dao.setProductSerailBatchIdValue("NA");       				
       				this.getSerialBatchIdList().add(dao);
       			}
       			result = SUCCESS;
       			
       		}	 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(); 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) {}
   				}
   				
   			}
       		
        }
		
		return result;
	}

	public List<ProductSerialBatchIdDAO> getSerialBatchIdList() {
		return serialBatchIdList;
	}

	public void setSerialBatchIdList(List<ProductSerialBatchIdDAO> serialBatchIdList) {
		this.serialBatchIdList = serialBatchIdList;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}



public SessionMap<String, Object> getSessionMap() {
	return sessionMap;
}

public String getProductName() {
	return productName;
}

public void setProductName(String productName) {
	this.productName = productName;
}

public void setSessionMap(SessionMap<String, Object> sessionMap) {
	this.sessionMap = sessionMap;
}


public void setSession(Map<String, Object> map) {  
    sessionMap=(SessionMap<String, Object>)map;  
}




	

}
