Package systole.persistence.brokersDB

Source Code of systole.persistence.brokersDB.CommentBrokerDB

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import systole.domain.report.template.Comment;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.CommentBroker;
import systole.view.messages.ErrorMessages;

/**
*
* @author jmj
*/
public class CommentBrokerDB extends BrokerDB implements CommentBroker {

    /**
     */
    public CommentBrokerDB() {
        super();
    }

    public void insert(Comment comment) throws ExceptionDAO {
        try {
            this.logger.logDebug("save comment ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(comment);
            this.logger.logDebug("save successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save comment, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public void update(Comment comment) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating comment ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(comment);
            this.logger.logDebug("update successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on update comment, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public List<Comment> getAllComments() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting all comments");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            List<Comment> list = currentSession.createQuery("from Comment c order by upper(c.name)").list();
            this.logger.logDebug("getting comments successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all comments, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los Comentarios", e.fillInStackTrace());
        }
    }

    public List<Comment> getCommentsForMale() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting all male comments");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<Comment> list = currentSession.createCriteria(Comment.class).add(Restrictions.eq("male", true)).list();
            this.logger.logDebug("getting comments successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all male comments, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los Comentarios", e.fillInStackTrace());
        }
    }

    public List<Comment> getCommentsForFemale() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting female comments");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<Comment> list = currentSession.createCriteria(Comment.class).add(Restrictions.eq("female", true)).list();
            this.logger.logDebug("getting comments successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all female comments, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los Comentarios", e.fillInStackTrace());
        }
    }

    @SuppressWarnings("unchecked")
    public boolean existComment(String name, Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("exist comment");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Comment.class).
                    add((id == null) ? Restrictions.eq("name", name)
                    : Restrictions.and(Restrictions.eq("name", name), Restrictions.ne("id", id)));
            List list = criteria.list();
            this.logger.logDebug("exist comment successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist comment, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar si ya existe el comentario", e.fillInStackTrace());
        }
    }
}
TOP

Related Classes of systole.persistence.brokersDB.CommentBrokerDB

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.