/*
 * Created on 22.12.2003
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package cz.triangle.dostal.knihajizd.action;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Locale;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cz.triangle.dostal.knihajizd.KnihaJizd;
import cz.triangle.dostal.knihajizd.form.KnihaJizdForm;

/**
 * @author xrusr01
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class ZobrazJizduAction extends Action {

	public ZobrazJizduAction() {
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Connection con = null;
		HttpSession session = request.getSession();
		
		ActionErrors errors = new ActionErrors();
		KnihaJizd jizda = new KnihaJizd();
		KnihaJizdForm kjf = new KnihaJizdForm();
		String forward = "zmenaJizdy";

		int index = 0;
		try {
			Context initContext = new InitialContext();
			Context envContext  = (Context)initContext.lookup("java:/comp/env");
			DataSource ds = (DataSource)envContext.lookup("jdbc/dostal");
			con = ds.getConnection();
			
			String s_id = request.getParameter("id");
			if ((s_id == null) || (s_id.equalsIgnoreCase(""))) {
				s_id = (String) request.getAttribute("id");
			}
			if ((s_id != null) || (!s_id.equalsIgnoreCase(""))) {
				try {
					index = Integer.parseInt(s_id);
				} catch (NumberFormatException e) {
					index = 0;
				}
			}
			
			String pozice_string = request.getParameter("pozice");
			int pozice = 0;
			if (pozice_string != null) {
				try {
					pozice = Integer.parseInt(pozice_string);
					if (pozice < 0) {
						pozice = 0;
					}
				} catch (NumberFormatException e) {
					pozice = 0;
				}
				request.setAttribute("pozice", String.valueOf(pozice));
			}
			
			try {
				if (index > 0) {
					jizda.setId(index);
					jizda.nacti(con);
					
					Calendar calOd = Calendar.getInstance(new Locale("cs", "CZ"));
					if (jizda.getCasOd() != null) {
						calOd.setTime(jizda.getCasOd());
					}
					Calendar calDo = Calendar.getInstance(new Locale("cs", "CZ"));
					if (jizda.getCasDo() != null) {
						calDo.setTime(jizda.getCasDo());
					}
					
					kjf.setId(jizda.getId());
					if (jizda.getCasOd() != null) {
						kjf.setCasOd(jizda.getCasOdAsString());
						kjf.setDatumOd((calOd.get(Calendar.DAY_OF_MONTH))+"."+(calOd.get(Calendar.MONTH)+1)+"."+(calOd.get(Calendar.YEAR)));
						kjf.setHodOd(Integer.toString(calOd.get(Calendar.HOUR_OF_DAY)));
						kjf.setMinOd(Integer.toString(calOd.get(Calendar.MINUTE)));
					} else {
						kjf.setCasOd("");
						kjf.setDatumOd("");
						kjf.setHodOd("");
						kjf.setMinOd("");
					}
					if (jizda.getCasDo() != null) {
						kjf.setCasDo(jizda.getCasDoAsString());
						kjf.setDatumDo((calDo.get(Calendar.DAY_OF_MONTH))+"."+(calDo.get(Calendar.MONTH)+1)+"."+(calDo.get(Calendar.YEAR)));
						kjf.setHodDo(Integer.toString(calDo.get(Calendar.HOUR_OF_DAY)));
						kjf.setMinDo(Integer.toString(calDo.get(Calendar.MINUTE)));
					} else {
						kjf.setCasDo("");
						kjf.setDatumDo("");
						kjf.setHodDo("");
						kjf.setMinDo("");
					}
					kjf.setZbozi(jizda.getZbozi());
					kjf.setZakaznik(jizda.getZakaznik());
					kjf.setTelZakaznik(jizda.getTelZakaznik());
					kjf.setPoznamka(jizda.getPoznamka());
					kjf.setPodpis("");
					kjf.setMisto_odvozu(String.valueOf(jizda.getMisto_odvozu()));
					
					request.setAttribute("knihaJizdForm", kjf);
					request.setAttribute("akce", "update");
				} else {
					errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.smazatJizdu.index"));
				}
			} catch (SQLException e) {
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.zobrazJizdu.cteni", e.getMessage()));
			}
			
			con.close();
			con = null;
		} finally {
			if (!errors.isEmpty()) {
				saveErrors(request, errors);
				forward = "jizdy";
			}
			if (con != null) {
				try {
					con.close();
					con = null;
				} catch (SQLException e) {
				}
			}
		}

		return mapping.findForward(forward);
	}

}
