* RelationTypes allow you to reflect scenarios like various products belonging into several categories and vice versa.
* New table will be created to store references of related items.
Requirement:-
Create one to many relation to customer and products model. Display all the products of the customer in the frontend.
Step 1:-
Define relation b/w Customer & Product model. Trainigcore-items.xml
<relations>
<relation localized="false" code="Customer2ProductRelation"
autocreate="true" generate="true">
<deployment table="Customer2Product" typecode="11722" />
<sourceElement type="Customer" qualifier="customer"
cardinality="many" collectiontype="set">
<description>Customer</description>
</sourceElement>
<targetElement type="Product"
qualifier="trainingproductlist" cardinality="many" collectiontype="set">
<description>Product</description>
</targetElement>
</relation>
</relations>
|
=>Do ant build & start the server and do an update.
Step 2:- Create DAO to get products to that customer.
public class TrainingProductDaoImpl implements TrainingProductDao
{
private static final Logger LOG = Logger.getLogger(TrainingProductDaoImpl.class);
private FlexibleSearchService flexibleSearchService;
private UserService userService;
@Override
public List<ProductModel> getCustomerProductDetails()
{
final CustomerModel currentUser = (CustomerModel) userService.getCurrentUser();
final String uid = currentUser.getUid();
final String query = "SELECT {" + CustomerModel.PK + "} FROM {" + CustomerModel._TYPECODE + "} where {" + CustomerModel.UID
+ "}='" + uid + "'";
final FlexibleSearchQuery searchQuery = new FlexibleSearchQuery(query.toString());
final SearchResult searchResult = getFlexibleSearchService().search(searchQuery);
final List<CustomerModel> model = searchResult.getResult();
final Set<ProductModel> productList = model.get(0).getTrainingproductlist();
final List<ProductModel> messageList = new ArrayList<ProductModel>(productList);
return messageList;
}
}
|
Step 3:- Create service class
public class TrainingProductServiceImpl implements TrainingProductService
{
private TrainingProductDao trainingProductDao;
private static final Logger LOG = Logger.getLogger(TrainingProductServiceImpl.class);
@Override
public List<ProductModel> getCustomerProductDetails()
{
LOG.info("########################### ProductServiceImpl #######################");
return TrainingProductDao.getCustomerProductDetails();
}
}
|
Step 4:-
public class TrainingProductFacadeImpl implements TrainingProductFacade
{
private TrainingProductService TrainingProductService;
private Converter<ProductModel, ProductData> productConverter;
@Override
public List<ProductData> getCustomerProductDetails()
{
LOG.info("####################### ProductFacadeImpl ###################");
final List<ProductModel> productModel =
getTrainingProductService().getCustomerProductDetails();
final List<ProductData> productData = productConverter.convertAll(productModel);
return productData;
}
}
|
=>Here I am using OOTB converter [productConverter] to populated data.
=>Here I just inject the bean and called the converter.
Step 5:- Create method in the controller to handle the request.
@Controller
@RequestMapping(value = "/Training")
public class TrainingProductDetailsController extends AbstractPageController
{
@Autowired
private TrainingProductFacade trainingProductFacade;
private static final String PRODUCT_DETAILS = "TrainingProducts";
@RequestMapping(value = "/customerProducts", method = RequestMethod.GET)
public String getCustomerProducts(final Model model) throws CMSItemNotFoundException
{
if (LOG.isDebugEnabled())
{
LOG.debug("####### ProductDetailsController getCustomerProducts() method ");
}
final List<ProductData> TrainingCustomerProductData =
TrainingProductFacade.getCustomerProductDetails();
model.addAttribute("trainingCustomerProductData", TrainingCustomerProductData);
storeCmsPageInModel(model, getContentPageForLabelOrId(PRODUCT_DETAILS));
setUpMetaDataForContentPage(model, getContentPageForLabelOrId(PRODUCT_DETAILS));
return ControllerConstants.Views.Pages.Product.trainingCustomerProductDetails;
}
}
|
=>Add this in ControllerConstants class
String trainingCustomerProductDetails = "pages/product/TrainingCustomerProductDetails";
Step 6:- Create JSP to display data
<%@ 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" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="nav" tagdir="/WEB-INF/tags/responsive/nav" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="product" tagdir="/WEB-INF/tags/responsive/product" %>
<template:page pageTitle="${pageTitle}">
<h2>Customer Specific Products</h2>
<c:forEach items="${TrainingCustomerProductData}" var="product">
<product:productListerItem product="${product}"/>
</c:forEach>
</template:page>
|
Step 7:- Add products to the Customer in Backoffice & give the request
Nice article brother !!!
ReplyDeletekeep posting new posts regularly.