UReport2报表引擎:自定义报表存储器(三)
一、默認(rèn)報(bào)表存儲(chǔ)器
UReport2默認(rèn)提供的名為“服務(wù)器文件系統(tǒng)”的報(bào)表存儲(chǔ)機(jī)制,實(shí)際上是實(shí)現(xiàn)了UReport2提供的com.bstek.ureport.provider.report.ReportProvider接口。接口源碼如下:
public interface ReportProvider {
/**
* 根據(jù)報(bào)表名加載報(bào)表文件
* @param file 報(bào)表名稱
* @return 返回的InputStream
*/
InputStream loadReport(String file);
/**
* 根據(jù)報(bào)表名,刪除指定的報(bào)表文件
* @param file 報(bào)表名稱
*/
void deleteReport(String file);
/**
* 獲取所有的報(bào)表文件
* @return 返回報(bào)表文件列表
*/
List<ReportFile> getReportFiles();
/**
* 保存報(bào)表文件
* @param file 報(bào)表名稱
* @param content 報(bào)表的XML內(nèi)容
*/
void saveReport(String file,String content);
/**
* @return 返回存儲(chǔ)器名稱
*/
String getName();
/**
* @return 返回是否禁用
*/
boolean disabled();
/**
* @return 返回報(bào)表文件名前綴
*/
String getPrefix();
}
實(shí)現(xiàn)了ReportProvider接口后,只需要將實(shí)現(xiàn)類配置到Spring中,讓其成為一個(gè)標(biāo)準(zhǔn)的Spring Bean,這樣UReport2就會(huì)檢測到它而將其加載。
如果想要禁用系統(tǒng)提供的默認(rèn)報(bào)表存儲(chǔ)器,只需要把ureport2-core包下的ureport.properties文件拷貝一份放到自己的根目錄下:
配置文件內(nèi)容如下:
ureport.disableHttpSessionReportCache=false ureport.disableFileProvider=true ureport.fileStoreDir=/WEB-INF/ureportfiles ureport.debug=true
將ureport.disableFileProvider改成true,即可禁用默認(rèn)報(bào)表存儲(chǔ)器。
二、自定義報(bào)表存儲(chǔ)器
如果想要定義自己的報(bào)表存儲(chǔ)器,只需要實(shí)現(xiàn)了ReportProvider接口后,并將實(shí)現(xiàn)類配置到Spring中,讓其成為一個(gè)標(biāo)準(zhǔn)的Spring Bean,這樣UReport2就會(huì)檢測到它而將其加載。
1、表結(jié)構(gòu)設(shè)計(jì)
| 字段名 | 類型 | 描述 |
| id | varchar(32) primary key | 主鍵 |
| name | varchar(100) | 報(bào)表名稱 |
| content | mediumblob | 報(bào)表內(nèi)容 |
| create_time | timestamp | 創(chuàng)建時(shí)間 |
| update_time | timestamp | 更新時(shí)間 |
2、需要的方法
public interface ReportFileService extends IService<UReportFile> {
/**
* 根據(jù)報(bào)表名稱檢查報(bào)表是否存在
*
* @param name 報(bào)表名稱
*/
boolean checkExistByName(String name);
/**
* 根據(jù)報(bào)表名稱查詢報(bào)表
*
* @param name 報(bào)表名稱
*/
UReportFile getReportFileByName(String name);
/**
* 查詢?nèi)繄?bào)表
*/
List<UReportFile> listAllReportFile();
/**
* 根據(jù)報(bào)表名稱刪除報(bào)表
*
* @param name 報(bào)表名稱
*/
boolean removeReportFileByName(String name);
/**
* 保存報(bào)表
*/
boolean saveReportFile(UReportFile entity);
/**
* 更新報(bào)表
*/
boolean updateReportFile(UReportFile entity);
}
3、自定義報(bào)表存儲(chǔ)器
@Component
@Setter
public class MySQLProvider implements ReportProvider {
/**
* 存儲(chǔ)器名稱
*/
private static final String NAME = "mysql-provider";
/**
* 報(bào)表文件名前綴
*/
private String prefix = "mysql:";
/**
* 是否禁用
*/
private boolean disabled = false;
@Autowired
private ReportFileService reportFileService;
/**
* 根據(jù)報(bào)表名加載報(bào)表文件
*
* @param file 報(bào)表名稱
* @return 返回的InputStream
*/
@Override
public InputStream loadReport(String file) {
UReportFile reportFile = reportFileService.getReportFileByName(getCorrectName(file));
ByteArrayInputStream stream = null;
if (ObjectUtil.isNotEmpty(reportFile)) {
try {
stream = new ByteArrayInputStream(reportFile.getContent());
} catch (Exception e) {
throw new ReportException(e);
}
}
return stream;
}
/**
* 根據(jù)報(bào)表名,刪除指定的報(bào)表文件
*
* @param file 報(bào)表名稱
*/
@Override
public void deleteReport(String file) {
reportFileService.removeReportFileByName(getCorrectName(file));
}
/**
* 獲取所有的報(bào)表文件
*
* @return 返回報(bào)表文件列表
*/
@Override
public List<ReportFile> getReportFiles() {
List<UReportFile> list = reportFileService.listAllReportFile();
List<ReportFile> reportList = new ArrayList<>();
for (UReportFile file : list) {
reportList.add(new ReportFile(file.getName(), file.getUpdateTime()));
}
return reportList;
}
/**
* 保存報(bào)表文件
*
* @param file 報(bào)表名稱
* @param content 報(bào)表的XML內(nèi)容
*/
@Override
public void saveReport(String file, String content) {
file = getCorrectName(file);
UReportFile reportFile = reportFileService.getReportFileByName(file);
if(ObjectUtil.isEmpty(reportFile)){
reportFile = new UReportFile();
reportFile.setName(file);
reportFile.setContent(content.getBytes());
reportFileService.save(reportFile);
}else{
reportFile.setContent(content.getBytes());
reportFileService.updateReportFile(reportFile);
}
}
/**
* @return 返回存儲(chǔ)器名稱
*/
@Override
public String getName() {
return NAME;
}
/**
* @return 返回是否禁用
*/
@Override
public boolean disabled() {
return disabled;
}
/**
* @return 返回報(bào)表文件名前綴
*/
@Override
public String getPrefix() {
return prefix;
}
/**
* 獲取沒有前綴的文件名
*
* @param name 報(bào)表名稱
*/
private String getCorrectName(String name){
if(name.startsWith(prefix)){
name = name.substring(prefix.length());
}
return name;
}
}
啟動(dòng)項(xiàng)目,查看效果
總結(jié)
以上是生活随笔為你收集整理的UReport2报表引擎:自定义报表存储器(三)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (二)软件测试分类
- 下一篇: ubuntu下创建python的虚拟环境