/**
 * Yana Novikova
 * Java file for the servlet
 */
package com.gmscheduler.web;

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

/**
 * This class gets the strings from the form, converts them to the Schedule
 * objects and finds the time using ScheduleUtil class  and puts it into result page
 */
public class ScheduleExpert extends HttpServlet {
    
    /**
     * This function perform HTTP post operation
     */
    public void doPost(HttpServletRequest request,
		       HttpServletResponse response)
	throws IOException, ServletException {

	//get strings from the form
	String user = request.getParameter("user");
    String password = request.getParameter("password");
 

	ScheduleUtil su = new ScheduleUtil();

    DatabaseManager dm = new DatabaseManager();

     Person person = dm.loadPersonByName(user);
     String fullname = person.getName();
     String id = person.getId().toString();
     String scheds = request.getParameter("one");

	//parse each of them into the schedule
	Schedule one = new Schedule(su.parseSchedule(scheds));    
	one.setPerson(person);	//set to user name
    String out = new String(one.toString());
	
	dm.storeSchedule(one);

        //save schedule to database
        request.setAttribute("fullname", fullname);
        request.setAttribute("id", id);
        request.setAttribute("scheddata",out);


        request.setAttribute("user", user);
        request.setAttribute("password", password);
        
	RequestDispatcher view = request.getRequestDispatcher("scheduleentry.jsp");
	view.forward(request, response);
    }


}

