/*
 * Created on 2003/10/09
 *
 * To change this generated comment go to 
 * Window>Preferences>Java>Code Generation>Code Template
 */
package org.F11.scada.tool.alist.history;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.F11.scada.tool.ToolUtility;
import org.F11.scada.tool.alist.RefConditionsForm;
import org.F11.scada.tool.io.AlarmListStore;
import org.F11.scada.tool.io.StrategyUtility;
import org.F11.scada.tool.login.PermissionCheck;
import org.F11.scada.util.ConnectionUtil;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * @author hori
 */
public class QueryListAction extends Action {

	/**
	 * 
	 */
	public QueryListAction() {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.apache.struts.action.Action#execute(org.apache.struts.action.
	 * ActionMapping, org.apache.struts.action.ActionForm,
	 * javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!PermissionCheck.check("career", request))
			return (mapping.getInputForward());

		long currNo = 0;
		try {
			String curr = ToolUtility.htmlEscape(request.getParameter("c"));
			currNo = Long.parseLong(curr);
			if (currNo < 0) {
				currNo = 0;
			}
		} catch (Exception e) {
		}

		HttpSession session = request.getSession();
		RefConditionsForm refcond =
			(RefConditionsForm) session.getAttribute("refcond");
		refcond.setCheckedStrings((String[]) session
			.getAttribute("checkedStrings"));

		Connection con = null;
		List historyList = null;
		Long allCount = new Long(0);
		try {
			con = ConnectionUtil.getConnection();
			StrategyUtility util = new StrategyUtility(con);

			AlarmListStore store = new AlarmListStore();
			allCount = new Long(store.getAllHistoryCount(util, refcond));
			historyList = store.getHistoryList(util, refcond, currNo);
			con.close();
			con = null;
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					con = null;
				}
			}
		}

		request.setAttribute("historyList", historyList);
		request.setAttribute("allCount", allCount);
		if (0 < allCount.longValue()) {
			request.setAttribute("startPos", String.valueOf(currNo + 1));
			request.setAttribute("endPos", String.valueOf(currNo
				+ historyList.size()));
		}
		if (0 < currNo)
			request.setAttribute("forward", String.valueOf(currNo
				- refcond.getLimit()));
		if (currNo + historyList.size() < allCount.longValue())
			request.setAttribute("next", String.valueOf(currNo
				+ historyList.size()));

		return (mapping.findForward("continue"));
	}

}
