com.opendesign.controller.ProductController.java Source code

Java tutorial

Introduction

Here is the source code for com.opendesign.controller.ProductController.java

Source

/*
 * Copyright (c) 2016 OpenDesign All rights reserved.
 *
 * This software is the confidential and proprietary information of OpenDesign.
 * You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with OpenDesign.
 */
package com.opendesign.controller;

import org.apache.commons.lang.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.opendesign.service.CommonService;
import com.opendesign.service.DesignerService;
import com.opendesign.service.ProductService;
import com.opendesign.utils.CmnConst.CateExclude;
import com.opendesign.utils.CmnConst.FileUploadDomain;
import com.opendesign.utils.CmnConst.MemberDiv;
import com.opendesign.utils.CmnConst.RstConst;
import com.opendesign.utils.CmnUtil;
import com.opendesign.utils.Day;
import com.opendesign.utils.StringUtil;
import com.opendesign.utils.ThumbnailManager;
import com.opendesign.vo.CategoryVO;
import com.opendesign.vo.DesignPreviewImageVO;
import com.opendesign.vo.DesignWorkFileVO;
import com.opendesign.vo.DesignWorkVO;
import com.opendesign.vo.DesignerVO;
import com.opendesign.vo.MainItemVO;
import com.opendesign.vo.UserVO;
import com.wdfall.spring.JsonModelAndView;

/**
 * <pre>
 * ??()? ?  
 *  ?
 * </pre>
 * 
 * @author hanchanghao
 * @since 2016. 8. 20.
 */
@Controller
@RequestMapping(value = "/product")
public class ProductController {

    /**
     * ? ? :10MB
     */
    private static final long LIMIT_FILE_SIZE = 10000000L;
    /**
     * ?  
     */
    private static final int SAVE_TYPE_INSERT = 10000;
    /**
     *  
     */
    private static final int SAVE_TYPE_UPDATE = 20000;

    /**
     * ??()  ?
     */
    @Autowired
    ProductService service;

    /**
     * ??/?  ?
     */
    @Autowired
    DesignerService designerService;

    /**
     *   ?
     */
    @Autowired
    CommonService commonService;

    /* ================ list(? ? ?) ========================  */
    /**
     * <pre>
     * ??() ? (??) 
     * ID#OD02-01-01
     * </pre>
     * 
     * @param request
     * @return
     */
    @RequestMapping(value = "/product.do")
    public ModelAndView product(HttpServletRequest request) {

        return new ModelAndView("product/product");
    }

    /**
     * ??() ? ?? 
     * 
     * @param request
     * @return
     */
    @RequestMapping(value = "/productList.ajax")
    public @ResponseBody Map<String, Object> ajaxProductList(HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        /*
         * ? ? 
         */
        int pageIndex = 1;
        try {
            pageIndex = Integer.parseInt(request.getParameter("schPage"));
        } catch (Exception e) {
        }

        if (pageIndex <= 0) {
            pageIndex = 1;
        }

        String schLimitCount = request.getParameter("schLimitCount");
        int limitCount = 50;
        if (StringUtil.isNotEmpty(schLimitCount)) {
            try {
                limitCount = Integer.parseInt(schLimitCount);
            } catch (Exception e) {
            }
        }

        /*
         * 
         */
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page_count", (pageIndex - 1) * limitCount);
        paramMap.put("limit_count", limitCount);
        paramMap.put("schCate", request.getParameter("schCate"));
        paramMap.put("schSort", request.getParameter("schSort"));

        if (CmnUtil.isUserLogin(request)) {
            paramMap.put("memberSeq", CmnUtil.getLoginUser(request).getSeq());
        }

        int allCount = service.selectProductWithProjCount(paramMap);

        List<MainItemVO> list = service.selectProductWithProjList(paramMap);

        MainItemVO item = new MainItemVO();

        if (!StringUtils.isEmpty(request.getParameter("schCate"))) {
            item = settingCategoryNm(request.getParameter("schCate"));
        }
        resultMap.put("all_count", allCount);
        resultMap.put("list", list);
        resultMap.put("item", item);

        return resultMap;
    }

    /* ================ list(? ? ?)========================  */

