Solr

Translator can be used to change the translation logic for a value

Translator can be applied on any attribute as below

  1. <!--             -->
  2. INSERT MyType;...;myAttribute[translator=de.hybris.platform.impex.jalo.translators.ItemPKTranslator]
<!--             -->
INSERT MyType;...;myAttribute[translator=de.hybris.platform.impex.jalo.translators.ItemPKTranslator]

The above configured translator class has to extend the AbstractValueTranslator

It defines 2 methods

  1. public abstract Object importValue( final String valueExpr, final Item toItem )
public abstract Object importValue( final String valueExpr, final Item toItem )

The valueExpr is the parsed cell value (possible decorators already applied if any), toItem is the resolved item

  1. public abstract String exportValue( final Object value )
public abstract String exportValue( final Object value )


Implement the translation logic for the export case here, The given object has to be translated to a String that has to be returned

Let's understand with below Requirement
Translator for price attribute to prevent importing negative prices

Step 1

We need to configure Translator by adding the modifier “translator” to a header attribute specifying our Translator class.

  1. #Price row update
  2. INSERT_UPDATE PriceRow;productId[unique=true];unit(code[unique=true,default=pieces]);currency(isocode)[unique=true];price[translator=de.hybris.platform.acceleratorservices.dataimport.batch.converter.MyPriceTranslator];minqtd;unitFactor;net
  3. ;300441142;pieces;GBP;8.46;1;1;false
#Price row update
INSERT_UPDATE PriceRow;productId[unique=true];unit(code[unique=true,default=pieces]);currency(isocode)[unique=true];price[translator=de.hybris.platform.acceleratorservices.dataimport.batch.converter.MyPriceTranslator];minqtd;unitFactor;net
;300441142;pieces;GBP;8.46;1;1;false

Step 2

Create the class which implements AbstractValueTranslator and override the importValue and exportValue methods with our custom logic

  1. package com.kb.translators;
  2. import de.hybris.platform.impex.jalo.translators.AbstractValueTranslator;
  3. import de.hybris.platform.jalo.Item;
  4. import de.hybris.platform.jalo.JaloInvalidParameterException;
  5.  
  6. import org.apache.commons.lang.StringUtils;
  7. /**
  8.  * @author KB
  9.  *
  10.  */
  11. public class MyPriceTranslator extends AbstractValueTranslator
  12. {
  13.  
  14.     @Override
  15.     public Object importValue(final String valueExpr, final Item toItem) throws JaloInvalidParameterException
  16.     {
  17.         clearStatus();
  18.         Double result = null;
  19.         if (!StringUtils.isBlank(valueExpr))
  20.         {
  21.             try
  22.             {
  23.                 result = Double.valueOf(valueExpr);
  24.             }
  25.             catch (final NumberFormatException exc)
  26.             {
  27.                 setError();
  28.             }
  29.             if (result != null && result.doubleValue() < 0.0)
  30.             {
  31.                 setError();
  32.             }
  33.         }
  34.         return result;
  35.     }
  36.  
  37.     @Override
  38.     public String exportValue(final Object value) throws JaloInvalidParameterException
  39.     {
  40.         return value == null ? "" : value.toString();
  41.     }
  42. }
package com.kb.translators;
import de.hybris.platform.impex.jalo.translators.AbstractValueTranslator;
import de.hybris.platform.jalo.Item;
import de.hybris.platform.jalo.JaloInvalidParameterException;

import org.apache.commons.lang.StringUtils;
/**
 * @author KB
 *
 */
public class MyPriceTranslator extends AbstractValueTranslator
{

	@Override
	public Object importValue(final String valueExpr, final Item toItem) throws JaloInvalidParameterException
	{
		clearStatus();
		Double result = null;
		if (!StringUtils.isBlank(valueExpr))
		{
			try
			{
				result = Double.valueOf(valueExpr);
			}
			catch (final NumberFormatException exc)
			{
				setError();
			}
			if (result != null && result.doubleValue() < 0.0)
			{
				setError();
			}
		}
		return result;
	}

	@Override
	public String exportValue(final Object value) throws JaloInvalidParameterException
	{
		return value == null ? "" : value.toString();
	}
}
Step 3

Run the impex with price value greater than or equal to Zero, it will run successfully otherwise it will throw an error

Share this article on

Comment (9)

    1. Karibasappa G C (KB)
      Karibasappa G C (KB)

      First thing, static variables will never be a part of method, it will be a part of class always.

      So only we cant declare any static variables inside a method but we can access them inside a method.

      All static variables will be stored in separate segment inside Heap area only.

      1. The blog is very helpful. Thanks a lot for sharing! Also the “Inheritance in interface” page is not opening. Can you please fix that?

Leave a Comment

  • You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">

    • Please answer this simple challenge to post your valuable comment *