EventFacade.java 2.26 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.List;

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

import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.LanEvent_;

@Stateless
@LocalBean
public class EventFacade extends IntegerPkGenericFacade<LanEvent> {

	public EventFacade() {

		super(LanEvent.class);
	}

	public LanEvent findByHostname(String hostname) {

		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<LanEvent> cq = cb.createQuery(LanEvent.class);
		Root<LanEvent> root = cq.from(LanEvent.class);
		cq.where(cb.equal(root.get(LanEvent_.eventEnabled), true));

		// TypedQuery<LanEvent> q =
		// em.createNamedQuery("LanEvent.findByReferer", LanEvent.class);
		// q.setParameter("referer", hostname);
		return getSingleNullableResult(getEm().createQuery(cq));

	}

	public LanEvent findByName(String name) {
		// TypedQuery<LanEvent> q = em.createNamedQuery("LanEvent.findByName",
		// LanEvent.class);
		// q.setParameter("name", name);
		// return getSingleNullableResult(q);
		return this.find(1);
	}

	public String flushCache() {
		getEm().getEntityManagerFactory().getCache().evictAll();
		return "Evicted all!";

	}

	public List<LanEvent> findAll() {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<LanEvent> cq = cb.createQuery(getEntityClass());

		cq.select(cq.from(getEntityClass()));
		TypedQuery<LanEvent> q = getEm().createQuery(cq);

		return q.getResultList();
	}

}