Przeglądaj źródła

Updates docs/storage-datasource-config.md

Auto commit by GitBook Editor
master
高杰 7 lat temu
rodzic
commit
be027229ff
  1. BIN
      docs/images/datasource-tools.png
  2. 258
      docs/storage-datasource-config.md

BIN
docs/images/datasource-tools.png

Binary file not shown.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 6.5 KiB

258
docs/storage-datasource-config.md

@ -8,5 +8,261 @@ If the project runs based on tomcat, there will be no directory of “ureportfil
Start running the project created, open the report designer, click the save button on the toolbar and select “save”. Then we can see the pop-up window as shown in the figure below:![](docs/images/save-dialog.png)
![](docs/images/save-dialog.png)![](docs/images/save-dialog.png)![](docs/images/save-dialog.png)
![](docs/images/save-dialog.png)![](/docs/images/save-dialog.png)We only need to input the name of the report and select the storage directory of the report in this window to save the current report file. We can see that the default storage location of UReport2 is the “server file system”, i.e. the “ureportfiles” directory under WEB-INF directory of our project.
This directory is automatically generated by the system by default. We may change the location of the directory by adding an attribute if needed.
Create a file config.properties under the WEB-INF directory of the project, open the spring configuration file named context.xml under WEB-INF and add the following Bean to load the file config.properties.
```
<bean id="propertyConfigurer" parent="ureport.props">
<property name="location">
<value>/WEB-INF/config.properties</value>
</property>
</bean>
```
**Note: **The above Bean configuraton is practically a standard configuration of org.springframework.beans.factory.config.PropertyPlaceholderConfigurer class. We know that we can load external properties of Spring by configuring PropertyPlaceholderConfigurer. If the project has been configured with PropertyPlaceholderConfigurer class, the corresponding UReport2 properties can be directly added to the properties file corresponding to the class of the configuration, and it is no need to configure the config.properties file.
Open the file config.properties and add the attribute named “ureport.fileStoreDir”, while the value of the attribute is applied to define the default file system-based report storage directory provided by UReport2. For example, the attribute can be defined as follows:
```
ureport.fileStoreDir=D:/ureportfiles
```
It means to save the report files in the directory ureportfiles under the Drive D. It shall be noted that when the specific directory is assigned, we shall save the directory and assure its existence, otherwise it will not be adopted. For an instance, the directory named ureportfiles in Drive D must exist and needs to be created in advance.
The report storage mechanism known as the “server file system” provided by UReport2 by fault actually realizes the interface of com.bstek.ureport.provider.report.Report Provider provided by UReport2. The source code of the interface is as follows:
```
package com.bstek.ureport.provider.report; import java.io.InputStream;
import java.util.List;
/**
* @author Jacky.gao
* @since 2016年12月4日
* @since Dec. 4 2016
*/
public interface ReportProvider {
/**
* 根据报表名加载报表文件
* Load the report file according to the report name
*@param file 报表名称
* @param file report name
*@return 返回的InputStream
* @return returned InputStream
*/
InputStream loadReport(String file);
/**
* 根据报表名,删除指定的报表文件
* Delete the specific report file according to the report name
* @param file 报表名称
* @param file report name
*/
void deleteReport(String file);
/**
* 获取所有的报表文件
* Acquire all report files
* @return 返回报表文件列表
* @return return the list of report file
*/
List<ReportFile> getReportFiles();
/**
* 保存报表文件
* Save report files
*@param file 报表名称
* @param file report name
*@param content 报表的XML内容
* @param content XML content of report
*/
void saveReport(String file,String content);
/**
* @return 返回存储器名称
* @return return storage name
*/
String getName();
/**
* @return 返回是否禁用
* @return return if deactivation
*/
boolean disabled();
/**
* @return 返回报表文件名前缀
* @return return prefix of report name
*/
String getPrefix();
}
```
After realizing the ReportProvider interface, configure the implementation class to Spring and make it a standard Spring Bean. Then UReport2 will detect, find and load it. The source code of the report storage known as the “server file system” provided by UReport2 by default is listed below:
```
package com.bstek.ureport.provider.report.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;
import com.bstek.ureport.exception.ReportException;
import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
/**
* @author Jacky.gao
* @since 2017年2月11日
*/
public class FileReportProvider implements ReportProvider,ApplicationContextAware{
private String prefix="file:";
private String fileStoreDir;
private boolean disabled;
@Override
public InputStream loadReport(String file) {
if(file.startsWith(prefix)){
file=file.substring(prefix.length(),file.length());
}
String fullPath=fileStoreDir+"/"+file;
try {
return new FileInputStream(fullPath);
} catch (FileNotFoundException e) {
throw new ReportException(e);
}
}
@Override
public void deleteReport(String file) {
if(file.startsWith(prefix)){
file=file.substring(prefix.length(),file.length());
}
String fullPath=fileStoreDir+"/"+file;
File f=new File(fullPath);
if(f.exists()){
f.delete();
}
}
@Override
public List<ReportFile> getReportFiles() {
File file=new File(fileStoreDir);
List<ReportFile> list=new ArrayList<ReportFile>();
for(File f:file.listFiles()){
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(f.lastModified());
list.add(new ReportFile(f.getName(),calendar.getTime()));
}
Collections.sort(list, new Comparator<ReportFile>(){
@Override
public int compare(ReportFile f1, ReportFile f2) {
return f2.getUpdateDate().compareTo(f1.getUpdateDate());
}
});
return list;
}
@Override
public String getName() {
return "服务器文件系统";
}
@Override
public void saveReport(String file,String content) {
if(file.startsWith(prefix)){
file=file.substring(prefix.length(),file.length());
}
String fullPath=fileStoreDir+"/"+file;
FileOutputStream outStream=null;
try{
outStream=new FileOutputStream(new File(fullPath));
IOUtils.write(content, outStream,"utf-8");
}catch(Exception ex){
throw new ReportException(ex);
}finally{
if(outStream!=null){
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public boolean disabled() {
return disabled;
}
public void setDisabled(boolean disabled) {
this.disabled = disabled;
}
public void setFileStoreDir(String fileStoreDir) {
this.fileStoreDir = fileStoreDir;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
File file=new File(fileStoreDir);
if(file.exists()){
return;
}
if(applicationContext instanceof WebApplicationContext){
WebApplicationContext context=(WebApplicationContext)applicationContext;
ServletContext servletContext=context.getServletContext();
String basePath=servletContext.getRealPath("/");
fileStoreDir=basePath+fileStoreDir;
file=new File(fileStoreDir);
if(!file.exists()){
file.mkdirs();
}
}
}
@Override
public String getPrefix() {
return prefix;
}
}
```
Disable the default report storage provided by the system. If we customize the report storage and do not want to use the default report storage of “server file system” provided by the system, we only needs to add an attribute named ureport.disableFileProvider in the config.properties file mentioned above and set its value as true.
In view of the introduction above, we can easily develop other types of report storages by realizing the ReportProvider interface, such as developing a new report storage to save reports in database and FTP etc.
# The data source Configuration
Open the report designer in UReport2. We can see three types of data sources of reports provided by UReport2, as shown in the figure below:
![](docs/images/datasource-tools.png)![](docs/images/datasource-tools.png)![](docs/images/datasource-tools.png)
![](docs/images/datasource-tools.png)

Ładowanie…
Anuluj
Zapisz