Impex – Cell Decorator
While importing impex value lines,sometime it is required to modify the value based on some business logic rather than directly inserting the value.
Hybris has provided different ways to handle the same.
1) Using Cell Decorator
2) Using Translator
3) Using Special Translator
In this article, we will discuss on Cell Decorator
Cell Decorator is used to obtain the specific cell of value line after parsing but before translating it
We can then decorate the cell value based on business logic.
Decorating cell value means modifying the cell value based on some business requirement.
Requirement
Assume we are Updating Customer record through impex We need to check customer belongs to which country using customer id provided in impex If customer belongs to US then append _US to customer id before saving If customer belongs to Canada then append _CA to customer id before saving
Let’s implement this requirement with the help of Cell Decorator
Step 1
We need to configure cell decorator by adding the modifier cellDecorator to a header attribute specifying our decorator class.
- #Updating only customer
- INSERT_UPDATE Customer;uid[unique=true][cellDecorator=com.kb.decorators.CustomerIdCellDecorator];name;groups(uid);
- ;dummyuser@dummy.com;dummy user;customergroup
#Updating only customer INSERT_UPDATE Customer;uid[unique=true][cellDecorator=com.kb.decorators.CustomerIdCellDecorator];name;groups(uid); ;dummyuser@dummy.com;dummy user;customergroup
Step 2
Create the class which implements CSVCellDecorator and override the decorate method with our custom logic
- package com.kb.decorators;
- import de.hybris.platform.core.Registry;
- import de.hybris.platform.core.model.user.UserModel;
- import de.hybris.platform.servicelayer.user.UserService;
- import de.hybris.platform.util.CSVCellDecorator;
- import java.util.Map;
- /**
- * @author KB
- *
- */
- public class CustomerIdCellDecorator implements CSVCellDecorator
- {
- private final String USER_SERVICE_BEAN = "userService";
- protected UserService getUserService()
- {
- return (UserService) Registry.getApplicationContext().getBean(USER_SERVICE_BEAN);
- }
- /*
- * (non-Javadoc)
- *
- * @see de.hybris.platform.util.CSVCellDecorator#decorate(int, java.util.Map)
- */
- @Override
- public String decorate(final int position, final Map<Integer, String> srcLine)
- {
- final String csvCell = srcLine.get(Integer.valueOf(position));
- if (csvCell == null || csvCell.length() == 0)
- {
- return csvCell;
- }
- else
- {
- final UserModel userModel = getUserService().getUserForUID(csvCell);
- if (userModel.getDefaultShipmentAddress().getCountry().getIsocode().equals("US"))
- {
- return csvCell + "_US";
- }
- else if (userModel.getDefaultShipmentAddress().getCountry().getIsocode().equals("CA"))
- {
- return csvCell + "_CA";
- }
- else
- {
- return csvCell;
- }
- }
- }
- }
package com.kb.decorators; import de.hybris.platform.core.Registry; import de.hybris.platform.core.model.user.UserModel; import de.hybris.platform.servicelayer.user.UserService; import de.hybris.platform.util.CSVCellDecorator; import java.util.Map; /** * @author KB * */ public class CustomerIdCellDecorator implements CSVCellDecorator { private final String USER_SERVICE_BEAN = "userService"; protected UserService getUserService() { return (UserService) Registry.getApplicationContext().getBean(USER_SERVICE_BEAN); } /* * (non-Javadoc) * * @see de.hybris.platform.util.CSVCellDecorator#decorate(int, java.util.Map) */ @Override public String decorate(final int position, final Map<Integer, String> srcLine) { final String csvCell = srcLine.get(Integer.valueOf(position)); if (csvCell == null || csvCell.length() == 0) { return csvCell; } else { final UserModel userModel = getUserService().getUserForUID(csvCell); if (userModel.getDefaultShipmentAddress().getCountry().getIsocode().equals("US")) { return csvCell + "_US"; } else if (userModel.getDefaultShipmentAddress().getCountry().getIsocode().equals("CA")) { return csvCell + "_CA"; } else { return csvCell; } } } }
In the above class, we have written a logic to return the customer Id by appending “_US” to US customers and “_CA” to Canada customers and return customer Id without appending anything if user is different from US and CA.
Step 3
Run the impex and check the updated customer record in HMC/BackOffice
in which extension i need to write this class.(core or facades)
we can use more than one cellDecorators?
so here I need to do entry of spring bean id for class “CustomerIdCellDecorator” to make it run.
Please confirm ??