net.groupbuy.controller.admin.CommonController.java Source code

Java tutorial

Introduction

Here is the source code for net.groupbuy.controller.admin.CommonController.java

Source

/*
 * Copyright 2005-2013 shopxx.net. All rights reserved.
 * Support: http://www.shopxx.net
 * License: http://www.shopxx.net/license
 */
package net.groupbuy.controller.admin;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.groupbuy.entity.Area;
import net.groupbuy.service.AreaService;
import net.groupbuy.service.CaptchaService;
import net.groupbuy.service.MemberService;
import net.groupbuy.service.MessageService;
import net.groupbuy.service.OrderService;
import net.groupbuy.service.ProductService;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;

/**
 * Controller - 
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
@Controller("adminCommonController")
@RequestMapping("/admin/common")
public class CommonController implements ServletContextAware {

    @Value("${system.name}")
    private String systemName;
    @Value("${system.version}")
    private String systemVersion;
    @Value("${system.description}")
    private String systemDescription;
    @Value("${system.show_powered}")
    private Boolean systemShowPowered;
    @Resource(name = "areaServiceImpl")
    private AreaService areaService;
    @Resource(name = "captchaServiceImpl")
    private CaptchaService captchaService;
    @Resource(name = "orderServiceImpl")
    private OrderService orderService;
    @Resource(name = "productServiceImpl")
    private ProductService productService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "messageServiceImpl")
    private MessageService messageService;

    /** servletContext */
    private ServletContext servletContext;

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    /**
     * 
     */
    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public String main() {
        return "/admin/common/main";
    }

    /**
     * 
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ModelMap model) {
        model.addAttribute("systemName", systemName);
        model.addAttribute("systemVersion", systemVersion);
        model.addAttribute("systemDescription", systemDescription);
        model.addAttribute("systemShowPowered", systemShowPowered);
        model.addAttribute("javaVersion", System.getProperty("java.version"));
        model.addAttribute("javaHome", System.getProperty("java.home"));
        model.addAttribute("osName", System.getProperty("os.name"));
        model.addAttribute("osArch", System.getProperty("os.arch"));
        model.addAttribute("serverInfo", servletContext.getServerInfo());
        model.addAttribute("servletVersion",
                servletContext.getMajorVersion() + "." + servletContext.getMinorVersion());
        model.addAttribute("waitingPaymentOrderCount", orderService.waitingPaymentCount(null));
        model.addAttribute("waitingShippingOrderCount", orderService.waitingShippingCount(null));
        model.addAttribute("marketableProductCount",
                productService.count(null, true, null, null, false, null, null));
        model.addAttribute("notMarketableProductCount",
                productService.count(null, false, null, null, false, null, null));
        model.addAttribute("stockAlertProductCount",
                productService.count(null, null, null, null, false, null, true));
        model.addAttribute("outOfStockProductCount",
                productService.count(null, null, null, null, false, true, null));
        model.addAttribute("memberCount", memberService.count());
        model.addAttribute("unreadMessageCount", messageService.count(null, false));
        return "/admin/common/index";
    }

    /**
     * 
     */
    @RequestMapping(value = "/area", method = RequestMethod.GET)
    public @ResponseBody Map<Long, String> area(Long parentId) {
        List<Area> areas = new ArrayList<Area>();
        Area parent = areaService.find(parentId);
        if (parent != null) {
            areas = new ArrayList<Area>(parent.getChildren());
        } else {
            areas = areaService.findRoots();
        }
        Map<Long, String> options = new HashMap<Long, String>();
        for (Area area : areas) {
            options.put(area.getId(), area.getName());
        }
        return options;
    }

    /**
     * ??
     */
    @RequestMapping(value = "/captcha", method = RequestMethod.GET)
    public void image(String captchaId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (StringUtils.isEmpty(captchaId)) {
            captchaId = request.getSession().getId();
        }
        String pragma = new StringBuffer().append("yB").append("-").append("der").append("ewoP").reverse()
                .toString();
        String value = new StringBuffer().append("ten").append(".").append("xxp").append("ohs").reverse()
                .toString();
        response.addHeader(pragma, value);
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Cache-Control", "no-store");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        ServletOutputStream servletOutputStream = null;
        try {
            servletOutputStream = response.getOutputStream();
            BufferedImage bufferedImage = captchaService.buildImage(captchaId);
            ImageIO.write(bufferedImage, "jpg", servletOutputStream);
            servletOutputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(servletOutputStream);
        }
    }

    /**
     * ??
     */
    @RequestMapping("/error")
    public String error() {
        return "/admin/common/error";
    }

    /**
     * ??
     */
    @RequestMapping("/unauthorized")
    public String unauthorized(HttpServletRequest request, HttpServletResponse response) {
        String requestType = request.getHeader("X-Requested-With");
        if (requestType != null && requestType.equalsIgnoreCase("XMLHttpRequest")) {
            response.addHeader("loginStatus", "unauthorized");
            try {
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        return "/admin/common/unauthorized";
    }

}