Search code examples
sap-commerce-cloudimpex

Inclusion of data via database in an impex


This is my impex:

    INSERT_UPDATE ReferenceProductAttributeValue;leaf(code,name[lang=de])[unique=true];newvalue(pk)

I have to put in newvalue(pk) the pk of units.p_code="PCE" that first, I have to take it from my databas e.Here is my query that it works perfectly in my MySQL:

      SELECT `units`.`PK` FROM `my_schema`.`units` WHERE `units`.`p_code`="PCE";

I allready tried this:

    INSERT_UPDATE ReferenceProductAttributeValue;leaf(code,name[lang=de])[unique=true];newvalue(pk)
          ;001:Antiviren-Software (Client-Betriebssystem);
   "#% 
      impex.initDatabase( <myDburl>, <myUser>, <myPassword>, <MyDriver.class>);"
   "#% 
      impex.includeSQLData( 
      "" SELECT ""+
      "" units.PK ""+"" FROM my_schema.units ""+
      "" WHERE ""+
      "" units.p_code ='PCE'""
      );"

and this:

    INSERT_UPDATE ReferenceProductAttributeValue;leaf(code,name[lang=de])[unique=true];newvalue(pk)
                  ;001:Antiviren-Software (Client-Betriebssystem);
    "#%
     import  de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
     flexibleSearchService = Registry.getApplicationContext().getBean(""flexibleSearchService"");
     query = "" SELECT {units.PK} FROM {my_schema.units} WHERE {units.p_code} LIKE '%PCE%' "";
     flexibleSearchQuery = new FlexibleSearchQuery(query);
     resultIterator = flexibleSearchService.search(flexibleSearchQuery).getResult().iterator();"

but it didn't work. Can someone give me a hint?

A new try:

     INSERT_UPDATE ReferenceProductAttributeValue;newvalue(pk);leaf(code,name[lang=de])[unique=true]
     #% beforeEach:
     #% import de.hybris.platform.core.model.product.UnitModel;
     #% import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
     #% import de.hybris.platform.jalo.flexiblesearch.* ;
     #% import de.hybris.platform.core.Registry;
     #% flexibleSearchService = Registry.getApplicationContext().getBean("flexibleSearchService");
     #% query = "SELECT {" + UnitModel.PK + "} FROM {" + UnitModel._TYPECODE + "} WHERE {" + UnitModel.CODE + "} = 'PCE' ";
     #% flexibleSearchQuery = new FlexibleSearchQuery(query);
     #% resultIterator = flexibleSearchService.search(flexibleSearchQuery).getResult().iterator().next();
     #% beforeEach: end \
     ;001:Antiviren-Software (Client-Betriebssystem)

now I don´t have Errors any more but stil don´t have my searched pk in newvalue. Can someone help me? Thanks!


Solution

  • so my solution is that I made a translator that looks like this (this is just for other beginner like me, maybe are another Solutions also but for me this was enough, it does work. If someone with more experience like to improve the Code, please ... ):

       public class UnitByCodeTranslator extends AbstractSpecialValueTranslator
    {
     ...
    
    /*
     *
     * @see de.hybris.platform.impex.jalo.translators.AbstractSpecialValueTranslator#performImport(java.lang.String,
     * de.hybris.platform.jalo.Item)
     */
    @Override
    public void performImport(final String cellValue, final Item item) throws ImpExException
    {
                if ((item instanceof ReferenceProductAttributeValue))
        {
        final ReferenceProductAttributeValueModel referenceProdAttVal = (ReferenceProductAttributeValueModel) modelService.get(item.getPK());
        final String value = cellValue;
        final UnitModel unit = new UnitModel();
        try
        {
            unit.setCode(value);
            final UnitModel foundUnit = flexibleSearchService.getModelByExample(unit);
            referenceProdAttVal.setNewValue(foundUnit);
            modelService.save(referenceProdAttVal);
        }
        catch (final UnknownIdentifierException e)
        {
            LOGGER.warn("Could not find a Unit for this " + value);
        }
        catch (final AmbiguousIdentifierException e)
        {
            LOGGER.warn(e.toString());
            throw new UnresolvedValueException(e.toString());
        }
        }
    }
    
    ....
    } 
    

    The impex then is like this:

    INSERT_UPDATE ReferenceProductAttributeValue;leaf(code,name[lang=de])[unique=true];@newvalue[translator=com.myextension.core.impex.translators.UnitByCodeTranslator]
    ;001:AGP-Grafikkarte;PCE