Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export data with condition

I need to export some instances of a model but I don't want them all. I need only the one that have a specific attribute.

I can do that by code, I wonder how to do this with Impex?

like image 667
alain.janinm Avatar asked Dec 13 '22 18:12

alain.janinm


2 Answers

You can export you instances in other way :

Step 1 :

Go to HMC interface, choose script generator tool System->Tools->Script Generator, then generate ImpEx scripts models, and choose your instance type script to export , something like :

"#% impex.setTargetFile( ""Customer.csv"" );"  // 1. where to export
insert_update Customer;&Item;@password[translator=de.hybris.platform.impex.jalo.translators.UserPasswordTranslator];CN;CodeNaf(code);DN;Europe1PriceFactory_UDG(code,itemtype(code));Europe1PriceFactory_UPG(code,itemtype(code));Europe1PriceFactory_UTG(code,itemtype(code));accountOrigine(&Item);accountVerificationType;addresses(&Item);authentificationDate[dateformat=dd.MM.yyyy hh:mm:ss];authorizedToUnlockPages[allownull=true];birthDate[dateformat=dd.MM.yyyy hh:mm:ss];carts(code);codeAPE;codeRCS;codeSiret;company;companyType(code,itemtype(code));creationtime[forceWrite=true,dateformat=dd.MM.yyyy hh:mm:ss];customerID;dateOfBirth[dateformat=dd.MM.yyyy hh:mm:ss];defaultPaymentAddress(&Item);defaultPaymentInfo(&Item);defaultShipmentAddress(&Item);description;domain;emailAlias;encodedPassword;enseigne(code)[allownull=true];erosId;europe1Discounts(&Item);firstname;fraudStatus(code,itemtype(code));hmcLoginDisabled;interestAreaList(code,itemtype(code));isprofessionnel;lastLogin[dateformat=dd.MM.yyyy hh:mm:ss];lastname;ldapaccount[allownull=true];ldaplogin;ldapsearchbase;locationType(code,itemtype(code));loginDisabled[allownull=true];modifiedtime[dateformat=dd.MM.yyyy hh:mm:ss];name;nbChild;nbGodSon;orders(code,versionID);origin;originalUid;owner(&Item)[allownull=true];password;passwordAnswer;passwordEncoding;passwordQuestion;paymentInfos(&Item);previewCatalogVersions(catalog(id),version);profilePicture(catalogVersion(catalog(id),version),code);recevedDocs;sessionCurrency(isocode);sessionLanguage(isocode);subscriptionTelContact(&Item);title(code);token;type(code,itemtype(code));uid[unique=true,allownull=true];userprofile(&Item);verificationDate[dateformat=dd.MM.yyyy hh:mm:ss];verificationflag // 2. how to export
"#% impex.exportItems( ""Customer"" , false );" // 3. what to export

To add more light on what you look for, I will focus on 3rd line

#% impex.exportItems( ""Customer"" , false );" // 3. what to export

So you can use exportItems method in different ways:

  • exportItems by item set:

    public void exportItems( Collection<Item> items ) public void exportItems( String[] pklist )

These methods export given items where the items can be passed either as a list of PK's (String) or directly using as a Collection of items.

  • exportItems by type code:

    public void exportItems( String typecode ) public void exportItems( String typecode, int count ) public void exportItems( String typecode, boolean inclSubTypes ) public void exportItems( String typecode, int count, boolean inclSubTypes )

  • exportItems by FlexibleSearch:

    public void exportItemsFlexibleSearch( String query ) public void exportItemsFlexibleSearch( String query, Map values, List resultClasses, final boolean failOnUnknownFields, final boolean dontNeedTotal, int start, int count )
    public void exportItemsFlexibleSearch( String query, int count )

Note : Exporter API by default uses pagination of search results. Therefore, to have accurate results your FlexibleSearch queries must contain an ORDER BY clause example :

impex.exportItemsFlexibleSearch(""select {PK} from {Customer} where {uid}='anonymous' ORDER BY {pk}"")

If you have access to help.hybris check :

  • https://help.hybris.com/6.6.0/hcd/8bd9a45d86691014b71aa404248b7ee5.html

If you can only access the wiki :

  • https://wiki.hybris.com/display/release4/ImpEx+API#ImpExAPI-ExportAPI
  • https://wiki.hybris.com/display/release4/How+To+Export+the+Content+of+a+Catalog+Version+-+Tutorial

Step 2 :

Go to HAC interface, ImpEx Export, then put your script and export your items.

like image 66
Nomade Avatar answered Dec 28 '22 23:12

Nomade


I can generate the export model from the Backoffice -> Tools -> Script generated.

Then I can add a flexible search query to filter exported results :

# ---- Extension: core ---- Type: Customer ----
"#% impex.setTargetFile( ""Customer.csv"" );"
insert_update Customer;&Item;Europe1PriceFactory_UDG(code,itemtype(code));Europe1PriceFactory_UPG(code,itemtype(code));Europe1PriceFactory_UTG(code,itemtype(code));allowSubstitution[allownull=true];...
"#% impex.exportItemsFlexibleSearch(""select {PK} from {Customer} where {uid}='anonymous'"", Collections.EMPTY_MAP, Collections.singletonList( Item.class ), true, true, -1, -1  );"

Cf : Impex API for the documentation about impex.exportItems[FlexibleSearch] and this page for more example

like image 28
alain.janinm Avatar answered Dec 29 '22 00:12

alain.janinm