/*
 * 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.text.DateFormat;
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.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 ZobrazNovouJizduAction extends Action {

	public ZobrazNovouJizduAction() {
	}
	
	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 = (KnihaJizdForm) session.getAttribute("knihaJizdForm");

		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 {
			Context initContext = new InitialContext();
			Context envContext  = (Context)initContext.lookup("java:/comp/env");
			DataSource ds = (DataSource)envContext.lookup("jdbc/dostal");
			con = ds.getConnection();

			if (kjf == null) {
				kjf = new KnihaJizdForm();
				//kjf.setCasOd(dnes());
				kjf.setPodpis("");
				kjf.setMisto_odvozu("1");
			} else {
				String misto = kjf.getMisto_odvozu();
				if (misto!= null) {
					if (Integer.valueOf(misto).intValue() < 0) {
						misto = "1";
					}
				}
				kjf = new KnihaJizdForm();
				//kjf.setCasOd(dnes());
				kjf.setPodpis("");
				kjf.setMisto_odvozu(misto);
			}
			request.setAttribute("knihaJizdForm", kjf);
			
			con.close();
			con = null;
		} finally {
			if (!errors.isEmpty()) {
				saveErrors(request, errors);
			}
			if (con != null) {
				try {
					con.close();
					con = null;
				} catch (SQLException e) {
				}
			}
		}

		return mapping.findForward("novaJizda");
	}
	
	public String dnes() {
		Calendar cal = Calendar.getInstance(new Locale("cs", "CZ"));
		DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, new Locale("cs", "CZ"));
		return df.format(cal.getTime());
	}

}
