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.DAO.TaxDAO;
import com.reallinkbilling.db.GetDBConnection;

public class AddNewTaxJsp extends ActionSupport implements SessionAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5953849125972373414L;
	private HttpSession session;
	private List<TaxDAO> taxList = new ArrayList<TaxDAO>();
	private SessionMap<String,Object> sessionMap;  
	
	@Override
	public String execute() throws Exception {
		
		String result = "login";
		session=ServletActionContext.getRequest().getSession(false); 
		 
	        if(session==null || session.getAttribute("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");
				String shopId = (String) this.getSessionMap().get("shopId");
				String shopCode = (String) this.getSessionMap().get("shopCode");
				try
				{
					
					connection = GetDBConnection.getDBConnection(db);					
					
					sql = "SELECT "
							+ "tax_name,"
							+ "taxpercentage "
							+ "FROM "
							+ "shop_tax_table_master "
							+ "WHERE "
							+ "shop_id = ? "
							+ "AND "
							+ "shop_code = ? ";
					
					preparedStatement = connection.prepareStatement(sql);
					preparedStatement.setString(1, shopId);
					preparedStatement.setString(2, shopCode);
					
					resultSet = preparedStatement.executeQuery();
					int serial = 1;
					while(resultSet.next())
					{
						TaxDAO taxDAO = new TaxDAO();
						taxDAO.setSerial(""+serial);
						taxDAO.setTaxName(resultSet.getString(1));
						taxDAO.setTaxPercentage(resultSet.getFloat(2));
						this.getTaxList().add(taxDAO);
						serial++;
					}
					
					result = SUCCESS;
					
					
				} catch (SQLException e) {
					result = ERROR;
					
					addActionError(e.getErrorCode()+" || "+e.getMessage() );
				} catch (IOException e) {
					result = ERROR;
					
					addActionError(e.getMessage()+" || "+e.getCause());
				}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 HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public List<TaxDAO> getTaxList() {
		return taxList;
	}

	public void setTaxList(List<TaxDAO> taxList) {
		this.taxList = taxList;
	}
	
	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;  
	}
	

}
