/*
 * Created on 21.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.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
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.actions.DispatchAction;
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 UlozitJizduAction extends DispatchAction {

	public UlozitJizduAction() {
	}

	public ActionForward unspecified(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 = (KnihaJizdForm) form;

		try {
			Context initContext = new InitialContext();
			Context envContext  = (Context)initContext.lookup("java:/comp/env");
			DataSource ds = (DataSource)envContext.lookup("jdbc/dostal");
			con = ds.getConnection();

//			String casOd_s = kjf.getCasOd();
			String casOd_s = kjf.getDatumOd()+" "+kjf.getHodOd()+":"+kjf.getMinOd();
			String casDo_s = kjf.getDatumDo()+" "+kjf.getHodDo()+":"+kjf.getMinDo();
			Timestamp casOd = null;
			Timestamp casDo = null;
			Timestamp casZmeny = null;

			DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, new Locale("cs", "CZ"));
			if ((casOd_s != null) && (!casOd_s.equalsIgnoreCase(""))) {
				try {
					casOd = new Timestamp(df.parse(casOd_s).getTime());
				} catch (ParseException e) {
					errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.ulozitJizdu.datum"));
				}
			}
			if ((casDo_s != null) && (!casDo_s.equalsIgnoreCase(""))) {
				try {
					casDo = new Timestamp(df.parse(casDo_s).getTime());
				} catch (ParseException e) {
					errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.ulozitJizdu.datum"));
				}
			}
			
			String podpis = kjf.getPodpis();
			if ((podpis == null) || (podpis.trim().equalsIgnoreCase(""))) {
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.ulozitJizdu.podpis"));
			}
			
			String forward = "jizdy";
			if (errors.isEmpty()) {
				Calendar cal = Calendar.getInstance(new Locale("cs", "CZ"));
				casZmeny = new Timestamp(cal.getTimeInMillis());
				String user = request.getRemoteUser();
				if (user == null) {
					user = "";
				}

				try {
					jizda.setCasOd(casOd);
					jizda.setCasDo(casDo);
					jizda.setZbozi(kjf.getZbozi());
					jizda.setZakaznik(kjf.getZakaznik());
					jizda.setTelZakaznik(kjf.getTelZakaznik());
					jizda.setPoznamka(kjf.getPoznamka());
					jizda.setZmenil(user);
					jizda.setCasZmeny(casZmeny);
					jizda.setSmazano(false);
					jizda.setPodpis(podpis);
					jizda.setMisto_odvozu(Integer.valueOf(kjf.getMisto_odvozu()).intValue());

					jizda.vlozit(con);
					session.setAttribute("knihaJizdForm", kjf);
					//kjf = null;
					//session.setAttribute("knihaJizdForm", null);

				} catch (SQLException e) {
					errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.ulozitJizdu.vlozeni", e.getMessage()));
				}
			} else {
				forward = "vlozitNovou";
				saveErrors(request, errors);
			}

			con.close();
			con = null;

			//return mapping.findForward("jizdy");
			return mapping.findForward(forward);

		} finally {
			if (con != null) {
				try {
					con.close();
					con = null;
				} catch (SQLException e) {
				}
			}
		}

	}
	
	
	public ActionForward update(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 = (KnihaJizdForm) form;
		String forward = "seznamBudouci";

		try {
			Context initContext = new InitialContext();
			Context envContext  = (Context)initContext.lookup("java:/comp/env");
			DataSource ds = (DataSource)envContext.lookup("jdbc/dostal");
			con = ds.getConnection();

//			String casOd_s = kjf.getCasOd();
			String casOd_s = kjf.getDatumOd()+" "+kjf.getHodOd()+":"+kjf.getMinOd();
			String casDo_s = kjf.getDatumDo()+" "+kjf.getHodDo()+":"+kjf.getMinDo();
			Timestamp casOd = null;
			Timestamp casDo = null;
			Timestamp casZmeny = null;
			
			String aktual = (String) session.getAttribute("akceJizdy");
			if (aktual.equalsIgnoreCase("minule")) {
				forward = "seznamMinule";
			}
			if (aktual.equalsIgnoreCase("smazane")) {
				forward = "seznamSmazane";
			}

			DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, new Locale("cs", "CZ"));
			if ((casOd_s != null) && (!casOd_s.equalsIgnoreCase(""))) {
				try {
					casOd = new Timestamp(df.parse(casOd_s).getTime());
				} catch (ParseException e) {
					errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.ulozitJizdu.datum"));
				}
			}
			if ((casDo_s != null) && (!casDo_s.equalsIgnoreCase(""))) {
				try {
					casDo = new Timestamp(df.parse(casDo_s).getTime());
				} catch (ParseException e) {
					errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.ulozitJizdu.datum"));
				}
			}
			String podpis = kjf.getPodpis();
			if ((podpis == null) || (podpis.trim().equalsIgnoreCase(""))) {
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.ulozitJizdu.podpis"));
			}
			
			if (errors.isEmpty()) {
				Calendar cal = Calendar.getInstance(new Locale("cs", "CZ"));
				casZmeny = new Timestamp(cal.getTimeInMillis());
				String user = request.getRemoteUser();
				if (user == null) {
					user = "";
				}

				try {
					jizda.setId(kjf.getId());
					jizda.setCasOd(casOd);
					jizda.setCasDo(casDo);
					jizda.setZbozi(kjf.getZbozi());
					jizda.setZakaznik(kjf.getZakaznik());
					jizda.setTelZakaznik(kjf.getTelZakaznik());
					jizda.setPoznamka(kjf.getPoznamka());
					jizda.setZmenil(user);
					jizda.setCasZmeny(casZmeny);
					jizda.setPodpis(podpis);
					jizda.setMisto_odvozu(Integer.valueOf(kjf.getMisto_odvozu()).intValue());

					jizda.zmenit(con);
					session.setAttribute("knihaJizdForm", kjf);
					//kjf = null;
					//session.setAttribute("knihaJizdForm", null);

				} catch (SQLException e) {
					errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.ulozitJizdu.zmena", e.getMessage()));
				}
			} else {
				forward = "updateJizdu";
				request.setAttribute("akce", "update");
				saveErrors(request, errors);
			}

			con.close();
			con = null;

			return mapping.findForward(forward);

		} finally {
			if (con != null) {
				try {
					con.close();
					con = null;
				} catch (SQLException e) {
				}
			}
		}

	}

}
