POST OCC(WebServices) API for Creating B2C Store Details in Sap Commerce(Hybris) 2211

Step 1:

Create the b2CStoreDetails method in the SampleB2CStoreController. This method defines an endpoint to create new B2C Store details.It Receives the request and passes the store data to the facade.

Location: /sampleocc/src/com/hybris/occ/controllers/SampleB2CStoreController.java

Note: I'm adding the b2CStoreDetails method in SampleB2CStoreController of Step 14 in OCC Webservices in SAP Commerce (Hybris) 2211 – Part 2


import java.util.ArrayList; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.http.ResponseEntity;
import com.hybris.simple.facades.B2CStoreData;
import com.hybris.simple.facades.B2CStoreDataList;
import com.hybris.simple.facades.B2CStoreDataListWSDTO;
import com.hybris.simple.facades.B2CStoreDataWSDTO;

@Controller
@RequestMapping(value = "/{baseSiteId}")
public class SampleB2CStoreController
{
  @Resource
  private B2CStoreFacade b2cStoreFacade;

  @RequestMapping(value = "/b2cStores/create", method = RequestMethod.POST)
  public ResponseEntity<List<String>> b2CStoreDetails(
    @RequestBody B2CStoreDataListWSDTO storeDataListDTO)
  {
    List<B2CStoreDataWSDTO> storeDTOList = storeDataListDTO.getB2cstore();
    List<B2CStoreData> b2cStoreDataList = new ArrayList<>();
    List<String> operationResults = new ArrayList<>();
    for (B2CStoreDataWSDTO storeDTO : storeDTOList)
    {
      if (storeDTO.getStoreId() == null || storeDTO.getStoreId().isEmpty())
      {
        operationResults.add("Store ID cannot be empty for store: " + storeDTO.getStoreName());
        continue;
      }
      B2CStoreData storeData = mapToStoreData(storeDTO);
      b2cStoreDataList.add(storeData);
    }
    boolean isAnyNewStoreCreated = b2cStoreFacade.createB2CStoreDetails(b2cStoreDataList);
    for (B2CStoreData storeData : b2cStoreDataList)
    {
      if (isAnyNewStoreCreated)
      {
        operationResults.add("New store created with ID: " + storeData.getStoreId());
      }
      else
      {
        operationResults.add("Store already exists with ID: " + storeData.getStoreId());
      }
    }
    return ResponseEntity.ok(operationResults);
  }
  private B2CStoreData mapToStoreData(B2CStoreDataWSDTO storeDTO)
  {
    B2CStoreData storeData = new B2CStoreData();
    storeData.setStoreId(storeDTO.getStoreId());
    storeData.setStoreName(storeDTO.getStoreName());
    storeData.setStoreOwner(storeDTO.getStoreOwner());
    storeData.setStoreLocation(storeDTO.getStoreLocation());
    storeData.setStoreGstNumber(storeDTO.getStoreGstNumber());
    return storeData;
  }
}
  

Step 2:

In this step, we will define createB2CStoreDetails method . This method would take a list of B2CStoreData and attempt to store each B2C store.


Location: /samplefacades/src/com/hybris/simple/facades/b2cstore/facade/B2CStoreFacade.java

packagecom.hybris.simple.facades.b2cstore.facade;
importjava.util.List;
importcom.hybris.simple.facades.B2CStoreData;

public interface B2CStoreFacade
{
boolean createB2CStoreDetails(List<B2CStoreData> b2cStoreDataList);
}

Step 3:

In this step, we will define the method createB2CStoreDetails in the B2CStoreService interface, which will handle creating B2C store details from a list of SampleB2CStoreModel objects.

Location: /samplecore/src/com/hybris/simple/b2cstore/service/B2CStoreService.java

package com.hybris.simple.b2cstore.service;
import java.util.List;
import com.hybris.simple.core.model.SampleB2CStoreModel;
public interface B2CStoreService
{
boolean createB2CStoreDetails(List<SampleB2CStoreModel> storeModelList);
}
Step 4:

In this step, we will create the B2CStoreDetailsPopulator class, which implements the Populator interface. This class will be responsible for converting data from a B2CStoreData object to a SampleB2CStoreModel object.

Location: /samplefacades/src/com/hybris/simple/facades/populators/B2CStoreDetailsPopulator.java

package com.hybris.simple.facades.populators;
import de.hybris.platform.converters.Populator;
import de.hybris.platform.servicelayer.dto.converter.ConversionException;
import com.hybris.simple.core.model.SampleB2CStoreModel;
import com.hybris.simple.facades.B2CStoreData;

public class B2CStoreDetailsPopulator implements Populator<B2CStoreData, SampleB2CStoreModel>
{
    @Override
    public void populate(final B2CStoreData source, final SampleB2CStoreModel target) throws ConversionException
    {
        target.setStoreId(source.getStoreId());
        target.setStoreName(source.getStoreName());
        target.setStoreOwner(source.getStoreOwner());
        target.setStoreGstNumber(source.getStoreGstNumber());
        target.setStoreLocation(source.getStoreLocation());
    }
}
    

