Defaultreferenceeditor中定制ReferenceSearchCondition控制初始值

问题

最近有一个需求,客户在修改Customer的用户组时,需要有一定的筛选条件。先看一下OOTB修改Customer时,默认界面如下:


屏幕快照 2018-07-16 上午10.45.13.png

把所有用户组都给客户展示了,没有必要也不方便,查看WIKI发现有配置描述如下:


屏幕快照 2018-07-16 上午10.47.35.png

在xxx-backoffice-config.xml加上试试,用CustomerList Item Code为例:

<config xmlns="http://www.hybris.com/cockpit/config"
        xmlns:editorArea="http://www.hybris.com/cockpitng/component/editorArea">
    
    <context merge-by="type" parent="Principal" type="User" component="cs-editor-area" module="customersupportbackoffice">
        <editorArea:editorArea name="">
            <editorArea:tab name="customersupport_backoffice_customer_tab_profile" position="1" tooltipText="customersupport_backoffice_customer_tab_profile_desc"
                merge-mode="replace">
                <editorArea:section name="hmc.properties">
                    <editorArea:attribute qualifier="sessionLanguage" position="1"/>
                    <editorArea:attribute qualifier="sessionCurrency" position="2"/>
                    <editorArea:attribute qualifier="groups" position="3">
                        <editorArea:editor-parameter>
                            <editorArea:name>restrictToType</editorArea:name>
                            <editorArea:value>CustomerList</editorArea:value>
                        </editorArea:editor-parameter>
                    </editorArea:attribute>
                    <editorArea:attribute qualifier="description" position="4"/>
                </editorArea:section>
            </editorArea:tab>
        </editorArea:editorArea>
    </context>

</config>

F4 Reset UI,效果如下:


屏幕快照 2018-07-16 上午10.51.46.png

目前为止还好,假如目前只想要instoreCustomers和bopisCustomers呢?
查看WIKI,发现可以Narrowing Search Result,大体介绍如下:

It is possible to narrow the search results for every Reference Selector by adding relevant configuration settings.

The main possibilities to limit the results are either by reference or by value. The references or values can be compared by different operators that come from the ValueComparisonOperator. If the operator is not specified, the default one is used: EQUALS.

加上一个试试:

<config xmlns="http://www.hybris.com/cockpit/config"
        xmlns:editorArea="http://www.hybris.com/cockpitng/component/editorArea">

    <context merge-by="type" parent="Principal" type="User" component="cs-editor-area" module="customersupportbackoffice">
        <editorArea:editorArea name="">
            <editorArea:tab name="customersupport_backoffice_customer_tab_profile" position="1" tooltipText="customersupport_backoffice_customer_tab_profile_desc"
                merge-mode="replace">
                <editorArea:section name="hmc.properties">
                    <editorArea:attribute qualifier="sessionLanguage" position="1"/>
                    <editorArea:attribute qualifier="sessionCurrency" position="2"/>
                    <editorArea:attribute qualifier="groups" position="3">
                        <editorArea:editor-parameter>
                            <editorArea:name>restrictToType</editorArea:name>
                            <editorArea:value>CustomerList</editorArea:value>
                        </editorArea:editor-parameter>
                        <editorArea:editor-parameter>
                            <editorArea:name>referenceSearchCondition_uid_equals</editorArea:name>
                            <editorArea:value>instoreCustomers</editorArea:value>
                        </editorArea:editor-parameter>
                    </editorArea:attribute>
                    <editorArea:attribute qualifier="description" position="4"/>
                </editorArea:section>
            </editorArea:tab>
        </editorArea:editorArea>
    </context>

</config>

F4刷新UI,效果如下:


屏幕快照 2018-07-16 上午11.05.54.png

So far so good~在来一个,加一个用户组,用OR连接,代码如下:

