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.ArrayList;
import java.util.List;
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 UpdateCustomerJsp extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6951638191575198719L;
	private String key;
	private HttpSession session;
	private String cutomerType;
	private String custName;
	private String custMobileNumber;
	private String custNameMarathi;
	private String custSecondaryMobileNumber;
	private String billingAddressMrathi;
	private String email;
	private String GSTINumber;
	private String TINumber;
	private String PANumber;
	private String BillingAddress;
	private String birthDate;
	private String creditLimit;
	private String custAddDate;
	private List<String> customerTypeList = new ArrayList<String>();
	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{
	        	
	        	this.getCustomerTypeList().add("REGULER");
	        	this.getCustomerTypeList().add("RETAILER");
	        	this.getCustomerTypeList().add("DEALER");
	        	
	        	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,"
	       					+ "DATE_FORMAT(add_date,'%d-%m-%Y'),"
	       					+ "customer_name,"
	       					+ "cust_name_marathi,"
	       					+ "mobile_number,"
	       					+ "cust_secondary_mobile,"
	       					+ "gst_number,"
	       					+ "tin_number,"
	       					+ "pan_number,"
	       					+ "customer_type,"
	       					+ "billing_address,"
	       					+ "billing_address_marathi,"
	       					+ "DATE_FORMAT(birth_date,'%d-%m-%Y'),"
	       					+ "credit_limit "
	       					+ "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.setKey(EncryptionDecryption.encrypt(resultSet.getString(1)));
		       				this.setCustAddDate(resultSet.getString(2));
		       				this.setCustName(resultSet.getString(3));
		       				this.setCustNameMarathi(resultSet.getString(4));
		       				this.setCustMobileNumber(resultSet.getString(5));		
		       				this.setCustSecondaryMobileNumber(resultSet.getString(6));
		       				this.setGSTINumber(resultSet.getString(7));
		       				this.setTINumber(resultSet.getString(8));
		       				this.setPANumber(resultSet.getString(9));
		       				this.setCutomerType(resultSet.getString(10));
		       				this.setBillingAddress(resultSet.getString(11));
		       				this.setBillingAddressMrathi(resultSet.getString(12));
		       				this.setBirthDate(resultSet.getString(13));
		       				this.setCreditLimit(resultSet.getString(14));
		       				
		       			}
		       			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 HttpSession getSession() {
	return session;
}

public void setSession(HttpSession session) {
	this.session = session;
}

public String getCustAddDate() {
	return custAddDate;
}

public void setCustAddDate(String custAddDate) {
	this.custAddDate = custAddDate;
}

public String getCustName() {
	return custName;
}

public void setCustName(String custName) {
	this.custName = custName;
}

public String getCustMobileNumber() {
	return custMobileNumber;
}

public void setCustMobileNumber(String custMobileNumber) {
	this.custMobileNumber = custMobileNumber;
}

public String getEmail() {
	return email;
}

public void setEmail(String email) {
	this.email = email;
}

public String getGSTINumber() {
	return GSTINumber;
}

public void setGSTINumber(String gSTINumber) {
	GSTINumber = gSTINumber;
}

public String getTINumber() {
	return TINumber;
}

public void setTINumber(String tINumber) {
	TINumber = tINumber;
}

public String getPANumber() {
	return PANumber;
}

public void setPANumber(String pANumber) {
	PANumber = pANumber;
}

public String getBillingAddress() {
	return BillingAddress;
}

public void setBillingAddress(String billingAddress) {
	BillingAddress = billingAddress;
}

public String getBirthDate() {
	return birthDate;
}

public void setBirthDate(String birthDate) {
	this.birthDate = birthDate;
}

public String getCreditLimit() {
	return creditLimit;
}

public void setCreditLimit(String creditLimit) {
	this.creditLimit = creditLimit;
}
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;  
}

public String getCutomerType() {
	return cutomerType;
}

public void setCutomerType(String cutomerType) {
	this.cutomerType = cutomerType;
}

public List<String> getCustomerTypeList() {
	return customerTypeList;
}

public void setCustomerTypeList(List<String> customerTypeList) {
	this.customerTypeList = customerTypeList;
}

public static long getSerialversionuid() {
	return serialVersionUID;
}

public String getCustNameMarathi() {
	return custNameMarathi;
}

public void setCustNameMarathi(String custNameMarathi) {
	this.custNameMarathi = custNameMarathi;
}

public String getCustSecondaryMobileNumber() {
	return custSecondaryMobileNumber;
}

public void setCustSecondaryMobileNumber(String custSecondaryMobileNumber) {
	this.custSecondaryMobileNumber = custSecondaryMobileNumber;
}

public String getBillingAddressMrathi() {
	return billingAddressMrathi;
}

public void setBillingAddressMrathi(String billingAddressMrathi) {
	this.billingAddressMrathi = billingAddressMrathi;
}

}
