com.fota.devMgt.controller.DevOpenMgtCTR.java Source code

Java tutorial

Introduction

Here is the source code for com.fota.devMgt.controller.DevOpenMgtCTR.java

Source

/*
 * M2M ServiceFOTA ONM version 1.0
 *
 *  Copyright  2014 kt corp. All rights reserved.
 *
 *  This is a proprietary software of kt corp, and you may not use this file except in
 *  compliance with license agreement with kt corp. Any redistribution or use of this
 *  software, with or without modification shall be strictly prohibited without prior written
 *  approval of kt corp, and the copyright notice above does not evidence any actual or
 *  intended publication of such software.
 */
package com.fota.devMgt.controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.fota.comm.util.Masking;
import com.fota.devMgt.service.DevOpenMgtSVC;
import com.fota.devMgt.vo.DevSearchVO;
import com.oreilly.servlet.MultipartRequest;

@Controller
@RequestMapping("/commonDevice/devMgt")
public class DevOpenMgtCTR extends MultiActionController {

    private static final Logger logger = LoggerFactory.getLogger(DevOpenMgtCTR.class);

    /**
     * @uml.property  name="mySVC"
     * @uml.associationEnd  readOnly="true"
     */
    @Autowired
    DevOpenMgtSVC mySVC;

    /**
     * @uml.property  name="ajaxMainView"
     * @uml.associationEnd  readOnly="true"
     */
    @Autowired
    private MappingJacksonJsonView ajaxMainView;

    /**
     * ?   
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/devOpenMgt")
    public String getView(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        if (!request.getServletPath().equals("/commonDevice/devMgt/devOpenMgt")) {
            response.setStatus(403);
            return "redirect:/lresources/errorPage.jsp";
            //         return null;
        }
        return "devMgt/devOpenMgtView";
    }

    /**
     *  ?  , ? ?   .
     * ?  ?.
     * @param vo
     * @param model
     * @return
     */
    @RequestMapping(value = "/openInfoUpload")
    public ModelAndView uploadTest(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute("devSearchVO") DevSearchVO vo, ModelMap model) throws Exception {
        if (!request.getServletPath().equals("/commonDevice/devMgt/openInfoUpload")) {
            response.setStatus(403);
            return null;
        }
        //   ? ?
        String path = "/jb_log/excelUpload/devOpenInfoFiles_tmp/";
        File pysicalFolder = new File(path);
        if (!pysicalFolder.exists() || !pysicalFolder.isDirectory()) {
            pysicalFolder.mkdirs();
        }

        //  ?
        MultipartRequest multi = new MultipartRequest(request, path, 10 * 1024 * 1024, "utf-8");
        String upFile = multi.getFilesystemName("file");

        if (upFile == null || upFile.equals("")) {
            model.addAttribute("msg", "?? .");
            return new ModelAndView(ajaxMainView, model);
        }

        // poi    ? ? ?
        File upfile = new File(path + upFile);

        // poi 
        try {
            FileInputStream file = new FileInputStream(upfile);

            //.xlsx file
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            // 
            XSSFSheet sheet = workbook.getSheetAt(0);

            //  
            Iterator<Row> rowIterator = sheet.iterator();

            //    ?? 
            List<DevSearchVO> rs = new ArrayList();

            // ? 
            rowIterator.next();

            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();

                DevSearchVO tmp = new DevSearchVO();
                rs.add(tmp);

                // 
                Cell cell = row.getCell(0);
                tmp.setBizNm(cell.toString().trim());
                if (tmp.getBizNm().length() > 50) {
                    tmp.setBizNm(tmp.getBizNm().substring(0, 49)); // ? 
                }

                // 
                cell = row.getCell(1);
                tmp.setSvcNm(cell.toString().trim());
                if (tmp.getSvcNm().length() > 50) {
                    tmp.setSvcNm(tmp.getSvcNm().substring(0, 49)); // ? 
                }

                // ??
                cell = row.getCell(2);
                tmp.setDevModelNm(cell.toString().trim());
                if (tmp.getDevModelNm().length() > 50) {
                    tmp.setDevModelNm(tmp.getDevModelNm().substring(0, 49)); // ? 
                }

                // CTN
                cell = row.getCell(3);
                tmp.setCtn(cell.toString().trim());
                if (tmp.getCtn().length() > 20) {
                    tmp.setCtn(tmp.getCtn().substring(0, 19)); // ? 
                }

                // 
                cell = row.getCell(4);
                tmp.setDealerNm(cell.toString().trim());
                if (tmp.getDealerNm().length() > 50) {
                    tmp.setDealerNm(tmp.getDealerNm().substring(0, 49)); // ? 
                }

                //  
                cell = row.getCell(5, row.CREATE_NULL_AS_BLANK);
                tmp.setCustomTag(cell.toString().trim());
                if (tmp.getCustomTag().length() > 30) {
                    tmp.setCustomTag(tmp.getCustomTag().substring(0, 29)); // ? 
                }

                // ? ?? 
                cell = row.getCell(6, row.CREATE_NULL_AS_BLANK);
                tmp.setApprovalYn(cell.toString().trim());
                if (tmp.getApprovalYn().equals(""))
                    tmp.setApprovalYn("Y");
                if (!tmp.getApprovalYn().equals("Y"))
                    tmp.setApprovalYn("N");

                tmp.setMemo("");
            }

            for (DevSearchVO tmp : rs) {
                mySVC.addDevInfo(tmp);
            }

            DevSearchVO footer = new DevSearchVO();
            rs.add(footer);

            file.close();
            upfile.delete();

            DevSearchVO tempVo;
            // -  ? java-Ctrl ?  - DMS ?
            if (rs != null && rs.size() > 0) {
                for (int i = 0; i < rs.size(); i++) {
                    tempVo = rs.get(i);
                    if (tempVo.getCtn() != null && !tempVo.getCtn().isEmpty()) {
                        tempVo.setCtn(Masking.convertCtn(tempVo.getCtn()));
                    }
                }
            }

            model.addAttribute("gridData", rs);
        } catch (Exception e) {
            e.printStackTrace();
            upfile.delete();
            model.addAttribute("msg", "?? ?   .");
        }

        return new ModelAndView(ajaxMainView, model);
    }

}