Sample Apps: Spring data MongoDB and JSF Integration tutorial (PART 5)

Table of Contents:
1. Introduction to sample application (MongoShop Product Catalog)
2. MongoDB schema design and data preparation
3. JSF (PrimeFaces) and Spring data MongoDB Integration
4. Enquriy data with spring data repository and mongotemplate
5. Create, Edit and delete data


Create, Edit and Delete data with Spring data repository


In the last part of this tutorial, we will add create, edit and delete function to the MongoShop Product Catalog application.
The search page is modified. A modal confirm dialogue box is added before the product is physically deleted
updated search.xhtml

 
 
 
 
 
 
 
 
 
  
 
  
  
 Product Search
 
 
  
  
 
 
 
  
 
  
  
 
 
  
  
  
  
  
 
 
 
  
 
 
 
  
 
  
  
 
 
  
 
 
  
 
  
 
 
 
  
 
  
  
 
 
  
 
 
  
 
 
 
 

updated ProductSearchBean.java
package com.borislam.view;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import com.borislam.domain.Product;
import com.borislam.service.ProductService;
@Component
@Scope("session")
public class ProductSearchBean {
 
 private Product selectedProduct;
 
 private ProductSearchCriteria criteria = new ProductSearchCriteria();
 
 private List productList;
 
 
 public Product getSelectedProduct() {
 return selectedProduct;
 }
 public void setSelectedProduct(Product selectedProduct) {
 this.selectedProduct = selectedProduct;
 }
 public List getProductList() {
 return productList;
 }
 public void setProductList(List productList) {
 this.productList = productList;
 }
 public ProductSearchCriteria getCriteria() {
 return criteria;
 }
 public void setCriteria(ProductSearchCriteria criteria) {
 this.criteria = criteria;
 }
 @Autowired
 private ProductService productService;
 
 public void doSearch(ActionEvent event){
 productList= productService.searchByCriteria(criteria);
 }
 
 
 public String doEditDetail() {
 (FacesContext.getCurrentInstance().getExternalContext().getFlash()).put("selected", selectedProduct);
 return "detail.xhtml";
 }
 
 public void doDelete(ActionEvent event){
 try { 
 productService.deleteProduct(selectedProduct);
 
 FacesContext context = FacesContext.getCurrentInstance(); 
 context.addMessage(null, new FacesMessage("Delete Successfully!"));
 }
 catch (DataAccessException e ) {
 FacesContext context = FacesContext.getCurrentInstance(); 
 context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error when deleting product!",null));
 }
 
 }
}

A product detail page is added to view the produc details. Creation and edition of product is done in the product detail page.
detail.xhtml

 
 
 
 
 
 
 
 
 
  
 
  
 
  
  
 Product Details
 
 
  
  
 
 
 
  
 
 
  
 
  
 
 
 
  
  
  
  
  
 
 
  
  
  
 
 
 
  
 
  
  
  
 
 
 
  
 
  
  
  
 
 
 
  
 
  
  
 
  
 
 
 
  
 
  
  
 
  
 
 
 
  
 
  
  
 
  
 
 
 
  
 
  
  
 
  
 
 
 
  
 
  
  
 
  
 
 
 
  
 
  
  
 
  
  
  
  
 
 
  
 
 
  
  
 
  
 #{track} 
  
 
 
  
 #{chapter} 
  
  
 
  
  
 
  
 
  
 
 
 
  
  
  
 
 
 
 
  
 
  
  
  
 
  
  
 
 
 
  
  
  
  
 
 
  
  
 
 
 

ProductDetailsBean.java
package com.borislam.view;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.dao.DataAccessException;
import com.borislam.domain.Detail;
import com.borislam.domain.Pricing;
import com.borislam.domain.Product;
import com.borislam.service.ProductService;
@Component
@Scope("session")
public class ProductDetailBean {
 
 @Autowired
 private ProductService productService;
 private boolean newProduct;
 private Product product;
 private String newTrack;
 private String newChapter;
 
 
 
 public boolean isNewProduct() {
 return newProduct;
 }
 public void setNewProduct(boolean newProduct) {
 this.newProduct = newProduct;
 }
 public Product getProduct() {
 return product;
 }
 public void setProduct(Product product) {
 this.product = product;
 }
 
 public String getNewTrack() {
 return newTrack;
 }
 public void setNewTrack(String newTrack) {
 this.newTrack = newTrack;
 }
 public String getNewChapter() {
 return newChapter;
 }
 public void setNewChapter(String newChapter) {
 this.newChapter = newChapter;
 }
 public void initProduct(){
 Object selectedProduct = (FacesContext.getCurrentInstance().getExternalContext().getFlash()).get("selected");
 
 if (selectedProduct==null && !FacesContext.getCurrentInstance().isPostback()) {
 product = new Product();
 product.setDetails(new Detail());
 product.setPricing(new Pricing(0,0));
 setNewProduct(true);
 }
 if (selectedProduct!=null) {
 product = (Product)selectedProduct;
 setNewProduct(false);
 }
 
 }
 
 public void doSave(ActionEvent event) {
 try {
 productService.saveProduct(product);
 
 FacesContext context = FacesContext.getCurrentInstance(); 
 context.addMessage(null, new FacesMessage("Save Successfully!"));
 }
 catch (DataAccessException e)
 { 
 e.printStackTrace();
 
 FacesContext context = FacesContext.getCurrentInstance(); 
 context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error when saving product!",null));
 
 }
 
 }
 
 public void doAddTracks(ActionEvent event) {
 List tracks = product.getDetails().getTracks();
 if (CollectionUtils.isEmpty(tracks)) {
 product.getDetails().setTracks(new ArrayList());
 }
 product.getDetails().getTracks().add(this.newTrack);
 }
 
 public void doAddChapters(ActionEvent event) {
 List tracks = product.getDetails().getChapters();
 if (CollectionUtils.isEmpty(tracks)) {
 product.getDetails().setChapters(new ArrayList() );
 }
 product.getDetails().getChapters().add(this.newChapter);
 }
 
 public void clearDetails(ValueChangeEvent event) {
 if ("Audio Album".equalsIgnoreCase(event.getNewValue().toString()) ) {
 product.getDetails().setChapters(null);
 }
 if ("Book".equalsIgnoreCase( event.getNewValue().toString())) {
 product.getDetails().setTracks(null);
 }
 }
}

updated ProductService.java
package com.borislam.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.borislam.domain.Product;
import com.borislam.repository.ProductRepository;
import com.borislam.view.ProductSearchCriteria;
@Service
public class ProductService {
 
 @Autowired
 private ProductRepository productRepository;
 
 public List searchByCriteria(ProductSearchCriteria criteria){
 return productRepository.searchByCriteria(criteria);
 }
 
 public Product getProduct(String sku) {
 return productRepository.findBySku(sku);
 }
 
 public void saveProduct(Product p){ 
 productRepository.save(p);
 }
 
 public void deleteProduct(Product p){ 
 productRepository.delete(p);
 }
 
}

Conclusion:
1. Spring Data Mongo DB provides MongoTemplate which allow you to perform MongoDB operation easily.
2. MongoDB JSON-style document could mapped to POJO easily with the help of Spring Data MongoDB
3. Repository abstraction of spring data reduces the boilerplate code write for accessing MongoDB.
4. You add custom behaviour to spring data repository.

Comments