com.ace.erp.controller.sys.company.OrganizationController.java Source code

Java tutorial

Introduction

Here is the source code for com.ace.erp.controller.sys.company.OrganizationController.java

Source

/**
 * Copyright (c) 2009-2015 http://demi-panda.com
 *
 * Licensed 
 */
package com.ace.erp.controller.sys.company;

import com.ace.erp.annotation.BaseComponent;
import com.ace.erp.annotation.CurrentUser;
import com.ace.erp.common.inject.support.InjectBaseDependencyHelper;
import com.ace.erp.controller.BaseController;
import com.ace.erp.entity.Response;
import com.ace.erp.entity.ResponseHeader;
import com.ace.erp.entity.sys.Organization;
import com.ace.erp.entity.sys.User;
import com.ace.erp.service.sys.GenericService;
import com.ace.erp.service.sys.OrganizationService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
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.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * Created with ace.
 * User: denghp
 * Date: 11/19/13
 * Time: 9:32 PM
 */
@Controller
@RequestMapping("/admin/sys/company")
public class OrganizationController extends BaseController<Organization, Integer> implements InitializingBean {

    @Autowired
    @BaseComponent
    private OrganizationService organizationService;

    protected GenericService<Organization, Integer> genericService;

    private Logger logger = LoggerFactory.getLogger(OrganizationController.class);

    /**
     * service
     *
     * @param genericService
     */
    public void setGenericService(GenericService<Organization, Integer> genericService) {
        this.genericService = genericService;
    }

    @Override
    public void afterPropertiesSet() {
        InjectBaseDependencyHelper.findAndInjectGenericServiceDependency(this);
        Assert.notNull(genericService, "GenericService required, Class is:" + getClass());
    }

    @RequestMapping(value = "/company", method = RequestMethod.GET)
    public String view(@CurrentUser User user, Model model) {

        //????
        Organization organization = user.getOrganizationList().get(0);
        model.addAttribute("organ", organization);
        //organizationService.getOne(organization.getId());

        return viewName("company");
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(@CurrentUser User user, Model model) {

        return viewName("list");
    }

    @RequestMapping(value = "/company/list", method = RequestMethod.GET)
    @ResponseBody
    public Response companyList(@CurrentUser User user, HttpServletRequest request, HttpServletResponse response) {
        String page = request.getParameter("page"); // ??,?jqgrid?
        String rows = request.getParameter("rows"); // ??,?jqgrid?
        if (StringUtils.isBlank(page)) {
            page = "1";
        }
        if (StringUtils.isBlank(rows)) {
            rows = "10";
        }
        int totalRecord = genericService.count();

        int totalPage = totalRecord % Integer.parseInt(rows) == 0 ? totalRecord / Integer.parseInt(rows)
                : totalRecord / Integer.parseInt(rows) + 1; // 
        //?
        int start = (Integer.parseInt(page) - 1) * Integer.parseInt(rows); // 
        int pageSize = Integer.parseInt(rows);
        List<Organization> list = genericService.getPageList(start, pageSize);

        Response responseJson = new Response();
        responseJson.setRows(list);
        responseJson.setPage(Integer.valueOf(page));
        responseJson.setTotal(totalPage);
        responseJson.setRecords(totalRecord);
        return responseJson;
    }

    @RequestMapping(value = "/company/upload", method = RequestMethod.POST)
    @ResponseBody
    public Response upload(@CurrentUser User user, String srcFile, MultipartHttpServletRequest request,
            HttpServletResponse response) {

        try {
            MultipartFile realPicFile = request.getFile(srcFile);
            InputStream in = realPicFile.getInputStream();
            String path = request.getSession().getServletContext().getRealPath("/");
            String fileName = user.getUsername() + "."
                    + FilenameUtils.getExtension(realPicFile.getOriginalFilename());
            File f = new File(path + "/" + fileName);
            FileUtils.copyInputStreamToFile(in, f);
            logger.info("path : " + f.getAbsolutePath());
        } catch (Exception e) {
            logger.error("upload header picture error : ", e);
        }
        return null;
    }

    @RequestMapping(value = "/company/uploadLogo", method = RequestMethod.POST)
    @ResponseBody
    public Response uploadLogo(@CurrentUser User user, String srcImageFile, int x, int y, int destWidth,
            int destHeight, int srcShowWidth, int srcShowHeight, HttpServletRequest request) {
        try {
            String path = request.getSession().getServletContext().getRealPath("/");
            String contextPath = request.getContextPath();
            Image img;
            ImageFilter cropFilter;
            //String srcFileName = FilenameUtils.getName(srcImageFile);
            String srcFileName = StringUtils.isNotBlank(contextPath) ? srcImageFile.replaceFirst(contextPath, "")
                    : srcImageFile;
            // ???
            File srcFile = new File(path + "/" + srcFileName);

            BufferedImage bi = ImageIO.read(srcFile);
            //??????????
            int srcWidth = bi.getWidth(); // ?
            int srcHeight = bi.getHeight(); // ?
            if (srcShowWidth == 0)
                srcShowWidth = srcWidth;
            if (srcShowHeight == 0)
                srcShowHeight = srcHeight;

            if (srcShowWidth >= destWidth && srcShowHeight >= destHeight) {
                //???
                Image image = bi.getScaledInstance(srcShowWidth, srcShowHeight, Image.SCALE_DEFAULT);

                cropFilter = new CropImageFilter(x, y, destWidth, destHeight);

                img = Toolkit.getDefaultToolkit()
                        .createImage(new FilteredImageSource(image.getSource(), cropFilter));
                BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
                Graphics g = tag.getGraphics();
                g.drawImage(img, 0, 0, null); // ??
                g.dispose();

                String ext = FilenameUtils.getExtension(srcImageFile);

                //path += HEADER_PIC;
                //User loginUser = SystemUtil.getLoginUser(request.getSession());
                //String fileName = user.getOrganizationList().get(0).getId()+"";
                File destImageFile = new File(path + "/" + System.currentTimeMillis() + ".jpg");
                // 
                ImageIO.write(tag, ext, destImageFile);

                //loginUser.setPicPath(SystemConst.SYSTEM_CONTEXT_PATH_VALUE + HEADER_PIC + "/" + fileName);
                //userService.update(loginUser);
                // 
                srcFile.delete();
                return new Response(new ResponseHeader(200, 20));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}