Java tutorial
/* * 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.Daily; import com.mycompany.CRMFly.entities.Employees; import com.mycompany.CRMFly.entities.Events; import java.util.List; 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 DailyDAOImpl implements DailyDAO { @Autowired private SessionFactory sessionFactory; @Autowired private EventsDAO eventsDAO; @Autowired private CallsDAO callsDAO; @Autowired private ContactsDAO contactsDAO; @Override public void addTask(Daily task) { sessionFactory.getCurrentSession().save(task); List<Events> temp = task.getEventsConnectedWithTask(); List<Calls> temp2 = task.getCallsConnectedWithTask(); List<Contacts> temp3 = task.getContactsConnectedWithTask(); if (temp != null && temp.size() != 0) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("tasks-with-events"); temp = eventsDAO.getFromProxy(temp); for (Events event : temp) { event.getConnectedTasks().add(task); eventsDAO.changeEvent(event); } } if (temp2 != null && temp2.size() != 0) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("tasks-with-calls"); temp2 = callsDAO.getFromProxy(temp2); for (Calls call : temp2) { call.getTasks().add(task); callsDAO.changeCall(call); } } if (temp3 != null && temp3.size() != 0) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("tasks-with-contacts"); temp3 = contactsDAO.getFromProxy(temp3); for (Contacts contact : temp3) { contact.getTasks().add(task); contactsDAO.changeContact(contact); } } } @Override public List<Daily> listTasks() { return sessionFactory.getCurrentSession().createQuery("from com.mycompany.CRMFly.entities.Daily").list(); } @Override public void removeTask(Daily task) { if (null != task) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); task = (Daily) sess.get(Daily.class, task.getId()); List<Events> temp = task.getEventsConnectedWithTask(); List<Calls> temp2 = task.getCallsConnectedWithTask(); List<Contacts> temp3 = task.getContactsConnectedWithTask(); if (temp != null && temp.size() != 0) { sess.enableFetchProfile("tasks-with-events"); temp = eventsDAO.getFromProxy(temp); for (Events event : temp) { event.getConnectedTasks().remove(task); sess.update(event); } } if (temp2 != null && temp2.size() != 0) { sess.enableFetchProfile("tasks-with-calls"); temp2 = callsDAO.getFromProxy(temp2); for (Calls call : temp2) { call.getTasks().remove(task); sess.update(call); } } if (temp3 != null && temp3.size() != 0) { sess.enableFetchProfile("tasks-with-contacts"); temp3 = contactsDAO.getFromProxy(temp3); for (Contacts contact : temp3) { contact.getTasks().remove(task); sess.update(contact); } } sess.update(task); sessionFactory.getCurrentSession().delete(task); } } @Override public void changeTask(Daily task) { sessionFactory.getCurrentSession().update(task); } @Override public Daily getTaskForId(Long id) { // return (Daily) sessionFactory.getCurrentSession(). // load(Daily.class, id); return (Daily) sessionFactory.getCurrentSession().get(Daily.class, id); } public Daily getTaskForName(String name) { return (Daily) sessionFactory.getCurrentSession().createCriteria(Daily.class) .add(Restrictions.eq("task", name)).uniqueResult(); } @Override public List<Daily> getAllTasks() { return sessionFactory.getCurrentSession().createCriteria(Daily.class).list(); } @Override public List<Events> getEventsforTask(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("tasks-with-events"); Daily task = (Daily) sess.get(Daily.class, id); return task.getEventsConnectedWithTask(); } @Override public List<Calls> getCallsforTask(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("tasks-with-calls"); Daily task = (Daily) sess.get(Daily.class, id); return task.getCallsConnectedWithTask(); } @Override public List<Contacts> getContactsforTask(Long id) { org.hibernate.Session sess = sessionFactory.getCurrentSession(); sess.enableFetchProfile("tasks-with-contacts"); Daily task = (Daily) sess.get(Daily.class, id); return task.getContactsConnectedWithTask(); } @Override public List<Daily> getFromProxy(List<Daily> proxy) { Disjunction or = Restrictions.disjunction(); for (Daily pr : proxy) { or.add(Restrictions.eq("id", pr.getId())); } return sessionFactory.getCurrentSession().createCriteria(Daily.class).add(or).list(); } @Override public List<Daily> SelectTasksForEmployee(Employees empl) { Long id = empl.getId(); Employees implementer = (Employees) sessionFactory.getCurrentSession().get(Employees.class, id); return sessionFactory.getCurrentSession().createCriteria(Daily.class) .add(Restrictions.eq("implementer", implementer)).list(); } public List<Daily> getTasksForEmployee(Long id) { return sessionFactory.getCurrentSession().createCriteria(Daily.class).createCriteria("implementer") .add(Restrictions.eq("id", id)).list(); } }