We will be extending the ProductFacade to create our own facade to add a method to the ProductFacade. And also because we are extending the Product Facade, we will not be needing to create a populator, we can just the OOTB populator.
Creating the DAO
Define the Bean
Inside <my>core-spring.xml, create the following bean:
<alias name="myProductsDao" alias="productDao" />
<bean id="myProductsDao" class="com.my.core.daos.impl.DefaultMyProductDao" parent="defaultProductDao" />
Create the DAO Interface
package com.my.core.daos;
import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.core.model.user.CustomerModel;
import de.hybris.platform.product.daos.ProductDao;
import java.util.List;
public interface MyProductDao extends ProductDao {
List findProductsPlacedByCustomer(CustomerModel customer);
}
Create the DAOImpl
package com.my.core.daos.impl;
import com.my.core.daos.MyProductDao;
import de.hybris.platform.core.model.order.OrderEntryModel;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.core.model.user.CustomerModel;
import de.hybris.platform.product.daos.impl.DefaultProductDao;
import de.hybris.platform.servicelayer.search.SearchResult;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static de.hybris.platform.servicelayer.util.ServicesUtil.validateParameterNotNull;
public class DefaultMyProductDao extends DefaultProductDao implements MyProductDao {
private static final Logger LOG = Logger.getLogger(DefaultMyProductDao.class);
public DefaultMyProductDao(String typecode) {
super(typecode);
}
@Override
public List findProductsPlacedByCustomer(CustomerModel customer) {
// ... Implementation
return result;
}
}
Createing the Service
Define the Bean
Inside <my>core-spring.xml, create the following bean:
<alias alias="productService" name="myProductService"/>
<bean id="myProductService" class="com.my.core.product.impl.MyProductServiceImpl" parent="defaultProductService" />
Create the Service Interface
package com.my.core.product;
import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.core.model.user.CustomerModel;
import de.hybris.platform.product.ProductService;
import java.util.List;
public interface MyProductService extends ProductService {
List getProductsPlacedByCustomer(CustomerModel customer);
}
Create the ServiceImpl
package com.my.core.product.impl;
import com.my.core.daos.MyProductDao;
import com.my.core.product.MyProductService;
import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.core.model.user.CustomerModel;
import de.hybris.platform.product.impl.DefaultProductService;
import javax.annotation.Resource;
import java.util.List;
import static de.hybris.platform.servicelayer.util.ServicesUtil.validateParameterNotNull;
public class MyProductServiceImpl extends DefaultProductService implements MyProductService {
@Resource
private MyProductDao productDao;
@Override
public List getProductsPlacedByCustomer(CustomerModel customer) {
validateParameterNotNull(customer, "Customer model cannot be null");
return getProductDao().findProductsPlacedByCustomer(customer);
}
@Override
protected MyProductDao getProductDao()
{
return productDao;
}
}
Creating the Facade
Define the Bean
Inside <my>facades-spring.xml, create the following bean:
<alias name="myProductVariantFacade" alias="productVariantFacade"/>
<bean id="myProductVariantFacade" class="com.my.facades.product.impl.MyProductVariantFacadeImpl" parent="defaultProductVariantFacade" />
Create the Facade Interface
package com.my.facades.product;
import de.hybris.platform.commercefacades.product.ProductFacade;
import de.hybris.platform.commercefacades.product.data.ProductData;
import java.util.List;
public interface MyProductVariantFacade extends ProductFacade {
List getProductsPlacedByCustomer();
}
Create the FacadeImpl
package com.my.facades.product.impl;
import com.my.core.product.MyProductService;
import com.my.facades.product.MyProductVariantFacade;
import de.hybris.platform.commercefacades.product.data.ProductData;
import de.hybris.platform.commercefacades.product.impl.DefaultProductVariantFacade;
import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.core.model.user.CustomerModel;
import javax.annotation.Resource;
import java.util.List;
public class MyProductVariantFacadeImpl extends DefaultProductVariantFacade implements MyProductVariantFacade {
@Resource
private MyProductService productService;
@Override
protected MyProductService getProductService() {
return productService;
}
@Override
public List getProductsPlacedByCustomer() {
List products = getProductService().getProductsPlacedByCustomer((CustomerModel) getUserService().getCurrentUser());
return getProductConverter().convertAll(products);
}
}
1 Comment
Gil Ji · May 30, 2019 at 4:49 pm
Hello, when the service is created, how can I create or view the generated service url to test it from postman or see it in the browser
Regards!