com.action.ExportAction.java Source code

Java tutorial

Introduction

Here is the source code for com.action.ExportAction.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.action;

import static com.opensymphony.xwork2.Action.ERROR;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.entity.JQ_zhibiao_entity;
import static com.opensymphony.xwork2.Action.ERROR;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.util.Export;
import com.util.ReadProperties;
import com.util.ZipTool;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author yishan_zhuang
 */
@Repository("exportAction")
@Scope("prototype")
@Transactional
public class ExportAction {

    @Autowired
    protected SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    private String message;
    private String json;
    private String tableCode;
    String realPath = ReadProperties.getProperties("path");

    public String getTableCode() {
        return tableCode;
    }

    public void setTableCode(String tableCode) {
        this.tableCode = tableCode;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getRealPath() {
        return realPath;
    }

    public void setRealPath(String realPath) {
        this.realPath = realPath;
    }

    public void beforeExport() throws IOException {
        String sql = "select distinct(table_code),table_name from jq_zhibiao_table";
        Session session = sessionFactory.getCurrentSession();
        List<Object[]> list = session.createSQLQuery(sql).list();
        Map map = null;
        ArrayList rsList = new ArrayList();
        for (Object[] object : list) {
            map = new HashMap();
            map.put("table_code", (String) object[0]);
            map.put("table_name", (String) object[1]);
            rsList.add(map);
        }
        JSONArray jo = JSONArray.fromObject(rsList);
        json = jo.toString();
        ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
        ServletActionContext.getResponse().getWriter().print(json);
    }

    public String exportXMLAction() {
        try {
            //document
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.newDocument();
            Element reportsElement = document.createElement("project");//
            reportsElement.setAttribute("date", (new Date()).toString());
            document.appendChild(reportsElement);
            System.out.println("exportXMLAction:" + tableCode);
            String[] ids = tableCode.split(",");
            for (String id : ids) {
                System.out.println("id:" + id);
                List<JQ_zhibiao_entity> list = Export.getDuizhaoByJqTbCode(id);
                document = Export.exportXML(reportsElement, document, list, id);
            }
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource(document);
            transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            PrintWriter pw = new PrintWriter(new FileOutputStream(realPath + "test.xml"));
            StreamResult result = new StreamResult(pw);
            transformer.transform(source, result);
            pw.close();
        } catch (Exception e) {
            ActionContext.getContext().put("message", "??" + e);
            return ERROR;
        }
        return SUCCESS;
    }

    public String exportCSVAction() {
        try {
            System.out.println("exportXMLAction:" + tableCode);
            String[] ids = tableCode.split(",");
            List<File> files = new ArrayList<File>();
            File file0 = new File(realPath + "all.xml");
            files.add(exportXMLAction(file0));
            for (String id : ids) {
                File file = new File(realPath + id + ".csv");
                files.add(Export.createCSVByJqTbCode(file, id));
                File zipfile = new File(realPath + "CSV.zip");
                ZipTool.zipFiles(files, zipfile);
                System.out.println("id:" + id);
            }
            for (File file : files) {
                if (file.exists()) {
                    file.delete();
                }
            }
        } catch (Exception e) {
            ActionContext.getContext().put("message", "??" + e);
            return ERROR;
        }
        message = realPath + "CSV.zip";
        return SUCCESS;
    }

    public File exportXMLAction(File file) throws Exception {
        //document
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.newDocument();
        Element reportsElement = document.createElement("reports");//
        reportsElement.setAttribute("date", (new Date()).toString());
        document.appendChild(reportsElement);
        System.out.println("exportXMLAction:" + tableCode);
        String[] ids = tableCode.split(",");
        for (String id : ids) {
            System.out.println("id:" + id);
            List<JQ_zhibiao_entity> list = Export.getDuizhaoByJqTbCode(id);
            document = Export.exportXML(reportsElement, document, list, id);
        }

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        PrintWriter pw = new PrintWriter(new FileOutputStream(file));
        StreamResult result = new StreamResult(pw);
        transformer.transform(source, result);
        pw.close();
        return file;
    }
}