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 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 UpdateCustomerOpeningBalJsp extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8707644622639103394L;

	private SessionMap<String,Object> sessionMap; 
	private String key;
	private String custName;
	private String custAddress;
	
	public String execute(){
			
			String result = "login";
		        if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
		        	addActionMessage("Please Login First !!!");
		        	result = "login";
		        }  
		        else{
		        	
		        	
		        	String custId = EncryptionDecryption.decrypt(this.getKey());
		        	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 {
		       			
		       			
						connection = GetDBConnection.getDBConnection(db);
						
						sql = "select "	       					
		       					+ "customer_id,"		       				
		       					+ "customer_name,"
		       					+ "billing_address "
		       					+ "FROM "
		       					+ "shop_customer_master "
		       					+ "WHERE "
		       					+ "shop_id = ? "
		       					+ "AND "
		       					+ "shop_code = ? "
		       					+ "AND "
		       					+ "customer_id = ?";
		       			
		       			preparedStatement = connection.prepareStatement(sql);
		       			preparedStatement.setString(1, shopId);
		       			preparedStatement.setString(2, shopCode);
		       			preparedStatement.setString(3, custId);
		       			
		       			resultSet = preparedStatement.executeQuery();
		       			resultSet.last();
		       			int rowCount = resultSet.getRow();
		       			resultSet.beforeFirst();
		       			if(rowCount>0)
		       			{
			       			if(resultSet.next())
			       			{
			       				this.setCustName(resultSet.getString(2));
			       				this.setCustAddress(resultSet.getString(3));
			       				
			       			}
			       			
			       			result = SUCCESS;
		       			}else
		       			{
		       				result = ERROR;
		       				addActionError("Details of specified customer not found !");
		       			}
			       			
		       		}
		       	 catch (SQLException e) {
		        		result = ERROR;
	   				
	   				addActionMessage(e.getErrorCode()+"||"+e.getMessage()+"|| SOME ERROR");
				
				} catch (IOException e) {
					
					result = ERROR;
					addActionMessage(e.getMessage()+"|| SOME 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) {}
	   				}
	   				
	   			}
		        }
		        
		        return result;
	}
	
	

public String getKey() {
		return key;
	}



	public void setKey(String key) {
		this.key = key;
	}



	public static long getSerialversionuid() {
		return serialVersionUID;
	}



public String getCustName() {
		return custName;
	}



	public void setCustName(String custName) {
		this.custName = custName;
	}



public String getCustAddress() {
		return custAddress;
	}



	public void setCustAddress(String custAddress) {
		this.custAddress = custAddress;
	}



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;  
}
}
