com.fota.fota3g.selffotaMgt.controller.ListManagementCTR.java Source code

Java tutorial

Introduction

Here is the source code for com.fota.fota3g.selffotaMgt.controller.ListManagementCTR.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.fota3g.selffotaMgt.controller;

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

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

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.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.fota.Link.sdpApi;
import com.fota.comm.util.Masking;
import com.fota.comm.util.VoV;
import com.fota.commMgt.vo.UserLoginVO;
import com.fota.fota3g.selffotaMgt.service.ListManagementSVC;
import com.fota.fota3g.selffotaMgt.vo.ListManagementVo;
import com.oreilly.servlet.MultipartRequest;

@Controller
@RequestMapping("/commonFota/selffotaMgt")
public class ListManagementCTR {

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

    /**
     * @uml.property  name="firmSVC"
     * @uml.associationEnd  readOnly="true"
     */
    @Autowired
    ListManagementSVC listManagementSVC;

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

    /**
     * dmRetry View
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/listManagement")
    public String getView(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        logger.warn("#############listManagement################");
        if (!request.getServletPath().equals("/commonFota/selffotaMgt/listManagement")) {
            response.setStatus(403);
            return "redirect:/lresources/errorPage.jsp";
        }
        //      int retvalue = 100;
        //      model.addAttribute("retvalue", retvalue);
        return "fota3g/selffotaMgt/listManagementView";
        //      return new ModelAndView ("fota3g/selffotaMgt/listManagementView" ,"dataParam", model);
    }

    /**
     * dmRetry list
     * @param param
     * @param response
     * @return
     */
    @RequestMapping(value = "/listManagementList")
    @ResponseBody
    public ModelAndView getDeviceLinkList(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute("ListManagementVo") ListManagementVo vo, ModelMap model) throws Exception {
        //      logger.warn("#############listManagementList################");

        vo = listManagementSVC.listManagementList(vo);

        // -  ?   java-Ctrl ?  - DMS ?
        // DB  ?  ?  
        //      ListManagementVo tempVo;
        //      if(vo.getResults() != null && vo.getResults().size() > 0) {
        //         for(int i=0; i<vo.getResults().size(); i++) {
        //            tempVo = vo.getResults().get(i);
        //            
        //            if(tempVo.getRegId() != null && !tempVo.getRegId().isEmpty()) {
        //               tempVo.setRegId(Masking.convertId(tempVo.getRegId()));
        //            }
        //            if(tempVo.getModId() != null && !tempVo.getModId().isEmpty()) {
        //               tempVo.setModId(Masking.convertId(tempVo.getModId()));
        //            }
        //         }
        //      }

        model.addAttribute("gridData", vo.getResults());
        model.addAttribute("page", vo.getPage());
        model.addAttribute("totalPage", vo.getTotalPage());

        return new ModelAndView(ajaxMainView, model);
    }

    /**
     * confirm 
     * @param param
     * @param response
     * @return
     */
    @RequestMapping(value = "/updateConfirmState")
    @ResponseBody
    public ModelAndView setConfirmState(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute("ListManagementVo") ListManagementVo vo, ModelMap model) throws Exception {
        logger.warn("#############updateConfirmState################");
        int retValue = 0;

        // user  
        HttpSession httpSession = request.getSession(false);
        UserLoginVO userVo = (UserLoginVO) httpSession.getAttribute("userInfo");
        String userId = userVo.getUserId();

        vo.setModId(userId);
        vo.setConfirmState("1");

        retValue = listManagementSVC.updataConfirm(vo);
        retValue = listManagementSVC.updateHold(vo);

        model.addAttribute("retValue", retValue);

        return new ModelAndView(ajaxMainView, model);
    }

    /**
     * confirm 
     * @param param
     * @param response
     * @return
     */
    @RequestMapping(value = "/updateHoldState")
    @ResponseBody
    public ModelAndView setHoldState(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute("ListManagementVo") ListManagementVo vo, ModelMap model) throws Exception {
        logger.warn("#############updateHoldState################");
        int retValue = 0;

        // user  
        HttpSession httpSession = request.getSession(false);
        UserLoginVO userVo = (UserLoginVO) httpSession.getAttribute("userInfo");
        String userId = userVo.getUserId();

        vo.setModId(userId);
        vo.setConfirmState("2");

        retValue = listManagementSVC.updataConfirm(vo);
        retValue = listManagementSVC.updateHold(vo);

        model.addAttribute("retValue", retValue);

        return new ModelAndView(ajaxMainView, model);
    }

