org.bbreak.excella.reports.processor.ReportProcessor.java Source code

Java tutorial

Introduction

Here is the source code for org.bbreak.excella.reports.processor.ReportProcessor.java

Source

/*************************************************************************
 *
 * Copyright 2009 by bBreak Systems.
 *
 * ExCella Reports - Excel??
 *
 * $Id: ReportProcessor.java 195 2010-11-19 07:34:06Z akira-yokoi $
 * $Revision: 195 $
 *
 * This file is part of ExCella Reports.
 *
 * ExCella Reports is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * ExCella Reports is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the COPYING.LESSER file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with ExCella Reports .  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0-standalone.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
package org.bbreak.excella.reports.processor;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.bbreak.excella.core.BookController;
import org.bbreak.excella.core.BookData;
import org.bbreak.excella.core.SheetData;
import org.bbreak.excella.core.exception.ExportException;
import org.bbreak.excella.core.exception.ParseException;
import org.bbreak.excella.core.exporter.book.BookExporter;
import org.bbreak.excella.reports.exporter.ReportBookExporter;
import org.bbreak.excella.reports.listener.BreakAdapter;
import org.bbreak.excella.reports.listener.RemoveAdapter;
import org.bbreak.excella.reports.listener.ReportProcessListener;
import org.bbreak.excella.reports.model.ConvertConfiguration;
import org.bbreak.excella.reports.model.ReportBook;
import org.bbreak.excella.reports.model.ReportSheet;
import org.bbreak.excella.reports.tag.ReportsTagParser;
import org.bbreak.excella.reports.util.ReportsUtil;

/**
 * ?
 *
 * @since 1.0
 */
public class ReportProcessor {

    /**
     * Map<BR>
     * ??<BR>
     * ??
     */
    private Map<String, ReportsTagParser<?>> parsers = new LinkedHashMap<String, ReportsTagParser<?>>();

    /**
     * ?Map<BR>
     * <BR>
     * ???
     */
    private Map<String, ReportBookExporter> exporters = new HashMap<String, ReportBookExporter>();

    /**
     * 
     */
    private List<ReportProcessListener> listeners = new ArrayList<ReportProcessListener>();

    /**
     * 
     */
    public ReportProcessor() {
        // ???
        // Parser?
        parsers.putAll(ReportCreateHelper.createDefaultParsers());

        // ????
        // Exporter?
        exporters.putAll(ReportCreateHelper.createDefaultExporters());
    }

    /**
     * ???
     *
     * @param reportBooks 
     * @throws IOException ???????
     * @throws ParseException ??????
     * @throws ExportException ?????
     */
    public void process(ReportBook... reportBooks) throws Exception {
        // ???
        for (ReportBook reportBook : reportBooks) {
            processBook(reportBook);
        }
    }

    /**
     * ????
     *
     * @param reportBook ??
     * @throws IOException ???????
     * @throws ParseException ??????
     * @throws ExportException ?????
     */
    private void processBook(ReportBook reportBook) throws Exception {

        if (reportBook == null) {
            return;
        }

        Workbook workbook = getTemplateWorkbook(reportBook);

        for (ReportProcessListener listener : listeners) {
            listener.preBookParse(workbook, reportBook);
        }

        checkReportBook(reportBook);

        // 
        Set<String> delSheetNames = expandTemplate(workbook, reportBook);

        // ?
        BookController controller = new BookController(workbook);

        // Parser?
        for (ReportsTagParser<?> tagParser : parsers.values()) {
            controller.addTagParser(tagParser);
        }
        // ?
        controller.addSheetParseListener(new RemoveAdapter());
        controller.addSheetParseListener(new BreakAdapter());
        for (ReportProcessListener listener : listeners) {
            controller.addSheetParseListener(listener);
        }

        // Exporter?
        for (ConvertConfiguration configuration : reportBook.getConfigurations()) {
            if (configuration == null) {
                continue;
            }
            for (ReportBookExporter reportExporter : exporters.values()) {
                if (configuration.getFormatType().equals(reportExporter.getFormatType())) {
                    reportExporter.setConfiguration(configuration);
                    reportExporter.setFilePath(reportBook.getOutputFileName() + reportExporter.getExtention());
                    controller.addBookExporter(reportExporter);
                }
            }
        }

        ReportsParserInfo reportsParserInfo = new ReportsParserInfo();
        reportsParserInfo.setReportParsers(new ArrayList<ReportsTagParser<?>>(parsers.values()));
        reportsParserInfo.setReportBook(reportBook);

        BookData bookData = controller.getBookData();
        bookData.clear();
        for (String sheetName : controller.getSheetNames()) {
            if (sheetName.startsWith(BookController.COMMENT_PREFIX)) {
                continue;
            }
            ReportSheet reportSheet = ReportsUtil.getReportSheet(sheetName, reportBook);
            if (reportSheet != null) {

                reportsParserInfo.setParamInfo(reportSheet.getParamInfo());

                // ??
                SheetData sheetData = controller.parseSheet(sheetName, reportsParserInfo);
                // ??
                controller.getBookData().putSheetData(sheetName, sheetData);
            }
        }

        // ???
        for (String delSheetName : delSheetNames) {
            int delSheetIndex = workbook.getSheetIndex(delSheetName);
            if (delSheetIndex != -1) {
                workbook.removeSheetAt(delSheetIndex);
            }
        }

        // ?????
        for (ReportProcessListener listener : listeners) {
            listener.postBookParse(workbook, reportBook);
        }

        // ??
        for (BookExporter exporter : controller.getExporter()) {
            if (exporter != null) {
                exporter.setup();
                try {
                    exporter.export(workbook, bookData);
                } finally {
                    exporter.tearDown();
                }
            }
        }
    }

