Extending the Media API
The Media API provides information about media attribute values. You can extend the Media API by exposing additional types and attributes.
There are three ways to extend the Media API:
- Adding queries to the existing FlexibleSearchNamedQueryService service.
- Adding new implementations of the NamedQueryService service.
- Exposing additional attributes of the Media DTO.
Adding Queries to the Existing FlexibleSearchNamedQueryService Service
If the query defined in the DefaultNamedQueryFactory does not suite your needs, you can extend the current namedQueryMap map to add new queries, as shown in the following code excerpt:
<alias alias="namedQueryFactory" name="customNamedQueryFactory" />
<bean id="customNamedQueryFactory" class="de.hybris.platform.cmswebservices.namedquery.service.impl.DefaultNamedQueryFactory" parent="defaultNamedQueryFactory">
<property name="namedQueryMap">
<map merge="true">
<entry key="customNamedQueryMediaSearchByCodeCatalogVersion" value="SELECT {pk} FROM {Media} WHERE LCASE({code}) LIKE LCASE(?code) AND {catalogVersion} = ?catalogVersion"/>
</map>
</property>
</bean>
Adding New Implementations of the NamedQueryService
If the current FlexibleSearchNamedQueryService implementation does not suite your needs (for example, create a NamedQueryService based on Solr search engine), you can create your own implementation as follows:
- Implement your own version of the NamedQueryService, for example: SolrNamedQueryService.
- Create a new Spring bean using the DefaultMediaFacade class and use the newly-implemented NamedQueryService instead, as shown in the following example:
<alias name="customMediaFacade" alias="mediaFacade" /> <bean id="customMediaFacade" class="de.hybris.platform.cmswebservices.media.facade.impl.DefaultMediaFacade" parent="defaultMediaFacade"> <property name="namedQueryService" ref="solrNamedQueryService" /> </bean>
Exposing Additional Attributes of the Media DTO
To expose additional attributes for the Media DTO, do the following:
- Create the DTO.
- Create the Populator.
- Provide the new Converter.
Create the DTO
You create the DTO by defining it and the properties required to support your implementation in the beans.xml file.
<bean class="com.mycompany.extendedapi.dto.CustomMediaData" extends="de.hybris.platform.cmswebservices.dto.MediaData">
<property name="content" type="String" />
</bean>
Create the Populator
To create the populator:
-
Create your populator by implementing the Populator interface as shown in the ExtendedComponentTypeAttributePopulator as shown in the following example:
public class CustomMediaPopulator implements Populator<MediaModel, CustomMediaData> { @Override public void populate(final MediaModel source, final CustomMediaData target) throws ConversionException { target.setContent(source.getContent()); } } -
Register your populator as a Spring bean in the spring.xml file as shown in the following example:
<bean id="customMediaModelPopulator" class="com.mycompany.extendedapi.media.facade.populator.CustomMediaPopulator" />
Provide the New Converter
To expose your customized converter, you must create a populator and define it in your spring.xml file, as shown in the following code example:
<!-- Converter -->
<alias name="customMediaModelConverter" alias="mediaModelConverter" />
<bean id="customMediaModelConverter" parent="abstractPopulatingConverter">
<property name="targetClass" value="com.mycompany.extendedapi.dto.CustomMediaData"/>
<property name="populators">
<list merge="true">
<ref bean="basicMediaModelPopulator" />
<ref bean="customMediaModelPopulator" />
</list>
</property>
</bean>