// A. Toy, A10 for CISC474, Spring 2006
// CalculateFreeTime class
// Modified: 5/15/06

package com.gmscheduler.web;

import com.gmscheduler.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class CalculateFreeTime extends HttpServlet {

  public void doPost(HttpServletRequest request,
			HttpServletResponse response)
		throws IOException, ServletException {

    String user = request.getParameter("user");
    String password = request.getParameter("password");
    String sched = request.getParameter("one");
    
    if (sched != null) {
    DatabaseManager dm = new DatabaseManager();
    ScheduleUtil su = new ScheduleUtil();
    List othersched = new LinkedList<Schedule>();
    
    Schedule constraint = new Schedule(su.parseSchedule(sched));
    List schedres = dm.loadAllSchedules();
    ListIterator lr = schedres.listIterator();
    
    while (lr.hasNext()) { othersched.add((Schedule)lr.next());}
    
    Schedule finals = new Schedule(su.findTime(constraint,(LinkedList<Schedule>)othersched));

    request.setAttribute("result", finals.toString());
    }
    request.setAttribute("user", user);
    request.setAttribute("password", password);

    RequestDispatcher view = request.getRequestDispatcher("calculatefreetime.jsp");

    view.forward(request, response);
  }
}

