<commit_before>package com.bk.bean;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.RateEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.bk.model.Book;
import com.bk.model.Review;
import com.bk.repository.ReviewRepository;
import com.bk.service.BookService;

/**
 * @author <NAME>
 * 4 Mar 2013
 */
@Component
@Scope("session")
public class BookBean implements Serializable {

	@Autowired
	private LoginBean loginBean;

	@Autowired
	private ReviewRepository reviewRepository;

	@Autowired
	private BookService bookService;

	private Book book;
	private Review bookReview;
	private Long id;

	public void init() {
		if (id == null) {
			String message = "Bad request. Please use a link from within the system.";
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
			return;
		}

		book = bookService.findById(id);

		if (book == null) {
			String message = "Bad request. Unknown book.";
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
			return;
		}

		if (loginBean.getLoggedInUser() != null) {
			bookReview = reviewRepository.getCustomerRating(book, loginBean.getLoggedInUser());
			if (bookReview == null) {
				newRating();
			}
		}
		else {
			newRating();
		}
	}

	public String getAverageRating() {
		Double averageRating = reviewRepository.getBookRating(book);
		if (averageRating == null) {
			return "0.00";
		}
		return String.format("%1$,.2f", averageRating);
	}

	public Long getNumberOfRatings() {
		return reviewRepository.getNumberOfBookRatings(book);
	}

	public void addRating(RateEvent event) {
		Integer rate = (Integer) event.getRating();
		if (bookReview.getBook() == null) {
			bookReview.setBook(book);
			bookReview.setCustomer(loginBean.getLoggedInUser());
		}
		bookReview.setRating(rate);
		reviewRepository.save(bookReview);
	}

	public void removeRating() {
		bookReview.setRating(null);
		reviewRepository.save(bookReview);
	}

	private void newRating() {
		bookReview = new Review();
		bookReview.setRating(0);
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Review getBookRating() {
		return bookReview;
	}

	public void setBookRating(Review bookRating) {
		this.bookReview = bookRating;
	}
}<commit_msg>Added lazy review data model to book bean<commit_after>package com.bk.bean;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.RateEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.bk.enums.Status;
import com.bk.lazydatamodel.ReviewLazyDataModel;
import com.bk.model.Book;
import com.bk.model.Review;
import com.bk.repository.ReviewRepository;
import com.bk.service.BookService;

/**
 * @author <NAME>
 * 4 Mar 2013
 */
@Component
@Scope("session")
public class BookBean implements Serializable {

	@Autowired
	private LoginBean loginBean;

	@Autowired
	private ReviewRepository reviewRepository;

	@Autowired
	private BookService bookService;

	@Autowired
	private ReviewLazyDataModel lazyDataModel;

	private Book book;
	private Review bookReview;
	private Long id;

	public void init() {
		if (id == null) {
			String message = "Bad request. Please use a link from within the system.";
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
			return;
		}

		book = bookService.findById(id);

		if (book == null) {
			String message = "Bad request. Unknown book.";
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
			return;
		}

		if (loginBean.getLoggedInUser() != null) {
			bookReview = reviewRepository.getCustomerRating(book, loginBean.getLoggedInUser());
			if (bookReview == null) {
				newRating();
			}
		}
		else {
			newRating();
		}
		lazyDataModel.setBook(book);
	}

	public String getAverageRating() {
		Double averageRating = reviewRepository.getBookRating(book);
		if (averageRating == null) {
			return "0.00";
		}
		return String.format("%1$,.2f", averageRating);
	}

	public Long getNumberOfRatings() {
		return reviewRepository.getNumberOfBookRatings(book);
	}

	public void addRating(RateEvent event) {
		Integer rate = (Integer) event.getRating();
		if (bookReview.getBook() == null) {
			bookReview.setBook(book);
			bookReview.setCustomer(loginBean.getLoggedInUser());
		}
		bookReview.setRating(rate);
		reviewRepository.save(bookReview);
	}

	public void removeRating() {
		bookReview.setRating(null);
		reviewRepository.save(bookReview);
	}

	private void newRating() {
		bookReview = new Review();
		bookReview.setRating(0);
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Review getBookRating() {
		return bookReview;
	}

	public void setBookRating(Review bookRating) {
		this.bookReview = bookRating;
	}

	public Status[] getStatuses() {
		return Status.values();
	}

	public ReviewLazyDataModel getLazyDataModel() {
		return lazyDataModel;
	}
}
{'fraction_non_alphanumeric': 0.11400881057268722, 'fraction_numerical': 0.0038766519823788545, 'mean_word_length': 4.48936170212766, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2635819', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1786, 'n_words': 404}