Impex – Translator
Translator can be used to change the translation logic for a value
Translator can be applied on any attribute as below
- <!-- -->
- 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
- 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
- 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.
- #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
#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
- 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();
- }
- }
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
Hi Kb,
What is difference between decorator and translator?
When we need to use decorator or translator?
Hi Ajay
The difference is:
In decorator you have to extends from CSVCellDecorator, that contain a method called decorate, that receives a String and returns a String.
In translator, you have to extends from AbstractValueTranslator, that contain one method to import and onde method to export, and this methods “translate” a String to and Object, and an Object to a String, respectively.
So we can conclude that, if you want to decorate a String with some sufix, prefix or another information, use Decorators. If you want to translate a String to an Object, use the Translator.