package com.reallinkbilling.view;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.db.GetDBConnection;

public class ViewGodownList extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 1L;

	private SessionMap<String, Object> sessionMap;

	private ArrayList<HashMap<String, String>> godownList =
			new ArrayList<HashMap<String, String>>();

	public String execute() {

		String result = "login";

		if (this.getSessionMap() == null
				|| this.getSessionMap().get("login") == null) {

			addActionMessage("Please Logout and Login again!");
			result = "login";

		} else {

			String shopId = (String) this.getSessionMap().get("shopId");
			String db = (String) this.getSessionMap().get("db");

			Connection connection = null;
			PreparedStatement preparedStatement = null;
			ResultSet resultSet = null;

			try {

				connection = GetDBConnection.getDBConnection(db);

				String sql = "SELECT godown_id, godown_code, godown_name, godown_address "
						+ "FROM shop_godown_master "
						+ "WHERE shop_id = ? "
						+ "ORDER BY godown_code";

				preparedStatement = connection.prepareStatement(sql);

				preparedStatement.setString(1, shopId);

				resultSet = preparedStatement.executeQuery();

				while (resultSet.next()) {

					HashMap<String, String> godown =
							new HashMap<String, String>();

					godown.put(
							"GODOWN_ID",
							resultSet.getString("godown_id"));

					godown.put(
							"GODOWN_CODE",
							resultSet.getString("godown_code"));

					godown.put(
							"GODOWN_NAME",
							resultSet.getString("godown_name"));

					godown.put(
							"GODOWN_ADDRESS",
							resultSet.getString("godown_address"));

					this.godownList.add(godown);
				}

				result = SUCCESS;

			} catch (SQLException e) {

				result = "error";

				addActionError(
						e.getErrorCode()
								+ " || "
								+ e.getMessage()
								+ " || Something Went Wrong!");

			} catch (Exception e) {

				result = "error";

				addActionError(
						e.getMessage()
								+ " || Something Went Wrong!");

			} finally {

				if (resultSet != null) {

					try {
						resultSet.close();
					} catch (SQLException e) {
					}
				}

				if (preparedStatement != null) {

					try {
						preparedStatement.close();
					} catch (SQLException e) {
					}
				}

				if (connection != null) {

					try {
						connection.close();
					} catch (SQLException e) {
					}
				}
			}
		}

		return result;
	}

	public ArrayList<HashMap<String, String>> getGodownList() {
		return godownList;
	}

	public void setGodownList(
			ArrayList<HashMap<String, String>> godownList) {

		this.godownList = godownList;
	}

	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(SessionMap<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

	@Override
	public void setSession(Map<String, Object> map) {
		sessionMap = (SessionMap<String, Object>) map;
	}
}
