/*
 * UniTime 3.0 (University Course Timetabling & Student Sectioning Application)
 * Copyright (C) 2007, UniTime.org, and individual contributors
 * as indicated by the @authors tag.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.unitime.timetable.form;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.unitime.timetable.model.ExactTimeMins;
import org.unitime.timetable.model.dao.ExactTimeMinsDAO;


/** 
 * @author Tomas Muller
 */
public class ExactTimeEditForm extends ActionForm {
    private String iOp;
    private Vector iExactTimeMins = new Vector();

	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
        
		return errors;
	}

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		iOp = null; 
		iExactTimeMins.clear();
		iExactTimeMins.addAll((new ExactTimeMinsDAO()).findAll());
		Collections.sort(iExactTimeMins);
	}
	
	public void save() {
		org.hibernate.Session hibSession = (new ExactTimeMinsDAO()).getSession(); 
		for (Enumeration e=iExactTimeMins.elements();e.hasMoreElements();) {
			ExactTimeMins ex = (ExactTimeMins)e.nextElement();
			hibSession.saveOrUpdate(ex);
		}
		hibSession.flush();
	}

	public String getOp() { return iOp; }
	public void setOp(String op) { iOp = op; }
	public int getSize() { return iExactTimeMins.size(); }
	public Vector getExactTimeMins() {
		return iExactTimeMins;
	}
	public int getMinsPerMtgMin(int idx) {
		return ((ExactTimeMins)iExactTimeMins.elementAt(idx)).getMinsPerMtgMin().intValue();
	}
	public int getMinsPerMtgMax(int idx) {
		return ((ExactTimeMins)iExactTimeMins.elementAt(idx)).getMinsPerMtgMax().intValue();
	}
	public int getNrTimeSlots(int idx) {
		return ((ExactTimeMins)iExactTimeMins.elementAt(idx)).getNrSlots().intValue();
	}
	public int getBreakTime(int idx) {
		return ((ExactTimeMins)iExactTimeMins.elementAt(idx)).getBreakTime().intValue();
	}
	public void setMinsPerMtgMin(int idx, int minsPerMtgMin) {
		((ExactTimeMins)iExactTimeMins.elementAt(idx)).setMinsPerMtgMin(new Integer(minsPerMtgMin));
	}
	public void setMinsPerMtgMax(int idx, int minsPerMtgMax) {
		((ExactTimeMins)iExactTimeMins.elementAt(idx)).setMinsPerMtgMax(new Integer(minsPerMtgMax));
	}
	public void setNrTimeSlots(int idx, int nrTimeSlots) {
		((ExactTimeMins)iExactTimeMins.elementAt(idx)).setNrSlots(new Integer(nrTimeSlots));
	}
	public void setBreakTime(int idx, int breakTime) {
		((ExactTimeMins)iExactTimeMins.elementAt(idx)).setBreakTime(new Integer(breakTime));
	}
}


@@ -1210,16 +1210,50 @@
 apping;%0A
+import org.hibernate.Transaction;%0A
 import o
@@ -1290,24 +1290,24 @@
 ctTimeMins;%0A
-
 import org.u
@@ -1905,20 +1905,86 @@
  save() 
-%7B%0A%09%09
+throws Exception %7B%0A%09%09Transaction tx = null;%0A        try %7B%0A            
 org.hibe
@@ -2048,20 +2048,175 @@
 ssion();
- %0A%09%09
+%0A            if (hibSession.getTransaction()==null %7C%7C !hibSession.getTransaction().isActive())%0A                tx = hibSession.beginTransaction();%0A            
 for (Enu
@@ -2276,19 +2276,32 @@
 s();) %7B%0A
-%09%09%09
+                
 ExactTim
@@ -2347,11 +2347,24 @@
 ();%0A
-%09%09%09
+                
 hibS
@@ -2392,33 +2392,164 @@
 x);%0A
-%09%09%7D%0A%09%09hibSession.flush();
+            %7D%0A            if (tx!=null) tx.commit();%0A        %7D catch (Exception e) %7B%0A            if (tx!=null) tx.rollback();%0A            throw e;%0A        %7D
 %0A%09%7D%0A

Edit Exact Time Pattern - fixed problem with the changes not being saved when MySQL is used