Creation and Use of UI Configuration Merging Mechanism

For better granularity of Backoffice configuration snippets, implement a mechanism that merges your configuration.

To understand better how the configuration merging mechanism works, consider the following example. The following configuration snippet for type="Product" is already in place:

cockpit-config.xml

<context component="cockpitSearchComponent">
	<context type="Product">
		<bo:cockpit-search xmlns:bo="http://www.hybris.com/cockpit/config/backoffice">
			<bo:field attribute="code"/>
			<bo:field attribute="name"/>
			<bo:field attribute="description"/>
		</bo:cockpit-search>
	</context>
</context>

Now, you have a subtype of the Product type, called Shoe, which has some additional fields, like color and size. You can add a configuration snippet for that type to your configuration file like this:

cockpit-config.xml

<context component="cockpitSearchComponent">
	<context type="Shoe">
		<bo:cockpit-search xmlns:bo="http://www.hybris.com/cockpit/config/backoffice">
			<bo:field attribute="code"/>
			<bo:field attribute="name"/>
			<bo:field attribute="description"/>
			<bo:field attribute="color"/>
			<bo:field attribute="size"/>
		</bo:cockpit-search>
	</context>
</context>

Even though it would work, you can avoid such a redundancy - Shoe type configuration had to repeat attributes of Product type. From technical point of view, you need to add a proper annotation in classes that you have created or generated for your custom widget configuration schema. In the annotation, declare a property key which indicates properties of the objects. These objects are defined in the collection to which the annotation is applied. The property key is used in the merging mechanism. In this tutorial, you will implement a merging mechanism, that will merge configuration by the key property, attribute, where the collection is the field element.

In our example you have two classes:

  • CockpitSearch: Represents cockpit-search element and defines possible subelements

    CockpitSearch.java

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = { "fields" })
    @XmlRootElement(name = "cockpit-search")
    public class CockpitSearch
    {
    	protected List<Field> field;
    									
    	public List<Field> getField()
    	{
    		if (field == null)
    		{
    			field = new ArrayList<Field>();
    		}
    		return this.field;
    	}
    }
  • Field: Represents field element

    Field.java

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "field", propOrder =
    { "attribute" })
    public class Field
    {
    	@XmlAttribute(name = "attribute", required = true)
    	protected String attribute;
    	@XmlAttribute(name = "sortable")
    	protected Boolean sortable;
    	@XmlAttribute(name = "asc")
    	protected Boolean asc;
    								
    	public String getAttribute()
    	{
    		return attribute;
    	}
    									
    	public void setAttribute(final String value)
    	{
    		this.attribute = value;
    	}
    									
    	public Boolean isSortable()
    	{
    		return sortable;
    	}
    									
    	public void setSortable(final Boolean value)
    	{
    		this.sortable = value;
    	}
    									
    	public Boolean isAsc()
    	{
    		return asc;
    	}
    									
    	public void setAsc(final Boolean value)
    	{
    		this.asc = value;
    	}
    }

Adding Annotations to Java Class

First step of creating and using UI Configuration Mechanism is adding annotations to java class.

Procedure

Add to CockpitSearch class the @Mergeable annotation:

CockpitSearch.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "fields" })
@XmlRootElement(name = "cockpit-search")
public class CockpitSearch
{
	@Mergeable(key = "attribute")  // This is it
	protected List<Field> field;

	public List<Field> getField()
	{
		if (field == null)
		{
			field = new ArrayList<Field>();
		}
		return this.field;
	}
}

The Field class does not need to be changed. Only if the field element would be more complex, having some subelements, you could make them mergeable as well.

Merging UI Configuration in XML

With the new merging mechanism, the initial UI configuration of your widget can be changed.

Procedure

Change the cockpit-config.xml file in the following way:

cockpit-config.xml

<context component="cockpitSearchComponent">
	<context type="Product">
		<bo:cockpit-search xmlns:bo="http://www.hybris.com/cockpit/config/backoffice">
		    <bo:field attribute="code"/>
		    <bo:field attribute="name"/>
		    <bo:field attribute="description"/>
		</bo:cockpit-search>
	</context>
</context>

<context component="cockpitSearchComponent">
	<!-- Pay attention to merge-by and parent properties -->
	<context type="Shoe" merge-by="type" parent="Product">
		<bo:cockpit-search xmlns:bo="http://www.hybris.com/cockpit/config/backoffice">
		    <bo:field attribute="color"/>
		    <bo:field attribute="size"/>
		</bo:cockpit-search>
	</context>
</context>

There are two new properties used in the context element:

  • merge-by: Using this property you configure by which context attribute the parent context should be searched. In this case, it is type.

  • parent: Using this property you specify the value for the context attribute defined by merge-by. The default value of this property is parent="auto".

Creation and Use of Merging Mechanism Using Merging Modes

There is an optional way of creating and using UI Configuration Merging mechanism. It can be done with the use of merging modes.

A merge mode is a string enumeration with the following possible values:

  • merge: The default mode. The matching element is merged with the parent element, and all subelements are processed according to their merging settings.

  • replace: The matching element is completely replaced by the new one with all its subelements.

  • remove: The matching element is removed.

Adding Additional Annotations

The Field class from previous steps, must be changed to allow merge modes to be present.

Procedure

Change the Field class in the following way:

Field.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "field", propOrder =
{ "attribute" })
public class Field
{
	@XmlAttribute(name = "attribute", required = true)
	protected String attribute;
	@XmlAttribute(name = "sortable")
	protected Boolean sortable;
	@XmlAttribute(name = "asc")
	protected Boolean asc;
	@XmlAttribute(name = "merge-mode")  
	protected String mergeMode;         // This is it, "mergeMode" is the default name for the
	                                    // merging mode attribute, you can use a different one and
	                                    // set it up in the @Mergeable annotation on the property
	                                    // holding the list of your elements

	public String getAttribute()
	{
		return attribute;
	}

	public void setAttribute(final String value)
	{
		this.attribute = value;
	}

	public Boolean isSortable()
	{
		return sortable;
	}

	public void setSortable(final Boolean value)
	{
		this.sortable = value;
	}

	public Boolean isAsc()
	{
		return asc;
	}

	public void setAsc(final Boolean value)
	{
		this.asc = value;
	}

	public String getMergeMode()
	{
		return mergeMode;
	}

	public void setMergeMode(final String value)
	{
		this.mergeMode = value;
	}
}

Merging UI Configuration in XML with Merge Modes

The cockpit-config.xml file needs to be changed to merge UI configuration with merge modes.

Context

The configuration for the Shoes is inherited from the Product configuration, meaning:

  • The name and the description attributes are removed.

  • The color and size attributes are added.

Not specifying the merging mode on an element defaults to the merging mode "merge".

Procedure

Change the configuration in the following way:

cockpit-config.xml

<context component="cockpitSearchComponent">
	<context type="Product">
		<bo:cockpit-search xmlns:bo="http://www.hybris.com/cockpit/config/backoffice">
		    <bo:field attribute="code"/>
		    <bo:field attribute="name"/>
		    <bo:field attribute="description"/>
		</bo:cockpit-search>
	</context>
</context>

<context component="cockpitSearchComponent">
	<context type="Shoe" merge-by="type" parent="Product">
		<bo:cockpit-search xmlns:bo="http://www.hybris.com/cockpit/config/backoffice">
		    <bo:field attribute="description" merge-mode="remove"/>
		    <bo:field attribute="name" merge-mode="remove"/>
		    <bo:field attribute="color"/>
		    <bo:field attribute="size"/>
		</bo:cockpit-search>
	</context>
</context>