cancel
Showing results for 
Search instead for 
Did you mean: 

How to Sync product from java code?

Former Member
0 Kudos

Hi, expert!

We want to syncronize some products from java code via Model layer (not Jalo). We founded solution for all catalog:

  final CatalogVersionModel catalogVersion = catalogVersionService.getCatalogVersion(fromCatalog, STAGED);
         final List<SyncItemJobModel> synchronizations = catalogVersion.getSynchronizations();
         for (final SyncItemJobModel syncItemJob : synchronizations) {
             final SyncItemCronJobModel cronJob = modelService.create(CatalogVersionSyncCronJobModel.class);
             cronJob.setLogToDatabase(Boolean.FALSE);
             cronJob.setLogToFile(Boolean.FALSE);
             cronJob.setForceUpdate(Boolean.FALSE);
             cronJob.setJob(syncItemJob);
             modelService.save(cronJob);
             modelService.refresh(cronJob);
             LOGGER.info("Generating cronjob  : " + cronJob.getCode() + " to synchronize the catalog : " + fromCatalog);
             cronJobService.performCronJob(cronJob, true);

But we need to sync some product, not all catalog version. Is it possible? How we can do it?

Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

We founded a solution by adding Configurator to Sync Job. Full example:

         final CatalogVersion source = modelService.getSource(stagedCatalogVersionModel);
          final CatalogVersion target = modelService.getSource(onlineCatalogVersionModel);
          final SyncItemJob syncJob = CatalogManager.getInstance().getSyncJob(source, target);
          final SyncItemCronJob synchronizeJob = syncJob.newExecution();
      final List itemPKs = new ArrayList();
       for (final ProductModel productModel : productModels)
       {
           final Product sourceProduct = modelService.getSource(productModel);
           final ProductModel targetProductModel = findProductFromOnline(productModel);
           Product targetProduct = null;
           if (targetProductModel != null)
           {
               targetProduct = modelService.getSource(targetProductModel);
           }
           itemPKs.add(new PK[]
           { sourceProduct.getPK(), targetProduct == null ? null : targetProduct.getPK() });
       }
     
       synchronizeJob.addPendingItems(itemPKs, false);
       synchronizeJob.setConfigurator(new SyncItemCronJob.Configurator()
       {
           @Deprecated
           @Override
           public void configureCronjob(final SyncItemCronJob syncItemCronJob, final SyncItemCopyContext syncItemCopyContext)
           {
           }
     
           public CompletionInfo getCompletionInfo()
           {
               return new SyncItemJob.CompletionInfo(&amp;quot;configuring full version sync&amp;quot;, 0, 0, 0, 0);
           }
       });
 
   syncJob.perform(synchronizeJob, true);

Yves, thanks for help.

Former Member
0 Kudos

Thanks for your feedback!

Former Member
0 Kudos

Now that SyncItemJob and SyncItemCronJob are deprecated, what should we use ?

former_member774140
Discoverer
0 Kudos

You're a life saver. Thanks !!

Answers (3)

Answers (3)

Former Member
0 Kudos

Overriding the configureCronojob worked for me to sync the selected products synchronizeJob.setConfigurator(new SyncItemCronJob.Configurator() {

 @Override
 public CompletionInfo getCompletionInfo()
 {
     return new SyncItemJob.CompletionInfo("configuring full version sync", 0, 0, 0, 0);

 }

 @Deprecated
 @Override
 public void configureCronjob(final SyncItemCronJob arg0, final SyncItemCopyContext arg1)
 {
     syncJob.addCatalogItemsToSync(synchronizeJob, items);
 }

});

former_member774140
Discoverer
0 Kudos

You're a life saver. Thanks !!

Former Member
0 Kudos

I tried two method:

1 Sync via Jalo layer:

     final CatalogVersion source = modelService.getSource(stagedCatalogVersionModel);
     final CatalogVersion target = modelService.getSource(onlineCatalogVersionModel);
     final SyncItemJob syncJob = CatalogManager.getInstance().getSyncJob(source, target);
     final SyncItemCronJob synchronizeJob = syncJob.newExecution();
     final Product sourceProduct = modelService.getSource(sourceproductModel);
     final Product targetProduct = modelService.getSource(targetProductModel);
     synchronizeJob.addPendingItem(sourceProduct, targetProduct);
     syncJob.perform(synchronizeJob, true);

Result: sync update ALL products (not sourceProduct)

2 Sync via SynchronizationServiceImpl:

     final CatalogVersion target = modelService.getSource(onlineCatalogVersionModel);
     final SyncItemJob syncJob = CatalogManager.getInstance().getSyncJob(source, target);
     final List<String> synkPkJobs = new ArrayList<String>();
     synkPkJobs.add(syncJob.getPK().toString());
     final SynchronizationServiceImpl synchronizationService = new SynchronizationServiceImpl();
     synchronizationService.performSynchronization(productModels, synkPkJobs, onlineCatalogVersionModel, syncJob.getCode());

Result: exception java.lang.NullPointerException at de.hybris.platform.cockpit.services.sync.impl.SynchronizationServiceImpl.performSynchronization(SynchronizationServiceImpl.java:242)

Could somebody share working example sync one product?

Thanks.

Former Member
0 Kudos

You need to initialize/load your bean the one. Either you call your bean as follows

SynchronizationService syncService = (SynchronizationService) SpringUtil.getBean("synchronizationService");

or in your spring file :

 <bean id="YourSyncJobPerformable" class="your package.YourSyncJobPerformable" parent="abstractJobPerformable"  >
         <property name="synchronizationService" ref="synchronizationService"/>
 </bean>

Yves

Former Member
0 Kudos

Hi, Yves. I can not use SpringUtil because i am using core extention, not cockpit(zkozz). When i try to set

  <property name="synchronizationService" ref="synchronizationService"/>

there are error: No bean named 'synchronizationService' is defined. Are you sure that service name is "SynchronizationService"? Not SynchronizationServiceImpl? Can i use SynchronizationService in core extention?

Former Member
0 Kudos

OK if you use only the core extension (platform extension) you can't use the synchronizationService bean as it belongs to the cockpit extension. Unless you add the cockpit extension to your custom one, you won't be able to use the bean.

Former Member
0 Kudos

Regarding your first option, at first I haven't noticed what you have implemented but what makes you say that it synchronizes all the products? Maybe by trying to use the de.hybris.platform.catalog.jalo.SyncItemCronJob.addPendingItems(List, boolean) method it should only sync the list you have passed as parameter ( instead of using synchronizeJob.addPendingItem(sourceProduct, targetProduct);)

Otherwise, you can contact Product Support by creating a BCP ticket, we will be glad to help you further or at least to give you some hints 😉

Yves

Former Member
0 Kudos

Yves, thank you for your answer. But de.hybris.platform.catalog.jalo.SyncItemCronJob.addPendingItems(List, boolean) sync all product catalog too.

Former Member
0 Kudos
Former Member
0 Kudos

I am using B2B Accelerator 5.1. SyncItemJob and SyncItemCronJob marked as Deprecated. Can i use this classes?

Former Member
0 Kudos

Actually, the sync process is still using the Jalo layer underneath, even though the Service Layer has been added for some sync features/function. Either you can use that classes or you can have a look at : de.hybris.platform.cockpit.services.sync.impl.SynchronizationServiceImpl.performSynchronization(...)

Hope that helps.

Cheers, Yves