Package com.tubeonfire.controller

Source Code of com.tubeonfire.controller.KeywordController

package com.tubeonfire.controller;

import java.io.IOException;
import java.net.URLDecoder;
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.tubeonfire.entity.Tube;
import com.tubeonfire.entity.Tag;
import com.tubeonfire.model.TagModel;
import com.tubeonfire.model.TubeModel;
import com.tubeonfire.util.JavaCacheHandle;

public class KeywordController extends HttpServlet {
  private static final long serialVersionUID = 1L;
  private static int itemPerPage = 12;

  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());
    // search by input key.
    try {
      String path = ((HttpServletRequest) request).getRequestURI();
      String[] splittedURI = path.split("/");
      int page = 1;
      String keyword = "";
      if (splittedURI.length == 3) {
        keyword = splittedURI[splittedURI.length - 1];
      } else if (splittedURI.length == 4) {
        keyword = splittedURI[splittedURI.length - 2];
        try {
          page = Integer
              .parseInt(splittedURI[splittedURI.length - 1]);
        } catch (Exception e) {
          System.out.println("Error when parse page string to int.");
          page = 1;
        }
      } else {
        System.out.println("Error path.");
        response.sendRedirect("/home");
      }

      keyword = URLDecoder.decode(keyword, "UTF8");
      System.out.println("Keyword to search : " + keyword);
      TagModel tagModel = new TagModel();
      Tag tag = tagModel.getByAlias(keyword);
      if (tag != null) {       
        List<Tube> result = new ArrayList<Tube>();
        int startPoint = (page - 1) * itemPerPage;
        if (tag.getListTubeId().size() > startPoint) {
          for (int i = startPoint; i < tag.getListTubeId().size(); i++) {
            if (i == (itemPerPage + startPoint)) {
              break;
            }
            String id = tag.getListTubeId().get(i).getValue();
            System.out.println(id);
            Tube tub = TubeModel.getByTubeId(id);
            if (tub != null) {
              result.add(tub);
            }
          }
        } else {
          page = 1;
          for (int i = 0; i < tag.getListTubeId().size(); i++) {
            if (i == itemPerPage) {
              break;
            }
            String id = tag.getListTubeId().get(i).getValue();
            Tube tub = TubeModel.getByTubeId(id);
            if (tub != null) {
              result.add(tub);
            }
          }
        }
        request.setAttribute("url", request.getRequestURL());
        request.setAttribute("listCategory",
            JavaCacheHandle.getCategory());
        request.setAttribute("listChannel",
            JavaCacheHandle.getChannels());
        request.setAttribute("result", result);
        request.setAttribute("currentPage", page);
        request.setAttribute("keyword", keyword.replaceAll("\\+", " "));
        request.setAttribute("totalResult", tag.getListTubeId().size());
        request.getRequestDispatcher("/keyword_show.jsp").forward(request,
            response);
      } else {
        System.out.println("Tag is not exits.");
        response.sendRedirect("/home");
      }
    } catch (Exception e) {
      System.out.println("Exception.");
      e.printStackTrace();
      response.sendRedirect("/home");
    }
  }

}
TOP

Related Classes of com.tubeonfire.controller.KeywordController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.