Advanced Configurations of OCC Webservices in SAP Commerce 2211 Using YOCC Template – Part 2

Go to Part 1 OCC: OCC Webservices in SAP Commerce (Hybris) 2211 – Part 1


Step 11:

In this step, you are adding a bean definition for B2CStoreDataList in samplefacades-beans.xml This bean will hold a list of B2CStoreData objects, which are returned by passing a storeId.


Location:/samplefacades/resources/samplefacades-beans.xml
<bean class="com.hybris.simple.facades.B2CStoreDataList">
    <property name="b2cstore" type="java.util.List<com.hybris.simple.facades.B2CStoreData>" />
</bean>

Step 12:

In this step, we will create the B2CStorePopulator to map data from the SampleB2CStoreModel to B2CStoreData.


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

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 B2CStorePopulator implements Populator<SampleB2CStoreModel,B2CStoreData> { @Override public void populate(SampleB2CStoreModel source, B2CStoreData target) throws ConversionException { target.setStoreId(source.getStoreId()); target.setStoreName(source.getStoreName()); target.setStoreLocation(source.getStoreLocation()); target.setStoreGstNumber(source.getStoreGstNumber()); target.setStoreOwner(source.getStoreOwner()); } }
Step 13:

In this step, we will configure the b2CSampleStoreConverter and b2CStorePopulator in the samplecore-spring.xml file.


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

Converter: The converter contains a chain of populators and uses populators to convert data from the source to the target. It creates new instances of Data objects.



Populator: The populator fills the data object by getting the data from the model.

<alias name="b2CSampleStoreConverter" alias="b2CStoreConverter" />
<bean id="b2CSampleStoreConverter" parent="abstractPopulatingConverter">
<property name="targetClass" value="com.hybris.simple.facades.B2CStoreData"/>
    <property name="populators">
        <list merge="true">
            <ref bean="b2CStorePopulator" />
        </list>
    </property>
</bean>

<bean id="b2CStorePopulator" class="com.hybris.simple.facades.populators.B2CStorePopulator" />

Step 14:

In this step, we will implement the DefaultB2CStoreFacade class, which provides the actual logic for fetching store details using the B2CStoreFacade interface.


Location: /samplecore/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
    AbstractPopulatingConverter  b2CStoreConverter;

    @Resource
    private B2CStoreService b2CStoreService;

    @Override
    public List<B2CStoreData> getB2CStoreDetailsById(final String storeId) {
        List b2CStoreModels = b2CStoreService.getB2CStoreDetailsById(storeId);
        return CollectionUtils.isNotEmpty(b2CStoreModels) ? b2CStoreConverter.convertAll(b2CStoreModels) : Collections.emptyList();
    }

}
  

Step 15: Configure the DefaultB2CStoreFacade in samplefacades-spring.xml
Location: /samplefacades/resources/samplefacades-spring.xml

<alias name="defaultB2CStoreFacade" alias="b2cStoreFacade" />
<bean id="defaultB2CStoreFacade" class="com.hybris.simple.facades.b2cstore.facade.impl.DefaultB2CStoreFacade" />

Step 16: Configure the B2CStoreDataWSDTO bean in sampleocc-beans.xml
Location: /sampleocc/resources/sampleocc-beans.xml
<bean class="com.hybris.simple.facades.B2CStoreDataWSDTO">
    <property name="storeId" type="String" />
    <property name="storeName" type="String" />
    <property name="storeLocation" type="String" />
    <property name="storeOwner" type="String" />
    <property name="storeGstNumber" type="String" />
</bean>
  

Step 17: Do ant all in platform, After the build completes, verify that the B2CStoreDataWSDTO.java file is generated or not.

Step 18:

Configure the B2CStoreDataListWSDTO bean in sampleocc-beans.xml. This bean holds a list of B2CStoreDataWSDTO objects.


Location: /sampleocc/resources/sampleocc-beans.xml
<bean class="com.hybris.simple.facades.B2CStoreDataListWSDTO">
    <property name="b2cstore" 
        type="java.util.List<com.hybris.simple.facades.B2CStoreDataWSDTO>" />
</bean>
  

Step 19:

Create the SampleB2CStoreController in the com.hybris.occ.controllers package. This controller defines an endpoint to fetch B2C Store details by storeId.


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

