package com.reallinkbilling.delete;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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 DeleteEmployeePresentEntry extends ActionSupport implements SessionAware{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -243072023730998668L;

	private String fromDate;
	private String toDate;
	private String key;
	private String secKey;
	
	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{
	        	
	       
	        	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);
	    		
	    			//custid + entryid + date + amount
	    			
	    			
	    			String keyData [] = EncryptionDecryption.decrypt(this.getKey()).split("#");
	    			
	    			String enrtyId = keyData[0];
	    			String employeeId = keyData[1];
	    			
	    			
	    			if(!hasActionErrors())
	    			{
	    				sql ="DELETE "
	    						+ "FROM "
	    						+ "shop_employee_presenty_details "
	    						+ "WHERE "
	    						+ "employee_id = ? "
	    						+ "AND "
	    						+ "entry_id = ? "
	    						+ "AND "
	    						+ "shop_id = ? "
	    						+ "AND "
	    						+ "shop_code = ? ";
	    				
	    				preparedStatement = connection.prepareStatement(sql);
	    				preparedStatement.setString(1, employeeId);
	    				preparedStatement.setString(2, enrtyId);
	    				preparedStatement.setString(3, shopId);
	    				preparedStatement.setString(4, shopCode);
	    				
	    				int k = preparedStatement.executeUpdate();
	    				
	    				if(k>0)
	    				{
	    					addActionMessage("Employee Absent entry deleted successfully!");
	    					result = SUCCESS;
	    				}
	    				else
	    				{
	    					addActionError("Employee Absent entry not deleted!");
	    					result = ERROR;
	    				}
	    			}
	    			
	    			
	    			
	    		}
	    		catch (SQLException e) {
	    			e.printStackTrace();
	    			result = ERROR;
	    			
	    			addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Employee Absent entry not deleted!");
	    		}
	    		catch (Exception e) {
	    			e.printStackTrace();
	    			result = ERROR;
	    			addActionError(e.getMessage() +" || Employee Absent 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 static long getSerialversionuid() {
		return serialVersionUID;
	}

	

	public String getFromDate() {
		return fromDate;
	}


	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}


	public String getToDate() {
		return toDate;
	}


	public void setToDate(String toDate) {
		this.toDate = toDate;
	}


	public String getKey() {
		return key;
	}


	public void setKey(String key) {
		this.key = key;
	}


	public String getSecKey() {
		return secKey;
	}


	public void setSecKey(String secKey) {
		this.secKey = secKey;
	}


	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;  
	}

	
}

