自定义参数函数-获取当前用户的角色
1 概述
Smartbi的参数在设置备选值时,支持选择【函数】,而函数可支持用户自定义添加。下文以 "获取当前用户的角色"为例,创建自定义参数函数,获取当前用户的角色作为参数的备选值。
注:创建自定义参数需要使用Smartbi扩展包(插件包)开发。
2 自定义参数函数
Smartbi自定义参数函数是按照以下规范实现对应接口,然后可以通过扩展Parameter.js,将自定义参数函数添加到函数选择的备选项中,供用户使用。
1,自定义参数函数可继承类:smartbi.freequery.expression.function.ParamFunction;
2,这个实现类必须放在smartbi.freequery.expression.function包下面,因为系统运行自定义参数函数时,会自动加上这个包路径查找函数对应的实际类。
3,扩展Paramer.js,将自定义参数函数的类名添加到函数选择的备选项中。
4,参数默认值一般只能返回1条数据,通常要另外创建一个自定义参数函数类,使其只返回1条数据。

2.1 编写自定义参数函数类GetParamDemo.java与GetParamDemoUnique.java
(1)创建GetParamDemo.java,继承smartbi.freequery.expression.function.ParamFunction,实现getGridData方法,获取当前用户的所有角色,组成表格数据返回即可。(作为参数备选值)
(2)创建GetParamDemoUnique.java,继承smartbi.freequery.expression.function.ParamFunction,实现getGridData方法,获取当前用户的一个角色,组成表格数据返回即可。(作为参数默认值,参数默认值一般只有一条数据)
注:自定义参数函数类依赖的jar包为:smartbi-Parameter.jar,可在Smartbi的war包中获取到:smartbi.war\WEB-INF\lib(Smartbi依赖的相关jar均在此目录)
import smartbi.freequery.querydata.GridData;
import smartbi.param.IParameter;
import smartbi.param.IParameterPanel;
import smartbi.user.IRole;
import smartbi.usermanager.UserManagerModule;
public class GetParamDemo extends ParamFunction {
@Override
public GridData getGridData(IParameter param, IParameterPanel panel) {
// 获取当前用户所有角色
List<? extends IRole> roles = UserManagerModule.getInstance().getAllRolesOfCurrentUser();
GridData gridData = new GridData(); // 初始化表格对象
ArrayList<String> stringHeaders = new ArrayList<String>();
stringHeaders.add("value");
stringHeaders.add("displayvalue");
gridData.setStringHeaders(stringHeaders); // 设置表头
List<List<CellData>> data = new ArrayList<List<CellData>>();
for (IRole role : roles) { // 将角色信息组成一个二维表对象
List<CellData> rowdata = new ArrayList<CellData>();
data.add(rowdata);
CellData tmpCell = new CellData();
tmpCell.setStringValue(role.getName());
tmpCell.setDisplayValue(role.getName());
rowdata.add(tmpCell);
tmpCell = new CellData();
tmpCell.setStringValue(role.getName());
tmpCell.setDisplayValue(role.getName());
rowdata.add(tmpCell);
}
gridData.setData(data); // 设置表格内容
return gridData;
}
}
package smartbi.freequery.expression.function;
import java.util.ArrayList;
import java.util.List;
import smartbi.freequery.querydata.CellData;
import smartbi.freequery.querydata.GridData;
import smartbi.param.IParameter;
import smartbi.param.IParameterPanel;
import smartbi.user.IRole;
import smartbi.usermanager.UserManagerModule;
public class GetParamDemoUnique extends ParamFunction {
@Override
public GridData getGridData(IParameter param, IParameterPanel panel) {
// 获取当前用户所有角色
List<? extends IRole> roles = UserManagerModule.getInstance().getAllRolesOfCurrentUser();
GridData gridData = new GridData(); // 初始化表格对象
ArrayList<String> stringHeaders = new ArrayList<String>();
stringHeaders.add("value");
stringHeaders.add("displayvalue");
gridData.setStringHeaders(stringHeaders); // 设置表头
List<List<CellData>> data = new ArrayList<List<CellData>>();
if (roles.size() > 0) {
IRole role = roles.get(0); // 只取第一个数据
List<CellData> rowdata = new ArrayList<CellData>();
data.add(rowdata);
CellData tmpCell = new CellData();
tmpCell.setStringValue(role.getName());
tmpCell.setDisplayValue(role.getName());
rowdata.add(tmpCell);
tmpCell = new CellData();
tmpCell.setStringValue(role.getName());
tmpCell.setDisplayValue(role.getName());
rowdata.add(tmpCell);
}
gridData.setData(data); // 设置表格内容
return gridData;
}
}
2.2扩展Parameter.js增加自定义参数函数
在扩展包中创建vision\js\freequery\sysresource目录,在该目录下创建Parameter.js.patch文件,重写Parameter.prototype.initControlsForPage2方法,将上一步创建的类名添加到对应的js对象中,即可将自定义参数函数添加到函数选择的备选项中。(如何重写js方法)
代码parameter.js
Parameter.prototype.initControlsForPage2_old190425 = Parameter.prototype.initControlsForPage2;
Parameter.prototype.initControlsForPage2 = function() {
var ret = this.initControlsForPage2_old190425.apply(this, arguments); // 先执行原方法逻辑
if(!this.ControlType || this.ControlType.selectedId != "DROPDOWNTREE"){
this.standbyFuncCombo.insertItem("GetParamDemo", "GetParamDemo"); // 添加到参数备选值函数选择的备选项中
}
this.defaultFuncCombo.insertItem("GetParamDemoUnique", "GetParamDemoUnique"); // 添加到参数默认值函数选择的备选项中
return ret;
}
3 自定义参数函数使用示例
(1)创建参数,设置参数备选值与默认值时,选择自定义的参数函数。

(2)创建可视化查询,加入上一步的参数,此时参数的默认值与备选值,将会取到自定义参数函数的返回值。


3 示例扩展包资源
扩展包:,扩展包源码:。