How to move the data from the supertype to the subtype in Hybris? - sap-commerce-cloud

I added a new attribute to my custom model (CustomApparelProduct), which extends from ApparelProduct model. ApparelProduct already extends ProductModel (SAP core model).
extension-items.xml
<typegroup name="Apparel">
<itemtype code="ApparelProduct" extends="Product"
autocreate="true" generate="true"
jaloclass="de.hybris.training.core.jalo.ApparelProduct">
<description>Base apparel product extension that contains additional attributes.</description>
<attributes>
<attribute qualifier="genders" type="GenderList">
<description>List of genders that the ApparelProduct is designed for</description>
<modifiers/>
<persistence type="property"/>
</attribute>
</attributes>
</itemtype>
<itemtype code="CustomApparelProduct" extends="ApparelProduct"
autocreate="true" generate="true" > // My custom type
<description> Custom Apparel Product that contains total amount attributes</description>
<attributes>
<attribute qualifier="amount" type="java.lang.Integer">
<description>Total buying amount </description>
<modifiers/>
<persistence type="property"/>
<defaultvalue>Integer.valueOf(0)</defaultvalue>
</attribute>
</attributes>
</itemtype>
When I go to Hybris Admin Console to check the data from my new Types by using FlexibleSearch, the result is that all the fields lack data. ( The picture below is from my CustomApparelProduct type)
When I check the FlexibleSearch for ApparelProduct type and Product type, the result is that all the fields hava data. ( The picture below is from ApparelProduct type)
I don't know what I need to transfer the original data from Product to my new type CustomApparelProduct. The original data was inserted to system using impex and csv file; the type used to insert is Product.
How I can transfer thedata to my custom type?

You cannot change from Type A to Type B easily/directly.
You need to migrate the data. i.e. Delete Type A item. Re-insert/create item as Type B. Also, check if there are other dependencies that need to be updated.

Related

Hybris define product prices for PointOfService

Hybris Out of the box doesn't provide any friendly way to define prices for products at point of service/vendor level, I need to implement price for products per store, what is the best way to implement this?
I think the best approach is to make a composite attribute in the POS model with UserPriceGroup.
<typegroup name="PointOfService">
<itemtype code="PointOfService" autocreate="false" generate="false">
<attributes>
<attribute qualifier="posUsergroup" type="UserPriceGroup">
<persistence type="property"/>
<modifiers optional="false" initial="true"/>
</attribute>
</attributes>
</attributes>
</itemtype>
</typegroup>
--> PriceRow supports different prices based on UserPriceGroup.

Importing a collectiontype of localized Strings in Hybris

I have a collection like this:
<collectiontype code="colors" elementtype="localized:java.lang.String">
and an itemtype containing this collection:
<itemtype code="MyShirt" autocreate="true" generate="true">
<attributes>
<attribute qualifier="code" type="java.lang.String">
<persistence type="property">
</attribute>
<attribute qualifier="colors" type="colors">
<persistence type="property">
</attribute>
</attributes>
</itemtype>
So what should be the proper ImpEx for inserting shirts in this itemtype:
$lang=en
INSERT_UPDATE MyShirt;code[unique=true];colors(code[lang=$lang])
The above statement gives error.Where am I going wrong?
CollectionTypes have been deprecated. Prefer relations. In your case I would introduce an itemtype "Color" and create a relation between "MyShirt" and "Color". For color you can create a unique id and reuse that color for all your shirts. I'm absolutely sure, different shirts may come in the same color. Additionally you can localize your colors at one point. You can be sure that changing a color name for a certain color has an effect on all your shirts. There are a multitude of advantages to this approach.
Here you try to add a localized collection, but I am not sure that Hybris allows you do that. So, I think you need to change the collection type to :
<collectiontype code="colors" elementtype="java.lang.String">
To import new elements, you have to change your Impex hedear and remove (code[lang=$lang]), colors is a list of String not Objects that have an attribute called code, So you have to use :
INSERT_UPDATE MyShirt;code[unique=true];colors
;shirt_1;color1,color2,color
But, almost of the time, for this kind of requirements, Hybris call for using Classifications, Attributes of classifications and Attributes value classifications.

Redefining data type in items.xml in Hybris

