OCC stands for Omni commerce connect. It is used to expose Hybris API to third party system using Rest Webservices.

Here, I will expose studentId as Hybris api to third party system like Swagger. It will fetch entire student details based on studentId and we can see the student details in Swagger.

Here are the steps below:

Step1:

Create new custom extension using  “ant extgen ” command ( choose template as “ycommercewebservices”).

Step 2:

I have generated extension name as “delltrainingcommercewebservices”. Adding extension name in  localextensions.xml as below.

<extension name='delltrainingcommercewebservices'/>

Remove “ycommercewebservices” and “ycommercewebservicestest” and perform build using ant all.

Import  above extension in  eclipse id.

Step3:

Create item type in delltrainingcommercewebservices-items.xml

<itemtype code="StudentDetails" autocreate="true" generate="true">
			<deployment table="StudentTable" typecode="15020"/>
			<attributes>
				<attribute qualifier="studentId" type="java.lang.String">
					<description>Student Id</description>
					<modifiers unique="true" read="true" write="true" search="true"/>
					<persistence type="property"/>
				</attribute>

				<attribute qualifier="studentName" type="java.lang.String">
					<description>Student Name</description>
					<modifiers  read="true" write="true" search="true" />
					<persistence type="property"/>
				</attribute>

				<attribute qualifier="studentPlace" type="java.lang.String">
					<description>Students Place name</description>
					<modifiers  read="true" write="true" search="true"/>
					<persistence type="property"/>
				</attribute>

				<attribute qualifier="studentGuardian" type="java.lang.String">
					<description>Student Guardian Name</description>
					<modifiers  read="true" write="true" search="true"/>
					<persistence type="property"/>
				</attribute>
			</attributes>
		</itemtype>

Perform ant all and respectively system update for generating model class ,table etc.

Insert value in table using impex below.

INSERT_UPDATE StudentDetails;studentId[unique=true];studentName;studentPlace;studentGuardian
;101;Guldu;Bangalore;Rahul
;102;Nayna;Hyderabad;Vivek

Step 4 :

Create Interface StudentDao  in trainingcore package.

package org.training.core.Dao;

import de.hybris.platform.servicelayer.internal.dao.Dao;
import org.anchor.model.StudentDetailsModel;

import java.util.List;

public interface StudentDao extends Dao
{
 List<StudentDetailsModel> getStudentDetailsByCode(String studentId);
}

Step 5:

Creation of Dao Implementation class.

package org.training.core.Dao.impl;

import de.hybris.platform.servicelayer.internal.dao.AbstractItemDao;
import de.hybris.platform.servicelayer.search.SearchResult;
import de.hybris.platform.servicelayer.util.ServicesUtil;
import org.training.core.Dao.StudentDao;
import org.training.model.StudentDetailsModel;
import org.apache.log4j.Logger;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class StudentDaoImpl extends AbstractItemDao implements StudentDao
{
    private static final Logger LOGGER= Logger.getLogger(StudentDaoImpl.class);
    private static final String STUDENT_QUERY="SELECT{" +StudentDetailsModel.PK+ "}FROM{"+StudentDetailsModel._TYPECODE+ "}WHERE{" +StudentDetailsModel.STUDENTID+ "}=?code";

    @Override
    public List<StudentDetailsModel> getStudentDetailsByCode(String studentId)
    {
        ServicesUtil.validateParameterNotNull(studentId,"Student ID must not be null");
        final Map<String, Object> params =new HashMap<>();
        params.put("code",studentId);
        LOGGER.info(getFlexibleSearchService().search(STUDENT_QUERY,params).getClass());
        final SearchResult<StudentDetailsModel> student=getFlexibleSearchService().search(STUDENT_QUERY,params);
        return student.getResult()==null ? Collections.emptyList(): student.getResult();
    }
}

Step 6:

Spring bean configuration of Dao in trainingcore-spring.xml file.

<alias name="studentDao" alias="studentDao"/>
	<bean id="studentDao" class="org.training.core.Dao.impl.StudentDaoImpl">
		<property name="flexibleSearchService" ref="flexibleSearchService"/>
		<property name="modelService" ref="modelService"/>
	</bean>

Step 7:

Creation of  Service class named as StudentServices.

package org.training.core.service;

import org.training.core.Dao.StudentDao;
import org.training.model.StudentDetailsModel;

import javax.annotation.Resource;
import java.util.List;

