Mafick
Mafick

Reputation: 1170

Let default generated hybris data bean extend own generated data bean

I am trying to extend a deafult hybris data bean by my own type. My Code currently looks like

 <bean class="de.hybris.platform.commercefacades.product.data.MediaData">
     <property name="mediaType" type="de.hybris.platform.commercefacades.product.data.MediaDataType"/>
     </bean>

     <enum class="de.hybris.platform.commercefacades.product.data.MediaDataType">
         <value>IMAGE</value>
         <value>VIDEO</value>
     </enum>

     <bean class="de.hybris.platform.commercefacades.product.data.ImageData"
           extends="de.hybris.platform.commercefacades.product.data.MediaData"/>

The problem here is that the generated ImageData object will not extend MediaData. The generated code looks like:

 package de.hybris.platform.commercefacades.product.data;

 import de.hybris.platform.commercefacades.product.data.ImageDataType;

 public class ImageData  implements java.io.Serializable 
 {

     /** <i>Generated property</i> for <code>ImageData.altText</code> property defined at extension <code>commercefacades</code>. */
     private String altText;
     /** <i>Generated property</i> for <code>ImageData.format</code> property defined at extension <code>commercefacades</code>. */
     private String format;
     /** <i>Generated property</i> for <code>ImageData.width</code> property defined at extension <code>acceleratorfacades</code>. */
     private Integer width;
     /** <i>Generated property</i> for <code>ImageData.galleryIndex</code> property defined at extension <code>commercefacades</code>. */
     private Integer galleryIndex;
     /** <i>Generated property</i> for <code>ImageData.imageType</code> property defined at extension <code>commercefacades</code>. */
     private ImageDataType imageType;
     /** <i>Generated property</i> for <code>ImageData.url</code> property defined at extension <code>commercefacades</code>. */
     private String url;

     public ImageData()
     {
         // default constructor
     }

     // Getter and Setter    
     [...]

 }

Is the only way here to define a new data object or is it possible to overwrite a default hybris data bean?

Upvotes: 2

Views: 2683

Answers (2)

alain.janinm
alain.janinm

Reputation: 20065

You can define a single bean or enum across different extensions. When you build the platform only one Java class is generated with a merged list of all attributes. Beans are merged according to the extensions dependencies.

It means that the java class is generated with the first extension, then the second one (that depends on the first ext) add it's attribute to the existing class and that's it. You can't add "extends" to the class anymore.

To conclude, if you want to have the ImageData from commercefacade extending your custom MediaData you'll need to add in extensioninfo.xml of commercefacade <requires-extension name="your_custom_extension"/>. This way your extension will be loaded first.

In your_custom_extension define in beans.xml

<bean class="de.hybris.platform.commercefacades.product.data.MediaData">
 <property name="mediaType" type="de.hybris.platform.commercefacades.product.data.MediaDataType"/>
 </bean>

 <enum class="de.hybris.platform.commercefacades.product.data.MediaDataType">
     <value>IMAGE</value>
     <value>VIDEO</value>
 </enum>
<bean class="de.hybris.platform.commercefacades.product.data.ImageData"
       extends="de.hybris.platform.commercefacades.product.data.MediaData"/>

Run ant clean all, refresh your platform, you'll see that the generated ImageData class will extends MediaData.

Of course modifying the dependancies of hybris extensions is not encouraged. You should not modify them. Instead, just add the attribute you need to the existing bean. In your custom extension define this in beans.xml :

<bean class="de.hybris.platform.commercefacades.product.data.ImageData">
 <property name="mediaType" type="de.hybris.platform.commercefacades.product.data.MediaDataType"/>
</bean>
<enum class="de.hybris.platform.commercefacades.product.data.MediaDataType">
     <value>IMAGE</value>
     <value>VIDEO</value>
</enum>

If you really need to have an ImageData class that extends your MediaData, the best solution would be to not use the Hybris bean generator and define yourself a class to use for your own logic.

Upvotes: 2

Vikrant
Vikrant

Reputation: 1899

You are doing incorrect configuration here. As I can quote you a hybris(v6.1) snippet which exhibit inheritance of beans.

Please look into the below snippet taken from acceleratorfacades-beans.xml

<bean class="de.hybris.platform.acceleratorfacades.payment.data.PaymentSubscriptionResultData" 
   extends="de.hybris.platform.acceleratorservices.payment.data.PaymentSubscriptionResult">
    <property name="storedCard" type="de.hybris.platform.commercefacades.order.data.CCPaymentInfoData"/>
</bean>

This shows that the bean PaymentSubscriptionResultData extends PaymentSubscriptionResult.

Now if you go and see the de.hybris.platform.acceleratorfacades.payment.data.PaymentSubscriptionResultData, it actually extends de.hybris.platform.acceleratorservices.payment.data.PaymentSubscriptionResult

enter image description here

Now for your example, the bean that you mentioned as

de.hybris.platform.commercefacades.product.data.ImageData is already present as hybris predefined bean. You can not extend that bean with any other base bean (class) because the base extension beans are generated first. The dependencies are calculated in the custom extensions and the (already generated) base beans are provided the extended properties, rather than getting a new bean created.

If you want any predefined bean to be customized, then you will have to create your own bean (hierarchy) and introduce the properties accordingly.

Could you please let me know the exact scenario for which you are looking.

Upvotes: 1

Related Questions