package com.reallinkbilling.jspRedirect;

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.FinishedProductInfoDAO;
import com.reallinkbilling.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;

public class UpdateFinishedProductDetailsJsp extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4016251245456587679L;
	private HttpSession session;
	private SessionMap<String,Object> sessionMap; 
	private String finishedProductName;
	private String productName;
	private Float quantity;
	private String key;

	private List<FinishedProductInfoDAO> finishedProductInfoList = new ArrayList<FinishedProductInfoDAO>();
	
	

	public String execute() throws IOException{
		 
			String result = "login";
			session=ServletActionContext.getRequest().getSession(false); 
			
		        if(session==null || session.getAttribute("login")==null){ 
		        	addActionMessage("Please Logout and Login again !");
		        	result = "error";
		        }  
		        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
		       		{
		       		// PorductId # Product Name # Product Code
		       			
		       			connection = GetDBConnection.getDBConnection(db);
		       			
		       			
		       			String productDetails []  = EncryptionDecryption.decrypt(this.getKey()).split("#");
		       			
		       			String productId = productDetails[0];
		       			String productName = productDetails[1];
		       			String productCode = productDetails[2];
		       			
		       			sql ="SELECT "
		       					+ "product_id,"
		       					+ "product_name_eng,"
		       					+ "product_short_code,"
		       					+ "product_quantity_required "
		       					+ "FROM "
		       					+ "shop_finished_product_details_master "
		       					+ "WHERE "
		       					+ "shop_id = ? "
		       					+ "AND "
		       					+ "shop_code = ? "
		       					+ "AND "
		       					+ "finished_product_id = ? "
		       					+ "AND "
		       					+ "finished_product_short_code = ? ";
		       			
		       			preparedStatement = connection.prepareStatement(sql);
		       			
		       			preparedStatement.setString(1, shopId);
		       			preparedStatement.setString(2, shopCode);
		       			preparedStatement.setString(3, productId);
		       			preparedStatement.setString(4, productCode);
		       			
		       			resultSet = preparedStatement.executeQuery();
		       			
		       		
		       			while(resultSet.next())
		       			{
		       				FinishedProductInfoDAO dao = new FinishedProductInfoDAO();
		       				
		       				dao.setProductId(resultSet.getString(1));
		       				dao.setProductName(resultSet.getString(2));
		       				dao.setProductCode(resultSet.getString(3));
		       				dao.setProductQuantity(resultSet.getFloat(4));
		       				dao.setFinishedProductId(productId);
		       				dao.setFinishedProductName(productName);
		       				dao.setFinishedProductCode(productCode);
		       				dao.setShopId(shopId);
		       				dao.setShopCode(shopCode);
		       				dao.setProductKey(EncryptionDecryption.encrypt(resultSet.getString(1)+"-"+resultSet.getString(2)+"-"+resultSet.getString(3)));
		       				
		       				this.getFinishedProductInfoList().add(dao);
		       			}
		       			
		       			this.setFinishedProductName(productId+"-"+productName+"-"+productCode);
		       			this.getSessionMap().put("finishedProductInfoList", this.getFinishedProductInfoList());
		       			
		       			result = SUCCESS;
		       			
		       		}catch (SQLException e) {
						result = ERROR;
						
						addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Finished product details not found !");
					} catch (IOException e) {
						
						result = "error";						
						addActionError(e.getMessage()+" || "+e.getCause()+" ||  Finished product details not found !");
					}finally {
						if(resultSet != null)
						{
							try {resultSet.close(); resultSet = null;} catch (SQLException e) {}
						}
						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 HttpSession getSession() {
		return session;
	}




	public void setSession(HttpSession session) {
		this.session = session;
	}




	public String getFinishedProductName() {
		return finishedProductName;
	}




	public void setFinishedProductName(String finishedProductName) {
		this.finishedProductName = finishedProductName;
	}




	public String getProductName() {
		return productName;
	}




	public void setProductName(String productName) {
		this.productName = productName;
	}




	public Float getQuantity() {
		return quantity;
	}




	public void setQuantity(Float quantity) {
		this.quantity = quantity;
	}




	public List<FinishedProductInfoDAO> getFinishedProductInfoList() {
		return finishedProductInfoList;
	}




	public void setFinishedProductInfoList(List<FinishedProductInfoDAO> finishedProductInfoList) {
		this.finishedProductInfoList = finishedProductInfoList;
	}




	public static long getSerialversionuid() {
		return serialVersionUID;
	}




	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;  
	}




	public String getKey() {
		return key;
	}




	public void setKey(String key) {
		this.key = key;
	}
	
	
}
