Header
A header is a single line defining a mapping of the following value lines to the type system. A header applies to all processed value lines until the next header or until the end of file, whichever comes first. You can put any number of headers into a CSV file.
An ImpEx header line has the following structure:
mode type[modifier=value];attribute[modifier=value];attribute[modifier=value];attribute[modifier=value][;...];attribute[modifier=value]
-
Mode: Specifies what is to be done with the following value lines (insert, update, and so on) - see Header Mode.
-
Type: Defines the type of item to be processed (category, product, media, type, and so on) - see Header Type.
-
Attribute: Describes which item attributes the columns are mapped to. The value lines supply the actual values for the items that are translated using the header settings - see Header Attributes
-
Modifier: Gives additional information for translating a value record to the mapped type attribute - see Header and Attribute Modifier.
Example for a header row:
INSERT_UPDATE category;code[unique=true];name[lang=de];name[lang=en];$supercategories;$thumbnail;description[lang=de];order
This header states that each following value line creates or updates a category instance, until another header occurs.
Header Mode
The mode of a header specifies what is to be done with the following data.
In case of an import it describes how to map and translate the following value lines to the data model of the platform. In case of an export, it describes how to map and translate the following items to the target CSV file. During export, headers are also written, so exported CSV files can be re-imported via the ImpEx extension.
There are four header modes:
|
Mode |
Comment/Description |
|---|---|
|
INSERT |
Creates a new Item in SAP Commerce from the processed value line. By default, ImpEx does not check if an item with the same attributes already exists. Therefore, trying to insert an item with a code that already exists may throw an exception caused by the SAP Commerce core. For that reason, it is recommended to use the unique modifier, where ImpEx prechecks a violation of attribute uniqueness. |
|
UPDATE |
Selects an existing item in SAP Commerce from the combination of the values of all columns marked with the unique modifier, which are referred to as key attributes, and sets the item attributes to the values specified in the value line. If there is no item that matches the values of all key attributes, the value line cannot be resolved. The value line is dumped into a temporary file and the ImpEx extension tries to resolve the value line later on. Refer to ImpEx API, section Import Using an Importer Instance for details. Also note that the default value for attributes is null. This means that any attributes that are referenced by the header but for which no values are specified in the value line are not ignored, but are explicitly set to null. For example, the following ImpEx lines cause the ahertz customer to have the name attribute set to null as the second line after the header specifies no value for the name attribute. UPDATE User;uid[unique=true];name;customerID; ;ahertz;Anja Hertz;; ;ahertz;;K2006-C0005; To set values for both the name and the customerID attributes, use one of the following approaches:
|
|
INSERT_UPDATE |
Combines the effects of the INSERT and UPDATE header modes: if an item with the values of all key attributes exists, this item is updated. If no item exists that matches the combination of all key attributes, the value line is used to create the item. The ImpEx extension always uses this order: UPDATE, then INSERT. |
|
REMOVE |
As in UPDATE mode, the ImpEx extension tries to find an existing item using the key attributes. If such an item exists, it is deleted, otherwise a log message of level warning is printed to the log. |
Header Type
The type code selects the type from the SAP Commerce Type System where the following data rows create, update or remove instances.
Optionally, each line can specify a subtype of the type specified in the header at the first column. That subtype is selected for that single line only - in the next line, the type specified in the header is active again. The following code snippet references the User type in the header, but creates an instance of the Customer and Employee types each.
INSERT User;uid[unique=true] Customer;SampleCustomer Employee;SampleEmployee
This allows specifying an abstract type, which cannot be instantiated, in the header for the INSERT mode, as with the Job type in the following code snippet. In that case, you need to specify a non-abstract type (that is, a type that can be instantiated) in every value line.
INSERT Job;code[unique=true] ImpExImportJob;sampleJob ImpExImportJob;sampleJob2
Although the header references the Job type, two instances of an ImpExImportJob will be created: sampleJob and sampleJob2.
Header Attributes
Each column followed by the header type at the header line describes an attribute of the header type where the column value of the following value lines are mapped to.
The ImpEx extension checks the specified type and its attributes and uses a Translator to match the value line's entry to the individual attribute. See Header and Attribute Modifier for more details on Translator.
It is easy to set an attribute value for attributes that are specified as AtomicTypes (such as java.lang.String or java.lang.Integer). For example, assume the following code snippet:
INSERT User;uid ;myUser
Here the uid attribute is of type java.lang.String, so additonal definitions are not required. You define the attribute at the header line and write a value line with the plain text of the desired user ID.
Item Reference Attributes
Setting attribute values is more complex with attributes specified to hold references to items in SAP Commerce.
By default, the ImpEx expects values that represent a primary key:
INSERT Product;code;unit ;MyProduct;2821057095693456 #2821057095693456 being the PK for the pieces unit
Since primary keys are not usually known externally, ImpEx alternatively allows using attributes of the referenced item for lookup. They're provided in brackets immediately after the column attribute name: <attributeOfTypeX>'('<attributeFromX>(','<attributeFromX>)* ')'.
For example, the following code snippet specifies the unit column to look up Unit items using its code attribute:
INSERT Product;code;unit(code) ;MyProduct;pieces
You can use item expressions in a nested way, in cases where a lookup attribute is a reference to itself.
In the following example the catalogVersion attribute refers to a CatalogVersion item that is looked up using its catalog and version attribute. Within the catalog is a reference to itself (to a Catalog item) that is looked up using the id attribute:
INSERT Product;code;unit(code);catalogVersion(catalog(id),version) ;testCode;pieces;clothescatalog:Staged
If you specify an item expression using several attributes at a header line, you have to separate them with a comma. The values for such an expression at a value line are separated by a colon.
There are ComposedTypes that reference attributes of a very general type; for example, the owner attribute of the Address type is specified to be of the Item type and can therefore accept any item in the SAP Commerce. By consequence, you cannot directly reference an attribute specified for a subtype of Item, such as the uid attribute of a Customer instance, which would allow you to specify a specific customer. To refer to an attribute that belongs to a subtype of a type attribute, use the following syntax:
attribute(subtype.subtype_attribute)
For example, the following code snippet creates an Address type instance whose owner attribute (attribute of type Item) holds a reference to the admin user using the Principal's type uid attribute.
INSERT Address;firstname;owner(Principal.uid) ;Hans;admin
Skipping Columns of Data
The ImpEx extension allows skipping of entire columns of data by omitting the attribute for that column in the header.
INSERT myProduct;myAttribute;myAttribute2;;myAttribute4 ;myValue1;myValue2;myValue3;myValue4
myValue3 is ignored, because there is no header attribute mapping defined for the related column. In the following code snippet, the second column (containing the value deutsch) is ignored as there is no header attribute to process it:
INSERT Language;code;;active ;de;deutsch;true
Localizing
Localized attributes are described by multiple columns using the lang modifier, each one containing the ISOcode or PK for a single language.
Refer to Header and Attribute Modifier section for more information about attribute modifiers.
INSERT Product;code[unique=true];name[lang=en];name[lang=de] ;myProduct1;myProduct1's localized name;lokalisierter Name von myProduct1 ;myProduct2;myProduct2's localized name;lokalisierter Name von myProduct2
The ImpEx extension only overwrites localized values for the languages specified in the column header. Existing localized values for other languages remain unchanged, as the following example illustrates:
#create some products with a German and English name INSERT Product;code[unique=true];name[lang=en];name[lang=de] ;myProduct1;myProduct1's localized name;lokalisierter Name von myProduct1 ;myProduct2;myProduct2's localized name;lokalisierter Name von myProduct2 #overwrite the English name -> the German one remains untouched UPDATE Product;code[unique=true];name[lang=en] ;myProduct1;"my product 1" ;myProduct2;
|
Product |
Resulting English localization |
Resulting German localization |
|---|---|---|
|
myProduct1 |
my product 1 |
lokalisierter Name von myProduct1 |
|
myProduct2 |
lokalisierter Name von myProduct2 |
For more information, see Wikipedia on ISO Country Codes
.
Document ID
Especially when importing partOf item values, you must reference these items by means other than the usual unique column technique because partOf items do not always provide a unique key but only hold their enclosing parent as a foreign key.
The Customer.addresses attribute provides an example of this. The customer owns addresses exclusively. The addresses hold the customer PK asa back reference. When using ImpEx, both customer and address have to be imported in separate lines.
Removing obsolete addresses requires that you fill the Customer.addresses cell with all currently valid addresses. This allows the setter method to remove obsolete ones. You must therefore reference valid addresses using a unique key, which addresses do not provide. For cases where a reference within the ImpEx script is required, ImpEx provides a special header attribute marked with the prefix & to specify a virtual ID called Document ID. Each imported item line can optionally define such a Document ID, which allows other lines to reference it later using the same attribute qualifier (with the & prefix).
Example:
INSERT Customer;uid[unique=true];...;defaultPaymentAddress(&addrID);... ;andi;...;addr1;... INSERT Address;&addrID;owner(Customer.uid);... ;addr1;andi;...
The Document ID is case-sensitive, so the following code snippet would not work as the Document ID would not be recognized by the ImpEx extension (the document ID for the Customer type being &addrID and the header for the Address type being &addrid).
INSERT Customer;uid[unique=true];...;defaultPaymentAddress(&addrID);... ;andi;...;addr1;... INSERT Address;&addrid;owner(Customer.uid);... ;addr1;andi;...
Special Attributes
Sometimes you need to add data to an item that is not covered by an attribute, for example the media data. In these cases, the syntax of ImpEx provides a special kind of attribute definition called special attributes that do not have a mapping to a real attribute of the configured type.
They are marked with the @ symbol and always need the translator modifier, because there is no default implementation. Furthermore the specified translator has to be a realization of the SpecialValueTranslator interface.
The following code sample shows how to create a media with the data set from the specified file:
INSERT_UPDATE Media;code[unique=true];@media[translator=de.hybris.platform.impex.jalo.media.MediaDataTranslator] ;myMedia;file://c:/myMedia.txt
The type Media has no media attribute, but specifying the media attribute as a special attribute instructs ImpEx not to search for such an attribute. Instead, it will call the special translator specified with the translator modifier, which performs the import logic.
Alternative Pattern
For type defining attributes that belong to a generic type, such as the Address type owner attribute, which is specified to hold an instance of Item, the ImpEx extension allows you to specify Alternative Patterns in the header.
An Alternative Pattern specifies more than one attribute and makes the ImpEx extension determine the matching type instance from all the specified attributes. Alternative Patterns are processed from left to right.
The following code snippet, specifies two possible target attributes for the Address instances:
-
the Principal type uid attribute
-
the AbstractOrder type code attribute:
INSERT Address;firstname;owner(Principal.uid|AbstractOrder.code) ;Hans;admin ;Klaus;O-K2006-C0000-001
During import, ImpEx compares the values for the owner attributes against all available instances of the Principal and AbstractOrder types and automatically chooses the instance whose target attribute value (uid or code, respectively) matches the target value (admin and O-K2006-C0000-001, respectively).
When importing the first value line ;Hans;admin, the ImpEx extension searches all instances of the Principal type to verify if any instance exists where the uid attribute is set to admin.
-
If an instance exists (and there is, by platform default), the new instance of the Address type has its owner attribute set to a reference to that Principal type instance.
-
If no instance exists of the Principal type whose uid attribute is set to admin, the ImpEx continues the retrieval by trying to find an instance of the AbstractOrder type whose code attribute is set to admin.
-
If there was no instance of the AbstractOrder type whose code attribute is set to admin, the ImpEx extension would try to continue the retrieval on the next specified Alternative Pattern attribute. Since there would be no further Alternative Pattern attribute, the ImpEx extension would dump the line and try to resolve it on the next import pass.
In this sample case, there is a Principal instance whose uid attribute is set to admin by platform default, so ImpEx is able to resolve the value line.
Basically, the same thing happens for the second value line ;Klaus;O-K2006-C0000-001: the ImpEx extension searches all instances of the Principal type if there is any instance whose uid attribute is set to O-K2006-C0000-001.
-
If there was an instance (but there is none, by platform default), then the new instance of the Address type would have its owner attribute set to a reference to that Principal type instance.
-
Since there is an instance of the AbstractOrder type whose code attribute is set to O-K2006-C0000-001 (at least for SAP Commerce installations with the sampledata extension enabled), the new instance of the Address type has its owner attribute set to a reference to that AbstractOrder type instance.
-
If there was no instance of the AbstractOrder type whose code attribute is set to O-K2006-C0000-001, then the ImpEx extension tries to continue the retrieval on the next specified Alternative Pattern attribute. Since there would be no further Alternative Pattern attribute, the ImpEx extension would dump the line and try to resolve it on the next import pass.
In this sample case, there is a AbstractOrder instance whose code attribute is set to O-K2006-C0000-001 by using the sampledata extension, so ImpEx is able to resolve the value line right now.
Header and Attribute Modifier
A modifier is an addition to an entry in a header line that specifies additional processing instructions.
For example:
...;attribute[modifier=value];...
The ImpEx extension allows specifying several modifiers at once, like this:
...;attribute[modifier=value,modifier=value,modifier=value];...
or
...;attribute[modifier=value][modifier=value][modifier=value];...
For example, the following code snippet defines that the value specified for the name attribute is the English localization version ("en" according to Wikipedia on ISO 639-1
) of the name attribute.
name[lang=en]
When you're not sure if a given language is available in your target system, use the following script to protect your files in this language from being imported:
#% if: de.hybris.platform.servicelayer.i18n.util.LanguageUtils.isLanguagePresent("fr")
INSERT_UPDATE Item1;code[unique=true];name[lang=fr]
...
#% endif:
To escape a modifier value, use the inverted comma ('). To use an inverted comma in a modifier value, escape the inverted comma itself:
attribute[modifier='My modifier''s value']
Attribute-Related Modifiers
|
Modifier |
Allowed Values |
Description |
|---|---|---|
|
alias Export only |
A text such as myAttribute Default: no alias is set |
With that modifier you can specify an alias name for an attribute, which is used in export case as attribute text in destination file. Example: [alias=myAttribute] |
|
allownull Import only |
true / false Default: false |
If set to true, this modifier explicitly allows null values for the column values. If there is no business code that blocks null values, this modifier even allows null values in mandatory attributes, such as the catalogVersion attribute of the Media type, for example. Example: [allownull=true] |
|
cellDecorator Import only |
An AbstractImpExCSVCellDecorator class Default: no decorator is applied. |
Specifies a decorator class for modifying the cell value before interpreting. See Impex API document, section Writing Own Cell Decorator for more information. Example: [cellDecorator=de.hybris.platform.catalog.jalo.classification.eclass.EClassSuperCategoryDecorator] |
|
collection-delimiter Restricted to attributes of type Collection. |
Any character Default: comma ( , ) |
Allows specifying a delimiter for separating values in a Collection. For more infomraton about Collections, see Type System Documentation. Example: INSERT_UPDATE Product;collectionAttribute[collection-delimiter = #];1#2#3#4; |
|
dateformat Restricted to Date and StandardDateRange types. |
A date format such as MM-dd-yyyy or dd.MM.yyyy (See the Date and Time Patterns in Java API documentation) Default: DateFormat.getDateTimeInstance (DateFormat.MEDIUM, DateFormat.MEDIUM, impexReader.getLocale()) |
Specifies the format in which the column specifies Date values. Example: UPDATE myProduct;myAttribute [dateformat=dd-MM-yyyy] ;17-01-1972 A more complex example would be to implement a ISO 8601 compliant combined date and time representation. The DateFormat class requires a syntax such as yyyy-MM-dd'T'HH:mm:ss.SSSZ. The T in the date format representation must be delimited by single quotes by convention. For use with ImpEx, the single quotes have to be escaped by doubling. Example: UPDATE myProduct;myAttribute[dateformat='yyyy-MM-dd''T''HH:mm:ss.SSSZ'] ;"2009-04-03T04:14:00.235-0700" |
|
default |
A value that matches the current attribute column type. |
Sets the default for values of this column Example: INSERT_UPDATE myProduct;myAttribute[default=MyName] |
|
forceWrite Import only |
true / false Default: false |
If set, it tries to write into read-only columns. Success depends on business logic of attribute setter. If not set, writing of read-only attributes is not to be tried. Example: [forceWrite=true] |
|
ignoreKeyCase Import only Restricted to attributes of type Item |
true / false Default: false |
If set, the case of the text that specifies attributes of items for resolving is ignored Example: [ignoreKeyCase=true] |
|
ignorenull Only for import Restricted to attributes of type Collection. |
true / false Default: false |
For Collection-type attribute values, this allows ignoring of null values.
Example: INSERT_UPDATE myProduct;myAttribute[ignoreNull=true] |
|
key2value-delimiter Restricted to attributes of type Map. |
any character Default: "->" |
Specifies the assignment delimiter for a key-value pair. Example: INSERT_UPDATE myProduct;myAttribute[key2value-delimiter=>>] ;myKey>>myValue; |
|
lang Restricted to localized attributes (Map<String, Language>) |
The ISO code or PK of a language defined for the SAP Commerce (such as de, en, or de_ch, for example) Default: session language |
Specifies the language for a column containing localized attribute values. Example: INSERT_UPDATE myProduct;myAttribute[lang=de] |
|
map-delimiter Restricted to attributes of type Map. |
Any character Default: ';' |
Specifies the delimiter between two key-value pairs. Example: INSERT_UPDATE myProduct;myAttribute[map-delimiter=|] ;myKey->myValue|myKey2->myValue2; |
|
mode Import only Restricted to attributes of type Collection |
append, remove, merge Default: replace (not explicitly specifiable) - an already existing Collection will be replaced |
Specifies the modification mode for Collection instances.
|
|
numberformat Restricted to attributes of type Number |
A number format like #.###,## (For further information, refer to the NumberFormat class documentation by Sun Microsystems) Default: NumberFormat.getNumberInstance( impexReader.getLocale() ) |
Specifies the format the column uses to specify Number values. Be aware that the delimiters for number values depend on the locale settings specified for the session, and you cannot specify the delimiters using the numberformat modifier only. If you need to specify the delimiters explicitly, you have to set a locale for the reader instance explicitly that is related to the delimiters using the BeanShell code. Example: #% impex.setLocale( Locale.GERMAN ); The following code snippet shows two settings for the numberformat parameter (typical for German-and English-related locales, respectively). Example: INSERT_UPDATE myProduct;myAttribute[numberformat=#.###,##] ;1.299,99 INSERT_UPDATE myProduct;myAttribute[numberformat=#,###.##] ;1,299.99 |
|
path-delimiter Restricted to attribute of type ComposedType |
Any character Default: ":" |
Defines the delimiter to separate values for attributes using an item expression, such as catalog versions, for example. Example: INSERT_UPDATE myProduct;myAttribute(code,id)[path-delimiter=:] ;myCode:myID |
|
pos |
A positive integer Default: column number as positioned in header. |
Specifies the column position where the values for this attribute can be found within the following value lines. Attention: if this modifier is used for an attribute in a header description, it has to be used for all attributes in that header to avoid conflicts Example: [pos=3] |
|
translator |
An AbstractValueTranslator or a SpecialValueTranslator Default varies; each Type has a predefined default Translator. |
A Translator class resolves a column of a value line into an attribute value for the item in the SAP Commerce. The ImpEx extension processes all instances of out-of-the-box AtomicType and ComposedType instances, such as Java.lang.String and Product respectively, for example, into attribute values using the default Translator classes. Example: INSERT_UPDATE myProduct;myAttribute[translator=de.hybris.platform.impex.jalo.translators.ItemPKTranslator] |
|
unique |
true / false default: false |
Marks this column as a key attribute - that is, the value is used to determine whether the item is unique or not. ImpEx uses the combination of the values of all key attributes to check whether the item already exists. This means that in the case of unique=true, each combination of the values of all key attributes in a row must be unique. Each row must provide a unique set of values for them (since there may be multiple key attributes), which are used to check whether or not an item already exists. Example: INSERT_UPDATE myProduct;myAttribute[unique=true]
|
|
virtual |
true / false Default: false |
If set, a related column within the following value lines is expected. This has to be used in conjunction with default modifier. Example: INSERT MyProduct;myAttribute1[virtual=true,default=myValue1];myAttribute2[unique=true] ;myValue2; Sets myValue1 to attribute myAttribute1 and myValue2 to attribute myAttribute2. Be aware that the virtual column has no dedicated field in the value line. This could lead to shiftings in the value line. For a better readability, it's advisable to add the virtual attributes at the end of the header line. |