package com.tubeonfire.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.datastore.Text;
import com.tubeonfire.entity.Tube;
import com.tubeonfire.entity.TubeComment;
import com.tubeonfire.service.TubeService;
import com.tubeonfire.util.JavaCacheHandle;
public class TubeDetailController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "public, max-age=86400");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
response.setHeader("Expires", cal.getTime().toString());
String[] splitted = request.getRequestURI().split("/");
String id = null;
if (splitted.length == 4) {
id = splitted[2];
} else {
System.out.println("Error path.");
response.sendRedirect("/home");
}
try {
System.out.println("Detail id : " + id);
Tube detail = TubeService.searchById(id);
List<TubeComment> comments = new ArrayList<TubeComment>();
if (detail != null) {
if (detail.getComments() != null) {
for (Text cmtText : detail.getComments()) {
TubeComment tubeComment = new TubeComment();
tubeComment.transformText(cmtText.getValue());
comments.add(tubeComment);
}
}
request.setAttribute("detail", detail);
request.setAttribute("url", request.getRequestURL());
request.setAttribute("listCategory",
JavaCacheHandle.getCategory());
request.setAttribute("listChannel",
JavaCacheHandle.getChannels());
request.setAttribute("comments", comments);
request.getRequestDispatcher("/tube_detail.jsp").forward(request,
response);
} else {
System.out.println("Video is not exits.");
request.getRequestDispatcher("/error/404.html").forward(
request, response);
}
} catch (Exception e) {
System.out.println("Exception.");
e.printStackTrace();
request.getRequestDispatcher("/error/404.html").forward(request,
response);
}
}
}