    /**
     * ??() ?
     * 
     * @param product
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/registerProduct.ajax")
    public ModelAndView registerProject(@ModelAttribute("product") DesignWorkVO product,
            MultipartHttpServletRequest request) throws Exception {

        return saveProduct(product, request, SAVE_TYPE_INSERT);

    }

    /**
     * ??() 
     * 
     * @param product
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateProduct.ajax")
    public ModelAndView updateProduct(@ModelAttribute("product") DesignWorkVO product,
            MultipartHttpServletRequest request) throws Exception {

        return saveProduct(product, request, SAVE_TYPE_UPDATE);

    }

    /**
     * ??() ?/
     * 
     * @param product
     * @param request
     * @param saveType
     * @return
     * @throws Exception
     */
    private JsonModelAndView saveProduct(DesignWorkVO product, MultipartHttpServletRequest request, int saveType)
            throws Exception {

        /*
         * ?? ? ?  
         */
        boolean isUpdate = saveType == SAVE_TYPE_UPDATE;

        Map<String, Object> resultMap = new HashMap<String, Object>();

        UserVO loginUser = CmnUtil.getLoginUser(request);
        if (loginUser == null || !StringUtil.isNotEmpty(loginUser.getSeq())) {
            resultMap.put("result", "100");
            return new JsonModelAndView(resultMap);
        }

        /*   ?  ? */
        if (isUpdate) {
            DesignWorkVO prevProduct = designerService.getDesignWork(product.getSeq());
            if (!loginUser.getSeq().equals(prevProduct.getMemberSeq())) {
                resultMap.put("result", "101");
                return new JsonModelAndView(resultMap);
            }
        }

        /*
         * ? ?
         */
        MultipartFile fileUrlFile = request.getFile("fileUrlFile");
        if (fileUrlFile != null) {
            String fileName = fileUrlFile.getOriginalFilename().toLowerCase();
            if (!(fileName.endsWith(".jpg") || fileName.endsWith(".png"))) {
                resultMap.put("result", "202");
                return new JsonModelAndView(resultMap);
            }

        } else {
            /* ?  ? . */
            if (!isUpdate) {
                resultMap.put("result", "201");
                return new JsonModelAndView(resultMap);
            }
        }

        /*
         * license
         */
        String license01 = request.getParameter("license01");
        String license02 = request.getParameter("license02");
        String license03 = request.getParameter("license03");
        String license = license01 + license02 + license03;
        product.setLicense(license);

        //??  ?///// 
        String stem = request.getParameter("origin");
        if (stem == null) {
            product.setOriginSeq("0");
        } else {
            product.setOriginSeq(stem);
        }
        //////////////////////////////

        /* 
         * ?? ?  "0"  
         */
        String point = request.getParameter("point");
        point = String.valueOf(CmnUtil.getIntValue(point)); //null--> 0 
        product.setPoint(point);

        /*
         * ? ?
         */
        List<MultipartFile> productFileList = new ArrayList<MultipartFile>();
        List<MultipartFile> openSourceFileList = new ArrayList<MultipartFile>();

        Iterator<String> iterator = request.getFileNames();
        while (iterator.hasNext()) {
            String fileNameKey = iterator.next();

            MultipartFile reqFile = request.getFile(fileNameKey);
            if (reqFile != null) {
                boolean existProuductFile = fileNameKey.startsWith("productFile");
                boolean existOpenSourceFile = fileNameKey.startsWith("openSourceFile");
                if (existProuductFile || existOpenSourceFile) {
                    long fileSize = reqFile.getSize();
                    if (fileSize > LIMIT_FILE_SIZE) {
                        resultMap.put("result", "203");
                        resultMap.put("fileName", reqFile.getOriginalFilename());
                        return new JsonModelAndView(resultMap);

                    }

                    if (existProuductFile) {
                        productFileList.add(reqFile);
                    }

                    if (existOpenSourceFile) {
                        openSourceFileList.add(reqFile);
                    }
                }
            }
        }
        product.setMemberSeq(loginUser.getSeq());

        /*
         * ?? 
         */
        String fileUploadDir = CmnUtil.getFileUploadDir(request, FileUploadDomain.PRODUCT);
        File thumbFile = null;
        if (fileUrlFile != null) {
            String saveFileName = UUID.randomUUID().toString();
            thumbFile = CmnUtil.saveFile(fileUrlFile, fileUploadDir, saveFileName);

            String fileUploadDbPath = CmnUtil.getFileUploadDbPath(request, thumbFile);
            product.setThumbUri(fileUploadDbPath);
        }

        /*
         * ??() 
         */
        List<DesignPreviewImageVO> productList = new ArrayList<DesignPreviewImageVO>();
        List<String> productFilePaths = new ArrayList<>();

        for (MultipartFile aFile : productFileList) {
            String saveFileName = UUID.randomUUID().toString();
            File file = CmnUtil.saveFile(aFile, fileUploadDir, saveFileName);

            productFilePaths.add(file.getAbsolutePath());

            String fileUploadDbPath = CmnUtil.getFileUploadDbPath(request, file);

            DesignPreviewImageVO productFile = new DesignPreviewImageVO();
            productFile.setFilename(aFile.getOriginalFilename());
            productFile.setFileUri(fileUploadDbPath);

            productList.add(productFile);

        }
        product.setImageList(productList);

        /*
         *   
         */
        List<DesignWorkFileVO> openSourceList = new ArrayList<DesignWorkFileVO>();
        for (MultipartFile aFile : openSourceFileList) {
            String saveFileName = UUID.randomUUID().toString();
            File file = CmnUtil.saveFile(aFile, fileUploadDir, saveFileName);
            String fileUploadDbPath = CmnUtil.getFileUploadDbPath(request, file);

            //openSourceFile? ??? client?  .
            String filenameOpenSourceFile = StringUtils
                    .stripToEmpty(request.getParameter("filename_" + aFile.getName()));

            DesignWorkFileVO openSourceFile = new DesignWorkFileVO();
            openSourceFile.setFilename(filenameOpenSourceFile);
            openSourceFile.setFileUri(fileUploadDbPath);

            openSourceList.add(openSourceFile);

        }
        product.setFileList(openSourceList);

        /*
         * ??/?   ?  ? ? 
         * ?? ? ?  ? ? ? 
         */
        String thumbFilePath = "";
        if (thumbFile != null) {
            thumbFilePath = thumbFile.getAbsolutePath();
        }
        ThumbnailManager.resizeNClone4DesignWork(thumbFilePath, productFilePaths);

        /*
         *  
         */
        String tag = request.getParameter("tag");
        if (StringUtil.isNotEmpty(tag)) {
            String[] tags = tag.split(",");

            int addIndex = 0;
            StringBuffer tagBuffer = new StringBuffer();
            for (String aTag : tags) {
                if (StringUtil.isNotEmpty(aTag)) {
                    aTag = aTag.trim();
                    tagBuffer.append(aTag);
                    tagBuffer.append("|");
                    addIndex++;
                }

                if (addIndex >= 5) {
                    break;
                }
            }

            if (addIndex > 0) {
                tagBuffer.insert(0, "|");

                tag = tagBuffer.toString();
            }
        }
        product.setTags(tag);

        String currentDate = Day.getCurrentTimestamp().substring(0, 12);
        product.setRegisterTime(currentDate);
        product.setUpdateTime(currentDate);

        String[] categoryCodes = request.getParameterValues("categoryCodes");

        /*
         *  
         */
        if (isUpdate) {
            String[] removeProductSeqs = request.getParameterValues("removeProductSeq");
            String[] removeOpenSourceSeqs = request.getParameterValues("removeOpenSourceSeq");

            int projectSeq = service.updateProduct(product, categoryCodes, removeProductSeqs, removeOpenSourceSeqs);

        } else {
            int projectSeq = service.insertProduct(product, categoryCodes);

        }

        resultMap.put(RstConst.P_NAME, RstConst.V_SUCESS);
        return new JsonModelAndView(resultMap);

    }

