/*
 * 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.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.PodpisForm;

/**
 * @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 SmazatJizduAction extends Action {

	public SmazatJizduAction() {
	}

	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();
		String forward = "seznamBudouci";

		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 aktual = (String) session.getAttribute("akceJizdy");
			if (aktual.equalsIgnoreCase("minule")) {
				forward = "seznamMinule";
			}
			if (aktual.equalsIgnoreCase("smazane")) {
				forward = "seznamSmazane";
			}

			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;
				}
			}
			Calendar cal = Calendar.getInstance(new Locale("cs", "CZ"));
			Timestamp casZmeny = new Timestamp(cal.getTimeInMillis());
			String user = request.getRemoteUser();
			if (user == null) {
				user = "";
			}
			PodpisForm pf = (PodpisForm) form;
			String podpis = pf.getPodpis();
			if ((podpis == null) || (podpis.trim().equalsIgnoreCase(""))) {
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.ulozitJizdu.podpis"));
			}

			if (errors.isEmpty()) {
				try {
					if (index > 0) {
						jizda.setId(index);
						jizda.setZmenil(user);
						jizda.setCasZmeny(casZmeny);
						jizda.setPodpis(podpis);
						jizda.smazat(con);
					} else {
						errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.smazatJizdu.index"));
					}
				} catch (SQLException e) {
					errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.smazatJizdu.smazani", e.getMessage()));
				}
			}

			if (!errors.isEmpty()) {
				saveErrors(request, errors);
			}

			con.close();
			con = null;

			return mapping.findForward(forward);

		} finally {
			if (con != null) {
				try {
					con.close();
					con = null;
				} catch (SQLException e) {
				}
			}
		}

	}

}
