com.kcs.action.GenerateXmlDfFxmAction.java Source code

Java tutorial

Introduction

Here is the source code for com.kcs.action.GenerateXmlDfFxmAction.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kcs.action;

import com.kcs.core.actions.BaseAction;
import com.kcs.core.utilities.PaggingList;
import com.kcs.core.utilities.Utility;
import com.kcs.service.DfFxmService;
import com.kcs.service.GenerateXmlService;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.ArrayUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author Sarawut
 */
public class GenerateXmlDfFxmAction extends BaseAction {

    Logger logger = Logger.getLogger(this.getClass());

    private static final String XML = "xml";

    private String dataSetDate;
    private String systemCode;
    private String tmpDataSetDate;
    private String tmpSystemCode;
    private String tmpTcb;
    private List<String> tcbList;
    private PaggingList paging;
    private String fileName;

    @Autowired
    private GenerateXmlService generateXmlService;

    @Autowired
    private DfFxmService dfFxmService;

    private InputStream inputStream;

    @Override
    public String success() throws Exception {
        logger.debug("success");
        if (Utility.isNull(tcbList)) {
            tcbList = new ArrayList<String>();
            tcbList.add("TCB");
        }
        return SUCCESS; //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String search() throws Exception {
        logger.debug("search : begin...");

        paging = dfFxmService.findMainPaging(createPaginate(), dataSetDate, tmpTcb);

        logger.debug("search : end...");
        return SEARCH;
    }

    public String generateXml() throws Exception {
        logger.debug("generateXml : begin...");
        logger.debug("dataSetDate : " + tmpDataSetDate);

        byte[] xmlBytes = generateXmlService.generateXmlFxm(tmpDataSetDate, tmpTcb);

        if (ArrayUtils.isEmpty(xmlBytes)) {
            //            throw new RuntimeException("Can't generate xml.");
            displayErrorMessage(new RuntimeException("Can't generate xml."));
        } else {
            inputStream = new ByteArrayInputStream(xmlBytes);
        }
        setCookieDownloadStatus("complete");
        fileName = Utility.createXmlFileName("DF_FXM", tmpDataSetDate);

        logger.debug("generateXml : end...");
        return XML;
    }

    private void setCookieDownloadStatus(String status) {
        Cookie cookie = new Cookie("downloadStatus", status);
        cookie.setMaxAge(60 * 60 * 24 * 365); // Make the cookie last a year
        HttpServletResponse response = ServletActionContext.getResponse();
        response.addCookie(cookie);
    }

    public String getDataSetDate() {
        return dataSetDate;
    }

    public void setDataSetDate(String dataSetDate) {
        this.dataSetDate = dataSetDate;
    }

    public String getTmpDataSetDate() {
        return tmpDataSetDate;
    }

    public void setTmpDataSetDate(String tmpDataSetDate) {
        this.tmpDataSetDate = tmpDataSetDate;
    }

    public String getSystemCode() {
        return systemCode;
    }

    public void setSystemCode(String systemCode) {
        this.systemCode = systemCode;
    }

    public String getTmpSystemCode() {
        return tmpSystemCode;
    }

    public void setTmpSystemCode(String tmpSystemCode) {
        this.tmpSystemCode = tmpSystemCode;
    }

    public List<String> getTcbList() {
        return tcbList;
    }

    public void setTcbList(List<String> tcbList) {
        this.tcbList = tcbList;
    }

    public PaggingList getPaging() {
        return paging;
    }

    public void setPaging(PaggingList paging) {
        this.paging = paging;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getTmpTcb() {
        return tmpTcb;
    }

    public void setTmpTcb(String tmpTcb) {
        this.tmpTcb = tmpTcb;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}