    /**
     * confirm 
     * @param param
     * @param response
     * @return
     */
    @RequestMapping(value = "/registListMgt")
    @ResponseBody
    public ModelAndView registListMgt(HttpServletRequest request, HttpServletResponse response, ModelMap model)
            throws Exception {
        //      logger.warn(" >>>>> Self Fota - ListManagementCTR - registListMgt() start...");

        String retMsg = "-1";

        //   ? ?
        String path = "/jb_log/excelUpload/selfFotaList/";
        File pysicalFolder = new File(path);
        if (!pysicalFolder.exists() || !pysicalFolder.isDirectory()) {
            pysicalFolder.mkdirs();
        }

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

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

        // db  Map
        Map<String, String> entity = new HashMap<String, String>();

        // user  
        HttpSession httpSession = request.getSession(false);
        UserLoginVO userVo = (UserLoginVO) httpSession.getAttribute("userInfo");
        String userId = userVo.getUserId();
        //      int devCnt = 0;

        entity.put("reg_id", userId);
        entity.put("file_name", upFileName);
        entity.put("file_path", path); // 3G FOTA src ? path  ? (path + name )
        entity.put("company_name", VoV.replaceRoundBracket(multi.getParameter("maker")));
        //      entity.put("device_cnt", devCnt + "");    // device count  excel file ? count   ?.

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

        List<Map<String, String>> devList = new ArrayList<Map<String, String>>();

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

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

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

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

            // ? ? 
            Map<String, String> devInfo = null;

            // ? 
            rowIterator.next();

            while (rowIterator.hasNext()) {
                devInfo = new HashMap<String, String>();

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

                // model
                Cell cell = row.getCell(0);
                devInfo.put("device_model_name", cell.toString().trim());

                // previous version
                cell = row.getCell(1);
                devInfo.put("device_pre_ver", cell.toString().trim());

                // next version
                cell = row.getCell(2);
                devInfo.put("device_app_ver", cell.toString().trim());

                // ctn
                cell = row.getCell(3);
                devInfo.put("ctn", cell.toString().trim());

                // imei
                cell = row.getCell(4);
                devInfo.put("device_imei", cell.toString().trim());

                devList.add(devInfo);
            }

            entity.put("device_cnt", devList.size() + "");

            //           logger.warn("insertXls data : " + entity.toString());
            int insertRet = listManagementSVC.insertXls(entity);
            //           logger.warn("insertXls ret : " + insertRet);

            String master_idx = "";

            if (insertRet > 0) {
                master_idx = listManagementSVC.getInsertedMasterIdx(entity);
                //            logger.warn("getInsertedMasterIdx ret (master_idx) : " + master_idx);
            }

            int count = 0;

            // ?  db ? insert
            for (int i = 0; i < devList.size(); i++) {
                devList.get(i).put("reg_id", userId);
                devList.get(i).put("master_idx", master_idx);
                devList.get(i).put("company_name", entity.get("company_name").toString());
                devList.get(i).put("ncn", sdpApi.getNcnInfo(devList.get(i).get("ctn").toString()));
                //            devList.get(i).put("ncn", devList.get(i).get("ctn").toString() + "0001");         // test 

                //            logger.warn("devList[" + i + "] insertXlsFotaMng data : " + devList.get(i).toString());
                insertRet = listManagementSVC.insertXlsFotaMng(devList.get(i));
                //            logger.warn("devList[" + i + "] insertXlsFotaMng ret : " + insertRet);
                count++;
            }

            String msg = "? [ " + devList.size() + " ]   [ " + count
                    + " ] ? ? ??.";

            //IMEI , NCN    ?  
            //         count = devList.size() - count;
            //         msg = msg + "(IMEI  : " + count + " , NCN  : " + ncnErrCnt + " )";

            retMsg = "1";
            model.addAttribute("msg", msg);

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

        model.addAttribute("retMsg", retMsg);

        return new ModelAndView(ajaxMainView, model);
    }

