第1章 实现对报表展现、导出、打印事件进行记录

当需要对报表进行的各种操作做记录,以便导出或以后进行查看,可以通过对报表事件接口EventHandler进行扩展来满足自己应用定制的需要。

1.1 接口扩展

自定义一个事件记录类ReportEventHandler继承接口EventHandler,实现接口方法:void handler(ReportEvent event),在方法中实现自定义处理逻辑。其中:ReportEvent类保存着需要进行记录的信息,其中包括:
uid,用户ID,字符串型
reportId,报表ID,字符串型
reportName,报表名称,字符串型
desc,描述,字符串型
time,长整型
id,Event类型id,整型,具体的类型定义如下:

/*Event类型*/
    public final static int DOWNLOAD = 0;    //导入报表
    public final static int UPLOAD = 1;    //发布报表
    public final static int EXPORTER_HTML = 2;    //页面展现
    public final static int EXPORTER_EXCEL = 3;    //excel导出
    public final static int EXPORTER_RTF = 4;    //rtf导出
    public final static int EXPORTER_CSV = 5;    //csv导出
    public final static int EXPORTER_PDF = 6;    //pdf导出
    public final static int DATAINPUT = 7;    //数据回填
    public final static int SEND_MAIL = 8;    //发送即时mail
    public final static int SAVE = 9;    //收藏报表
    public final static int PRINT = 10;    //打印报表
    public final static int EXPORTER_MHT = 11;  //html 
    public static final int EXPORTER_ODS = 12;  // ods导出
    public static final int EXPORTER_TXT = 13;  // txt导出

getContext()方法,返回Map类型对象context,可用两个常量作为key值:REPORT_PARAMETERS和REQUEST。以REPORT_PARAMETERS为key得到一个Map对象,等同于getReportParameters()方法的返回值,而以REQUEST为key得到的是HttpServletRequest请求对象,进而可以得到本次请求的参数等。

两个常量的定义如下:

public static final String REPORT_PARAMETERS="reportParameters";
public static final String REQUEST="request";

getReportParameters()方法,得到包含所有报表参数的Map,以参数名称作为key,返回值都是string类型,需要在使用时转换为所需的类型。

1.2 将自定义接口实现类在业务系统中进行配置

在配置文件中配置接口属性,使报表事件记录接口的实现类可以在业务系统中生效。在文件report-engine-config.properties中配置属性com.neusoft.report.common.event.Handler为自定义实现类。

1.3 业务样例代码

1)自定义类ReportEventHandler继承接口EventHandler,实现接口方法,样例代码如下:

public class ReportDemoEventHandler implements EventHandler {
        public void handler(ReportEvent event) {}
}

2)在report-engine-config.properties文件中配置该接口的实现类。

com.neusoft.report.common.event.Handler=com.neusoft.report.common.event.ReportDemoEventHandler

3)在实现的接口方法中添加自定义处理逻辑,样例代码如下:

public class ReportDemoEventHandler implements EventHandler {
protected static final Logger logger = Logger
        .getLogger(ReportDemoEventHandler.class);

public void handler(ReportEvent event) {
    int eventType = event.getId();  //事件类型
    String uid = event.getUid();    //用户ID
    String reportId = event.getReportId();
    String reportName = event.getReportName();
    String desc = event.getDesc();
    event.getTime();
    long milTime = event.getTime();
    String time = null;

    try{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        time = format.format(new Date(milTime));
    }catch (Exception e) {}

    String[] info = new String[] {uid, time, reportName, reportId, desc};
    switch (eventType) {
    case ReportEvent.DOWNLOAD:  //报表下载
        logger.info(createMessage(
                "{1}在{2}下载了报表{3}_{4}({5})", info));
        break;
    case ReportEvent.EXPORTER_PDF:    //报表导出PDF
        logger.info(createMessage(
                "{1}在{2}导出了报表{3}_{4}({5})", info));
        break;
    case ReportEvent.PRINT:         //报表打印
        logger.info(createMessage(
"{1}在{2}打印了报表{3}_{4}({5})",info));
        break;
    }
}
public String createMessage(String message, String[] info){
    String result = message;
    for(int i = 0; i < info.length; i++){
        String parrent = "{"+(i+1)+ "}";
        result = result.replace(parrent,info[i]);
    }
    return result;
}
}

4)执行报表打印,日志记录如图1所示

1

results matching ""

    No results matching ""

    results matching ""

      No results matching ""