...
需求场景: 报表有个客户id字段, 每个业务人员负责一批客户(这个是动态的, 可能有不同批次), 业务人员需要在报表中筛选自己负责的某批客户. 因此期望筛选器的备选值是批次号, 表格真正刷新时, 是用该批次的客户id进行筛选.
来源jira: SMS-63446
...
是用该批次的客户id进行筛选。
接口说明
版本:since Hotfix_SmartbiV11_20250616
IBeforeQueryHandler
名称: 查询前处理器
...
更完整的用例, 请访问扩展包http://10.10.201.35:8888/RD/Extensions的 XPlayWrightExtensionTemplate 项目
1. 新建筛选器取数器, 用于自定义筛选器的备选值
代码块 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
package smartbi.smartbix.extension.custom.analysis;
import smartbi.smartbix.extension.custom.StandbyValueProvider;
import smartbix.analysis.query.AbstractCustomExecutor;
/**
* 即席透视列表筛选器(参数)取数器(命名不允许有双下划线)
*/
public class CustomPortletExecutor extends AbstractCustomExecutor {
@Override
public Object refresh() {
String keyword = this.getQueryOption().getKeyword();
return StandbyValueProvider.getStandbyValue(keyword);
}
}
|
代码块 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
package smartbi.smartbix.extension.custom; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import smartbix.util.SmartbiXObjectMapper; import smartbixlibs.com.fasterxml.jackson.databind.node.ArrayNode; import smartbixlibs.com.fasterxml.jackson.databind.node.ObjectNode; public final class StandbyValueProvider { private StandbyValueProvider() { } /** * 备选值 * @param keyword 搜索关键词 * @return 备选值 */ public static ObjectNode getStandbyValue(String keyword) { List<String> values = getRealValueList(keyword); ObjectNode result = SmartbiXObjectMapper.getInstance().createObjectNode(); ArrayNode data = result.putArray("data"); values.stream().forEach(v -> { ObjectNode col = data.addObject(); col.put("value", v); col.put("displayValue", v); }); return result; } private static List<String> getRealValueList(String keyword) { List<String> values = new ArrayList<>(); values.add("CUSTOM_X北_CUSTOM"); values.add("华南"); values.add("华东"); values.add("西南"); if (keyword == null || "".equals(keyword)) { return values; } return values.stream().filter(v -> v.indexOf(keyword) >= 0) .collect(Collectors.toList()); } } |
2. 新建查询前处理器, 用于修改被该筛选器影响的组件的条件值(比如批次号替换为一系列客户id)
代码块 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
package smartbi.smartbix.extension.custom;
import java.util.ArrayList;
import java.util.List;
import smartbix.query.handler.IBeforeQueryHandler;
import smartbix.query.handler.IQueryFieldConditionNode;
import smartbix.query.handler.IQueryParamConditionNode;
import smartbix.query.handler.QueryConditionUtil;
import smartbix.query.handler.QueryContext;
/**
* 组件查询前处理器
*/
public class BeforeQueryHandler implements IBeforeQueryHandler {
@Override
public void before(QueryContext context) {
QueryConditionUtil.handleCondition(context.getQueryOption().getQueryCondition(), (condition) -> {
if (condition instanceof IQueryFieldConditionNode) {
IQueryFieldConditionNode node = (IQueryFieldConditionNode) condition;
node.setValues(processConditionValues(node.getValues()));
} else if (condition instanceof IQueryParamConditionNode) {
IQueryParamConditionNode node = (IQueryParamConditionNode) condition;
node.setValues(processConditionValues(node.getValues()));
}
return condition;
});
}
private List<Object> processConditionValues(List<Object> oldValues) {
List<Object> newValues = new ArrayList<>();
oldValues.forEach(v -> {
if ("CUSTOM_X北_CUSTOM".equals(v)) {
newValues.add("华北");
newValues.add("东北");
newValues.add("西北");
} else {
newValues.add(v);
}
});
return newValues;
}
} |
...
3. 将上面两个处理器注册
代码块 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
package smartbi.smartbix.extension.custom;
import smartbi.framework.IModule;
import smartbi.framework.IShutdownHook;
import smartbix.query.handler.QueryHandlerManager;
/**
* 自定义取数器Module
*/
public class CustomExecutorModule implements IModule, IShutdownHook {
private static CustomExecutorModule instance = new CustomExecutorModule();
private BeforeQueryHandler handler = new BeforeQueryHandler();
/**
* @return CustomExecutorModule
*/
public static CustomExecutorModule getInstance() {
return instance;
}
@Override
public void shutdown() {
QueryHandlerManager.getInstance().remove("DETAILED_QUERY", handler);
QueryHandlerManager.getInstance().remove("AD_HOC_ANALYSIS", handler);
QueryHandlerManager.getInstance().removeExecutor("Custom_Portlet_Executor");
}
@Override
public void activate() {
QueryHandlerManager.getInstance().add("DETAILED_QUERY", handler);
QueryHandlerManager.getInstance().add("AD_HOC_ANALYSIS", handler);
QueryHandlerManager.getInstance().addExecutor("Custom_Portlet_Executor", smartbi.smartbix.extension.custom.analysis.CustomPortletExecutor.class);
}
|
...
} |
4. 前端二开设置筛选器取数时, 使用自定义的取数器
代码块 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// 透视
this.on(AD_HOC_FILTER_ON_INIT, (filter, iAdHocAnalysis) => {
filter.setQueryType('Custom_Portlet_Executor')
}) |