    /**
     * confirm 
     * @param param
     * @param response
     * @return
     */
    @RequestMapping(value = "/setXmlData_back")
    @ResponseBody
    public ModelAndView setXmlDataRead_back(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute("ListManagementVo") ListManagementVo vo, ModelMap model) throws Exception {
        logger.warn("#############setXmlData################");

        {
            Date now = new Date();
            logger.info("File update start : " + now.toString());

            String default_path = "F:\\servicefota\\jboss\\download\\";

            //    ? ?
            File temp_folder = new File(default_path);
            if (!temp_folder.exists() || !temp_folder.isDirectory()) {
                temp_folder.mkdirs();
            }

            // ?? : 500M
            int maxfilesize = 500 * 1024 * 1024;
            MultipartRequest multi = null;
            File upfile = null;

            try {
                //  ?
                logger.warn("#############multi start################");
                multi = new MultipartRequest(request, default_path, maxfilesize, "utf-8");
                upfile = new File(default_path + multi.getFilesystemName("file"));

                vo.setFileName(upfile.getName());
                //            logger.warn("#############multi end################" + upfile);
                //            logger.warn("#############multi end################" + upfile);
                File upfiles = new File(default_path + upfile.getName());

                FileInputStream file = new FileInputStream(upfiles);
                //            logger.warn("#############getCell model################");
                //.xlsx file
                XSSFWorkbook workbook = new XSSFWorkbook(file);
                //              logger.warn("#############getCell model################");
                // 
                XSSFSheet sheet = workbook.getSheetAt(0);
                //              logger.warn("#############getCell model################");
                //  
                Iterator<Row> rowIterator = sheet.iterator();
                //              logger.warn("#############getCell model################");
                //    ?? 
                List<ListManagementVo> rs = new ArrayList();

                // ? 
                rowIterator.next();

                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    logger.warn("#############getCell model################" + row.cellIterator());
                    //For each row, iterate through all the columns
                    Iterator<Cell> cellIterator = row.cellIterator();

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

                    // model
                    Cell cell = row.getCell(0);
                    logger.warn("#############getCell model################" + cell);
                    tmp.setDeviceModelName(cell.toString().trim());
                    if (tmp.getDeviceModelName().length() > 50) {
                        tmp.setDeviceModelName(tmp.getDeviceModelName().substring(0, 49)); // ? 
                        logger.warn("#############getCell model################" + tmp.getDeviceModelName());
                    }

                    //before version
                    cell = row.getCell(1);
                    logger.warn("#############getCell before version################" + cell);
                    tmp.setDevicePerVer(cell.toString().trim());
                    if (tmp.getDevicePerVer().length() > 50) {
                        tmp.setDevicePerVer(tmp.getDevicePerVer().substring(0, 49)); // ? 
                    }

                    //after version
                    cell = row.getCell(2);
                    logger.warn("#############getCell after version################" + cell);
                    tmp.setDeviceAppVer(cell.toString().trim());
                    if (tmp.getDeviceAppVer().length() > 50) {
                        tmp.setDeviceAppVer(tmp.getDeviceAppVer().substring(0, 49)); // ? 
                    }

                    //ctn version
                    cell = row.getCell(3);
                    logger.warn("#############getCell ctn version################" + cell);
                    tmp.setCtn(cell.toString().trim());
                    if (tmp.getCtn().length() > 50) {
                        tmp.setCtn(tmp.getCtn().substring(0, 49)); // ? 
                    }

                    //imei version
                    cell = row.getCell(4);
                    logger.warn("#############getCell imei version################" + cell);
                    tmp.setDeviceImei(cell.toString().trim());
                    if (tmp.getDeviceImei().length() > 50) {
                        tmp.setDeviceImei(tmp.getDeviceImei().substring(0, 49)); // ? 
                    }

                }

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

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

                file.close();
                upfile.delete();
                model.addAttribute("gridData", rs);
                //            vo.setBizTypeId(VoV.valid(multi.getParameter("bizTypeId"), 30));
                //            vo.setSvcId(VoV.valid(multi.getParameter("svcId"), 30, 4));
                //            vo.setMakerId(VoV.valid(multi.getParameter("makerId"), 30, 4));
                //            vo.setDevModelId(VoV.valid(multi.getParameter("devModelId"), 30, 4));
                //            vo.setFotaType(VoV.valid(multi.getParameter("fotaType"), 50));
                //            if (!vo.getFotaType().equals("") && !vo.getFotaType().equals("PUSH") && !vo.getFotaType().equals("POLLING") && !vo.getFotaType().equals("PUSHPOLLING"))
                //               throw new Exception();
                //            
                //            //vo.setFirmwareId(VoV.valid(multi.getParameter("firmwareId"), 30, 4));
                //            //vo.setFirmwareDesc(VoV.valid(multi.getParameter("firmwareDesc"), 1000, 9));
                //            vo.setFirmwareVer(VoV.valid(multi.getParameter("firmwareVer"), 100, 4));
                //            vo.setFirmwareMakerVer(VoV.valid(multi.getParameter("firmwareMakerVer"), 100, 8));
                //            vo.setVerMemo(VoV.valid(multi.getParameter("verMemo"), 1000, 9));
                //            
                //            //   ? ?
                //            String path = default_path + vo.getDevModelId() + "/";
                //            File pysicalfolder = new File(path);
                //            if(!pysicalfolder.exists() || !pysicalfolder.isDirectory()) {
                //               pysicalfolder.mkdirs();
                //            }
                //            vo.setFilePath(VoV.valid(path, 200));
                //            
                //            //  ???
                //            File destFile = new File(path + upfile.getName());
                //            if(destFile.exists()){
                //               destFile.delete();
                //            }
                //            FileUtils.moveFile(upfile, destFile);
                //            
                //            //CRC, '0' 
                //            vo.setCrc("0");
                //            vo.setCretNm(((UserLoginVO)request.getSession().getAttribute("userInfo")).getUserNm());
                //            
            } catch (Exception e) { //  ?? 
                // ??? ? ? 
                if (upfile != null && upfile.exists()) {
                    upfile.delete();
                }

                String errorMSG = e.getMessage();
                if (errorMSG.indexOf("exceeds") != 0) {
                    model.addAttribute("error", "?? 500MB ?.");
                }

                logger.info("File update error : " + errorMSG);
                response.setStatus(500);
                return new ModelAndView(ajaxMainView, model);
            }

            //      mySVC.regi(vo);

            now = new Date();
            logger.info("File update end : " + now.toString());

            //      return new ModelAndView (ajaxMainView, model);
        }
        //      int retValue = 0;
        //      vo.setModId("sooya118");
        //      vo.setConfirmState("2");
        //      
        //      retValue = listManagementSVC.updataConfirm(vo);
        //      retValue = listManagementSVC.updateHold(vo);
        //      
        //      model.addAttribute("retValue", retValue);
        //      
        return new ModelAndView(ajaxMainView, model);
    }

}