<config xmlns="http://www.hybris.com/cockpit/config"
        xmlns:editorArea="http://www.hybris.com/cockpitng/component/editorArea">
    <context merge-by="type" parent="Principal" type="User" component="cs-editor-area" module="customersupportbackoffice">
        <editorArea:editorArea name="">
            <editorArea:tab name="customersupport_backoffice_customer_tab_profile" position="1" tooltipText="customersupport_backoffice_customer_tab_profile_desc"
                merge-mode="replace">
                <editorArea:section name="hmc.properties">
                    <editorArea:attribute qualifier="sessionLanguage" position="1"/>
                    <editorArea:attribute qualifier="sessionCurrency" position="2"/>
                    <editorArea:attribute qualifier="groups" position="3">
                        <editorArea:editor-parameter>
                            <editorArea:name>restrictToType</editorArea:name>
                            <editorArea:value>CustomerList</editorArea:value>
                        </editorArea:editor-parameter>
                        <editorArea:editor-parameter>
                            <editorArea:name>referenceSearchCondition_uid_equals</editorArea:name>
                            <editorArea:value>instoreCustomers</editorArea:value>
                        </editorArea:editor-parameter>
                        <editorArea:editor-parameter>
                            <editorArea:name>referenceSearchCondition_uid_equals</editorArea:name>
                            <editorArea:value>bopisCustomers</editorArea:value>
                        </editorArea:editor-parameter>
                    </editorArea:attribute>
                    <editorArea:attribute qualifier="description" position="4"/>
                </editorArea:section>
            </editorArea:tab>
        </editorArea:editorArea>
    </context>
</config>

看下结果:


屏幕快照 2018-07-16 上午11.57.47.png

貌似结果不如期望,只留下了最后一个配置的条件,像极了Map的Key或者Set的的唯一性导致被覆盖的情况,査一下代码确实如此,Editor的参数是按照Map传入的,所以针对此需求,Narrow Search Result不能很好的支持。


屏幕快照 2018-07-16 下午3.11.43.png

OOTB代码如下:
public List<SearchQueryCondition> getAllSearchQueryConditions(String textQuery, Map<String, Object> searchConditions, SimpleSearch simpleSearch, Map<String, Object> contextMap) {
        List<SearchQueryCondition> queryConditions = (List)searchConditions.keySet().stream().filter((key) -> {
            return !key.equals("referenceSearchConditionGroupOperator");
        }).map((key) -> {
            SearchQueryCondition searchQueryCondition = new SearchQueryCondition();
            searchQueryCondition.setOperator(this.getConditionOperator(key));
            searchQueryCondition.setValue(this.getValue(searchConditions, contextMap, key));
            String qualifier = StringUtils.substringBeforeLast(key, "_");
            searchQueryCondition.setDescriptor(new SearchAttributeDescriptor(qualifier));
            return searchQueryCondition;
        }).collect(Collectors.toList());
        SearchQueryConditionList queryConditionList = new SearchQueryConditionList(this.getSearchOperator(searchConditions), queryConditions);
        SearchQueryConditionList textConditionList = this.getTextQuery(textQuery, simpleSearch);
        if (CollectionUtils.isNotEmpty(queryConditions) && textConditionList != null) {
            return Arrays.asList(queryConditionList, textConditionList);
        } else {
            return textConditionList != null ? Collections.singletonList(textConditionList) : Collections.singletonList(queryConditionList);
        }
    }

再查查WIKI,还有如下配置:


屏幕快照 2018-07-16 下午3.15.42.png

灵活到可以指定到Bean,理论上所有需求都可以满足了,做个简单的例子,主要代码如下:

