Import-Export Data
After create a data model, we need to insert some sample data in Data model which can be use for testing purpose . Inserting data in data model most common way.
By Backoffice (Out of Box Hybris Data Manage Tool)
Import ImpEx using the Backoffice import wizard
Import ImpEx using the HAC
Enter data via Backoffice
First, you need to create data manually in the Backoffice :
Start Hybris server and login in to backoffice :
Search "Types" in the navigation Tree or Explore System -> Type
Search for your data model like "User", Customer etc
In the Results Area, double click on Data Model "User"
Click on Left corner button "Search By Type"
Click on Plus button
"Create New User" Pop Up will Open , Enter the data and click to save button.
ImpEx : Import-Export framework
ImpEx is an out-of-the-box import-export framework. It is integrated text-based import/export extension . You can create, update, export, and remove items in Hybris Platform.
Use of ImpEx
In live operation:
To import customer data into a production system
To synchronize data with other systems, such as an ERP or LDAP
To create backups
To update data at runtime
Can be run from CronJobs
In migrations:
To migrate data from one hybris installation to another
In development:
To import sample data (e.g. on system initialization)
To import test data into testing system
Syntax
Header syntax:
Operation itemType; attributes(refAttr)[modifiers];...
Example
INSERT Product; code; name[lang=en];
UPDATE Bird; code[unique=true]; name[lang=en];
INSERT_UPDATE User; userID[unique=true]; groups(uid);
REMOVE Media; code[unique=true];
Data row syntax:
attr1value; attr2value; ...
Example
;ABC 123; Car;
;xyz; abc;
;12345ABC;
ImpEx Elements
Comments
Comments in impex file starts with # symbol
#Inserting product details
INSERT_UPDATE Product;code[unique=true];name[lang=en];
In the above lines, we have one comment line starts with # which will be ignored while importing.
Macros
It is allows aliases to stand in for frequently used statements. When we write Impex file. Some of value are static and used multiple impex. We can basically used to define the value at one place and use the macro in all the places. It helps to eliminate writing same lines again and again.
Syntax : Macros starts with “$”
$macroName
Example
#define macros
$productCatalog=apparelProductCatalog
$catalog=catalog(id[default=$productCatalog])
$catalogVersion=catalogVersion($catalog,version[default='Staged'])
#use macro in impex. Now the macros $catalogVersion and $catalog are replaced by their actual value. No need to #define value each row.
INSERT Product; code[unique=true]; $catalogVersion
;P012345;
INSERT Category; code[unique=true]; $catalogVersion
;C012345;
Translators
While importing impex value lines, sometime we required to modify the value based on some business logic rather than directly inserting the value. It is used to translate the attribute value to something before storing in database. It is implement custom ImpEx logic. Example :To encode a password before storing in Database. Hybris OOB example of this translator is ConvertPlainTextToEncodedUserPasswordTranslator.java
INSERT MyType;...;myAttribute[translator=<Translatorclass>]
We will configure Translator by adding the modifier “translator” to a header attribute and specifying our Translator class. Europe1PricesTranslator or MediaDataTranslator are out of box define in Hybris. We can use in direct impex as per requirement.
INSERT_UPDATE Product;code[unique=true];name[lang=en];unit(code);europe1prices[translator=de.hybris.platform.europe1.jalo.impex.Europe1PricesTranslator];
;HW1210-3422;testEN;pieces;50 EUR;
INSERT_UPDATE Media;$contentCV[unique=true];code[unique=true];mime;realfilename;@media[translator=de.hybris.platform.impex.jalo.media.MediaDataTranslator][forceWrite=true];folder(qualifier)[default='images']
Customize Translator Implementation
Example : if Price is less than zero then save product default price 10 in DB.
1) We will configure custom Translator by adding the modifier “translator” to a header attribute in impex.
INSERT_UPDATE Product;code[unique=true];name[lang=en];unit(code);europe1prices[translator=com.experts.hybris.translator.MyPricesTranslator];
;HW1210-3422;testEN;pieces;50;
2) Create the class which implements AbstractValueTranslator and override the importValue and exportValue methods with our custom logic.
public class MyPriceTranslator extends AbstractValueTranslator
{
@Override
public Object importValue(final String valueExpr, final Item toItem) throws JaloInvalidParameterException
{ // Add your custom logic
Double result = null;
if (!StringUtils.isBlank(valueExpr))
{
try
{
result = Double.valueOf(valueExpr);
}
catch (final NumberFormatException exc)
{
result =Double.valueOf("0.0");
}
if (result != null && result.doubleValue() < 0.0)
{
result =Double.valueOf("10.0");
}
}
return result;
}
@Override
public String exportValue(final Object value) throws JaloInvalidParameterException
{
//Add your custom logic
return value == null ? "" : value.toString();
}
}
Now when we run impex. each record price will be translate as per translator class logic.
Cell Decorator
Cell Decorator is used to obtain the specific cell of value line after parsing but before translating it. It means the cell value is parsed, then the cell decorator will call, which will manipulate the parsed value and then call translator. We can modifying the cell value based on business requirement.
We can configure the a cell decorator by adding the modifier cell Decorator to a header attribute in impex and implement our decorator class as per business requirement.
INSERT MyType;...;myAttribute[cellDecorator=<cellDecoratorclass>]
Some of OOB Hybris cell decorator like eClassSuperCategoryDecorator. We can use if requirement is match. Otherwise we can create custom custom Cell Decorator class which will implement CSVCellDecorator interface and has to override decorate( int position, Map<Integer, String> srcLine ) method in custom class.
Customize Cell Decorator Implementation
Example: We need to append "-Emp" in name of each employee name.
1) We have to configure cell decorator by adding the modifier cellDecorator to a header attribute specifying our custom decorator class.
INSERT_UPDATE Employee;UID[unique=true]; name[lang=en,cellDecorator=com.kb.decorators.CustomEmpCellDecorator];groups(uid);
;test@user.com;Testing;employeegroup
;dev@user.com;Developer;employeegroup
2) We will create a class which implements CSVCellDecorator interface and override the decorate method with our custom logic
public class CustomerIdCellDecorator implements CSVCellDecorator
{
@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)
{ // add "-Emp" of each Employee name. if name is Testing then it will return "Testing-Emp".
return csvCell + "-Emp";
}
return "";
}
}
BeanShell, Groovy scripting
We can use Beanshell, Groovy or Javascript as scripting languages within ImpEx. ImpEx has special control markers that determine simple by action beforeEach, afterEach, getLastImportedItem(),if etc. Below example : BeanShell call line.clear() method before each line import data.
INSERT_UPDATE Title;code[unique=true]
#% beforeEach: line.clear();
;MR.
########## Custom class static method calling
#% import com.experts.hybris.CustomClass;
INSERT_UPDATE Title;code[unique=true]
#% beforeEach: CustomClass.callStaticMethod();
;Mr.
Same we can write for groovy script
INSERT_UPDATE Title;code[unique=true]
#%groovy% beforeEach: line.clear();
;MR.
########## Custom class static method calling via groovy
INSERT_UPDATE Title;code[unique=true]
"#%groovy% beforeEach:
import com.experts.hybris.CustomClass
customClass.callStaticMethod()
";
;Mr.
User Rights
You can modify access rights for users and user groups by using Impex. It has special type of syntax. You can easily define user rights using below syntax
$START_USERRIGHTS
Type;UID;MemberOfGroups;Password;Target;read;change;create;delete;change_perm
UserGroup;impexgroup;employeegroup;
;;;;Product;+;+;+;+;-
$END_USERRIGHTS
First line, $START_USERRIGHTS is indicates impex is user right impex is started.
Second line, It is define header for specific Type and defines the attributes that are to be set.
Third Line, It is use for define Type, id and Parent group where you will modify given access rights.
Fourth Line, It is define to target Item and access. (+) allows, (-) denies the respective access right.
Last line : $START_USERRIGHTS is indicates impex is user right impex is ended.
We can set access rights to an attribute level as well by using below syntax.
$START_USERRIGHTS
Type;UID;MemberOfGroups;Password;Target;read;change;create;delete;change_perm
UserGroup;impexgroup;employeegroup;
;;;;Product.code;+;-;+;+;-
;;;;Product.name;+;-;+;+;-
$END_USERRIGHTS
Collections and HashMaps: It is allows you to use these types as attributes
Enter data via ImpEx using the Backoffice import wizard
Login into the BackOffice
Open the node System -> Tools -> Import
Set the import file name
Start the import: Click on the Start icon