org.sloth.persistence.impl.CategorieDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.sloth.persistence.impl.CategorieDaoImpl.java

Source

/*
 * Copyright (C) 2009-2010  Stefan Arndt, Christian Autermann, Dustin Demuth,
 *                  Christoph Fendrich, Simon Ottenhues, Christian Paluschek
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.sloth.persistence.impl;

import java.util.Collection;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.sloth.exception.EntityAlreadyKnownException;
import org.sloth.exception.EntityNotKnownException;
import org.sloth.model.Categorie;
import org.sloth.model.Categorie_;
import org.sloth.persistence.CategorieDao;
import org.sloth.persistence.ObservationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/*
 * Implementation of a {@code CategorieDoa}. 
 *
 * @author Christian Autermann
 * @author Stefan Arndt
 * @author Dustin Demuth
 * @author Christoph Fendrich
 * @author Simon Ottenhues
 * @author Christian Paluschek
 */
@Repository
public class CategorieDaoImpl extends EntityManagerDao<Categorie> implements CategorieDao {

    private ObservationDao observationDao;

    @Override
    public void delete(Categorie oc) {
        if (!isAttached(oc)) {
            throw new EntityNotKnownException();
        }
        this.observationDao.delete(this.observationDao.getByCategorie(oc));
        logger.info("Deleting Categorie with Id: {}", oc.getId());
        getEntityManager().remove(oc);
        getEntityManager().flush();

    }

    @Override
    public void delete(Collection<Categorie> t) throws NullPointerException, IllegalArgumentException {
        for (Categorie c : t) {
            if (!isAttached(c)) {
                throw new EntityNotKnownException();
            }
            this.observationDao.delete(this.observationDao.getByCategorie(c));
            logger.info("Deleting Categorie with Id: {}", c.getId());
            getEntityManager().remove(c);
        }
        getEntityManager().flush();
    }

    @Override
    public Collection<Categorie> getAll() {
        CriteriaQuery<Categorie> cq = getEntityManager().getCriteriaBuilder().createQuery(Categorie.class);
        cq.select(cq.from(Categorie.class));
        Collection<Categorie> list = getEntityManager().createQuery(cq).getResultList();
        logger.info("Getting all Categories; Found: {}", list.size());
        return list;
    }

    @Override
    public Categorie getById(Long id) {
        if (id == null) {
            throw new NullPointerException();
        }
        logger.info("Searching for Categorie with Id: {}", id);
        Categorie oc = getEntityManager().find(Categorie.class, id);
        if (oc != null) {
            logger.info("Found Categorie with Id {}; Title: {}; Description: {}",
                    new Object[] { oc.getId(), oc.getTitle(), oc.getDescription() });
        } else {
            logger.info("Can't find Categorie with Id {}", id);
        }
        return oc;
    }

    @Override
    public Categorie getByTitle(String title) {
        if (title == null) {
            throw new NullPointerException();
        }
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Categorie> cq = cb.createQuery(Categorie.class);
        Root<Categorie> categorie = cq.from(Categorie.class);
        cq.select(categorie);
        cq.where(cb.equal(categorie.get(Categorie_.title), title));
        Categorie result = null;
        try {
            result = getEntityManager().createQuery(cq).getSingleResult();
        } catch (NoResultException e) {
            logger.info("Categorie with Title {} not found", title);
        } catch (NonUniqueResultException e) {
            logger.warn("Corrupt Database", e);
        }
        return result;
    }

    @Override
    public void save(Categorie oc) {
        if (isAttached(oc)) {
            throw new EntityAlreadyKnownException();
        }
        getEntityManager().persist(oc);
        getEntityManager().flush();
        logger.info("Persisting Categorie; Generated Id is: {}", oc.getId());
    }

    /**
     * @param observationDao
     *            the observationDao to set
     */
    @Autowired
    public void setObservationDao(ObservationDao observationDao) {
        this.observationDao = observationDao;
    }

    @Override
    public void update(Categorie oc) {
        if (!isAttached(oc)) {
            throw new EntityNotKnownException();
        }
        logger.info("Updating Categorie with Id: {}", oc.getId());
        getEntityManager().merge(oc);
        getEntityManager().flush();

    }
}