package com.reallinkbilling.login;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.sql.Blob;
import java.sql.Connection;
import javax.sql.rowset.serial.SerialBlob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.reallinkbilling.DAO.CustomerPaymentReminderDetailsDAO;
import com.reallinkbilling.utils.GetNotificationList;

import com.reallinkbilling.cipher.EncryptionDecryption;
import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.DAO.ProductBarcodeDetailsDAO;
import com.reallinkbilling.action.CaptchaAction;
import com.reallinkbilling.db.GetDBConnection;


public class LoginAction extends ActionSupport implements SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2049037212664544440L;
	private String username;
	private String password;
	private String userFullName;
	private SessionMap<String,Object> sessionMap;  
	private  HttpSession session;
	private String billDate;
	private String sessionCaptcha = (String) ServletActionContext.getRequest().getSession()
			.getAttribute(CaptchaAction.CAPTCHA_KEY);
	private String captcha;
	private Map<String,String> customerSearchOptionList = new HashMap<String,String>();
	private String customerSearchOption;
	private String defaultValue;
	private boolean showValidityExpiryAlert = false;
	private String shopValidityDaysRemaining;
	private String shopValidityEndDate;
	
	private ArrayList<CustomerPaymentReminderDetailsDAO> customerPaymentReminderList =
	        new ArrayList<CustomerPaymentReminderDetailsDAO>();
	
	
	@Override
	public String execute(){
		String result = "login";
		
		Connection connection = null;
		PreparedStatement preparedStatement= null;
		ResultSet resultSet = null;
		String sql = "";
		
		String schema = "";
		Date d = new Date();
    	SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    	setBillDate(dateFormat.format(d));
    	
    	this.getSessionMap().clear();
		if(getUsername() != null && getPassword() != null)
		{
		
			schema = "reallinkshop";
			
			this.getCustomerSearchOptionList().put("BYNAME", "BY NAME");
			this.getCustomerSearchOptionList().put("BYMOBILE", "BY MOBILE");
			this.getCustomerSearchOptionList().put("BYADDRESS","BY ADDRESS");
			this.setDefaultValue("BYNAME");
			this.setCustomerSearchOption("BYNAME");
	
	try {
			
			
		connection = GetDBConnection.getDBConnection(schema);
		
		if(connection != null)
		{
			if(!getUsername().equals("") && !getPassword().equals(""))
			{
				if(this.getSessionCaptcha() != null)
				{
					if (this.getSessionCaptcha().equals(this.getCaptcha())) 
					{
						
					
				
				sql = "SELECT "
						+ "shop_id,"
						+ "shop_code,"
						+ "shop_user_full_name,"
						+ "shop_user_role,"
						+ "shop_user_mobile_number,"
						+ "shop_user_staus,"
						+ "assigned_godown_code "
						+ "FROM "
						+ "shop_user_master "
						+ "WHERE "
						+ "shop_user_name = ? "
						+ "AND "
						+ "shop_user_password_1=? ";
				
				
				preparedStatement = connection.prepareStatement(sql);
				preparedStatement.setString(1,this.getUsername().trim());
				preparedStatement.setString(2, EncryptionDecryption.encrypt(this.getPassword().trim()));
				resultSet = preparedStatement.executeQuery();
				
						
						if(resultSet.next())
						{
							if(resultSet.getString("shop_user_staus").equals("ACTIVE"))
							{
								 String shopId = resultSet.getString(1);
								 String shopCode = resultSet.getString(2);
								 
								 this.getSessionMap().put(
										    "assignedGodownCode",
										    resultSet.getString("assigned_godown_code")
										);
								 
								
										this.getSessionMap().put(
										    "role",
										    resultSet.getString("shop_user_role")
										);
										 
							 this.getSessionMap().put("shopId",resultSet.getString(1));
							 this.getSessionMap().put("shopCode",resultSet.getString(2));
								
							  this.getSessionMap().put("login","true");  
							  this.getSessionMap().put("userFullName",resultSet.getString(3));							
							  this.getSessionMap().put("role", resultSet.getString(4));
							  this.getSessionMap().put("accountstatus", resultSet.getString(6));							
							  this.getSessionMap().put("db", schema);
							  this.setUserFullName(resultSet.getString(3));
							  
							  if(resultSet != null)
							  {
								  resultSet.close();
								  resultSet = null;
							  }
							  if(preparedStatement != null)
							  {
								  preparedStatement.close();
								  preparedStatement = null;
							  }
							  
							  sql = "SELECT "
							  		+ "shop_name_english,"
							  		+ "shop_name_marathi,"
							  		+ "shop_addressline1_eng,"
							  		+ "shop_addressline1_mr,"
							  		+ "message_key,"
							  		+ "message_sender_id,"
							  		+ "message_service_provider,"
							  		+ "shop_name_mr_2,"
							  		+ "shop_bill_address,"
							  		+ "customer_care_number,"
							  		+ "shop_one_line_address,"
							  		+ "shop_declaration,"
							  		+ "shop_gst_number,"
							  		+ "shop_pan_number,"
							  		+ "head_intro_line_content,"
							  		+ "shop_type,"
							  		+ "product_batchcode_based,"
							  		+ "billing_finanacial_year_date,"
							  		+ "enable_eway_bill,"
							  		+ "shop_name_english_estimate_quote,"
							  		+ "shop_validity_end_date "
							  		+ "FROM "
							  		+ "shop_info_master "
							  		+ "WHERE "
							  		+ "shop_id = ? "
							  		+ "AND "
							  		+ "shop_code = ? ";
							  
							  preparedStatement = connection.prepareStatement(sql);
							  
							  preparedStatement.setString(1, shopId);
							  preparedStatement.setString(2, shopCode);
							  
							  resultSet = preparedStatement.executeQuery();
							  
							  
							  if(resultSet.next())
							  {
								  boolean allowLogin = true;
								  java.sql.Date validitySqlDate = resultSet.getDate("shop_validity_end_date");

								  if (validitySqlDate != null) {
									  LocalDate validityEndDate = validitySqlDate.toLocalDate();
									  long daysToExpiry = ChronoUnit.DAYS.between(LocalDate.now(), validityEndDate);
									  String validityEndDateStr = validityEndDate
											  .format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));

									  if (daysToExpiry < 0) {
										  allowLogin = false;
										  this.getSessionMap().clear();
										  result = ERROR;
										  addActionMessage(this.getText(
												  "message.error.validityExpiredPlzContactAdministrator",
												  new String[] { validityEndDateStr }));
									  } else if (daysToExpiry <= 7) {
										  this.setShowValidityExpiryAlert(true);
										  this.setShopValidityDaysRemaining(String.valueOf(daysToExpiry));
										  this.setShopValidityEndDate(validityEndDateStr);
										  this.getSessionMap().put("shopValidityAlert", "YES");
										  this.getSessionMap().put("shopValidityDaysRemaining",
												  String.valueOf(daysToExpiry));
										  this.getSessionMap().put("shopValidityEndDate", validityEndDateStr);
									  }
								  }

								  if (allowLogin) {

								  this.getSessionMap().put("shopnameenglish", resultSet.getString(1));
								  this.getSessionMap().put("shopnamemarathi", resultSet.getString(2));
								  this.getSessionMap().put("shopaddresseng", resultSet.getString(3));
								  this.getSessionMap().put("shopaddressmr", resultSet.getString(4));
								  this.getSessionMap().put("message_key", resultSet.getString(5));
								  this.getSessionMap().put("message_sender_id", resultSet.getString(6));
								  this.getSessionMap().put("serviceprovider", resultSet.getString(7));
								  this.getSessionMap().put("shopname2", resultSet.getString(8));
								  this.getSessionMap().put("shopbillAddress", resultSet.getString(9));
								  this.getSessionMap().put("shopcustcarenumber", resultSet.getString(10));
								  this.getSessionMap().put("shoponelineaddress", resultSet.getString(11));
								  this.getSessionMap().put("shopdeclaration", resultSet.getString(12));
								  this.getSessionMap().put("shopgstnumber", resultSet.getString(13));
								  this.getSessionMap().put("shoppannumber", resultSet.getString(14));
								  this.getSessionMap().put("headintroline", resultSet.getString(15));
								  this.getSessionMap().put("shoptype", resultSet.getString(16));
								  this.getSessionMap().put("batchbased", resultSet.getString(17));
								  this.getSessionMap().put("financialyearstartdate", resultSet.getString(18));
								  this.getSessionMap().put("enableewaybill", resultSet.getString(19));									
								  this.getSessionMap().put("shopnameenglishestiquote", resultSet.getString(20));
									
								  HashMap<String,String> shopSettingsMap = new HashMap<String,String>();
								  
								  PreparedStatement getSettingsPSMT = null;
								  ResultSet getSettingsRS = null;
								  String getSettingsSQL = "SELECT "
								  		+ "setting_name,"
								  		+ "setting_value "
								  		+ "FROM "
								  		+ "shop_settings "
								  		+ "WHERE "
								  		+ "shop_id = ? "
								  		+ "AND "
								  		+ "shop_code = ?";
								  getSettingsPSMT = connection.prepareStatement(getSettingsSQL);
								  
								  getSettingsPSMT.setString(1, shopId);
								  getSettingsPSMT.setString(2, shopCode);
								 
								  getSettingsRS = getSettingsPSMT.executeQuery();
								  
								  while(getSettingsRS.next())
								  {
									  shopSettingsMap.put(getSettingsRS.getString(1), getSettingsRS.getString(2));								  
									  //System.out.println(getSettingsRS.getString(1)+","+ getSettingsRS.getString(2));
								  }
								  
								  this.getSessionMap().put("shop_settings",shopSettingsMap);
								  this.getSessionMap().put("sendWhatsAppMessage", shopSettingsMap.get("sendWhatsAppMessage"));
								  this.getSessionMap().put("defaultWhatsAppNumber", shopSettingsMap.get("defaultWhatsAppNumber"));
								  this.getSessionMap().put("sendWhatsAppMessageType", shopSettingsMap.get("sendWhatsAppMessageType"));
								  
								  if(getSettingsRS != null)
								  {
									  getSettingsRS.close();
									  getSettingsRS = null;
								  }
								  if(getSettingsPSMT != null)
								  {
									  getSettingsPSMT.close();
									  getSettingsPSMT = null;
								  }
								  
								  
								  PreparedStatement getQRFilePSMT = null;
								  ResultSet getQRFileRS = null;
								  String getQRFileSQL = "SELECT "
								  		+ "qr_code_file,"
								  		+ "left_logo_file,"
								  		+ "right_logo_file ,"
								  		+ "thermal_print_image_file "
								  		+ "FROM "
								  		+ "shop_info_master "
								  		+ "WHERE "
								  		+ "shop_id = ? "
								  		+ "AND "
								  		+ "shop_code = ?";
								  
								  getQRFilePSMT = connection.prepareStatement(getQRFileSQL);
								  getQRFilePSMT.setString(1, shopId);
								  getQRFilePSMT.setString(2, shopCode);
								  
								  getQRFileRS = getQRFilePSMT.executeQuery();
								  
								  if(getQRFileRS.next())
								  {
									  Blob blob = getQRFileRS.getBlob(1);
									  Blob leftLogo =  getQRFileRS.getBlob(2);
									  Blob rightLogo =  getQRFileRS.getBlob(3);
									  Blob thermalPrintHeaderImage = getQRFileRS.getBlob(4);
									  
									  InputStream leftLogoIS = null;
								      InputStream rightLogoIS = null;
								      InputStream thermalPrintImageIS = null;
									  
									  if(blob != null)
									  {
										  InputStream qrIS = blob.getBinaryStream();
										  if(qrIS != null)
										  {
											  byte[] qrBytes = IOUtils.toByteArray(qrIS);
											  qrIS.close();
											  this.getSessionMap().put("qr_image_bolb", new SerialBlob(qrBytes));
										  }
										  blob.free();
									  }
									  
									  if(leftLogo != null)
									  {
										  
										  leftLogoIS = leftLogo.getBinaryStream();
										  byte[] leftLogoBytes = IOUtils.toByteArray(leftLogoIS);
											 
										  this.getSessionMap().put("left_logo_image_array",leftLogoBytes); 
										  
										  if(leftLogoIS != null)
										  {
											  leftLogoIS.close();
											  leftLogoIS = null;
										  }
									  }
									  
									  if(rightLogo != null)
									  {
										  
										 
										  
										  rightLogoIS = rightLogo.getBinaryStream();
										  byte[] rightLogoBytes = IOUtils.toByteArray(rightLogoIS);
											 
										  this.getSessionMap().put("right_logo_image_array",rightLogoBytes); 
										  
										  if(rightLogoIS != null)
										  {
											  rightLogoIS.close();
											  rightLogoIS = null;
										  }
										  
									  }
									  
									  
									  if(thermalPrintHeaderImage != null)
									  {
										  
										 
										  
										  thermalPrintImageIS = thermalPrintHeaderImage.getBinaryStream();
										  byte[] thermalPrintHeaderImageBytes = IOUtils.toByteArray(thermalPrintImageIS);
											 
										  this.getSessionMap().put("thermal_print_header_image",thermalPrintHeaderImageBytes); 
										  
										  if(thermalPrintImageIS != null)
										  {
											  thermalPrintImageIS.close();
											  thermalPrintImageIS = null;
										  }
										  
									  }
								  }
								  
								  
								  
								  if(getQRFileRS != null)
								  {
									  getQRFileRS.close();
									  getQRFileRS = null;
								  }
								  
								  if(getQRFilePSMT != null)
								  {
									  getQRFilePSMT.close();
									  getQRFilePSMT = null;
								  }
								  
								  if(resultSet != null)
								  {
									  resultSet.close();
									  resultSet = null;
								  }
								  
								  if(preparedStatement != null)
								  {
									  preparedStatement.close();
									  preparedStatement = null;
								  }
								  
								  HashMap<String,String> templateMap = new HashMap<String,String>();
					        		
					        		sql = "SELECT "
					        				+ "template_name,"
					        				+ "tamplate_id,"
					        				+ "message "
					        				+ "FROM "
					        				+ "shop_message_template_map_master "
					        				+ "WHERE "
					        				+ "shop_id = ? "
					        				+ "AND "
					        				+ "shop_code = ? ";
					        		
					        		
					        		preparedStatement = connection.prepareStatement(sql);
					        		
					        		preparedStatement.setString(1, shopId);
					        		preparedStatement.setString(2, shopCode);
					        		
					        		resultSet = preparedStatement.executeQuery();
					        
					        		while(resultSet.next())
					        		{
					        			
					        			templateMap.put(resultSet.getString(1), resultSet.getString(2)+"#"+resultSet.getString(3));
					        		}
					        		
					        		this.getSessionMap().put("template_map_list", templateMap);
					        		
					        		
					        		  if(resultSet != null)
									  {
										  resultSet.close();
										  resultSet = null;
									  }
									  
									  if(preparedStatement != null)
									  {
										  preparedStatement.close();
										  preparedStatement = null;
									  }
					        		
					        		Map<String,String> taxList = new LinkedHashMap<String,String>();
					        		
					        		
					        		sql = "SELECT "
											+ "id_tax_table,"
											+ "tax_name "
											+ "FROM "
											+ "shop_tax_table_master "
											+ "WHERE "
											+ "shop_id = ? "
											+ "AND "
											+ "shop_code = ? "
											+ "ORDER BY tax_name";
									
									preparedStatement = connection.prepareStatement(sql);
									preparedStatement.setString(1, shopId);
									preparedStatement.setString(2, shopCode);
									
									resultSet = preparedStatement.executeQuery();
									
									while(resultSet.next())
									{
										taxList.put(resultSet.getString(1),resultSet.getString(2));
									}
									
									this.getSessionMap().put("tax_list", taxList);
									
									  if(resultSet != null)
									  {
										  resultSet.close();
										  resultSet = null;
									  }
									  
									  if(preparedStatement != null)
									  {
										  preparedStatement.close();
										  preparedStatement = null;
									  }
					        		
					        		Map<String,String> bankDetails = new LinkedHashMap<String,String>();
					        		
					        		
					        		sql = "SELECT "
											+ "shop_banking_name,"
											+ "bank_name,"
											+ "bank_branch_name,"
											+ "account_number,"
											+ "IFSC_code,"
											+ "micr_code,"
											+ "upi_id "
											+ "FROM "
											+ "shop_bank_details_master "
											+ "WHERE "
											+ "shop_id = ? "
											+ "AND "
											+ "shop_code = ? ";
									
									preparedStatement = connection.prepareStatement(sql);
									preparedStatement.setString(1, shopId);
									preparedStatement.setString(2, shopCode);
									
									resultSet = preparedStatement.executeQuery();
									
									while(resultSet.next())
									{
										bankDetails.put("bankingname",resultSet.getString(1));
										bankDetails.put("bankname",resultSet.getString(2));
										bankDetails.put("branchname",resultSet.getString(3));
										bankDetails.put("accnumber",resultSet.getString(4));
										bankDetails.put("ifsccode",resultSet.getString(5));
										bankDetails.put("micrcode",resultSet.getString(6));
										bankDetails.put("upicode",resultSet.getString(7));
									}
				        		
				        		
									this.getSessionMap().put("bank_details", bankDetails);
									

									 if(resultSet != null)
									  {
										  resultSet.close();
										  resultSet = null;
									  }
									  
									  if(preparedStatement != null)
									  {
										  preparedStatement.close();
										  preparedStatement = null;
									  }
									  
									 HashMap<String,String> empDesignationMap = new HashMap<String,String>();
						        		
						        		sql = "SELECT "
						        				+ "designation_code,"
						        				+ "designation_name "
						        				+ "FROM "
						        				+ "shop_employee_designation_master "
						        				+ "WHERE "
						        				+ "shop_id = ? "
												+ "AND "
												+ "shop_code = ? "
						        				+ "ORDER BY designation_name";
						        		
						        		preparedStatement = connection.prepareStatement(sql);
						        		preparedStatement.setString(1, shopId);
						        		preparedStatement.setString(2, shopCode);
						        		
						        		
						        		resultSet = preparedStatement.executeQuery();
						        		
						        		while(resultSet.next())
						        		{
						        			empDesignationMap.put(resultSet.getString(1), resultSet.getString(2));
						        		}
						        		
						        		this.getSessionMap().put("designationlist", empDesignationMap);
						        		
						        		
						        	
									 if(resultSet != null)
									  {
										  resultSet.close();
										  resultSet = null;
									  }
									  
									  if(preparedStatement != null)
									  {
										  preparedStatement.close();
										  preparedStatement = null;
									  }
									  
									 HashMap<String,ProductBarcodeDetailsDAO> barcodeDetailsMap = new HashMap<String, ProductBarcodeDetailsDAO>();
									 HashMap<String,String> productHSNDetails = new HashMap<String,String>();
									 sql = "SELECT "
									 		+ "product_id,"
									 		+ "product_short_code,"
									 		+ "product_name_eng,"
									 		+ "product_name_mar,"
									 		+ "tax_id,"
									 		+ "tax_percentage,"
									 		+ "sales_unit,"
									 		+ "hsn_sac_code,"
									 		+ "barcode_mrp,"
									 		+ "discount_type,"
									 		+ "discount_amount_percentage,"
									 		+ "sales_price,"
									 		+ "default_barcode_quantity,"
									 		+ "purchase_rate,"
									 		+ "customer_sales_price,"
									 		+ "product_alias,"
									 		+ "in_stock_quantity,"
									 		+ "in_stock_quantity_2 "
									 		+ "FROM "
									 		+ "shop_product_master "
									 		+ "WHERE "
									 		+ "shop_id = ? "
									 		+ "AND "
									 		+ "shop_code = ? ";
									 
									 preparedStatement = connection.prepareStatement(sql);
									 preparedStatement.setString(1, shopId);
									 preparedStatement.setString(2, shopCode);
									 
									 resultSet = preparedStatement.executeQuery();
									 
									 while(resultSet.next())
									 {
										 ProductBarcodeDetailsDAO  detailsDAO = new ProductBarcodeDetailsDAO();
										 detailsDAO.setProductId(resultSet.getString(1));
										 detailsDAO.setProductCode(resultSet.getString(2));
										 detailsDAO.setProductNameEn(resultSet.getString(3));
										 detailsDAO.setProductNameMr(resultSet.getString(4));
										 detailsDAO.setTaxId(resultSet.getInt(5));
										 detailsDAO.setTaxPercentage(resultSet.getFloat(6));
										 detailsDAO.setSalesUnit(resultSet.getString(7));
										 detailsDAO.setHsnSacCode(resultSet.getString(8));
										 detailsDAO.setBarcodeMRP(resultSet.getFloat(9));
										 detailsDAO.setDiscountType(resultSet.getString(10));
										 detailsDAO.setDiscount(resultSet.getFloat(11));
										 detailsDAO.setSalesPrice(resultSet.getFloat(12));
										 detailsDAO.setBarCodeQuantity(resultSet.getFloat(13));
										 detailsDAO.setPurchasePrice(resultSet.getFloat(14));
										 detailsDAO.setCustomerSalePrice(resultSet.getFloat(15));
										 detailsDAO.setProductAlias(resultSet.getString(16));
										 detailsDAO.setInStockQuantity(resultSet.getFloat(17));
										 detailsDAO.setInStockQuantity2(resultSet.getFloat(18));
										 barcodeDetailsMap.put(resultSet.getString(2), detailsDAO);
										 
										 productHSNDetails.put(resultSet.getString(1), resultSet.getString(8));
										 
									 }
									
									 	this.getSessionMap().put("productHSNcode", productHSNDetails);
									 
										this.getSessionMap().put("barcodeProductDetails", barcodeDetailsMap);
										
										
										 if(resultSet != null)
										  {
											  resultSet.close();
											  resultSet = null;
										  }
										  
										  if(preparedStatement != null)
										  {
											  preparedStatement.close();
											  preparedStatement = null;
										  }
										  
										HashMap<String, String> whatsAppMsgTokenList = new HashMap<String, String>();

										sql = "SELECT mobile_number,token_number " + "FROM "
												+ "shop_whatsapp_message_mapping_master " + "WHERE "
												+ "shop_id = ? " + "AND " + "shop_code = ? ";

										preparedStatement = connection.prepareStatement(sql);

										preparedStatement.setString(1, shopId);

										preparedStatement.setString(2, shopCode);

										resultSet = preparedStatement.executeQuery();

										while (resultSet.next()) {

											whatsAppMsgTokenList.put(resultSet.getString(1),
													resultSet.getString(2));

										}

										this.getSessionMap().put("whatsappmsgtokenlist", whatsAppMsgTokenList);
										
										if(resultSet != null)
										{
											resultSet.close();
											resultSet = null;
										}
										
										if(preparedStatement != null)
										{
											preparedStatement.close();
											preparedStatement = null;
										}
										
										// Customer Payment Reminder Notifications
										this.setCustomerPaymentReminderList(
										        GetNotificationList.getCustPaymentReminderList(this.getSessionMap()));

										this.getSessionMap().put("customerpaymentnotificationlist",
										        this.getCustomerPaymentReminderList());
										
										
										
									
								  result = SUCCESS;

								  } // end allowLogin
							  }
							  else
							  {
								  result = ERROR;
								  addActionError("Sorry! Couldn't find shop details! Please contact adminisatrator!");
							  }
							  
							  
							 
							}
							else
							{
								result = ERROR;
								addActionMessage("Your Account is deactivated ! Please contact adminisatrtor! ");
							}
							
						}
						else
						{
							result = ERROR;
							addActionMessage("Invalid Username and Password !!!");
						}
					}else
					{
						result = ERROR;
						addActionMessage("Inavlid Captcha !!!");
					}
				}
				else
				{
					result = ERROR;
					addActionMessage("Null Captcha !!!");
				}
				}
			}
			else
			{
				result = ERROR;
				addActionMessage("Connection Error !!!");
			}
			
				
			} catch (SQLException e) {
				e.printStackTrace();
				result = ERROR;
				addActionMessage(e.getErrorCode()+ "| Connection Error !!!");
			
			}catch (IOException e) {
				e.printStackTrace();
				result = ERROR;
				addActionMessage(e.getMessage()+ "| Connection Error !!!");
			}
			catch (Exception e) {
				e.printStackTrace();
				result = ERROR;
				addActionMessage(e.getMessage()+ "| Connection Error !!!");
			}
			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) {
						
						//e.printStackTrace();
					}
				}
				
			}
		
		}
		
		return result;
	}
	
	
	@Override
	public void validate() 
	{		
		if(this.getUsername() == null || this.getPassword() == null)
		{
			addActionMessage("Please Provide UserName and Password !");
		}
		else if(this.getUsername().equals("") || this.getPassword().equals(""))
		{
			addActionMessage("Please Provide UserName and Password !");
		}
	}


	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getUserFullName() {
		return userFullName;
	}


	public void setUserFullName(String userFullName) {
		this.userFullName = userFullName;
	}
	public void setSession(Map<String, Object> map) {
		// TODO Auto-generated method stub
		 sessionMap=(SessionMap<String, Object>)map;  
		
	}

	

	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}


	public void setSessionMap(SessionMap<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}


	public HttpSession getSession() {
		return session;
	}


	public void setSession(HttpSession session) {
		this.session = session;
	}


	public String getBillDate() {
		return billDate;
	}


	public void setBillDate(String billDate) {
		this.billDate = billDate;
	}


	public String getSessionCaptcha() {
		return sessionCaptcha;
	}


	public void setSessionCaptcha(String sessionCaptcha) {
		this.sessionCaptcha = sessionCaptcha;
	}


	public String getCaptcha() {
		return captcha;
	}


	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}


	


	public Map<String, String> getCustomerSearchOptionList() {
		return customerSearchOptionList;
	}


	public void setCustomerSearchOptionList(Map<String, String> customerSearchOptionList) {
		this.customerSearchOptionList = customerSearchOptionList;
	}


	public String getDefaultValue() {
		return defaultValue;
	}


	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}


	public String getCustomerSearchOption() {
		return customerSearchOption;
	}


	public void setCustomerSearchOption(String customerSearchOption) {
		this.customerSearchOption = customerSearchOption;
	}


	public boolean isShowValidityExpiryAlert() {
		return showValidityExpiryAlert;
	}


	public void setShowValidityExpiryAlert(boolean showValidityExpiryAlert) {
		this.showValidityExpiryAlert = showValidityExpiryAlert;
	}


	public String getShopValidityDaysRemaining() {
		return shopValidityDaysRemaining;
	}


	public void setShopValidityDaysRemaining(String shopValidityDaysRemaining) {
		this.shopValidityDaysRemaining = shopValidityDaysRemaining;
	}


	public String getShopValidityEndDate() {
		return shopValidityEndDate;
	}


	public void setShopValidityEndDate(String shopValidityEndDate) {
		this.shopValidityEndDate = shopValidityEndDate;
	}


	public ArrayList<CustomerPaymentReminderDetailsDAO> getCustomerPaymentReminderList() {
		return customerPaymentReminderList;
	}


	public void setCustomerPaymentReminderList(ArrayList<CustomerPaymentReminderDetailsDAO> customerPaymentReminderList) {
		this.customerPaymentReminderList = customerPaymentReminderList;
	}

	
	
	
}
