Package com.multysite.controller

Source Code of com.multysite.controller.SearchServlet

package com.multysite.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.logging.Logger;

import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.NamespaceManager;
import com.multysite.entity.admin.ApplicationConfig;
import com.multysite.entity.admin.ApplicationTemplate;
import com.multysite.model.CategoryModel;
import com.multysite.model.TagModel;
import com.multysite.quercus.CompileScriptEngine;
import com.multysite.search.NewsSearchModel;
import com.multysite.util.RecentViewHelper;
import com.multysite.util.Setting;

@SuppressWarnings("serial")
public class SearchServlet extends HttpServlet {

  private static final Logger log = Logger.getLogger(SearchServlet.class
      .getName());

  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {
    try {
      System.out.println("Current NS : " + NamespaceManager.get());
      req.setCharacterEncoding("utf-8");
      ApplicationTemplate template = (ApplicationTemplate) req
          .getAttribute("template");
      ApplicationConfig config = (ApplicationConfig) req
          .getAttribute("config");
      if (template != null && config != null) {
        /*
         * get detail content
         */
        String[] splitted = req.getRequestURI().split("/");
        if (splitted.length == 3) {
          String keyword = splitted[2];
          keyword = URLDecoder.decode(keyword, "UTF-8");
          int page = 1;
          try {
            page = Integer.parseInt((String) req
                .getParameter("page"));
          } catch (Exception e) {
            page = 1;
          }
          log.warning("----------Key-------" + keyword);
          NewsSearchModel model = new NewsSearchModel();
          model.setPage(page);
          model.search(keyword);

          CompiledScript compiledscript = CompileScriptEngine
              .getCompileScript(config.getApplicationId(),
                  "search", template.getSearch());

          Bindings bind = compiledscript.getEngine().getBindings(
              ScriptContext.ENGINE_SCOPE);
          bind.put("title_for_layout", config.getTitle());
          bind.put("description_for_layout", config.getDescription());
          bind.put("keyword_for_layout", config.getKeyword());
          bind.put("css_for_layout", template.getCss());
          bind.put("js_for_layout", template.getJs());

          CategoryModel cateModel = new CategoryModel();
          cateModel.prepareAll();
          TagModel tagModel = new TagModel();
          tagModel.prepareList();

          bind.put("keyword", keyword);
          bind.put("recent_view", RecentViewHelper.getRecentView());
          bind.put("list_tag", tagModel.getListResult());
          bind.put("list_category", cateModel.getListResult());

          bind.put("current_page", model.getPage());
          bind.put("total_page", model.getTotalPage());
          bind.put("articles", model.getListResult());
          try {
            resp.getWriter().print((String) compiledscript.eval());
          } catch (Exception e) {
            e.printStackTrace();
            log.warning(e.toString());
          }
        } else {
          resp.sendRedirect("/");
        }
      } else {
        NamespaceManager.set(Setting.getGeneralNamespace());
        resp.sendRedirect("http://" + Setting.getDomain());
      }
    } catch (Exception e) {
      e.printStackTrace();
      log.warning(e.toString());
      resp.sendRedirect("/");
    }
  }
}
TOP

Related Classes of com.multysite.controller.SearchServlet

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.