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;

public class UpdateSupplierJsp extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6182062425953711991L;

	private String key;
	private String supplierId;
	private HttpSession session;
	private String supplierAddDate;
	private String supplierName;
	private String personName;
	private String mobileNumber;
	private String address;
	private String gstNumber;
	private String pinCodeNumber;
	
	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");
	    		
	        	
	        	
	        	String supplierId = EncryptionDecryption.decrypt(this.getKey());
	        	
	        	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 "
	       					+ "supplier_id,"
	       					+ "suplier_name,"
	       					+ "contact_person_name,"
	       					+ "contact_number,"	       					
	       					+ "supplier_address,"
	       					+ "gst_number,"
	       					+ "pin_code_number "
	       					+ "FROM "
	       					+ "shop_supplier_master "
	       					+ "WHERE "
	       					+ "shop_id = ? "
	       					+ "AND "
	       					+ "shop_code = ? "
	       					+ "AND "
	       					+ "supplier_id = ? ";
	       			
	       			preparedStatement = connection.prepareStatement(sql);
	       			preparedStatement.setString(1, shopId);
	       			preparedStatement.setString(2, shopCode);
	       			preparedStatement.setString(3, supplierId);
	       			
	       			resultSet = preparedStatement.executeQuery();
	       			
	       			int rowCount = 0;
	       			
	       			resultSet.last();
	       			rowCount = resultSet.getRow();
	       			resultSet.beforeFirst();
	       			
	       			if(rowCount>0)
	       			{
	       				while(resultSet.next())
	       				{
	       					this.setSupplierId(supplierId);
	       					this.setSupplierName(resultSet.getString(2));
	       					this.setPersonName(resultSet.getString(3));
	       					this.setMobileNumber(resultSet.getString(4));
	       					this.setAddress(resultSet.getString(5));
	       					this.setGstNumber(resultSet.getString(6));
	       					this.setPinCodeNumber(resultSet.getString(7));
	       					
	       					this.setKey(EncryptionDecryption.encrypt(supplierId));
	       				}
	       				
	       				result = "success";
	       			}
	       			else
	       			{
	       				result = "error";
	       				addActionError("URL Tampered! Details not found!");
	       			}
	       			
	       		
	       		}
	       		catch (SQLException e) 
	       		{
	        		result = "error";					
					addActionMessage(e.getErrorCode()+"||"+e.getMessage()+"||SUPPLIER DETAILS NOT FOUND! SOME ERROR!");
		
	       		}
	       		catch (IOException e) 
	       		{
					
					result = "error";
					addActionMessage(e.getMessage()+"|| SUPPLIER DETAILS NOT FOUND! SOME ERROR!");
				}	
	   			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 String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public String getSupplierName() {
		return supplierName;
	}

	public void setSupplierName(String supplierName) {
		this.supplierName = supplierName;
	}

	public String getPersonName() {
		return personName;
	}

	public void setPersonName(String personName) {
		this.personName = personName;
	}

	public String getMobileNumber() {
		return mobileNumber;
	}

	public void setMobileNumber(String mobileNumber) {
		this.mobileNumber = mobileNumber;
	}

	

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}
	public String getSupplierAddDate() {
		return supplierAddDate;
	}

	public void setSupplierAddDate(String supplierAddDate) {
		this.supplierAddDate = supplierAddDate;
	}

	public String getGstNumber() {
		return gstNumber;
	}

	public void setGstNumber(String gstNumber) {
		this.gstNumber = gstNumber;
	}

	public String getPinCodeNumber() {
		return pinCodeNumber;
	}

	public void setPinCodeNumber(String pinCodeNumber) {
		this.pinCodeNumber = pinCodeNumber;
	}

	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;  
	}
	
	
	
}