I had a datatype defined as follows in items.xml:
<attribute qualifier="daysOfWeek" type="java.lang.String">
<persistence type="property" />
</attribute>
To modify the data type to enumeration,I redefined it as follows:
<enumtype code="DaysOfWeek" autocreate="true" generate="true">
<value code="Monday" />
<value code="Tuesday" />
</enumtype>
<attribute qualifier="daysOfWeek" type="DaysOfWeek">
<persistence type="property" />
</attribute>
After updating the extension,I am getting SQLException.Is there anything wrong with this approach?
hybris doesn't support updates like this in a "running" system.
The reason is that hybris won't drop any db columns and recreate them as any data contained in there would be lost (plus its probably difficult to write this logic for multiple supported databases).
If you are in a development phase of your project, the easiest way to fix this is to initialize your system from scratch (i.e. it will drop the database and recreate it).
If you have a live system / production system, you would have to take another approach:
You would define a new attribute (different name!) with your enumeration type.
You would then probably update any code to use the new field.
You would also have to take care of data migration, i.e. write some scripts that transfer the old data (e.g. the String "Monday" to the new respective enum value).
Hope this helps!

Can someone explain details of hybris auditing system?

By Default hybris auditing system comes in picture whenever any change in model or any attribute.
I want to use Hybris auditing module in my application?(SavedValuesModel).Does this module has any issue in use? does this work under the same transaction model.? I am newbie for hybris. Please provide some details on this.
Hybris auditing system comes with the item SavedValues. this is the item which maintains the model value history in it. Please refer to the item type declaration below:
<!-- hmc legacy - Saved Values -->
<itemtype code="SavedValues"
generate="true"
jaloclass="de.hybris.platform.hmc.jalo.SavedValues"
extends="GenericItem"
autocreate="true">
<deployment table="SavedValues" typecode="334"/>
<attributes>
.
.
.
</attributes>
</itemtype>
Now this item has a 1-n relation with the SavedValueEntry,
<!-- hmc legacy - Saved values -->
<relation code="SavedValueEntriesRelation" autocreate="true" generate="true" localized="false">
<sourceElement type="SavedValues" qualifier="parent" cardinality="one">
<modifiers read="true" write="false" initial="true" optional="false" search="true"/>
</sourceElement>
<targetElement type="SavedValueEntry" qualifier="savedValuesEntries" cardinality="many"
collectiontype="set">
<modifiers read="true" write="false" optional="true" partof="true"></modifiers>
</targetElement>
</relation>
This item type contains the original and changed values of the item, along with the other details such as modification type (create/update/remove), the user doing the change etc.
The main disadvantages of this system are mentioned below:
Any changes done to the item type outside hmc (e.g. by impex etc.) are not recorded. However, if the changes need to be logged then one can use the below syntax
HMCManager.getInstance().logItemModification(item.getPk(), newValues, oldValues, false);
This audit trail has a performance overhead and needs to be cleaned up for performance tune ups.
You can follow the below references for further reading:
https://wiki.hybris.com/display/release5/SavedValues+-+Keeping+Track+of+Attribute+Value+Modification
https://experts.hybris.com/questions/9332/logging-to-savedvalues-table.html

Hybris - overriding existing unique attributes of an Item Type to make them non-unique

In my hybris application, I wanted to override my CustomerReview item type so that its attributes product and user are not unique anymore.
The uniqueness of these attributes are declared in the relationships between CustomerReview and Product/User. I tried adding the relationship declaration again to my extname-items.xml file and set the appropriate unique="false" attributes, as follows:
<relation generate="false" localized="false" code="ReviewToUserRel" autocreate="false">
<sourceElement type="User" qualifier="user" cardinality="one">
<modifiers write="false" initial="true" optional="false" unique="false" />
</sourceElement>
<targetElement type="CustomerReview" qualifier="customerReviews" cardinality="many">
<modifiers write="false" initial="true" />
</targetElement>
</relation>
This doesn't do the trick though. After I rebuild the application and Update the Running System, the user and product attributes of a CustomerReview are still unique attributes.
So what's the best solution for this problem?
It's true that rewriting the relation will not override it.
Another way to solve it would be to add an attribute to the type and set that as unique. For instance emailAddress:
<itemtype code="CustomerReview" autocreate="false" generate="false">
<attributes>
<attribute type="java.lang.String" qualifier="email">
<persistence type="property" />
<modifiers read="true" write="true" unique="true"/>
</attribute>
</attributes>
</itemtype>
Then you could have multiple reviews from the same user for the same product, as long as the emailAddress differs.
Updating the relation will not overwrite the existing relation. Better create new item definition for CustomerReview and then relate this with product.

Resources