public class StudentServices
{
    @Resource
    private StudentDao studentDao;

    public List<StudentDetailsModel> getStudentDetails(final String studentId)
    {
        List<StudentDetailsModel> studentModels=studentDao.getStudentDetailsByCode(studentId);
        return studentModels;
    }
}

Step 8:

Bean Id configuration of Service class in trainingcore-spring.xml.

<bean id="studentServices" class="org.training.core.service.StudentServices"/>

Step 9:

In trainingfacades-beans.xml ,configure Data class.

<bean class="de.hybris.platform.commercefacades.Student.data.StudentData">
		<property name="studentId" type="java.lang.String"/>
		<property name="studentName" type="java.lang.String"/>
		<property name="studentPlace" type="java.lang.String"/>
		<property name="studentGuardian" type="java.lang.String"/>
</bean>

Perform ant all for generating data class.

Step 10:

Create StudentPopulator by implementing Populator Interface where taking source as StudentDetailsModel and target as StudentData.

package org.training.facades.populators;

import de.hybris.platform.commercefacades.Student.data.StudentData;
import de.hybris.platform.converters.Populator;
import de.hybris.platform.servicelayer.dto.converter.ConversionException;
import org.training.model.StudentDetailsModel;

public class StudentPopulator implements Populator<StudentDetailsModel, StudentData>
{

    @Override
    public void populate(final StudentDetailsModell source, final StudentData target)
            throws ConversionException
    {
        target.setStudentId(source.StudentId());
        target.setStudentPlace(source.getStudentPlace());
        target.setStudentName(source.getStudentName());
        target.setStudentGuardian(source.getStudentGuardian());
    }
}

Step 11:

Create StudentFacade Interface in trainingfacades extension.

package org.training.facades.student;

import de.hybris.platform.commercefacades.Student.data.StudentData;

import java.util.List;

public interface StudentFacade
{
    public List<StudentData> getStudentDetails(String studentId);
}

Step 12:

Create StudentFacadeImpl by implementing StudentFacade Interface.

package org.trainng.facades.student.impl;

import de.hybris.platform.commercefacades.Student.data.StudentData;
import de.hybris.platform.converters.Converters;
import de.hybris.platform.servicelayer.dto.converter.Converter;

import java.util.List;

import org.training.core.service.StudentServices;
import org.training.facades.student.StudentFacade;
import org.training.model.StudentDetailsModel;
public class StudentFacadeImpl implements StudentFacade
{

	private StudentServices studentServices;
	private Converter<StudentDetailsModel, StudentData> studentConverter;

	@Override
	public List<StudentData> getStudentDetails(final String studentId)
	{
		final List<StudentDetailsModel> studentDetailsModels = studentServices.getStudentDetails(studentId);
		return Converters.convertAll(studentDetailsModels, getStudentConverter());
	}

	public StudentServices getStudentServices()
	{
		return studentServices;
	}

	public void setStudentServices(final StudentServices studentServices)
	{
		this.studentServices = studentServices;
	}

	public Converter<StudentDetailsModel, StudentData> getStudentConverter()
	{
		return studentConverter;
	}

	public void setStudentConverter(final Converter<StudentDetailsModel, StudentData> studentConverter)
	{
		this.studentConverter = studentConverter;
	}


}

Step 13:

Declare Facade, Converter and Populator in trainingfacades-spring.xml.

<alias name="studentFacade" alias="studentFacade"/>
	<bean id="studentFacade" class="org.training.facades.student.impl.StudentFacadeImpl">
		<property name="studentServices" ref="studentServices"/>
		<property name="studentConverter" ref="studentConverter"/>
	</bean>

    <alias name="studentConverter" alias="studentConverter"/>
    <bean id="studentConverter" parent="abstractPopulatingConverter">
		<property name="targetClass" value="de.hybris.platform.commercefacades.Student.data.StudentData"/>
		<property name="populators">
			<list>
				<ref bean="studentPopulator"/>
			</list>
		</property>
	</bean>

	<bean id="studentPopulator" class="org.training.facades.populators.StudentPopulator"/>

Step 14:

Define bean configuration of StudentDataWSDTO, StudentDataListWSDTO, StudentsDataList  in delltrainingcommercewebservices-beans.xml.

