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

Omnichannel Commerce Connect(OCC), which is a RESTful web services framework provided by SAP Commerce (Hybris).We can expose sap commerce(hybris) apis to external systems.

Requirement : Create a custom OCC endpoint that retrieves detailed store information based on the storeId

Step 1: Run the following command ant extgen to create a new OCC extension in platform
Select Yocc template ,give name of your extension like sampleocc and package like com.hybris.occ

Note: Remove or comment yocc extension ,add your custom occ extension in localextensions.xml and do ant clean all
Step 2: Create SampleB2CStore an item type with attributes like storeId,storeName,storeLocation,storeOwner,storeGstNumber in samplecore-items.xml
<itemtype code="SampleB2CStore" autocreate="true" generate="true">
    <deployment table="SampleStore" typecode="15786" />
    <attributes>
        <attribute qualifier="storeId" type="java.lang.String">
            <description>Unique Store Identifier</description>
            <modifiers unique="true" read="true" search="true" write="true"/>
            <persistence type="property" />
        </attribute>
        <attribute qualifier="storeName" type="java.lang.String">
            <description>Store Name</description>
            <modifiers read="true" write="true" search="true"/>
            <persistence type="property" />
        </attribute>
        <attribute qualifier="storeLocation" type="java.lang.String">
            <description>Store Location Name</description>
            <modifiers read="true" write="true" search="true"/>
            <persistence type="property" />
        </attribute>
        <attribute qualifier="storeOwner" type="java.lang.String">
            <description>Store Owner Name</description>
            <modifiers read="true" write="true" search="true"/>
            <persistence type="property" />
        </attribute>
        <attribute qualifier="storeGstNumber" type="java.lang.String">
            <description>Store GST Number</description>
            <modifiers read="true" write="true" search="true"/>
            <persistence type="property" />
        </attribute>
    </attributes>
</itemtype>

Note :- After Creating an Item-Type ,Do ant all ,make server up and update Hac by selecting samplecore extension

Step 3:

In this step, we will create the B2CStoreDao interface, which will define the method to fetch the store details by storeId.

Location: /samplecore/src/com/hybris/simple/b2cstore/dao/B2CStoreDao.java

package com.hybris.simple.b2cstore.dao;
import de.hybris.platform.servicelayer.internal.dao.Dao;
import java.util.List;
import com.hybris.simple.core.model.SampleB2CStoreModel;

public interface B2CStoreDao extends Dao {
    List<SampleB2CStoreModel> getStoreDetailsById(String storeId);
}
    
Step 4: Implement B2CStoreDao Interface Use the FlexibleSearchService to execute a query that fetches store details based on the provided storeId. Handle cases where no results are found by returning an empty list.
Location: /samplecore/src/com/hybris/simple/b2cstore/dao/impl/DefaultB2CStoreDao.java
package com.hybris.simple.b2cstore.dao.impl;

import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;
import de.hybris.platform.servicelayer.search.SearchResult;
import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
public class DefaultB2CStoreDao implements B2CStoreDao {
        private static final String GET_B2CSTORE_BY_ID = 
            "SELECT{" + SampleB2CStoreModel.PK + "}FROM{" + SampleB2CStoreModel._TYPECODE + "}WHERE{" + SampleB2CStoreModel.STOREID + "}=?storeId";

        @Resource
        private FlexibleSearchService flexibleSearchService;

        @Override
        public List<SampleB2CStoreModel> getStoreDetailsById(
            final String storeId) {
            FlexibleSearchQuery query = new FlexibleSearchQuery(GET_B2CSTORE_BY_ID);
            query.addQueryParameter("storeId", storeId);
            SearchResult<SampleB2CStoreModel> searchResult = flexibleSearchService.search(query);
            return (searchResult == null || searchResult.getResult().isEmpty()) 
                ? Collections.emptyList() 
                : searchResult.getResult();
        }
    }
    
Step 5:

In this step, we will configure the B2CStoreDao in samplecore-spring.xml.

Location:/samplecore/resources/samplecore-spring.xml
<alias name="defaultB2CStoreDao" alias="b2CStoreDao" />
<bean id="defaultB2CStoreDao" class="com.hybris.simple.b2cstore.dao.impl.DefaultB2CStoreDao" />

Step 6:

In this step, we will create the B2CStoreService interface, which will define the method to fetch the store details by storeId.

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
{
  List<SampleB2CStoreModel> getB2CStoreDetailsById(String storeId);
}

Step 7:

In this step, we will implement the DefaultB2CStoreService class, which will provide the implementation for the B2CStoreService interface.

Location: /samplecore/src/com/hybris/simple/b2cstore/service/impl/DefaultB2CStoreService.java
package com.hybris.simple.b2cstore.service.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import com.hybris.simple.b2cstore.dao.B2CStoreDao;
import com.hybris.simple.b2cstore.service.B2CStoreService;
import com.hybris.simple.core.model.SampleB2CStoreModel;

public class DefaultB2CStoreService implements B2CStoreService
{
  @Resource
  private B2CStoreDao b2CStoreDao;

  @Override
  public List<SampleB2CStoreModel> getB2CStoreDetailsById(final String storeId)
  {
    return StringUtils.isBlank(storeId) ? Collections.emptyList() : b2CStoreDao.getStoreDetailsById(storeId);
  }

}

Step 8: Configure the B2CStoreService in samplecore-spring.xml
Location: /samplecore/resources/samplecore-spring.xml
<alias name="defaultB2CStoreService" alias="b2CStoreService" />
<bean id="defaultB2CStoreService" class="com.hybris.simple.b2cstore.service.impl.DefaultB2CStoreService" />

Step 9:

In this step, we will add the data class in samplefacades-beans.xml.

Location:/samplefacades/resources/samplefacades-beans.xml
<bean class="com.hybris.simple.facades.B2CStoreData">
    <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 10: Do ant all in platform, After the build completes, verify that the B2CStoreData.java file is generated or not.

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

Comments

Anonymous said…
Keep doing good work
Anonymous said…
Good information, Thanks for sharing
Anonymous said…
Good information, nice
Anonymous said…
Thank you for sharing
Smm said…
Great job. Thanks for sharing
Anonymous said…
If a requirement to customize a method from CartsController or ProductsController, then in which extension we can customize ?
Could you please make a documents on it
suppose if you choose yocc template , you can extend CartsController in that custom occ extension

Popular posts from this blog

Latest SAP Commerce (Hybris) Interview Questions

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