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.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.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;
import com.reallinkbilling.utils.LabelCapSchema;
import com.reallinkbilling.utils.PackingMasterOptions;

public class ViewProductionDetailsJsp extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7712266632621052916L;

	private HttpSession session;
	private String key;
	private String productionDate;
	private String productionTime;
	private String productionId;
	private String finishedProductId;
	private String finishedProductName;
	private String batchId;
	private String shift;
	private String productionQuantity;
	private String productionBy;
	private String productoinDetails;
	private String labelName;
	private String capName;
	private String productName;
	
	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 {
	       			
	       		String productionId = EncryptionDecryption.decrypt(this.getKey());
					connection = GetDBConnection.getDBConnection(db);
					LabelCapSchema.ensure(this.getSessionMap());
					
					sql = "SELECT "
							+ "spdm.production_id,"
							+ "spm.product_name_eng,"
							+ "spdm.finished_product_id,"
							+ "DATE_FORMAT(spdm.production_date,'%d-%m-%Y'),"
							+ "spdm.production_time,"
							+ "spdm.production_quantity,"
							+ "spdm.batch_id,"
							+ "spdm.shift,"
							+ "spdm.production_by,"
							+ "spdm.production_details,"
							+ "lm.label_name,"
							+ "lm.label_code,"
							+ "cm.cap_name,"
							+ "cm.cap_code,"
							+ "produced.product_name_eng "
							+ "FROM "
							+ "shop_production_details_master AS spdm "
							+ "INNER JOIN "
							+ "shop_product_master as spm "
							+ "ON "
							+ "spdm.finished_product_id = spm.product_id "
							+ "LEFT JOIN "
							+ "shop_label_master AS lm "
							+ "ON "
							+ "spdm.label_id = lm.label_id "
							+ "AND "
							+ "spdm.shop_id = lm.shop_id "
							+ "AND "
							+ "spdm.shop_code = lm.shop_code "
							+ "LEFT JOIN "
							+ "shop_cap_master AS cm "
							+ "ON "
							+ "spdm.cap_id = cm.cap_id "
							+ "AND "
							+ "spdm.shop_id = cm.shop_id "
							+ "AND "
							+ "spdm.shop_code = cm.shop_code "
							+ "LEFT JOIN "
							+ "shop_product_master AS produced "
							+ "ON "
							+ "spdm.product_id = produced.product_id "
							+ "AND "
							+ "produced.shop_id = spdm.shop_id "
							+ "AND "
							+ "produced.shop_code = spdm.shop_code "
							+ "WHERE "
							+ "spdm.shop_id = ? "
							+ "AND "
							+ "spdm.shop_code = ? "
							+ "AND "
							+ "spm.shop_id = ? "
							+ "AND "
							+ "spm.shop_code = ? "
							+ "AND "
							+ "spdm.production_id = ? ";
					
					preparedStatement = connection.prepareStatement(sql);
					
					preparedStatement.setString(1, shopId);
					preparedStatement.setString(2, shopCode);
					preparedStatement.setString(3, shopId);
					preparedStatement.setString(4, shopCode);
					preparedStatement.setString(5, productionId);
					
					resultSet = preparedStatement.executeQuery();
					if(resultSet.next())
					{
						this.setProductionId(resultSet.getString(1));
						this.setFinishedProductName(resultSet.getString(2));
						this.setFinishedProductId(resultSet.getString(3));
						this.setProductionDate(resultSet.getString(4));
						this.setProductionTime(resultSet.getString(5));
						this.setProductionQuantity(resultSet.getString(6));
						this.setBatchId(resultSet.getString(7));
						this.setShift(resultSet.getString(8));
						this.setProductionBy(resultSet.getString(9));
						this.setProductoinDetails(resultSet.getString(10));
						this.setLabelName(PackingMasterOptions.displayName(resultSet.getString(11), resultSet.getString(12)));
						this.setCapName(PackingMasterOptions.displayName(resultSet.getString(13), resultSet.getString(14)));
						this.setProductName(resultSet.getString(15));
						this.setKey(EncryptionDecryption.encrypt(resultSet.getString(1)));
						result = SUCCESS;
						
					}
					else
					{
						result = ERROR;
						
						addActionError("Details not found!");
					}
	       		}catch (SQLException e) {
	       			//e.printStackTrace();
	       			result = "error";
	    			addActionError(e.getErrorCode()+" || "+e.getMessage() +" || DETAILS NOT FOUND !");
	    		} catch (IOException e) {
	    			
	    			result = "error";
	    			addActionError(e.getMessage()+" || "+e.getCause()+" || DETAILS NOT FOUND !");
	    		}finally {
	    			if(resultSet != null)
    				{
    					try {
    						resultSet.close();
    					} catch (SQLException e) {
    						
    					}
    				}
	    			if(preparedStatement != null)
	    			{
	    				try {preparedStatement.close();} catch (SQLException e) {}
	    			}
	    			if(connection != null)
	    			{
	    				
	    				try {connection.close();}catch (SQLException e) {}
	    			}
	    			
	    		}
	        	
	        }
	      return result; 
	}
	

	

	public HttpSession getSession() {
		return session;
	}




	public void setSession(HttpSession session) {
		this.session = session;
	}




	public String getKey() {
		return key;
	}




	public void setKey(String key) {
		this.key = key;
	}




	public static long getSerialversionuid() {
		return serialVersionUID;
	}




	public String getProductionId() {
		return productionId;
	}




	public void setProductionId(String productionId) {
		this.productionId = productionId;
	}




	public String getFinishedProductId() {
		return finishedProductId;
	}




	public void setFinishedProductId(String finishedProductId) {
		this.finishedProductId = finishedProductId;
	}




	public String getFinishedProductName() {
		return finishedProductName;
	}




	public void setFinishedProductName(String finishedProductName) {
		this.finishedProductName = finishedProductName;
	}




	public String getBatchId() {
		return batchId;
	}




	public void setBatchId(String batchId) {
		this.batchId = batchId;
	}




	public String getShift() {
		return shift;
	}




	public void setShift(String shift) {
		this.shift = shift;
	}




	public String getProductionQuantity() {
		return productionQuantity;
	}




	public void setProductionQuantity(String productionQuantity) {
		this.productionQuantity = productionQuantity;
	}




	public String getProductionBy() {
		return productionBy;
	}




	public void setProductionBy(String productionBy) {
		this.productionBy = productionBy;
	}




	public String getProductoinDetails() {
		return productoinDetails;
	}




	public void setProductoinDetails(String productoinDetails) {
		this.productoinDetails = productoinDetails;
	}




	public String getProductionDate() {
		return productionDate;
	}




	public void setProductionDate(String productionDate) {
		this.productionDate = productionDate;
	}




	public String getProductionTime() {
		return productionTime;
	}




	public void setProductionTime(String productionTime) {
		this.productionTime = productionTime;
	}




	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 getLabelName() {
		return labelName;
	}

	public void setLabelName(String labelName) {
		this.labelName = labelName;
	}

	public String getCapName() {
		return capName;
	}

	public void setCapName(String capName) {
		this.capName = capName;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

}
