package com.reallinkbilling.delete;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

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 DeleteSupplierPaymentEntry extends ActionSupport implements SessionAware{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5386174976391483948L;

	private String paymentId;
	private String key;
	private Map<String,String> customerSearchOptionList = new HashMap<String,String>();
	private String customerSearchOption;
	private String defaultValue;
	private SessionMap<String,Object> sessionMap;
	
	public String execute() throws Exception {
		String result = "login";
		
		     if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
	        	addActionMessage("Please Login First !!!");
	        	result = "login";
	        }  
	        else{
	        	
	        	
	        	customerSearchOptionList.put("BYNAME", "BY NAME");
	        	customerSearchOptionList.put("BYMOBILE", "BY MOBILE");
	        	customerSearchOptionList.put("BYADDRESS","BY ADDRESS");
				setCustomerSearchOption("BYNAME");
				setDefaultValue("BYNAME");
	        
	        	Connection connection = null;
	    		PreparedStatement preparedStatement= null;
	    		ResultSet resultSet = null;
	    	 
	    		String sql = "";
	    		String db = (String) this.getSessionMap().get("db");
	    		
	    		try
	    		{
	    			
	    			String shopId = (String) this.getSessionMap().get("shopId");
		    		String shopCode = (String) this.getSessionMap().get("shopCode");
		    		
					connection = GetDBConnection.getDBConnection(db);
	    			
	    			if(this.getPaymentId() ==null || this.getPaymentId().equals("") || this.getPaymentId().length()>6)
	    			{
	    				addActionError("URL Tampered! Please try again!");
	    				result = "error";
	    			}
	    			if(!hasActionErrors())
	    			{
	    				sql ="DELETE "
	    						+ "FROM "
	    						+ "shop_supplier_payment_details "
	    						+ "WHERE "
	    						+ "entry_id = ? "
	    						+ "AND "
	    						+ "shop_id = ? "
	    						+ "AND "
	    						+ "shop_code = ? ";
	    				
	    				preparedStatement = connection.prepareStatement(sql);
	    				preparedStatement.setString(1, EncryptionDecryption.decrypt(this.getPaymentId()));
	    				preparedStatement.setString(2, shopId);
	    				preparedStatement.setString(3, shopCode);
	    				
	    				int k = preparedStatement.executeUpdate();
	    				
	    				if(k>0)
	    				{
	    					addActionMessage("Supplier payment entry deleted succesfully!");
	    					result = "success";
	    				}
	    				else
	    				{
	    					addActionError("Supplier payment entry not deleted!");
	    					result = "error";
	    				}
	    			}
	    			
	    			
	    			
	    		}
	    		catch (SQLException e) {
	    			
	    			result = "error";
	    			
	    			addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Supplier payment entry not deleted!");
	    		}
	    		catch (Exception e) {
	    			result = "error";
	    			addActionError(e.getMessage() +" || Supplier payment entry not deleted!");
				}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 String getPaymentId() {
		return paymentId;
	}

	public void setPaymentId(String paymentId) {
		this.paymentId = paymentId;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Map<String, String> getCustomerSearchOptionList() {
		return customerSearchOptionList;
	}

	public void setCustomerSearchOptionList(Map<String, String> customerSearchOptionList) {
		this.customerSearchOptionList = customerSearchOptionList;
	}

	public String getCustomerSearchOption() {
		return customerSearchOption;
	}

	public void setCustomerSearchOption(String customerSearchOption) {
		this.customerSearchOption = customerSearchOption;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}


	public String getKey() {
		return key;
	}


	public void setKey(String key) {
		this.key = key;
	}


	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;  
	}

	
}