    /**
     * ??
     *
     * @param reportBook ??
     */
    private void checkReportBook(ReportBook reportBook) {
    }

    /**
     * ???
     *
     * @param filepath 
     * @return 
     * @throws IOException ???????
     */
    private Workbook getTemplateWorkbook(ReportBook reportBook) throws Exception {
        Workbook wb = null;
        // URL???????URL
        if (reportBook.getTemplateFileURL() != null) {
            InputStream in = null;
            try {
                in = reportBook.getTemplateFileURL().openStream();
                wb = WorkbookFactory.create(in);
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        } else {
            FileInputStream fileIn = null;
            try {
                fileIn = new FileInputStream(reportBook.getTemplateFileName());
                wb = WorkbookFactory.create(fileIn);
            } finally {
                if (fileIn != null) {
                    fileIn.close();
                }
            }
        }

        return wb;
    }

    /**
     * ?
     *
     * @param tagParser ?
     */
    public void addReportsTagParser(ReportsTagParser<?> tagParser) {
        parsers.put(tagParser.getTag(), tagParser);
    }

    /**
     * ??
     *
     * @param tag 
     */
    public void removeReportsTagParser(String tag) {
        parsers.remove(tag);
    }

    /**
     * ?????
     */
    public void clearReportsTagParser() {
        parsers.clear();
    }

    /**
     * ??
     *
     * @param exporter ??
     */
    public void addReportBookExporter(ReportBookExporter exporter) {
        exporters.put(exporter.getFormatType(), exporter);
    }

    /**
     * ???
     *
     * @param formatType 
     */
    public void removeReportBookExporter(String formatType) {
        exporters.remove(formatType);
    }

    /**
     * ??????
     */
    public void clearReportBookExporter() {
        exporters.clear();
    }

    /**
     * ?
     *
     * @param listener 
     */
    public void addReportProcessListener(ReportProcessListener listener) {
        listeners.add(listener);
    }

    /**
     * ?
     *
     * @param listener 
     */
    public void removeReportProcessListener(ReportProcessListener listener) {
        listeners.remove(listener);
    }

    /**
     * ???????
     *
     * @param workbook 
     * @param reportBook 
     * @return ?????
     */
    private Set<String> expandTemplate(Workbook workbook, ReportBook reportBook) {

        Set<String> delSheetNames = new TreeSet<String>(Collections.reverseOrder());
        Set<String> useSheetNames = new HashSet<String>();

        // ????
        for (ReportSheet reportSheet : reportBook.getReportSheets()) {

            if (reportSheet != null) {
                if (reportSheet.getSheetName().equals(reportSheet.getTemplateName())) {
                    // ?????
                    int lastSheetIndex = workbook.getNumberOfSheets() - 1;
                    workbook.setSheetOrder(reportSheet.getSheetName(), lastSheetIndex);
                    useSheetNames.add(reportSheet.getTemplateName());
                } else {
                    int tempIdx = workbook.getSheetIndex(reportSheet.getTemplateName());
                    Sheet sheet = workbook.cloneSheet(tempIdx);
                    ReportsUtil.copyPrintSetup(workbook, tempIdx, sheet);
                    workbook.setSheetName(workbook.getSheetIndex(sheet), reportSheet.getSheetName());
                    delSheetNames.add(reportSheet.getTemplateName());
                }
            }
        }

        // ?
        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            Sheet sheet = workbook.getSheetAt(i);
            if (!isOutputSheet(sheet, reportBook)) {
                delSheetNames.add(sheet.getSheetName());
            }
        }

        delSheetNames.removeAll(useSheetNames);

        // ?????

        return delSheetNames;

    }

    /**
     * ???
     *
     * @param sheet 
     * @param reportBook 
     * @return
     */
    private boolean isOutputSheet(Sheet sheet, ReportBook reportBook) {
        for (ReportSheet reportSheet : reportBook.getReportSheets()) {
            if (reportSheet != null && sheet.getSheetName().equals(reportSheet.getSheetName())) {
                return true;
            }
        }
        return false;
    }

}