PollFacade.java 3.05 KB
/*
 * Copyright Codecrew Ry
 * 
 * All rights reserved.
 * 
 * This license applies to any software containing a notice placed by the 
 * copyright holder. Such software is herein referred to as the Software. 
 * This license covers modification, distribution and use of the Software. 
 * 
 * Any distribution and use in source and binary forms, with or without 
 * modification is not permitted without explicit written permission from the 
 * copyright owner. 
 * 
 * A non-exclusive royalty-free right is granted to the copyright owner of the 
 * Software to use, modify and distribute all modifications to the Software in 
 * future versions of the Software. 
 * 
 */
package fi.codecrew.moya.facade;

import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import fi.codecrew.moya.model.Poll_;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.Poll;

@Stateless
@LocalBean
public class PollFacade extends IntegerPkGenericFacade<Poll> {

	@EJB
	private EventBeanLocal eventBean;

	public PollFacade() {

		super(Poll.class);
	}

	// private boolean pollIsUsable(Poll poll) {
	// Date now = new Date();
	//
	// // Already valid
	// if (poll.getBegin().after(now)) {
	// logger.debug("Poll {} not opened yet", poll);
	// return false;
	// }
	//
	// // Still valid
	// if (poll.getEnd().before(now)) {
	// logger.debug("Poll {} already closed", poll);
	// return false;
	// }
	//
	// logger.debug("Poll has {} questions", poll.getQuestions());
	// // At least one question that is on a page
	// for (PollQuestion q : poll.getQuestions()) {
	// logger.debug("Check question  {} page {}", q, q.getPage());
	// if (q.getPage() > 0) {
	// return true;
	// }
	// }
	//
	// // No usable questions
	// return false;
	// }

	@Override
	public Poll find(Integer id)
	{
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<Poll> cq = cb.createQuery(Poll.class);
		Root<Poll> root = cq.from(Poll.class);
		cq.where(cb.equal(root.get(Poll_.id), id),
				cb.equal(root.get(Poll_.event), eventBean.getCurrentEvent()));
		return super.getSingleNullableResult(getEm().createQuery(cq));
	}

	public List<Poll> findAllUsable(LanEvent currentEvent) {

		Date now = new Date();
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<Poll> cq = cb.createQuery(Poll.class);
		Root<Poll> root = cq.from(Poll.class);

		cq.where(cb.equal(root.get(Poll_.event), currentEvent),
				cb.lessThan(root.get(Poll_.begin), now),
				cb.greaterThan(root.get(Poll_.end), now)
				);

		return getEm().createQuery(cq).getResultList();

	}

	public List<Poll> findAll() {

		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<Poll> cq = cb.createQuery(Poll.class);
		Root<Poll> root = cq.from(Poll.class);
		cq.where(cb.equal(root.get(Poll_.event), eventBean.getCurrentEvent()));
		return getEm().createQuery(cq).getResultList();

	}
}