
package pl.otwartapw.opw.pre.management.facade;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.otwartapw.opw.pre.entity.OpwLink;
import pl.otwartapw.opw.pre.entity.OpwProtokol;

/**
 * Facade for JPA operations on entity {@link pl.otwartapw.opw.pre.entity.OpwLink}.
 *
 * @author Adam Kowalewski
 */
@Stateless
public class LinkFacade extends AbstractOpwFacade<OpwLink> {

  private static final Logger logger = LoggerFactory.getLogger(LinkFacade.class);

  @PersistenceContext(unitName = PU_OPW)
  private EntityManager em;

  public LinkFacade() {
    super(OpwLink.class);
  }

  @Override
  protected EntityManager getEntityManager() {
    return em;
  }

  @Override
  public void create(OpwLink entity) {
    entity.setDateCreated(new Date());
    super.create(entity);
  }

  /**
   * Returns all links to particular protokol.
   *
   * @param protokol protokol.
   * @param active flag.
   * @return list of all links.
   *
   */
  public List<OpwLink> findAll(OpwProtokol protokol, boolean active) {
    Query q = em.createQuery("SELECT o FROM OpwLink o WHERE o.opwProtokolId = :protokol AND o.active = :active");
    q.setParameter("wynik", protokol);
    q.setParameter("active", active);
    List<OpwLink> result = q.getResultList();
    return result;
  }

}
