com.mycompany.CRMFly.hibernateAccess.CallsDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.CRMFly.hibernateAccess.CallsDAOImpl.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 com.mycompany.CRMFly.hibernateAccess;

import com.mycompany.CRMFly.entities.Calls;
import com.mycompany.CRMFly.entities.Daily;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.FetchMode;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 *
 * @author ??
 */
@Repository
public class CallsDAOImpl implements CallsDAO {

    @Autowired(required = true)
    private SessionFactory sessionFactory;

    @Autowired
    private DailyDAO dailyDAO;

    @Override
    public void addCall(Calls call) {
        List<Daily> temp = call.getTasks();
        sessionFactory.getCurrentSession().save(call);
        // if (sessionFactory== null) System.out.println ("null factory");
        if (temp != null && temp.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("calls-with-tasks");

            temp = dailyDAO.getFromProxy(temp);
            for (Daily task : temp) {
                task.getCallsConnectedWithTask().add(call);
                dailyDAO.changeTask(task);
            }
        }

    }

    @Override
    public List<Calls> listCalls() {

        return sessionFactory.getCurrentSession().createQuery("from com.mycompany.CRMFly.entities.Calls").list();
    }

    @Override
    public void removeCall(Calls call) {
        if (null != call) {
            sessionFactory.getCurrentSession().delete(call);
        }
    }

    @Override
    public void changeCall(Calls call) {
        sessionFactory.getCurrentSession().update(call);
    }

    @Override
    public Calls getCallForId(Long id) {
        //   return (Calls) sessionFactory.getCurrentSession().
        //            load(Calls.class, id);
        return (Calls) sessionFactory.getCurrentSession().get(Calls.class, id);

    }

    public List<Calls> getFromProxy(List<Calls> proxy) {
        Disjunction or = Restrictions.disjunction();
        for (Calls pr : proxy) {
            or.add(Restrictions.eq("id", pr.getId()));
        }
        return sessionFactory.getCurrentSession().createCriteria(Calls.class).add(or).list();
    }

    @Override
    public List<Calls> getAllCalls() {
        //    org.hibernate.Session sess =  sessionFactory.getCurrentSession();
        //   sess.enableFetchProfile("111");
        return sessionFactory.getCurrentSession().createCriteria(Calls.class).list();
    }

    @Override
    public List<Daily> getTasksforCall(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("calls-with-tasks");
        Calls call = (Calls) sess.get(Calls.class, id);
        return call.getTasks();
    }

    /*  @Override
      public Contacts getContactForCall(Long id)
      {
     Criteria cr = sessionFactory.getCurrentSession().
             createCriteria(Contacts.class)
             .add(null);
          
      }
      }*/

}