美文网首页Hybris
Defaultreferenceeditor中定制Referen

Defaultreferenceeditor中定制Referen

作者: cyclone_mi | 来源:发表于2018-07-16 16:02 被阅读0次

    问题

    最近有一个需求,客户在修改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

    相关文章

      网友评论

        本文标题:Defaultreferenceeditor中定制Referen

        本文链接:https://www.haomeiwen.com/subject/ddempftx.html