package com.yammer.stresstime.managers;

import com.yammer.stresstime.managers.exceptions.EntityNotFoundException;
import com.yammer.stresstime.managers.exceptions.HibernateUncaughtException;
import com.yammer.stresstime.managers.exceptions.StresstimeException;
import io.dropwizard.hibernate.AbstractDAO;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;

public class EntityManager<E> extends AbstractDAO<E> {

    private final Class<? extends E> mEntityClass;

    public EntityManager(SessionFactory sessionFactory, Class<? extends E> entityClass) {
        super(sessionFactory);
        mEntityClass = entityClass;
    }

    public void save(E entity) {
        try {
            persist(entity);
        } catch (HibernateException e) {
            throw new HibernateUncaughtException(e);
        }
    }

    public boolean safeSave(E entity) {
        try {
            save(entity);
            return true;
        } catch (StresstimeException e) {
            e.printStackTrace();
            return false;
        }
    }

    // Needs to be a valid entity
    public void delete(E entity) {
        currentSession().delete(entity);
    }

    public boolean safeDelete(E entity) {
        try {
            delete(entity);
            return true;
        } catch (StresstimeException e) {
            e.printStackTrace();
            return false;
        }
    }

    public E getById(long id) {
        E entity = safeGetById(id);
        if (entity == null) {
            throw new EntityNotFoundException(mEntityClass, id);
        }
        return entity;
    }

    @SuppressWarnings("unchecked")
    public E safeGetById(long id) {
        return (E) currentSession().get(mEntityClass, id);
    }

    public void deleteById(long id) {
        delete(getById(id));
    }

    public boolean safeDeleteById(long id) {
        try {
            deleteById(id);
            return true;
        } catch (StresstimeException e) {
            e.printStackTrace();
            return false;
        }
    }
}

@@ -34,16 +34,91 @@
 agers;%0A%0A
+import com.yammer.stresstime.managers.exceptions.EntityNonUniqueException;%0A
 import c
@@ -374,16 +374,47 @@
 actDAO;%0A
+import org.hibernate.Criteria;%0A
 import o
@@ -2152,11 +2152,579 @@
  %7D%0A    %7D
+%0A%0A    protected E getExactOne(Criteria criteria) %7B%0A        E entity = getUnique(criteria);%0A        if (entity == null) %7B%0A            throw new EntityNotFoundException(mEntityClass);%0A        %7D%0A        return entity;%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    protected E getUnique(Criteria criteria) %7B%0A        try %7B%0A            return (E) criteria.uniqueResult();%0A        %7D catch (HibernateException e) %7B%0A            EntityNonUniqueException error = new EntityNonUniqueException(mEntityClass);%0A            error.initCause(e);%0A            throw error;%0A        %7D%0A    %7D
 %0A%7D%0A

Add getExactOne() and getUnique() methods to EntityManager