br.mdarte.exemplo.academico.cd.CursoDAO.java Source code

Java tutorial

Introduction

Here is the source code for br.mdarte.exemplo.academico.cd.CursoDAO.java

Source

// license-header java merge-point
/**
 * Attention: Generated source! Do not modify by hand!
 * Generated by: HibernateEntityFactory.vsl in andromda-hibernate-cartridge.
 */
package br.mdarte.exemplo.academico.cd;

import org.hibernate.HibernateException;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.Criteria;
import org.hibernate.CallbackException;
import org.hibernate.CacheMode;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;

import org.apache.commons.beanutils.PropertyUtils;

import br.mdarte.exemplo.academico.to.CursoTOImpl;

import br.mdarte.exemplo.academico.cd.AbstractDAO;
import br.mdarte.exemplo.academico.cd.DAOException;
import br.mdarte.exemplo.academico.cd.AbstractEntity;
import br.ufrj.coppetec.ValueObject;
import br.ufrj.coppetec.DataObject;
import br.ufrj.coppetec.to.AbstractTO;
import br.mdarte.exemplo.academico.util.PaginationStrategy;
import br.mdarte.exemplo.academico.util.NoPagination;
import br.mdarte.exemplo.academico.util.Constantes;

import org.dom4j.Document;
import org.dom4j.Element;

/**
 * <p>
 * Factory class.
 * Is able to find and create objects of type Curso.
 * The Hibernate <em>subclass</em> inheritance
 * strategy is followed.
 * Those can be described as follows:
 * </p>
 * @see br.mdarte.exemplo.academico.cd.Curso
 */
public abstract class CursoDAO extends AbstractDAO {

    protected void removeRelations(AbstractEntity obj) throws DAOException {

        try {
            CursoAbstract entidade = (CursoAbstract) obj;
            Session session = currentSession();
            entidade.setEstudantesInverse(null);
        } catch (HibernateException e) {
            throw new DAOException(e);
        }
    }

    public List<AbstractEntity> select(long id) throws DAOException {
        List<AbstractEntity> lista = new ArrayList<AbstractEntity>();
        try {
            Session session = currentSession();
            CursoAbstract res = (CursoAbstract) session.load(CursoImpl.class, new Long(id));
            org.hibernate.Hibernate.initialize(res);
            lista.add(res);
        } catch (ObjectNotFoundException onfe) {
            throw new DAOException(onfe);
        } catch (HibernateException he1) {
            throw new DAOException(he1);
        }
        return lista;
    }

    public List<AbstractEntity> insert(AbstractEntity obj) throws DAOException {
        List<AbstractEntity> lista = new ArrayList<AbstractEntity>();

        try {
            Session session = currentSession();
            session.setCacheMode(CacheMode.GET);
            session.save(obj);
            session.flush();
            lista.add(obj);
            return lista;
        } catch (HibernateException e) {
            throw new DAOException(e);
        }
    }

    public List<AbstractEntity> insertOrUpdate(AbstractEntity obj) throws DAOException {

        List<AbstractEntity> lista = new ArrayList<AbstractEntity>();

        try {
            Session session = currentSession();
            session.setCacheMode(CacheMode.GET);
            session.saveOrUpdate(obj);
            session.flush();
            lista.add(obj);
            return lista;
        } catch (HibernateException e) {
            throw new DAOException(e);
        }
    }

    public List<AbstractEntity> update(AbstractEntity obj) throws DAOException {

        List<AbstractEntity> lista = new ArrayList<AbstractEntity>();

        try {
            Session session = currentSession();
            session.setCacheMode(CacheMode.GET);
            session.update(obj);
            session.flush();
            lista.add(obj);
            return lista;
        } catch (HibernateException e) {
            throw new DAOException(e);
        }
    }

    public List<AbstractEntity> delete(AbstractEntity obj) throws DAOException {
        try {
            Session session = currentSession();
            removeRelations(obj);
            session.delete(obj);
            session.flush();
            return null;
        } catch (HibernateException e) {
            throw new DAOException(e);
        }
    }

    /**
     * Lista todos os objetos.
     * 
     * @return Lista de objetos
     */
    public List<AbstractEntity> list(PaginationStrategy paginacao, String propriedade, Boolean desc)
            throws DAOException {

        try {
            Session session = currentSession();

            String hql = "from br.mdarte.exemplo.academico.cd.Curso";

            if (propriedade != null && !propriedade.equals("")) {
                String order = desc.booleanValue() ? " desc" : " asc";
                hql += " order by " + propriedade + order;
            }

            org.hibernate.Query qry = session.createQuery(hql);

            if (paginacao == null)
                paginacao = new NoPagination();

            paginacao.paginateResult(qry);
            qry.setCacheable(true);
            return qry.list();

        } catch (HibernateException h) {
            throw new DAOException(h);
        }
    }

    public List<AbstractEntity> list() throws DAOException {
        try {
            Session session = currentSession();
            org.hibernate.Query qry = session.createQuery("from br.mdarte.exemplo.academico.cd.Curso");
            qry.setCacheable(true);
            List res = qry.list();
            return res;

        } catch (HibernateException h) {
            throw new DAOException(h);
        }
    }