    /**
     * ??() 
     * 
     * @param product
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deleteProduct.ajax")
    public ModelAndView deleteProduct(@ModelAttribute("product") DesignWorkVO product, HttpServletRequest request)
            throws Exception {

        Map<String, Object> resultMap = new HashMap<String, Object>();

        UserVO loginUser = CmnUtil.getLoginUser(request);
        if (loginUser == null || !StringUtil.isNotEmpty(loginUser.getSeq())) {
            resultMap.put("result", "100");
            return new JsonModelAndView(resultMap);
        }

        DesignWorkVO prevProduct = designerService.getDesignWork(product.getSeq());
        if (!loginUser.getSeq().equals(prevProduct.getMemberSeq())) {
            resultMap.put("result", "101");
            return new JsonModelAndView(resultMap);
        }

        int projectSeq = service.deleteProduct(product);

        resultMap.put(RstConst.P_NAME, RstConst.V_SUCESS);
        return new JsonModelAndView(resultMap);

    }

    /**
     * <pre>
     * ??() ? ? (??) 
     * ID#OD04-02-01
     * </pre>
     * 
     * @param dsWorkVO:
     *            seq
     * @param request
     * @return
     */
    @RequestMapping(value = "/productModify.do")
    public ModelAndView productModify(@ModelAttribute DesignWorkVO dsWorkVO, HttpServletRequest request) {

        Map<String, Object> resultMap = designerService.productView(dsWorkVO, request);
        resultMap.put("cateList", commonService.selectCategoryListDepth1());

        return new ModelAndView("/product/product_modify", resultMap);
    }

