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

Java tutorial

Introduction

Here is the source code for com.mycompany.CRMFly.hibernateAccess.ContactsDAOImpl.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.Contacts;
import com.mycompany.CRMFly.entities.Contracts;
import com.mycompany.CRMFly.entities.Daily;
import com.mycompany.CRMFly.entities.Organisations;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
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 ContactsDAOImpl implements ContactsDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private ContractsDAO contractsDAO;

    @Autowired
    private CallsDAO callsDAO;

    @Autowired
    private DailyDAO dailyDAO;

    @Override
    public void addContact(Contacts contact) {

        List<Calls> temp = contact.getCalls();
        List<Daily> temp2 = contact.getTasks();
        List<Contracts> temp3 = contact.getContractsConnectedWithContact();
        boolean first = (temp != null && temp.size() != 0) ? true : false;
        boolean second = (temp2 != null && temp2.size() != 0) ? true : false;
        boolean third = (temp3 != null && temp3.size() != 0) ? true : false;
        sessionFactory.getCurrentSession().save(contact);

        if (first) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("contacts-with-calls");
            temp = callsDAO.getFromProxy(temp);
            for (Calls call : temp) {
                call.setContactForCall(contact);
                callsDAO.changeCall(call);
            }

        }

        if (second) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("contacts-with-tasks");
            temp2 = dailyDAO.getFromProxy(temp2);
            for (Daily task : temp2) {
                task.getContactsConnectedWithTask().add(contact);
                dailyDAO.changeTask(task);
            }

        }

        if (third) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("contacts-with-contracts");
            temp3 = contractsDAO.getFromProxy(temp3);
            for (Contracts contract : temp3) {
                contract.getAddContacts().add(contact);
                contractsDAO.changeContract(contract);
            }

        }
    }

    public void addCallConnection(Contacts contact, Calls call) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("contacts-with-calls");
        contact = (Contacts) sess.load(Contacts.class, contact.getId());
        contact.getCalls().add(call);
        call.setContactForCall(contact);
        //     this.getCallsOfContact(contact.getId()).add(call);
        // contact.getCalls().add(call);
        sess.update(contact);
        //  call = (Calls) sess.get(Calls.class, call.getId());
        //   Contacts oldContact = call.getContactForCall();
        //   if(oldContact!=null)
        //   oldContact.getCalls().remove(call);
        //   sess.update(oldContact);

        //    sess.merge(call);
    }

    public void addTaskConnection(Contacts contact, Daily task) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("contacts-with-tasks");
        contact = (Contacts) sess.load(Contacts.class, contact.getId());
        contact.getTasks().add(task);
        sess.update(contact);
        task = (Daily) sess.load(Daily.class, task.getId());
        task.getContactsConnectedWithTask().add(contact);
        sess.update(task);
    }

    public void addContractConnection(Contacts contact, Contracts contract) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("contacts-with-contracts");
        contact = (Contacts) sess.load(Contacts.class, contact.getId());
        contact.getContractsConnectedWithContact().add(contract);
        sess.update(contact);
        contract = (Contracts) sess.load(Contracts.class, contract.getId());
        contract.getAddContacts().add(contact);
        sess.update(contract);
    }

    public void deleteCall(Contacts contact, Calls call) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("contacts-with-calls");
        contact = (Contacts) sess.load(Contacts.class, contact.getId());
        contact.getCalls().remove(call);
        callsDAO.removeCall(call);
        sess.update(contact);

    }

    public void deleteTaskConnection(Contacts contact, Daily task) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("contacts-with-tasks");
        contact = (Contacts) sess.get(Contacts.class, contact.getId());
        contact.getTasks().remove(task);
        sess.update(contact);
        task = (Daily) sess.get(Daily.class, task.getId());
        task.getContactsConnectedWithTask().remove(contact);
        sess.update(task);
    }

    public void deleteContractConnection(Contacts contact, Contracts contract) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("contacts-with-contracts");
        contact = (Contacts) sess.load(Contacts.class, contact.getId());
        contact.getContractsConnectedWithContact().remove(contract);
        sess.update(contact);
        contract = (Contracts) sess.load(Contracts.class, contract.getId());
        contract.getAddContacts().remove(contact);
        sess.update(contract);
    }

    @Override
    public List<Contacts> listContacts() {

        return sessionFactory.getCurrentSession().createQuery("from com.mycompany.CRMFly.entities.Contacts").list();
        //  List<Contacts> contl = sessionFactory.getCurrentSession().createQuery("from "+ Contacts.class.getName())
        //     .list();
        /*   System.out.println(Contacts.class.getName());
           for (Contacts con: contl )
          System.out.println(con.getFirstname());
              
          return contl;*/

    }

    /*   @Override
     public void removeContact(Contacts contact) {
    if (null != contact) {
        for (Calls child : contact.getCalls()) {
    child.setContactForCall(null);
    }
        sessionFactory.getCurrentSession().delete(contact);
    }
     }*/

    @Override
    public void removeContact(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        Contacts contact = (Contacts) sess.get(Contacts.class, id);
        List<Calls> temp = contact.getCalls();
        List<Daily> temp2 = contact.getTasks();
        List<Contracts> temp3 = contact.getContractsConnectedWithContact();
        boolean first = (temp != null && temp.size() != 0) ? true : false;
        boolean second = (temp2 != null && temp2.size() != 0) ? true : false;
        boolean third = (temp3 != null && temp3.size() != 0) ? true : false;
        if (first) {
            sess.enableFetchProfile("contacts-with-calls");
            temp = callsDAO.getFromProxy(temp);
            for (Calls call : temp) {
                callsDAO.removeCall(call);
            }
        }
        if (second) {
            sess.enableFetchProfile("contacts-with-tasks");
            temp2 = dailyDAO.getFromProxy(temp2);
            for (Daily task : temp2) {
                task.getContactsConnectedWithTask().remove(contact);
                sess.update(task);

            }
        }
        if (third) {
            sess.enableFetchProfile("contacts-with-contracts");
            temp3 = contractsDAO.getFromProxy(temp3);
            for (Contracts contract : temp3) {
                contract.getAddContacts().remove(contact);
                sess.update(contract);
            }
        }
        contact.setCalls(null);
        contact.setConnectedRequests(null);
        contact.setContractsConnectedWithContact(null);
        contact.setOrganisation(null);
        contact.setTasks(null);
        sess.update(contact);
        sessionFactory.getCurrentSession().delete(contact);
    }

    @Override
    public void changeContact(Contacts contact) {
        sessionFactory.getCurrentSession().update(contact);
    }

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

    }

    /*   @Override
      public Contacts getContactForIdInstant (Long id) {
     return (Contacts) sessionFactory.
             getCurrentSession().get(Contacts.class, id);
      }*/

    @Override
    public List<Contacts> getAllContacts() {
        return sessionFactory.getCurrentSession().createCriteria(Contacts.class).list();
    }

    @Override
    public List<Calls> getCallsOfContact(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("contacts-with-calls");
        Contacts contact = (Contacts) sess.get(Contacts.class, id);
        return contact.getCalls();
    }

    @Override
    public List<Contracts> getContractsOfContact(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("contacts-with-contracts");
        Contacts contact = (Contacts) sess.get(Contacts.class, id);
        return contact.getContractsConnectedWithContact();
    }

    @Override
    public Organisations getOrganisation(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        //    sess.enableFetchProfile("contacts-with-organisations");
        Contacts contact = (Contacts) sess.get(Contacts.class, id);
        return contact.getOrganisation();
    }

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

    public List<Contacts> searchContact(Contacts contact) {
        Criteria results = sessionFactory.getCurrentSession().createCriteria(Contacts.class);
        if (contact.getId() != null && contact.getId() != 0)
            results.add(Restrictions.eq("id", contact.getId()));
        if (contact.getFirstname() != null && !contact.getFirstname().isEmpty())
            results.add(Restrictions.eq("firstname", contact.getFirstname()));
        if (contact.getLastname() != null && !contact.getLastname().isEmpty())
            results.add(Restrictions.eq("lastname", contact.getLastname()));
        if (contact.getTelephone() != null && !contact.getTelephone().isEmpty())
            results.add(Restrictions.eq("telephone", contact.getTelephone()));
        if (contact.getEmail() != null && !contact.getEmail().isEmpty())
            results.add(Restrictions.eq("email", contact.getEmail()));
        return (List<Contacts>) results.list();
    }

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

}