/*
 * 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.KnihaJizd;

/**
 * @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 ObnovitJizduAction extends Action {

	public ObnovitJizduAction() {
	}

	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();
		
		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;
				}
			}

			Calendar cal = Calendar.getInstance(new Locale("cs", "CZ"));
			Timestamp casZmeny = new Timestamp(cal.getTimeInMillis());
			String user = request.getRemoteUser();
			if (user == null) {
				user = "";
			}

			try {
				if (index > 0) {
					jizda.setId(index);
					jizda.setZmenil(user);
					jizda.setCasZmeny(casZmeny);
					jizda.setPodpis("obnoveno");
					jizda.obnovit(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()));
			}
			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("smazaneJizdy");
	}

}
