/*
* AUTHOR: Kevin Lam
*/
package com.apps.ubc.cc;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.apps.datastore.NotifierDatastore;
import com.apps.datastore.UBCCourseSpiderDatastore;
import com.apps.datastore.dao.BookInformationObject;
import com.apps.datastore.dao.CourseInformationObject;
import com.apps.datastore.dao.DepartmentInformationObject;
import com.apps.datastore.dao.SectionInformationObject;
import com.apps.services.UBCCourseSpiderService;
import com.apps.services.factory.UBCCourseSpiderServiceFactory;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskHandle;
import com.google.appengine.api.taskqueue.TaskOptions;
public class WebSpiderServlet extends HttpServlet {
private static UBCCourseSpiderDatastore d = new UBCCourseSpiderDatastore();
private UBCCourseSpiderService spider = UBCCourseSpiderServiceFactory.getUBCCourseSpiderService();
private Queue q = QueueFactory.getQueue("spider");
public static UBCCourseSpiderDatastore getDatastore(){
return d;
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
if (req.getParameter("type").equals("init")) {
d.dropSchema();
d.initSchema();
}
else if (req.getParameter("type").equals("department")) {
List<DepartmentInformationObject> diol = spider.getDepartments();
for (Iterator i = diol.iterator(); i.hasNext();) {
DepartmentInformationObject dio = (DepartmentInformationObject) i.next();
d.addDepartment(dio);
}
}
else if (req.getParameter("type").equals("course")) {
List<DepartmentInformationObject> diol = d.getDepartmentList();
for (Iterator i = diol.iterator(); i.hasNext();) {
DepartmentInformationObject dio = (DepartmentInformationObject) i.next();
q.add(TaskOptions.Builder.withUrl("/spider").param("type","coursebydept").param("departmentId", dio.getDepartmentId()));
}
}
else if (req.getParameter("type").equals("section")) {
List<DepartmentInformationObject> diol = d.getDepartmentList();
for (Iterator i = diol.iterator(); i.hasNext();) {
DepartmentInformationObject dio = (DepartmentInformationObject) i.next();
q.add(TaskOptions.Builder.withUrl("/spider").param("type","sectionbydept").param("departmentId", dio.getDepartmentId()));
}
}
else if (req.getParameter("type").equals("coursebydept")){
String departmentId = req.getParameter("departmentId");
DepartmentInformationObject dio = d.queryDepartmentFromId(departmentId);
List<CourseInformationObject> ciol = spider.getCourses(dio);
for(Iterator j = ciol.iterator(); j.hasNext();){
CourseInformationObject cio = (CourseInformationObject) j.next();
d.addCourse(cio);
}
}
else if (req.getParameter("type").equals("sectionbydept")){
String departmentId = req.getParameter("departmentId");
DepartmentInformationObject dio = d.queryDepartmentFromId(departmentId);
List<CourseInformationObject> ciol = spider.getCourses(dio);
for(Iterator j = ciol.iterator(); j.hasNext();){
CourseInformationObject cio = (CourseInformationObject) j.next();
q.add(TaskOptions.Builder.withUrl("/spider").param("type","sectionbycourse").param("departmentId", cio.getDepartmentId()).param("courseId", cio.getCourseId()));
}
}
else if (req.getParameter("type").equals("sectionbycourse")){
String departmentId = req.getParameter("departmentId");
String courseId = req.getParameter("courseId");
CourseInformationObject cio = d.queryCourseFromId(departmentId, courseId);
List<SectionInformationObject> siol = spider.getSections(cio);
for(Iterator k = siol.iterator(); k.hasNext();){
SectionInformationObject sio = (SectionInformationObject) k.next();
d.addSection(sio);
}
}
}
}