package com.reallinkbilling.jspRedirect;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
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.CapMasterDAO;
import com.reallinkbilling.db.GetDBConnection;
import com.reallinkbilling.utils.LabelCapSchema;
import com.reallinkbilling.utils.PackingMasterOptions;

public class AddNewCapJsp extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 1L;

	private HttpSession session;
	private SessionMap<String, Object> sessionMap;
	private Map<String, String> customerList = new LinkedHashMap<String, String>();
	private List<CapMasterDAO> capMasterList = new ArrayList<CapMasterDAO>();
	private String customerId;

	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 {
			Connection connection = null;
			String db = (String) this.getSessionMap().get("db");
			String shopId = (String) this.getSessionMap().get("shopId");
			String shopCode = (String) this.getSessionMap().get("shopCode");
			try {
				LabelCapSchema.ensure(this.getSessionMap());
				connection = GetDBConnection.getDBConnection(db);
				this.setCustomerList(PackingMasterOptions.customerOptions(connection, shopId, shopCode));
				this.setCapMasterList(PackingMasterOptions.capRows(connection, shopId, shopCode));
				this.setCustomerId("");
				result = SUCCESS;
			} catch (SQLException e) {
				result = ERROR;
				addActionError(e.getErrorCode() + " || " + e.getMessage() + " || Something Went Wrong!");
			} catch (IOException e) {
				result = ERROR;
				addActionError(e.getMessage() + " || " + e.getCause() + " || Something Went Wrong!");
			} finally {
				if (connection != null) {
					try {
						connection.close();
					} catch (SQLException e) {
					}
				}
			}
		}
		return result;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public Map<String, String> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(Map<String, String> customerList) {
		this.customerList = customerList;
	}

	public List<CapMasterDAO> getCapMasterList() {
		return capMasterList;
	}

	public void setCapMasterList(List<CapMasterDAO> capMasterList) {
		this.capMasterList = capMasterList;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	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;
	}
}
