Here I am doing a task to get data from DB in Hybris flow.
Follow the below steps,
1. Create an Item in trainingcore-items.xml
2. Create data class in trainingfacades-beans.xml
3. Create DAO interface and implementation classes and configure
4. Create a Service interface , implementation classes and configure
5. Create populator and configure
6. Create a converter and add populator to the converter
7. Create a facade interface, implementation classes and configure. Call converter in the facade
8. Write Controller call facade.
9. Create a JSP and give in Controller to render data.
10. Create ContentPage with labelid same as RequestMapping Url.
Step 1:- Create an Item in trainingcore-items.xml
<typegroup name="HybrisInstructive">
<itemtype code="TrainingProduct" extends="GenericItem"
jaloclass="com.training.core.jalo.TrainingProduct" autocreate="true" generate="true">
<deployment table="TrainingProduct" typecode="15111" />
<attributes>
<attribute type="java.lang.String" qualifier="code" autocreate="true" generate="true">
<persistence type="property" />
<modifiers search="true" optional="false" unique="true" />
</attribute>
<attribute type="java.lang.String" qualifier="name" autocreate="true" generate="true">
<persistence type="property" />
<modifiers/>
</attribute>
<attribute type="java.lang.String" qualifier="description"autocreate="true" generate="true">
<persistence type="property" />
<modifiers/>
</attribute>
</attributes>
</itemtype>
</typegroup>
|
* Do ant build and start server
Step 2:- Create data class in trainingfacades-beans.xml
<bean class="de.hybris.platform.commercefacades.user.data.TrainingProductData">
<property name="code" type="java.lang.String"/>
<property name="name" type="java.lang.String"/>
<property name="description" type="java.lang.String"/>
</bean>
|
* Do build, start the server.
Step 3:- Create a DAO interface and implementation classes and configure
public interface TrainingProductDao
{
public List<TrainingProductModel> getTrainingProductDetails();
}
|
package com.training.hybris.dao;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;
import de.hybris.platform.servicelayer.search.SearchResult;
import java.util.List;
import org.apache.log4j.Logger;
import com.training.core.model.TrainingProductModel;
public class TrainingProductDaoImpl implements TrainingProductDao
{
private static final Logger LOG = Logger.getLogger(TrainingProductDaoImpl.class);
private FlexibleSearchService flexibleSearchService;
@Override
public List<TrainingProductModel> getTrainingProductDetails()
{
LOG.info("############################# Training ProductDaoImpl ########");
final String query = "SELECT {PK} FROM {TrainingProduct}";
final FlexibleSearchQuery searchQuery = new FlexibleSearchQuery(query.toString());
final SearchResult searchResult = getFlexibleSearchService().search(searchQuery);
return searchResult.getResult();
}
|
Configure:- trainingcore-spring.xml
<bean id="trainingProductDao" class="com.training.hybris.dao.TrainingProductDaoImpl">
<property name="flexibleSearchService" ref="flexibleSearchService"/>
</bean>
|
Step 4:- Create Service interface , implementation classes and configure
public interface TrainingProductService
{
public List<TrainingProductModel> getTrainingProductDetails();
}
|
package com.training.hybris.service;
import java.util.List;
import org.apache.log4j.Logger;
import com.training.hybris.dao.TrainingProductDao;
import com.training.core.model.TrainingProductModel;
public class TrainingProductServiceImpl implements TrainingProductService
{
private TrainingProductDao trainingProductDao;
private static final Logger LOG = Logger.getLogger(TrainingProductServiceImpl.class);
@Override
public List<TrainingProductModel> getTrainingProductDetails()
{
LOG.info("########################### TrainingProductServiceImpl ###############");
return trainingProductDao.getTrainingProductDetails();
}
|
Configure:- trainingcore-spring.xml
<bean id="trainingProductService" class="com.training.hybris.service.TrainingProductServiceImpl">
<property name="trainingProductDao" ref="trainingProductDao"/>
</bean>
|
Step 5:- Create populator and configure
package com.training.facade.populator;
import de.hybris.platform.commercefacades.user.data.TrainingProductData;
import de.hybris.platform.converters.Populator;
import de.hybris.platform.servicelayer.dto.converter.ConversionException;
import org.apache.log4j.Logger;
import org.springframework.util.Assert;
import com.training.core.model.TrainingProductModel;
public class TrainingProductDataPopulator implements Populator<TrainingProductModel, TrainingProductData>
{
private static final Logger LOG = Logger.getLogger(TrainingProductDataPopulator.class);
@Override
public void populate(final TrainingProductModel source, final TrainingProductData target) throws ConversionException
{
LOG.info("############### TrainingProductDataPopulator ###########");
Assert.notNull(source, "Parameter source can not be null");
Assert.notNull(target, "Parameter target can not be null");
target.setCode(source.getCode());
target.setName(source.getName());
target.setDescription(source.getDescription());
}
}
|
Configure:- trainingfacades-spring.xml
<bean id="trainingProductDataPopulator" class="com.training.facade.populator.TrainingProductDataPopulator">
</bean>
|
Step 6:- Create a converter and add populator to converter.
<bean id="trainingProductDataConverter" parent="abstractPopulatingConverter">
<property name="targetClass"
value="de.hybris.platform.commercefacades.user.data.TrainingProductData"/>
<property name="populators">
<list>
<ref bean="trainingProductDataPopulator"/>
</list>
</property>
</bean>
|
Step 7:-
Create facade interface, implementation classes and configure. Call converter in facade
Create facade interface, implementation classes and configure. Call converter in facade
public interface TrainingProductFacade
{
public List<TrainingProductData> getTrainingProductDetails();
}
|
package com.training.hybris.facade;
import de.hybris.platform.commercefacades.user.data.TrainingProductData;
import de.hybris.platform.servicelayer.dto.converter.Converter;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import com.training.hybris.service.TrainingProductService;
import com.training.core.model.TrainingProductModel;
public class TrainingProductFacadeImpl implements TrainingProductFacade
{
private TrainingProductService trainingProductService;
@Resource(name = "trainingProductDataConverter")
private Converter<TrainingProductModel, TrainingProductData> trainingProductDataConverter;
private static final Logger LOG = Logger.getLogger(TrainingProductFacadeImpl.class);
@Override
public List<TrainingProductData> getTrainingProductDetails()
{
LOG.info("####################### TrainingProductFacadeImpl ###################");
final List<TrainingProductData> products = new ArrayList<TrainingProductData>();
final List<TrainingProductModel> model = getTrainingProductService().getTrainingProductDetails();
final List<TrainingProductData> trainingProductData = trainingProductDataConverter.convertAll(model);
products.addAll(trainingProductData );
return products;
}
|
Configure:- trainingfacades-spring.xml
<bean id="trainingProductFacade" class="com.training.hybris.facade.TrainingProductFacadeImpl">
<property name="trainingProductService" ref="trainingProductService"/>
</bean>
|
Step 8:- Write Controller call facade
package com.training.storefront.controllers.pages;
import de.hybris.platform.acceleratorstorefrontcommons.controllers.pages.AbstractPageController;
import de.hybris.platform.cms2.exceptions.CMSItemNotFoundException;
import de.hybris.platform.commercefacades.user.data.TrainingProductData;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.training.hybris.facade.TrainingProductFacade;
import com.training.storefront.controllers.ControllerConstants;
@Controller
@RequestMapping(value = "/training")
public class TrainingProductDetailsController extends AbstractPageController
{
@Autowired
private TrainingProductFacade trainingProductFacade;
private static final String UPDATE_OLD_PASSWORD_CMS_PAGE = "trainingProducts";
private static final Logger LOG = Logger.getLogger(TrainingProductDetailsController.class);
@RequestMapping(value = "/trainingProducts", method = RequestMethod.GET)
public String getTrainingProductDetails(final Model model) throws CMSItemNotFoundException
{
LOG.info("######## TrainingProductDetailsController updateOldPassword() method ####");
final List<TrainingProductData> trainingProductData = trainingProductFacade.getTrainingProductDetails();
model.addAttribute("trainingProductData", trainingProductData );
storeCmsPageInModel(model, getContentPageForLabelOrId(UPDATE_OLD_PASSWORD_CMS_PAGE));
setUpMetaDataForContentPage(model, getContentPageForLabelOrId(UPDATE_OLD_PASSWORD_CMS_PAGE));
return ControllerConstants.Views.Pages.Product.TrainingProductDetails;
}
|
* Add jsp in ControllersConstants class
String TrainingProductDetails = "pages/product/trainingProductDetails";
* Do ant clean all, Start to sever.
Step 9:-
Create a JSP and give in Controller to render data.
trainingProductDetails.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.lang.*" %>
<%@ taglib prefix="template" tagdir="/WEB-INF/tags/responsive/template" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<template:page pageTitle="${pageTitle}">
<c:if test="${not empty trainingProductData}">
<table>
<tr>
<th><spring:theme code="electronics.trainingproduct.code"/></th>
<th><spring:theme code="electronics.trainingproduct.name"/></th>
<th><spring:theme code="electronics.trainingproduct.description"/></th>
</tr>
<c:forEach items="${trainingProductData}" var="trainingProductData">
<tr>
<td>${trainingProductData.code}</td>
<td>${trainingProductData.name}</td>
<td>${trainingProductData.description}</td>
</tr>
</c:forEach>
</table>
</c:if>
</template:page>
|
Step 10:-
Create ContentPage with labelid same as RequestMapping Url. And give data to TrainingProduct item.
$contentCatalog=electronicsContentCatalog
$contentCatalogName=Electronics Content Catalog
$contentCV=catalogVersion(CatalogVersion.catalog(Catalog.id[default=$contentCatalog]),CatalogVersion.version[default=Online])[default=$contentCatalog:Online]
$productCatalog=electronicsProductCatalog
$productCatalogName=Electronics Product Catalog
$productCV=catalogVersion(catalog(id[default=$productCatalog]),version[default='Online'])[unique=true,default=$productCatalog:Online]
$picture=media(code, $contentCV);
$image=image(code, $contentCV);
$media=media(code, $contentCV);
$page=page(uid, $contentCV);
$contentPage=contentPage(uid, $contentCV);
$product=product(code, $productCV)
$category=category(code, $productCV)
$siteResource=jar:de.hybris.platform.electronicsstore.constants.ElectronicsstoreConstants&/electronicsstore/import/sampledata/contentCatalogs/$contentCatalog
$productResource=jar:de.hybris.platform.electronicsstore.constants.ElectronicsstoreConstants&/electronicsstore/import/sampledata/productCatalogs/$productCatalog
$jarResourceCms=jar:de.hybris.platform.electronicsstore.constants.ElectronicsstoreConstants&/electronicsstore/import/sampledata/cockpits/cmscockpit
#update password content page
#
INSERT_UPDATE ContentPage;$contentCV[unique=true];uid[unique=true];name;masterTemplate(uid,$contentCV);label;defaultPage[default='true'];approvalStatus(code)[default='approved']
;;trainingproductdetails;training products page;AccountPageTemplate;trainingProducts;;
|
# Insert Products
INSERT_UPDATE TrainingProduct;code[unique=true];name;description;
;1234;iphone;apple product;;;
;1235;moto;lenovo product ;;;
;1236;mi;mi product;;;
;1237;Nokia;Nokia product;;;
;1238;Samsung;Samsung product;;;
|
Give request to this
https://electronics.local:9002/trainingstorefront/electronics/en/training/trainingProducts
Hi! you have not mentioned the converter service property in the trainingfacades-spring.xml file!
ReplyDeletewhich hybris suit version will this work
ReplyDelete