    /**
     * <pre>
     * ??() ? ? (??)
     * ID#OD02-01-02
     * </pre>
     * 
     * @param request
     * @return
     */
    @RequestMapping(value = "/productRegi.do")
    public ModelAndView productRegi(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("product/product_regi");

        view.addObject("cateList", commonService.selectCategoryListDepth1());
        return view;
    }

    //---------------------------- -----------------------------------//
    @RequestMapping(value = "/productStemRegi.do")
    public ModelAndView productStemRegi(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("product/product_stem");
        view.addObject("cateList", commonService.selectCategoryListDepth1());
        return view;
    }

    @RequestMapping(value = "/getOriginLicense.ajax")
    public @ResponseBody Map<String, Object> productgetOriginLicense(HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String license = service.getLicense(request.getParameter("origin"));
        resultMap.put("license", license);
        return resultMap;
    }
    //------------------------------___ ---------------------------------//

    // ===================== ? ===========================

    /**
     * <pre>
     * ?? ?? ? (??) 
     * ID#OD04-01-02
     * </pre>
     * 
     * @param desiVO:
     *            seq
     * @return
     */
    @RequestMapping(value = "/portfolio.do")
    public ModelAndView portfolio(@ModelAttribute DesignerVO desiVO, HttpServletRequest reqeust) {

        Map<String, Object> resultMap = designerService.selectDesingerDetail(desiVO, reqeust);
        resultMap.put("schMemberDiv", MemberDiv.PRODUCT); // ?
        return new ModelAndView("/designer/portfolio", resultMap);
    }

    /**
     * <pre>
     * ??() ? ? (??)
     * ID#OD04-02-01
     * </pre>
     * 
     * @param dsWorkVO:
     *            seq
     * @param request
     * @return
     */
    @RequestMapping(value = "/productView.do")
    public ModelAndView productView(@ModelAttribute DesignWorkVO dsWorkVO, HttpServletRequest request) {

        Map<String, Object> resultMap = designerService.productView(dsWorkVO, request);
        resultMap.put("schMemberDiv", MemberDiv.PRODUCT); // ?
        return new ModelAndView("/product/product_view", resultMap);
    }

    ////////////// ////////
    @RequestMapping(value = "/productOriginView.do")
    public ModelAndView productOriginView(@ModelAttribute DesignWorkVO dsWorkVO, HttpServletRequest request) {

        Map<String, Object> resultMap = designerService.productView(dsWorkVO, request);
        resultMap.put("schMemberDiv", MemberDiv.PRODUCT); // ?
        return new ModelAndView("/product/product_originView", resultMap);
    }

    //////////_///////////////////
    /**
     * <pre>
     * ??()  ? (??)
     * ID#OD04-02-02
     * </pre>
     * 
     * @param dsWorkVO:
     *            seq
     * @param request
     * @return
     */
    @RequestMapping(value = "/productPurchase.do")
    public ModelAndView productPurchaseView(@ModelAttribute DesignWorkVO dsWorkVO, HttpServletRequest request) {

        UserVO user = CmnUtil.getLoginUser(request);

        Map<String, Object> resultMap = designerService.productPurchaseView(dsWorkVO, request);
        resultMap.put("schMemberDiv", MemberDiv.PRODUCT); // ?

        /*
         * ?  ? ? ? ? ?,    ?.
         */
        String userSeq = "";
        String workSeq = "";

        if (user != null) {
            userSeq = user.getSeq();
        }
        if (dsWorkVO != null) {
            workSeq = dsWorkVO.getSeq();
        }

        DesignWorkVO workVO = designerService.getDesignWork(workSeq);

        String productUserSeq = workVO.getMemberSeq();

        boolean hasPurchasedWork = designerService.hasPurchasedWork(workSeq, userSeq);

        if (!userSeq.equals(productUserSeq)) {
            if (!hasPurchasedWork) {
                return new ModelAndView("/product/product_purchase", resultMap);
            } else {
                return new ModelAndView("/product/product_view", resultMap);
            }
        } else {
            return new ModelAndView("/product/product_view", resultMap);
        }

    }

    // ===================== ]]? ===========================

    /**
     *  ? 
     * @param category
     * @return
     */
    public MainItemVO settingCategoryNm(String category) {

        return service.settingCategoryNm(category);
    }
}