<config xmlns="http://www.hybris.com/cockpit/config"
        xmlns:editorArea="http://www.hybris.com/cockpitng/component/editorArea">
    <context merge-by="type" parent="Principal" type="User" component="cs-editor-area" module="customersupportbackoffice">
        <editorArea:editorArea name="">
            <editorArea:tab name="customersupport_backoffice_customer_tab_profile" position="1" tooltipText="customersupport_backoffice_customer_tab_profile_desc"
                merge-mode="replace">
                <editorArea:section name="hmc.properties">
                    <editorArea:attribute qualifier="sessionLanguage" position="1"/>
                    <editorArea:attribute qualifier="sessionCurrency" position="2"/>
                    <editorArea:attribute qualifier="groups" position="3">
                        <editorArea:editor-parameter>
                            <editorArea:name>restrictToType</editorArea:name>
                            <editorArea:value>CustomerList</editorArea:value>
                        </editorArea:editor-parameter>
                        <editorArea:editor-parameter>
                            <editorArea:name>availableValuesProvider</editorArea:name>
                            <editorArea:value>demoReferenceSearchFacade</editorArea:value>
                        </editorArea:editor-parameter>
                    </editorArea:attribute>
                    <editorArea:attribute qualifier="description" position="4"/>
                </editorArea:section>
            </editorArea:tab>
        </editorArea:editorArea>
    </context>
</config>

spring bean的配置:

<alias name="demoReferenceSearchFacade" alias="demoReferenceSearchFacade"/>
    <bean id="demoReferenceSearchFacade" class="com.demo.customer.DemoCustomerUserGroupReferenceSearchFacade" parent="backofficeReferenceEditorSearchFacade">
        <property name="wishToSeeGroups">
            <list>
                <value>instoreCustomers</value>
                <value>bopisCustomers</value>
            </list>
        </property>
    </bean>

Java实现:

public class DemoCustomerUserGroupReferenceSearchFacade extends SampleReferenceEditorSearchFacade<PrincipalGroupModel>
{
    private List<String> wishToSeeGroups = new ArrayList<>();

    @Override
    public Pageable<PrincipalGroupModel> search(final SearchQueryData searchQueryData)
    {
        Pageable<PrincipalGroupModel> tempResult = super.search(searchQueryData);
        List<PrincipalGroupModel> result = tempResult.getAllResults().stream().filter(principalGroupModel -> wishToSeeGroups.contains(principalGroupModel.getUid()))
                .collect(Collectors.toList());
        return new PageableList<>(result, searchQueryData.getPageSize(), PrincipalGroupModel._TYPECODE);
    }

    public List<String> getWishToSeeGroups()
    {
        return wishToSeeGroups;
    }

    @Required
    public void setWishToSeeGroups(List<String> wishToSeeGroups)
    {
        this.wishToSeeGroups = wishToSeeGroups;
    }
}

在自己的backoffice extension中ant build, 点击hot redeploy按钮,F4 Reset UI之后看下效果:


屏幕快照 2018-07-16 下午3.22.35.png

完成!

另外ReferenceEditorSearchConditionHandler也是Spring管理的接口,也可以通过定制此接口满足要求,比如目前的Pattern就是referenceSearchCondition_[fieldname][operation],对于此需求当然我们也可以定义自己的Pattern来避开共同Key的限制,比如1|referenceSearchCondition[fieldname]_[operation]这种,在覆盖ReferenceEditorSearchConditionHandler类做解析的逻辑。

总结下来很多功能其实在Hybris中已有提供,同时也提供了很多可以扩展的接口,我们能做的就是尽量寻找官方提供的最佳实践达到事半功倍的效果。

完整代码在:https://github.com/baogang/narrowdownsearchdemo

Enjoy code, enjoy hybris.

参考资料:https://help.hybris.com/6.7.0/hcd/8bab997f86691014852cfbeabd21a5c8.html

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,019评论 3 119
  • 夜里十一点半,两个人在书房里面对面,一人一个电脑加班。 窗户外面是还没有竣工的二期高层,黑森森的一片,一片寂静,正...
    stonerain阅读 148评论 0 0
  • 你是天, 明朗、清澈、宽广。 你是地, 厚重、踏实、有希望。 你是风, 烦躁、暴戾、飘荡。 你是雨, 忧郁、冷漠、...
    三毛不想再流浪阅读 226评论 0 2
  • 前言 前两篇主要讲解了一些理论,今天我们举一些简单的例子。全部上实例。 任务 学会举一反三。 Data Query...
    yzzCool阅读 407评论 0 1