All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fi.foyt.fni.persistence.dao.forum.ForumTopicWatcherDAO Maven / Gradle / Ivy

There is a newer version: 3.3.13
Show newest version
package fi.foyt.fni.persistence.dao.forum;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import fi.foyt.fni.persistence.dao.GenericDAO;
import fi.foyt.fni.persistence.model.forum.ForumTopic;
import fi.foyt.fni.persistence.model.forum.ForumTopicWatcher;
import fi.foyt.fni.persistence.model.forum.ForumTopicWatcher_;
import fi.foyt.fni.persistence.model.users.User;

public class ForumTopicWatcherDAO extends GenericDAO {

	private static final long serialVersionUID = 1L;

	public ForumTopicWatcher create(ForumTopic topic, User user) {
	  ForumTopicWatcher forumTopicWatcher = new ForumTopicWatcher();
	  forumTopicWatcher.setTopic(topic);
	  forumTopicWatcher.setUser(user);
	  return persist(forumTopicWatcher);
	}

  public ForumTopicWatcher findByUserAndForumTopic(User user, ForumTopic forumTopic) {
    EntityManager entityManager = getEntityManager();

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery criteria = criteriaBuilder.createQuery(ForumTopicWatcher.class);
    Root root = criteria.from(ForumTopicWatcher.class);
    criteria.select(root);
    criteria.where(
      criteriaBuilder.and(
        criteriaBuilder.equal(root.get(ForumTopicWatcher_.user), user),
        criteriaBuilder.equal(root.get(ForumTopicWatcher_.topic), forumTopic)
      )
    );

    return getSingleResult(entityManager.createQuery(criteria));
  }
  
  public List listByForumTopic(ForumTopic forumTopic) {
    EntityManager entityManager = getEntityManager();

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery criteria = criteriaBuilder.createQuery(ForumTopicWatcher.class);
    Root root = criteria.from(ForumTopicWatcher.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(ForumTopicWatcher_.topic), forumTopic));

    return entityManager.createQuery(criteria).getResultList();
  }

  public List listByUser(User user) {
    EntityManager entityManager = getEntityManager();

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery criteria = criteriaBuilder.createQuery(ForumTopicWatcher.class);
    Root root = criteria.from(ForumTopicWatcher.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(ForumTopicWatcher_.user), user));

    return entityManager.createQuery(criteria).getResultList();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy