/*
 * 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.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.SeznamJizd;
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 SmazaneJizdyAction extends Action {

	public SmazaneJizdyAction() {
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Connection con = null;
		HttpSession session = request.getSession();
		
		ActionErrors errors = new ActionErrors();
		SeznamJizd jizdy = new SeznamJizd();
		KnihaJizdForm kjf = (KnihaJizdForm) session.getAttribute("knihaJizdForm");
		
		try {
			Context initContext = new InitialContext();
			Context envContext  = (Context)initContext.lookup("java:/comp/env");
			DataSource ds = (DataSource)envContext.lookup("jdbc/dostal");
			con = ds.getConnection();

			Calendar cal = Calendar.getInstance(new Locale("cz, CS"));
			Timestamp nyni = new Timestamp(cal.getTimeInMillis());
			Integer misto_odvozu = null;
			
			if (kjf != null) {
				try {
					misto_odvozu = new Integer(kjf.getMisto_odvozu());
				} catch (Exception e) {
					misto_odvozu = null;
				}
				/*
				if (kjf.getId() > 0) {
					kjf = null;
					session.setAttribute("knihaJizdForm", kjf);
				}
				*/
			} else {
				kjf = new KnihaJizdForm();
			}
			kjf.setMisto_odvozu(String.valueOf(misto_odvozu));
			if (misto_odvozu.intValue() < 0) {
				misto_odvozu = null;
			}

			try {
				jizdy.nactiSmazane(con, nyni, misto_odvozu);
			} catch (SQLException e) {
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.seznamJizd.cteni", e.getMessage()));
			}
			request.setAttribute("jizdy", jizdy);
			session.setAttribute("akceJizdy", "smazane");
			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("seznamJizd");
	}

}
