com.fota.firmMgt.controller.FirmRegiCTR.java Source code

Java tutorial

Introduction

Here is the source code for com.fota.firmMgt.controller.FirmRegiCTR.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.firmMgt.controller;

import java.io.File;
import java.util.Date;

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

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.fota.comm.util.PropUtil;
import com.fota.comm.util.VoV;
import com.fota.commMgt.vo.UserLoginVO;
import com.fota.firmMgt.service.FirmMgtSVC;
import com.fota.firmMgt.vo.FirmHistoryVO;
import com.fota.firmMgt.vo.FirmSearchVO;
import com.fota.firmMgt.vo.FirmUpdateVO;
import com.oreilly.servlet.MultipartRequest;

@Controller
@RequestMapping("/commonDevice/firmRegiMgt")
public class FirmRegiCTR extends MultiActionController {

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

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

    private static final String DEFAULT_PATH = "/home/jboss/firmware/";
    //   private static final String DEFAULT_PATH = "/firmwareDiff_ServiceFOTA/";

    /**
     *  ?
     * @return
     */
    @RequestMapping(value = "/regiView")
    public String firmRegiView(HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (!request.getServletPath().equals("/commonDevice/firmRegiMgt/regiView")) {
            response.setStatus(403);
            return "redirect:/lresources/errorPage.jsp";
            //         return null;
        }

        return "firmMgt/firmRegiView";
    }

    /**
     *  
     * @param vo
     * @param model
     * @return
     */
    @RequestMapping(value = "/searchMaker")
    @ResponseBody
    public ModelAndView searchMaker(@ModelAttribute("FirmSearchVO") FirmSearchVO vo, ModelMap model)
            throws Exception {
        // validation check
        vo.setMakerTypeCd(VoV.valid(vo.getMakerTypeCd(), 1, 13));
        vo.setMakerNm(VoV.valid(vo.getMakerNm(), 50, 2));
        //-------------------

        vo = mySVC.searchMaker(vo);
        model.addAttribute("gridData", vo.getResults());

        return new ModelAndView(ajaxMainView, model);
    }

    /**
     *   
     * @param vo
     * @param model
     * @return
     */
    @RequestMapping(value = "/searchFirmwareVer")
    @ResponseBody
    public ModelAndView searchFirmwareVer(@ModelAttribute("FirmSearchVO") FirmSearchVO vo, ModelMap model)
            throws Exception {
        // validation check
        vo.setDevModelId(VoV.valid(vo.getDevModelId(), 30, 4));
        vo.setFirmwareVerId(VoV.valid(vo.getFirmwareVerId(), 30, 4));
        //-------------------

        vo = mySVC.searchFirmwareVer(vo);
        int firmwareVerInt = Integer.parseInt(vo.getFirmwareVer()) + 1;

        vo.setFirmwareVer(String.valueOf(firmwareVerInt));

        model.addAttribute("firmwareVer", vo.getFirmwareVer());

        return new ModelAndView(ajaxMainView, model);
    }

    /**
     * FOTA Type 
     * @param vo
     * @param model
     * @return
     */
    @RequestMapping(value = "/searchFotaType")
    @ResponseBody
    public ModelAndView searchFotaType(@ModelAttribute("FirmSearchVO") FirmSearchVO vo, ModelMap model)
            throws Exception {
        // validation check
        vo.setDevModelId(VoV.valid(vo.getDevModelId(), 30, 4));
        //-------------------

        vo = mySVC.searchFotaType(vo);
        model.addAttribute("fotaType", vo.getFotaType());

        return new ModelAndView(ajaxMainView, model);
    }

    /**
     * ?? (FOTA TYPE?)
     * @param vo
     * @param model
     * @return
     */
    @RequestMapping(value = "/getDevModelParamList")
    @ResponseBody
    public ModelAndView getDevModelParamList(@ModelAttribute("FirmHistoryVO") FirmHistoryVO vo, ModelMap model)
            throws Exception {
        // validation check
        vo.setSvcId(VoV.valid(vo.getSvcId(), 30, 4));
        vo.setMakerId(VoV.valid(vo.getMakerId(), 30, 4));
        vo.setFotaType(VoV.valid(vo.getFotaType(), 50));
        if (!vo.getFotaType().equals("") && !vo.getFotaType().equals("PUSH") && !vo.getFotaType().equals("POLLING")
                && !vo.getFotaType().equals("PUSHPOLLING"))
            throw new Exception();
        //-------------------

        vo = mySVC.getDevModelParamList(vo);
        model.addAttribute("devModelSet", vo.getResults());

        return new ModelAndView(ajaxMainView, model);
    }

    /**
     *  ?
     * @param vo
     * @param model
     * @return
     */
    @RequestMapping(value = "/regi")
    @ResponseBody
    public ModelAndView regi(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute("firmUpdateVO") FirmUpdateVO vo, ModelMap model) throws Exception {
        if (PropUtil.getPropValue("onm.mode").equals("P")) {
            Runtime.getRuntime()
                    .exec("sudo mount -t nfs 10.220.250.11:/vol/vol2/c-dms-pd1-w01 /home/jboss/firmware");
            logger.warn("Firmware Regist - File Upload Start... "
                    + "( Mount On : sudo mount -t nfs 10.220.250.11:/vol/vol2/c-dms-pd1-w01 /home/jboss/firmware )");

            // ?? ?  ?  2~3     sleep 3 
            Thread.sleep(3000);
        }

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

        //    ? ?
        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 {
            //  ?
            multi = new MultipartRequest(request, DEFAULT_PATH, maxfilesize, "utf-8");
            upfile = new File(DEFAULT_PATH + multi.getFilesystemName("file"));

            vo.setFileName(VoV.valid(upfile.getName(), 200, 8));
            //         2015-10-07
            //         vo.setFileSize(String.valueOf(upfile.length()));
            vo.setFileSize(Integer.parseInt(String.valueOf(upfile.length())));

            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());

        //      Runtime.getRuntime().exec("sudo umount /home/jboss/firmware");
        //      logger.warn("Firmware Regist - File Upload end... "
        //            + "( Mount Off : sudo umount /home/jboss/firmware )");

        return new ModelAndView(ajaxMainView, model);
    }

}