com.zergiu.tvman.dao.impl.TVShowDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.zergiu.tvman.dao.impl.TVShowDaoImpl.java

Source

/*
 * Copyright (c) Sergiu Giurgiu 2011.
 *
 * This file is part of TVMan.
 *
 * TVMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TVMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TVMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.zergiu.tvman.dao.impl;

import java.util.List;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.zergiu.tvman.dao.TVShowDao;
import com.zergiu.tvman.entities.TVShow;
import com.zergiu.tvman.entities.TVShowEpisode;
import com.zergiu.tvman.entities.TVShowSeason;
import com.zergiu.tvman.entities.TVShowStatus;

/**
 * Created by IntelliJ IDEA. User: sergiu Date: 11/6/11 Time: 12:41 AM
 */
@Repository
public class TVShowDaoImpl implements TVShowDao {
    private final Logger log = LoggerFactory.getLogger(TVShowDaoImpl.class);

    @PersistenceContext
    private EntityManager em;

    @Override
    public List<TVShow> listTVShows() {
        return em.createQuery("select t from TVShow t order by t.name", TVShow.class).getResultList();
    }

    @Override
    public TVShow addTVShow(TVShow show) {
        return em.merge(show);
    }

    @Override
    public Long getTVShowsCount() {
        CriteriaBuilder criteria = em.getCriteriaBuilder();
        CriteriaQuery<Long> query = criteria.createQuery(Long.class);
        query.select(criteria.countDistinct(query.from(TVShow.class)));
        Long count = em.createQuery(query).getSingleResult();
        log.debug("all count: " + count);
        return count;
    }

    @Override
    public Long getActiveTVShowsCount() {
        CriteriaBuilder criteria = em.getCriteriaBuilder();
        CriteriaQuery<Long> query = criteria.createQuery(Long.class);
        Root<TVShow> show = query.from(TVShow.class);
        query.select(criteria.countDistinct(show));
        query.where(criteria.equal(show.<TVShowStatus>get("status"), TVShowStatus.Continuing));
        Long count = em.createQuery(query).getSingleResult();
        log.debug("active count: " + count);
        return count;
    }

    @Override
    public TVShow getTVShow(Long id) {
        return em.find(TVShow.class, id);
    }

    @Override
    public TVShow getTVShowFully(Long id) {
        TVShow show = em.createQuery("select t from TVShow t "
                + " left join fetch t.seasons as s left join fetch s.episodes as ep " + " where t.id=? ",
                TVShow.class).setParameter(1, id).getSingleResult();
        for (TVShowSeason s : show.getSeasons()) {
            for (TVShowEpisode ep : s.getEpisodes()) {
                log.debug("episode: " + ep);
            }
        }
        return show;
    }

    @Override
    public TVShow updateTVShow(TVShow show) {
        return em.merge(show);
    }

    /* (non-Javadoc)
     * @see com.zergiu.tvman.dao.TVShowDao#deleteTVShow(java.lang.Long)
     */
    @Override
    public void deleteTVShow(Long id) {
        TVShow show = em.find(TVShow.class, id);
        if (show != null) {
            em.remove(show);
        }
    }
}