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.

  1. #Updating only customer
  2.  
  3. INSERT_UPDATE Customer;uid[unique=true][cellDecorator=com.kb.decorators.CustomerIdCellDecorator];name;groups(uid);
  4. ;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

  1. package com.kb.decorators;
  2.  
  3. import de.hybris.platform.core.Registry;
  4. import de.hybris.platform.core.model.user.UserModel;
  5. import de.hybris.platform.servicelayer.user.UserService;
  6. import de.hybris.platform.util.CSVCellDecorator;
  7.  
  8. import java.util.Map;
  9.  
  10.  
  11. /**
  12.  * @author KB
  13.  *
  14.  */
  15. public class CustomerIdCellDecorator implements CSVCellDecorator
  16. {
  17.     private final String USER_SERVICE_BEAN = "userService";
  18.  
  19.     protected UserService getUserService()
  20.     {
  21.         return (UserService) Registry.getApplicationContext().getBean(USER_SERVICE_BEAN);
  22.     }
  23.  
  24.     /*
  25.      * (non-Javadoc)
  26.      *
  27.      * @see de.hybris.platform.util.CSVCellDecorator#decorate(int, java.util.Map)
  28.      */
  29.     @Override
  30.     public String decorate(final int position, final Map<Integer, String> srcLine)
  31.     {
  32.         final String csvCell = srcLine.get(Integer.valueOf(position));
  33.         if (csvCell == null || csvCell.length() == 0)
  34.         {
  35.             return csvCell;
  36.         }
  37.         else
  38.         {
  39.             final UserModel userModel = getUserService().getUserForUID(csvCell);
  40.  
  41.  
  42.             if (userModel.getDefaultShipmentAddress().getCountry().getIsocode().equals("US"))
  43.             {
  44.                 return csvCell + "_US";
  45.             }
  46.             else if (userModel.getDefaultShipmentAddress().getCountry().getIsocode().equals("CA"))
  47.             {
  48.                 return csvCell + "_CA";
  49.             }
  50.             else
  51.             {
  52.                 return csvCell;
  53.             }
  54.         }
  55.     }
  56.  
  57. }
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

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on