Package com.multysite.controller

Source Code of com.multysite.controller.CategoryServlet

package com.multysite.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
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.Category;
import com.multysite.entity.News;
import com.multysite.entity.admin.ApplicationConfig;
import com.multysite.entity.admin.ApplicationTemplate;
import com.multysite.model.CategoryModel;
import com.multysite.model.NewsModel;
import com.multysite.model.TagModel;
import com.multysite.quercus.CompileScriptEngine;
import com.multysite.util.RecentViewHelper;
import com.multysite.util.Setting;

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

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

  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {
    try {
      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) {
          boolean check = false;
          String categoryAlias = splitted[2];
          categoryAlias = URLDecoder.decode(categoryAlias, "UTF-8");
          Category obj = CategoryModel.getById(categoryAlias);
          if (obj != null || check == false) {

            CompiledScript compiledscript = CompileScriptEngine
                .getCompileScript(config.getApplicationId(),
                    "category", template.getCategory());
            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());

            int page = 1;
            try {
              page = Integer.parseInt((String) req
                  .getParameter("page"));
            } catch (Exception e) {
              page = 1;
            }
            List<News> listNews = new ArrayList<News>();
            NewsModel model = new NewsModel();
            model.setPage(page);
            model.prepareListByCategory(categoryAlias);
            listNews = model.getListResult();
            CategoryModel cateModel = new CategoryModel();
            cateModel.prepareAll();
            TagModel tagModel = new TagModel();
            tagModel.prepareList();
            bind.put("recent_view",
                RecentViewHelper.getRecentView());
            bind.put("list_tag", tagModel.getListResult());
            bind.put("list_category", cateModel.getListResult());

            bind.put("category", obj);
            bind.put("current_page", model.getPage());
            bind.put("total_page", model.getTotalPage());
            bind.put("articles", listNews);
            try {
              resp.getWriter().print(
                  (String) compiledscript.eval());
            } catch (Exception e) {
              e.printStackTrace();
              log.warning(e.toString());
            }
          } else {
            resp.sendRedirect("/");
          }
        } 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.CategoryServlet

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.