co.utb.softeng.contactsapp.dao.impl.ContactDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for co.utb.softeng.contactsapp.dao.impl.ContactDAOImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.utb.softeng.contactsapp.dao.impl;

import co.utb.softeng.contactsapp.entities.Cita;
import co.utb.softeng.contactsapp.entities.Contact;
import java.util.List;
import javax.persistence.FetchType;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import co.utb.softeng.contactsapp.dao.ContactDAO;

/**
 *
 * @author William
 */
@Repository
public class ContactDAOImpl implements ContactDAO {

    @Autowired
    SessionFactory sessionFactory;

    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    public Contact getById(Long id) {

        return (Contact) getSession().createCriteria(Contact.class).add(Restrictions.idEq(id))
                .setFetchMode("citas", FetchMode.JOIN).uniqueResult();
    }

    public List<Contact> getByName(String name) {
        return getSession().createCriteria(Contact.class).add(Restrictions.ilike("name", name, MatchMode.ANYWHERE))
                .setFetchMode("citas", FetchMode.JOIN).list();
    }

    public void saveOrUpdate(Contact contact) {
        getSession().saveOrUpdate(contact);
    }

    public void delete(Long id) {
        Contact contact = new Contact();
        contact.setId(id);
        getSession().delete(contact);
    }

    public List<Contact> getAllContacts(boolean includeCitas) {
        Criteria crit = getSession().createCriteria(Contact.class);
        if (includeCitas)
            crit = crit.setFetchMode("citas", FetchMode.JOIN);
        return crit.list();
    }

    @Override
    public List<Contact> getByCitaName(String citaName) {
        Criterion crit1 = Restrictions.ilike("asunto", citaName, MatchMode.ANYWHERE);
        Criterion crit2 = Restrictions.ilike("lugar", citaName, MatchMode.ANYWHERE);

        DetachedCriteria subQuery = DetachedCriteria.forClass(Contact.class).createCriteria("citas")
                .add(Restrictions.or(crit1, crit2)).setProjection(Projections.id());

        Criteria mainCriteria = getSession().createCriteria(Contact.class)
                .add(Subqueries.propertyIn("id", subQuery)).setFetchMode("citas", FetchMode.JOIN);

        return mainCriteria.list();
    }

    @Override
    public List<Contact> getByCitaId(Long citaId) {
        return getSession().createCriteria(Contact.class).createCriteria("citas").add(Restrictions.idEq(citaId))
                .list();
    }

    public Contact addCitasToContact(List<Cita> citas, Long contactId) {
        Session session = getSession();
        Contact contact = (Contact) session.get(Contact.class, contactId);

        //Lado dominante es Contact
        contact.getCitas().addAll(citas);
        session.saveOrUpdate(contact);

        return contact;
    }

}