    protected abstract Object handleFilter(DataObject vo) throws DAOException;

    public List<AbstractEntity> defaultFilter(AbstractTO to) throws DAOException {
        return defaultFilter(to, new NoPagination());
    }

    public List<AbstractEntity> defaultFilter(AbstractTO to, PaginationStrategy paginacao) throws DAOException {

        Session session = currentSession();
        Criteria criterios = ((org.hibernate.Session) session).createCriteria(CursoImpl.class);

        if (to instanceof CursoTOImpl) {

            CursoAbstract entityAbstract;
            try {
                entityAbstract = CursoAbstract.getEntityFromTO((CursoTOImpl) to);
                criterios.add(Example.create(entityAbstract).enableLike(MatchMode.ANYWHERE));
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } else {
            return null;
        }

        try {
            if (paginacao == null)
                paginacao = new NoPagination();

            paginacao.paginateResult(criterios);
            criterios.setCacheable(true);
            return criterios.list();

        } catch (HibernateException h) {
            throw new DAOException(h);
        }
    }

    public List<AbstractEntity> filter(DataObject vo) throws DAOException {
        return filter(vo, new NoPagination());
    }

    public List<AbstractEntity> filter(DataObject vo, PaginationStrategy paginacao) throws DAOException {
        try {
            Object obj = handleFilter(vo);

            if (paginacao == null)
                paginacao = new NoPagination();

            if (obj instanceof org.hibernate.Query) {
                org.hibernate.Query res = (org.hibernate.Query) obj;
                paginacao.paginateResult(res);
                res.setCacheable(true);
                return res.list();

            } else if (obj instanceof org.hibernate.Criteria) {
                org.hibernate.Criteria res = (org.hibernate.Criteria) obj;
                paginacao.paginateResult(res);
                res.setCacheable(true);
                return res.list();
            } else
                return null;

        } catch (HibernateException h) {
            throw new DAOException(h);
        }
    }

    protected abstract org.hibernate.Criteria handleXmlExport(ValueObject vo, org.hibernate.Session session)
            throws DAOException;

    public org.hibernate.Criteria xmlExport(ValueObject vo, org.hibernate.Session session) throws DAOException {
        return handleXmlExport(vo, session);
    }

    public int xmlImportEntity(org.dom4j.Element raiz) throws DAOException {
        Session session = currentSession();
        org.hibernate.Criteria critBusca_Entidade = session.createCriteria(CursoImpl.class);

        java.util.List listaRes = critBusca_Entidade.setMaxResults(2).list();
        if (listaRes.size() > 1)
            throw new DAOException("error.default.exportid.notunique.exception" + "," + listaRes.get(0).toString()
                    + "," + listaRes.get(1).toString());

        CursoImpl entidade;

        if (listaRes.size() == 0)
            entidade = new CursoImpl();
        else
            entidade = (CursoImpl) listaRes.get(0);

        if (raiz.selectNodes("./codigo").size() != 0) {
            java.lang.String valorcodigo = java.lang.String.valueOf(raiz.valueOf("./codigo"));
            entidade.setCodigo(valorcodigo);
        }

        if (raiz.selectNodes("./nome").size() != 0) {
            java.lang.String valornome = java.lang.String.valueOf(raiz.valueOf("./nome"));
            entidade.setNome(valornome);
        }
        insertOrUpdate(entidade);

        return 0;
    }

    public Element xmlExportEntity(AbstractEntity entity) throws DAOException {
        Element raiz = org.dom4j.DocumentHelper.createElement("raiz_elemento");
        return xmlExportEntity(entity, raiz);
    }

    public Element xmlExportEntity(AbstractEntity entity, Element raiz) throws DAOException {
        try {

            CursoImpl obj = (CursoImpl) entity;

            if (obj.getCodigo() != null) {
                Element attcodigo = raiz.addElement("codigo");
                attcodigo.addText(obj.getCodigo().toString());
            }
            if (obj.getNome() != null) {
                Element attnome = raiz.addElement("nome");
                attnome.addText(obj.getNome().toString());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return raiz;
    }

    /**
     * 
     */

    public static java.util.Collection getEstudantesByCurso(long id, PaginationStrategy paginacao)
            throws DAOException {

        try {
            Session session = currentSession();
            //org.hibernate.Query qry = session.createQuery("select t2 from br.mdarte.exemplo.academico.cd.Curso as t1 inner join t1.estudantes as t2 where t1.id = " + id);
            org.hibernate.Query qry = session.createQuery(
                    "select elements(t1.estudantes) from br.mdarte.exemplo.academico.cd.Curso as t1 where t1.id = "
                            + id);

            if (paginacao == null)
                paginacao = new NoPagination();

            paginacao.paginateResult(qry);
            qry.setCacheable(true);
            return qry.list();
        } catch (HibernateException h) {
            throw new DAOException(h);
        }
    }

    public void logEvent(String msg, AbstractEntity entity) throws CallbackException {
    }

}