Implementing a Custom Extended Type
Learn how to add the custom extended types to the cockpit type system and how to enhance a TypedObject with custom properties not related to item attributes, and transparently use them in the cockpit.
One of the big advantages of using the extended type mechanism is the possibility to add a dynamic set of property descriptors. It means they can change on run time, depending on certain conditions, for example belonging to a classifying category.
Creating a custom extended type can be separated into two parts: a type-related part and a value-related part. Both parts are covered in this tutorial. The first part describes how to create an extended type, its property descriptors, and a type loader which serves as the plugin for the ExtensibleTypeService. The second part shows how to load values for the extended type's property descriptors.
Creating an Extended Type
There are three classes needed to create an extended type: the type itself, its property descriptor class and a type loader class.
Extended Type
The extended type consists of the typecode, a name, and a set of property descriptors.
In the following code example, the type has the code CustomType and two property descriptors.
package ycockpitpackage.services.meta.impl;
import de.hybris.platform.cockpit.model.meta.PropertyDescriptor;
import de.hybris.platform.cockpit.model.meta.impl.AbstractExtendedType;
import java.util.HashSet;
import java.util.Set;
public class CustomType extends AbstractExtendedType
{
public final static String CUSTOM_TYPECODE = "CustomType";
@Override
public String getCode()
{
return CUSTOM_TYPECODE;
}
@Override
public Set<PropertyDescriptor> getDeclaredPropertyDescriptors()
{
final Set<PropertyDescriptor> ret = new HashSet<PropertyDescriptor>();
ret.add(new CustomPropertyDescriptor("Custom 1", CUSTOM_TYPECODE + ".customProperty1"));
ret.add(new CustomPropertyDescriptor("Custom 2", CUSTOM_TYPECODE + ".customProperty2"));
return ret;
}
@Override
public String getName()
{
return "Custom Type";
}
@Override
public String getName(final String languageIsoCode)
{
return getName();
}
}
There are some additional methods defined in the ExtendedType interface which could be interesting to override, such as subtype and supertype handling.
Custom Property Descriptor
It's not necessary to implement a custom property descriptor class, but it makes it easier to identify it later in the value handler.
The relevant attributes for a property descriptor are the qualifier, and the name, which is less important.
The property descriptor class in the following example class has a fixed editor type TEXT. That means that all of your custom properties are of type string.
package ycockpitpackage.services.meta.impl;
import de.hybris.platform.cockpit.model.meta.impl.AbstractPropertyDescriptor;
public class CustomPropertyDescriptor extends AbstractPropertyDescriptor
{
private final String name;
public CustomPropertyDescriptor(final String name, final String qualifier)
{
super();
this.name = name;
this.setQualifier(qualifier);
this.setEditorType(TEXT);
}
@Override
public boolean isWritable()
{
return true;
}
@Override
public boolean isReadable()
{
return true;
}
@Override
public String getName(final String languageIso)
{
return name;
}
@Override
public String getName()
{
return name;
}
@Override
public String getDescription()
{
return null;
}
}
Type Loader
Finally, the custom type has to be registered at the TypeService. This is done by the ExtendedTypeLoader.
The two most important methods in this interface are implemented in the following code example.
The method loadType(...) returns an instance of your custom type. The method getExtendedTypeCodes(...) connects your type to a typed object, in the example all typed objects that wrap instances of ItemModel or its subclasses.
Again there are some other methods that have default implantations in the AbstractTypeLoader. Refer to the API doc, if you need more information.
package ycockpitpackage.services.meta.impl;
import de.hybris.platform.cockpit.model.meta.ExtendedType;
import de.hybris.platform.cockpit.model.meta.TypedObject;
import de.hybris.platform.cockpit.services.meta.TypeService;
import de.hybris.platform.cockpit.services.meta.impl.AbstractTypeLoader;
import de.hybris.platform.core.model.ItemModel;
import java.util.Collections;
import java.util.Set;
public class CustomTypeLoader extends AbstractTypeLoader
{
@Override
public ExtendedType loadType(final String code, final TypeService typeService)
{
if (code.startsWith(CustomType.CUSTOM_TYPECODE))
{
return new CustomType();
}
return null;
}
@Override
public Set<String> getExtendedTypeCodes(final TypedObject item)
{
if (item.getObject() instanceof ItemModel)
{
return Collections.singleton(CustomType.CUSTOM_TYPECODE);
}
return null;
}
}
Create a Value Handler for Your Custom Properties
When you have your custom property descriptors attached to all item types, you need to define how to deal with the loading and storing of values. This is done with an ObjectValueHandler. For simplicity, the following example shows only how to load values, and adds a simple string as value to the value container.
package ycockpitpackage.services.values.impl;
import de.hybris.platform.cockpit.model.meta.ObjectType;
import de.hybris.platform.cockpit.model.meta.PropertyDescriptor;
import de.hybris.platform.cockpit.services.values.ObjectValueContainer;
import de.hybris.platform.cockpit.services.values.ObjectValueHandler;
import de.hybris.platform.cockpit.services.values.ValueHandlerException;
import java.util.Set;
import ycockpitpackage.services.meta.impl.CustomPropertyDescriptor;
public class CustomPropertyValueHandler implements ObjectValueHandler
{
@Override
public void loadValues(final ObjectValueContainer valueContainer, final ObjectType type, final Object source,
final Set<PropertyDescriptor> descriptors, final Set<String> languageIsoCodes) throws ValueHandlerException
{
for (final PropertyDescriptor propertyDescriptor : descriptors)
{
if (propertyDescriptor instanceof CustomPropertyDescriptor)
{
valueContainer.addValue(propertyDescriptor, null, "Some value for " + propertyDescriptor.getQualifier());
}
}
}
@Override
public void updateValues(final ObjectValueContainer container, final Set<String> languageIsoCodes)
throws ValueHandlerException
{
// NO OP
}
@Override
public void updateValues(final ObjectValueContainer container, final Set<String> languageIsoCodes,
final Set<PropertyDescriptor> descriptors) throws ValueHandlerException
{
// NO OP
}
@Override
public void storeValues(final ObjectValueContainer container) throws ValueHandlerException
{
// NO OP
}
@Override
public void storeValues(final ObjectValueContainer container, final boolean forceWrite) throws ValueHandlerException
{
// NO OP
}
}
Register Your Type Loader and Value Handler in Spring
The last step is to configure the type loader and value handler in the spring configuration file of the particular cockpit, for example in the ycockpit-spring-services.xml file.
The type loader has to be added to the extendedTypeLoaders list of the cockpitTypeLoaderChain. The value handler must be added to the ObjectValueHandlerMapping bean which is registered automatically at the value handler chain.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <alias alias="cockpitTypeLoaderChain" name="myCockpitTypeLoaderChain"/> <bean id="myCockpitTypeLoaderChain" parent="defaultCockpitTypeLoaderChain"> <property name="extendedTypeLoaders"> <list merge="true"> <bean class="ycockpitpackage.services.meta.impl.CustomTypeLoader" /> </list> </property> </bean> <bean id="customPropertyValueHandlerMapping" class="de.hybris.platform.cockpit.services.values.impl.ObjectValueHandlerMapping"> <property name="typeCode" value="Item"/> <property name="valueHandler" ref="customPropertyValueHandler"/> </bean> <bean id="customPropertyValueHandler" class="ycockpitpackage.services.values.impl.CustomPropertyValueHandler" /> </beans>
That's all. If you have added the unassigned section for the editor area configuration for some type, the two custom property descriptors should appear there if you open an item of this type.
You can add them in all configuration files for any ItemModel, for example in editor area configuration for product like in the following XML snippet:
<editor>
<group qualifier="General" visible="true" initially-opened="true">
<label lang="de">Stammdaten</label>
<label lang="en">Properties</label>
<property qualifier="product.code" />
<property qualifier="product.name" />
...
<property qualifier="CustomType.customProperty1" />
<property qualifier="CustomType.customProperty2" />
...
</group>
...