Step 5:

In this step, we will configure the b2CSampleStoreConverter and b2CStorePopulator

Location:/samplefacades/resources/samplefacades-spring.xml

<alias name="b2CPostStoreDetailsConverter" alias="b2CPostStoreConverter" />
<bean id="b2CPostStoreConverter" parent="abstractPopulatingConverter">
<property name="targetClass" value="com.hybris.simple.core.model.SampleB2CStoreModel" />
    <property name="populators">
        <list merge="true">
            <ref bean="b2CStoreDetailsPopulator" />
        </list>
    </property>
</bean>

<bean id="b2CStoreDetailsPopulator" class="com.hybris.simple.facades.populators.B2CStoreDetailsPopulator" />
    

Step 6:

In this step, we will implement method createB2CStoreDetails in DefaultB2CStoreFacade.It Converts the DTOs to model objects and delegates to the service layer

Location:/samplefacades/src/com/hybris/simple/facades/b2cstore/facade/impl/DefaultB2CStoreFacade.java

package com.hybris.simple.facades.b2cstore.facade.impl;
import de.hybris.platform.converters.impl.AbstractPopulatingConverter;
import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import com.hybris.simple.b2cstore.service.B2CStoreService;
import com.hybris.simple.core.model.SampleB2CStoreModel;
import com.hybris.simple.facades.B2CStoreData;
import com.hybris.simple.facades.b2cstore.facade.B2CStoreFacade;

public class DefaultB2CStoreFacade implements B2CStoreFacade
{
      @Resource
     private AbstractPopulatingConverter<B2CStoreData, SampleB2CStoreModel> b2CPostStoreConverter;

    @Resource
     private B2CStoreService b2CStoreService;

    @Override
    public boolean createB2CStoreDetails(final List<B2CStoreData> b2cStoreDataList)
    {
        final List<SampleB2CStoreModel> storeModels = b2CPostStoreConverter.convertAll(b2cStoreDataList);
        return b2CStoreService.createB2CStoreDetails(storeModels);
    }
}

Step 7:

In this step, we will implement createB2CStoreDetails method in DefaultB2CStoreService class. It Checks if each store exists using FlexibleSearchService, creates new stores using ModelService if necessary, and returns whether any store was created. Location: /samplecore/src/com/hybris/simple/b2cstore/service/impl/DefaultB2CStoreService.java

package com.hybris.simple.b2cstore.service.impl;

import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;
import java.util.List;

import javax.annotation.Resource;
import com.hybris.simple.b2cstore.service.B2CStoreService;
import com.hybris.simple.core.model.SampleB2CStoreModel;

public class DefaultB2CStoreService implements B2CStoreService
{
   @Resource
   private ModelService modelService;

    @Resource
    private FlexibleSearchService flexibleSearchService;

    @Override
    public boolean createB2CStoreDetails(List<SampleB2CStoreModel> storeModelList)
    {
        boolean isAnyNewStoreCreated = false;

        for (SampleB2CStoreModel incomingStoreModel : storeModelList) {
            SampleB2CStoreModel exampleStoreModel = new SampleB2CStoreModel();
            exampleStoreModel.setStoreId(incomingStoreModel.getStoreId());
            List<SampleB2CStoreModel> existingStores = flexibleSearchService.getModelsByExample(exampleStoreModel);
            if (existingStores.isEmpty()) {
                SampleB2CStoreModel newStoreModel = modelService.create(SampleB2CStoreModel.class);
                newStoreModel.setStoreId(incomingStoreModel.getStoreId());
                newStoreModel.setStoreLocation(incomingStoreModel.getStoreLocation());
                newStoreModel.setStoreName(incomingStoreModel.getStoreName());
                newStoreModel.setStoreOwner(incomingStoreModel.getStoreOwner());
                newStoreModel.setStoreGstNumber(incomingStoreModel.getStoreGstNumber());
                modelService.save(newStoreModel);

                isAnyNewStoreCreated = true;
            }
        }
        return isAnyNewStoreCreated;
    }
}

Step 8:

Pass the provided JSON to a URL using an HTTP request, you would typically use a POST method, as you're sending data to the hybrisdatabase.

After successfully posting data from the API in SAP Hybris (SAP Commerce), you can verify that the data has been stored and processed correctly by checking the Backoffice

When you try to post data same store Id through the API and encounter the message "Store already exists with ID:" in response

If the storeId is empty or null,you will be encounter the message"Store ID cannot be empty for store:" in response

Comments

Anonymous said…
Good Content
Sandhya said…
Good InformationšŸ‘keep sharing..
Anonymous said…
Keep sharing,good information,thank u bro
Anonymous said…
Your doing good work , advance congratulation for upcoming best blog on sap commerece

Popular posts from this blog

Latest SAP Commerce (Hybris) Interview Questions

Steps to Install SAP Commerce Cloud 2211/Install SAP Hybris 2105 to 2211

OCC Webservices in SAP Commerce(hybris) 2211 using YOCC template – Part 1