import de.hybris.platform.webservicescommons.mapping.DataMapper;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

    @Resource
    private DataMapper dataMapper;

    @RequestMapping(value = "/{storeId}", method = RequestMethod.GET)
    @ResponseBody
    public B2CStoreDataListWSDTO getb2CStoreDetailsById(
            @PathVariable final String storeId,
            @RequestParam(defaultValue = "DEFAULT") final String fields) {
        if (storeId == null || storeId.trim().isEmpty()) {
            return new B2CStoreDataListWSDTO();
        }
        B2CStoreDataList b2CStoreDataList = new B2CStoreDataList();
        b2CStoreDataList.setB2cstore(b2cStoreFacade.getB2CStoreDetailsById(storeId));
        return dataMapper.map(b2CStoreDataList, B2CStoreDataListWSDTO.class, fields);
    }
}
  

Step 20:

This step involves adding the configuration for b2CStoreFieldMapper in the sampleocc-web-spring.xml file. Define fieldmapper to map fields between B2CStoreData and B2CStoreDataWSDTO.



Location: /sampleocc/resources/occ/v2/sampleocc/web/spring/sampleocc-web-spring.xml
<bean id="b2CStoreFieldMapper" parent="fieldMapper">
    <property name="sourceClass" value="com.hybris.simple.facades.B2CStoreData" />
    <property name="destClass" value="com.hybris.simple.facades.B2CStoreDataWSDTO" />
</bean>

Step 21: Adding FieldSet Level Mappings

In this sampleocc-web-spring.xml, the B2CStoreDataWSDTOis used to represent a single store, while the B2CStoreDataListWSDTO is used to represent a collection of stores. The level mappings define the different fields that should be included based on the request level (BASIC, DEFAULT, FULL).



Location: /sampleocc/resources/occ/v2/sampleocc/web/spring/sampleocc-web-spring.xml
<bean parent="fieldSetLevelMapping" id="b2CStoreDataWSDTOFieldSetLevelMapping">
    <property name="dtoClass" value="com.hybris.simple.facades.B2CStoreDataWSDTO" />
    <property name="levelMapping">
        <map>
            <entry key="BASIC" value="storeId,storeName" />
            <entry key="DEFAULT" value="storeId,storeName,storeOwner" />
            <entry key="FULL" value="storeId,storeName,storeOwner,storeLocation,storeGstNumber" />
        </map>
    </property>
</bean>

<bean parent="fieldSetLevelMapping" id="b2CStoreDataListWSDTOFieldSetLevelMapping">
    <property name="dtoClass" value="com.hybris.simple.facades.B2CStoreDataListWSDTO" />
    <property name="levelMapping">
        <map>
            <entry key="BASIC" value="b2cstore(BASIC)"/>
            <entry key="DEFAULT" value="b2cstore(DEFAULT)"/>
            <entry key="FULL" value="b2cstore(FULL)"/>
        </map>
    </property>
</bean>
  

Step 22: Do ant all in platform, After the build completes,make hybris server up.Add some storeId through backoffice or Impex
Pass the storeId and fields like BASIC,FULL,DEFAULT

Comments

Anonymous said…
Very informative, thanks for sharing
Anonymous said…
Very helpful bro, thanks for sharing.
Anonymous said…
Great documentation
Anonymous said…
Nice Blog and good content
Anonymous said…
Nice content please create for payment integration and checkout flow also
Anonymous said…
Hi, I am getting this error. I think some step you missed for defining Interface. controller.java:9: error: package org.training.facades.facade does not exist
[yjavac] import org.training.facades.facade.B2CStoreFacade;
[yjavac] ^
[yjavac] C:\spartacussetup\saphybris\HybrisSuite\hybris\bin\custom\training\testocc\src\com\shashi\occ\controllers\SampleB2CStoreController.java:18: error: cannot find symbol
[yjavac] private B2CStoreFacade b2CStoreFacade;
[yjavac] ^
[yjavac] symbol: class B2CStoreFacade
[yjavac] location: class SampleB2CStoreController
please add your customfacade extension in extensioninfo.xml of customocc, then do ant clean all ,it will pick . thank you i hope you go it
Anonymous said…
Please make a document on installing a custom extension using ycommercewebserices as template for hybris version 2205 or 2211 .

Since 2205 and 2211 doesn't contain ycommercewebservices extension in localextensions.xml, I tried to create an extension using command "ant extgen" (choosing ycommercewebservices) and generated extension added to localextensions.xml .but I failed to access swagger as well as Spartacus site
okay sure i shall make document on it ,dont worry

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