package com.sharomank.service;
import com.sharomank.domain.*;
import com.sharomank.repository.*;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;
/**
* @author sharomank
* @since 26/01/13 12:35
*/
@Repository
public class ClientServiceImpl implements ClientService {
@Inject
private AccountRepository accountRepository;
@Inject
private TagRepository tagRepository;
@Inject
private BadgeRepository badgeRepository;
@Inject
private QuestionRepository questionRepository;
@Inject
private AnswerRepository answerRepository;
@Inject
private CommentRepository commentRepository;
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public void createFullTextIndex() {
try {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List fullTextSearch(String filter) {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Question.class).get();
org.apache.lucene.search.Query query = qb
.keyword()
.onFields("title", "text", "comments.text")
.matching(filter)
.createQuery();
Query persistenceQuery = fullTextEntityManager.createFullTextQuery(query, Question.class);
return persistenceQuery.getResultList();
}
@Override
public Account saveAccount(Account account) {
return accountRepository.save(account);
}
@Override
public Long countAccounts() {
return accountRepository.count();
}
@Override
public Iterable<Account> findAllAccounts() {
return accountRepository.findAll();
}
@Override
public Tag saveTag(Tag tag) {
return tagRepository.save(tag);
}
@Override
public Long countTags() {
return tagRepository.count();
}
@Override
public Iterable<Tag> findAllTags() {
return tagRepository.findAll();
}
@Override
public Badge saveBadge(Badge badge) {
return badgeRepository.save(badge);
}
@Override
public Long countBadges() {
return badgeRepository.count();
}
@Override
public Iterable<Badge> findAllBadges() {
return badgeRepository.findAll();
}
@Override
public Question saveQuestion(Question question) {
return questionRepository.save(question);
}
@Override
public Long countQuestions() {
return questionRepository.count();
}
@Override
public Iterable<Question> findAllQuestions() {
return questionRepository.findAll();
}
@Override
public Question findQuestion(Long id) {
return questionRepository.findOne(id);
}
@Override
public Answer saveAnswer(Answer answer) {
return answerRepository.save(answer);
}
@Override
public Long countAnswers() {
return answerRepository.count();
}
@Override
public Iterable<Answer> findAllAnswers() {
return answerRepository.findAll();
}
@Override
public void addCommentToQuestion(Long questionId, Comment comment) {
Question q = questionRepository.findOne(questionId);
q.addComment(comment);
saveQuestion(q);
}
@Override
public void addCommentToAnswer(Long answerId, Comment comment) {
Answer a = answerRepository.findOne(answerId);
a.addComment(comment);
saveAnswer(a);
}
@Override
public Comment saveComment(Comment comment) {
return commentRepository.save(comment);
}
@Override
public Long countComments() {
return commentRepository.count();
}
@Override
public Iterable<Comment> findAllComments() {
return commentRepository.findAll();
}
}