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

Java tutorial

Introduction

Here is the source code for com.mycompany.CRMFly.hibernateAccess.EmployeesDAOImpl.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.Contracts;
import com.mycompany.CRMFly.entities.Daily;
import com.mycompany.CRMFly.entities.Employees;
import com.mycompany.CRMFly.entities.Projects;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Query;
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 EmployeesDAOImpl implements EmployeesDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private ContractsDAO contractsDAO;

    @Autowired
    private ProjectsDAO projectsDAO;

    @Autowired
    private DailyDAO dailyDAO;

    @Override
    public void addEmployee(Employees employee) {
        sessionFactory.getCurrentSession().save(employee);

        List<Contracts> temp = employee.getResponsibleForContracts();
        List<Projects> temp2 = employee.getParticipate();
        List<Daily> temp3 = employee.getTasks();

        if (temp != null && temp.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("employees-with-contracts");
            temp = contractsDAO.getFromProxy(temp);
            for (Contracts contract : temp) {
                contract.setResponsibleManager(employee);
                contractsDAO.changeContract(contract);
            }

        }

        if (temp2 != null && temp2.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("employees-with-projects");
            temp2 = projectsDAO.getFromProxy(temp2);
            for (Projects project : temp2) {
                project.getParticipants().add(employee);
                projectsDAO.changeProject(project);
            }

        }

        if (employee.getResponsibleForProjects() != null) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("employees-with-projects");
            temp2 = projectsDAO.getFromProxy(employee.getResponsibleForProjects());
            for (Projects project : temp2) {
                project.setResponsibleManager(employee);
                projectsDAO.changeProject(project);
            }

        }

        if (temp3 != null && temp3.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("employees-with-tasks");
            temp3 = dailyDAO.getFromProxy(temp3);
            for (Daily task : temp3) {
                task.setImplementer(employee);
                dailyDAO.changeTask(task);
            }

        }

    }

    public void addSubordinadeConnection(Employees chief, Employees subordinade) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        chief = (Employees) sess.load(Employees.class, chief.getId());
        chief.getSubordinates().add(subordinade);
        sess.update(chief);
        subordinade = (Employees) sess.load(Employees.class, subordinade.getId());
        subordinade.setManagerForEmployee(chief);
        sess.update(subordinade);
    }

    public void addTaskConnection(Employees employee, Daily task) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        employee = (Employees) sess.load(Employees.class, employee.getId());
        employee.getTasks().add(task);
        sess.update(employee);
        task = (Daily) sess.load(Daily.class, task.getId());
        task.setImplementer(employee);
        sess.update(task);
    }

    public void addProjectConnection(Employees employee, Projects project) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        employee = (Employees) sess.load(Employees.class, employee.getId());
        employee.getParticipate().add(project);
        sess.update(employee);
        project = (Projects) sess.load(Projects.class, project.getId());
        project.getParticipants().add(employee);
        sess.update(project);
    }

    public void addManagedProjectConnection(Employees employee, Projects project) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        employee = (Employees) sess.load(Employees.class, employee.getId());
        employee.getResponsibleForProjects().add(project);
        sess.update(employee);
        project = (Projects) sess.load(Projects.class, project.getId());
        project.setResponsibleManager(employee);
        sess.update(project);
    }

    public void addManagedContractConnection(Employees employee, Contracts contract) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        employee = (Employees) sess.load(Employees.class, employee.getId());
        employee.getResponsibleForContracts().add(contract);
        sess.update(employee);
        contract = (Contracts) sess.load(Contracts.class, contract.getId());
        contract.setResponsibleManager(employee);
        sess.update(contract);
    }

    public void deleteSubordinadeConnection(Employees chief, Employees subordinade) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        chief = (Employees) sess.load(Employees.class, chief.getId());
        chief.getSubordinates().remove(subordinade);
        sess.update(chief);
        subordinade = (Employees) sess.load(Employees.class, subordinade.getId());
        subordinade.setManagerForEmployee(null);
        sess.update(subordinade);
    }

    public void deleteTaskConnection(Employees employee, Daily task) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        employee = (Employees) sess.load(Employees.class, employee.getId());
        employee.getTasks().remove(task);
        sess.update(employee);
        task = (Daily) sess.load(Daily.class, task.getId());
        task.setImplementer(null);
        sess.update(task);
    }

    public void deleteProjectConnection(Employees employee, Projects project) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        employee = (Employees) sess.load(Employees.class, employee.getId());
        employee.getParticipate().remove(project);
        sess.update(employee);
        project = (Projects) sess.load(Projects.class, project.getId());
        project.getParticipants().remove(employee);
        sess.update(project);
    }

    public void deleteManagedProjectConnection(Employees employee, Projects project) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        employee = (Employees) sess.load(Employees.class, employee.getId());
        employee.getResponsibleForProjects().remove(project);
        sess.update(employee);
        project = (Projects) sess.load(Projects.class, project.getId());
        project.setResponsibleManager(null);
        sess.update(project);
    }

    public void deleteManagedContractConnection(Employees employee, Contracts contract) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        employee = (Employees) sess.load(Employees.class, employee.getId());
        employee.getResponsibleForContracts().remove(contract);
        sess.update(employee);
        contract = (Contracts) sess.load(Contracts.class, contract.getId());
        contract.setResponsibleManager(null);
        sess.update(contract);
    }

    @Override
    public List<Employees> listEmployees() {

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

    @Override
    public void removeEmployee(Employees employee) {
        if (null != employee) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            employee = (Employees) sess.get(Employees.class, employee.getId());
            List<Contracts> temp = employee.getResponsibleForContracts();
            List<Projects> temp2 = employee.getParticipate();
            List<Daily> temp3 = employee.getTasks();
            List<Employees> subordinades = employee.getSubordinates();

            if (temp != null && temp.size() != 0) {
                sess.enableFetchProfile("employees-with-contracts");
                temp = contractsDAO.getFromProxy(temp);
                for (Contracts contract : temp) {
                    contract.setResponsibleManager(null);
                    sess.update(contract);
                }

            }

            if (temp2 != null && temp2.size() != 0) {
                sess.enableFetchProfile("employees-with-projects");
                temp2 = projectsDAO.getFromProxy(temp2);
                for (Projects project : temp2) {
                    project.getParticipants().remove(employee);
                    sess.update(project);
                }

            }

            if (employee.getResponsibleForProjects() != null) {
                sess.enableFetchProfile("employees-with-projects");
                temp2 = projectsDAO.getFromProxy(employee.getResponsibleForProjects());
                for (Projects project : temp2) {
                    project.setResponsibleManager(null);
                    sess.update(project);
                }

            }

            if (temp3 != null && temp3.size() != 0) {
                sess.enableFetchProfile("employees-with-tasks");
                temp3 = dailyDAO.getFromProxy(temp3);
                for (Daily task : temp3) {
                    task.setImplementer(null);
                    sess.update(task);
                }
            }

            if (subordinades != null && subordinades.size() != 0) {
                for (Employees empl : subordinades) {
                    empl.setManager(null);
                    sess.update(empl);
                }
            }

            sess.update(employee);
            sessionFactory.getCurrentSession().delete(employee);

        }
    }

    @Override
    public void changeEmployee(Employees employee) {
        sessionFactory.getCurrentSession().update(employee);
    }

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

    }

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

    @Override
    public List<Contracts> getContractsOnWorker(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        //  sess.enableFetchProfile("employees-with-contracts");
        // Employees employee = (Employees) sess.
        //        get(Employees.class, id);
        //   return employee.getResponsibleForContracts();
        return sessionFactory.getCurrentSession().createCriteria(Contracts.class)
                .setFetchMode("ResponsibleManager", FetchMode.JOIN).add(Restrictions.eq("id", id)).list();

    }

    @Override
    public List<Projects> getProjectsEmployeeResponsibleFor(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        // sess.enableFetchProfile("managers-with-projects");
        //  Employees employee = (Employees) sess.
        //      get(Employees.class, id);
        //  return employee.getResponsibleForProjects();
        return sessionFactory.getCurrentSession().createCriteria(Projects.class)
                .setFetchMode("ResponsibleManager", FetchMode.JOIN).add(Restrictions.eq("id", id)).list();
    }

    @Override
    public List<Projects> getProjectsPaticipation(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("employees-with-projects");
        //    Employees employee = (Employees) sess.
        //            get(Employees.class, id);
        //   return employee.getParticipate();
        Query query = sess
                .createQuery("select empl.Participate from" + " com.mycompany.CRMFly.entities.Employees empl");
        // query.setParameter("code", "7277");
        return query.list();

    }

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

    @Override
    public List<Employees> getSubordinatesEmployee(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        Employees employee = (Employees) sess.get(Employees.class, id);
        return employee.getSubordinates();
    }

    public List<Employees> searchEmployee(Employees employee) {
        Criteria results = sessionFactory.getCurrentSession().createCriteria(Employees.class);
        if (employee.getId() != null && employee.getId() != 0) {
            results.add(Restrictions.eq("id", employee.getId()));
        }
        if (employee.getFirstname() != null && !employee.getFirstname().isEmpty()) {
            results.add(Restrictions.eq("firstname", employee.getFirstname()));
        }
        if (employee.getLastname() != null && !employee.getLastname().isEmpty()) {
            results.add(Restrictions.eq("lastname", employee.getLastname()));
        }
        if (employee.getEmail() != null && !employee.getEmail().isEmpty()) {
            results.add(Restrictions.eq("email", employee.getEmail()));
        }
        if (employee.getJob_telephone() != null && !employee.getJob_telephone().isEmpty()) {
            results.add(Restrictions.eq("job_telephone", employee.getJob_telephone()));
        }
        if (employee.getDepartment() != null && !employee.getDepartment().toString().isEmpty()) {
            results.add(Restrictions.eq("department", employee.getDepartment()));
        }
        return (List<Employees>) results.list();
    }

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

    public byte[] getImage(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        Employees employee = (Employees) sess.get(Employees.class, id);
        return employee.getPhoto();
    }

}