<bean class="de.hybris.platform.commercewebservicescommons.dto.Student.StudentDataWSDTO">
		<property name="studentId" type="java.lang.String"/>
		<property name="studentName" type="java.lang.String"/>
		<property name="studentPlace" type="java.lang.String"/>
		<property name="studentGuardian" type="java.lang.String"/>
	</bean>

	<bean class="de.hybris.platform.commercewebservicescommons.dto.Student.StudentDataListWSDTO">
		<property name="student" type="java.util.List &lt;de.hybris.platform.commercewebservicescommons.dto.Student.StudentDataWSDTO>"/>
	</bean>
	
	<bean class="org.anchor.queues.data.StudentDataList">
		<property name="student" type="java.util.List &lt;de.hybris.platform.commercefacades.Student.data.StudentData>"/>
	</bean>

Perform ant all.

Step 15:

Mapping of Facades extension in extensioninfo.xml of delltrainingcommercewebservices.

<requires-extension name="trainingfacades"/>

Configure trainingfacades extension in Build path of StudentController class.

Create StudentController extending BaseCommerceController by injecting  StudentFacade.

package org.training.v2.controller;
import de.hybris.platform.commercewebservicescommons.dto.Student.StudentDataListWSDTO;
import de.hybris.platform.webservicescommons.swagger.ApiBaseSiteIdParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import org.training.facades.student.StudentFacade;
import org.training.queues.data.StudentDataList;
import org.apache.log4j.Logger;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@Controller
@RequestMapping(value="/{baseSiteId}")
@Api(tags="Student")
public class StudentController extends BaseCommerceController
{
private static final Logger LOGGER= Logger.getLogger(StudentController.class);
@Resource(name="studentFacade")
private StudentFacade studentFacade;

@Secured("ROLE_TRUSTED_CLIENT")

    @RequestMapping(value="/{studentId}", method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(nickname="getStudentDetails", value="Get a Specific Student Details",
                        notes="Return a specific Student based on studentId",authorizations={@Authorization
(value="oauth2_client_credentials")})

    @ApiBaseSiteIdParam
    public StudentDataListWSDTO getStudentDetails(@ApiParam(value="studentId", required=true)

    @PathVariable
     final String studentId,@ApiParam(value="Response configuaration. This is the list of filelds that should be returned in the response body",allowableValues="BASIC,DEFAULT,FULL")
       @RequestParam(defaultValue=DEFAULT_FIELD_SET) final String fields)
    {
        LOGGER.info("Student is "+studentId);
        final StudentDataList studentDataList=new StudentDataList();
        studentDataList.setStudent(studentFacade.getStudentDetails(studentId));
        return getDataMapper().map(studentDataList,StudentDataListWSDTO.class,fields);
    }
}

 

Step 16:

Mapping of Data to WSDTO in dto-mappings-v2-spring.xml.

<bean id="studentFieldMapper" parent="fieldMapper">
		<property name="sourceClass" value="de.hybris.platform.commercefacades.Student.data.StudentData"/>
		<property name="destClass" value="de.hybris.platform.commercewebservicescommons.dto.Student.StudentDataWSDTO"/>
</bean>

Step 17:

Defining scopes like BASIC, DEFAULT and  FULL in dto-level-mappings-v2-spring.xml

<bean parent="fieldSetLevelMapping" id="studentDataWSDTOFieldSetLevelMapping">
		<property name="dtoClass"
				  value="de.hybris.platform.commercewebservicescommons.dto.Student.StudentDataWSDTO"/>
		<property name="levelMapping">
			<map>
				<entry key="BASIC" value="studentId,studentName,studentPlace"/>
				<entry key="DEFAULT" value="studentId,studentName"/>
				<entry key="FULL" value="studentId,studentName,studentPlace,studentGuardian"/>
			</map>
		</property>
</bean>

Step 18:

Authorization by providing role for OAuth mechanism.

INSERT_UPDATE OAuthClientDetails;clientId[Unique=true];resourceIds;scope;authorizedGrantTypes;clientSecret;authorities
;Student18;hybris;besic;authorization_code,refresh_token,password,client_credentials;Password11;ROLE_TRUSTED_CLIENT

Step 19:

Start the server by performing ant clean all and Open Swagger Api by hitting url “https://localhost:9002/delltrainingcommercewebservices/v2/swagger-ui.html”. Here “delltrainingcommercewebservices”  is name of extension generated by “ant extgen” command.

 

Conclusion:

Therefore, follow above mentioned steps for exposing API to third party system like Swagger, Postman etc. in Hybris.

 

If you have any query related to OCC then you can reach me out by commenting here.

 

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x