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

Java tutorial

Introduction

Here is the source code for com.mycompany.CRMFly.hibernateAccess.ProjectsDAOImpl.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.ContragentsInShipment;
import com.mycompany.CRMFly.entities.Daily;
import com.mycompany.CRMFly.entities.Employees;
import com.mycompany.CRMFly.entities.Events;
import com.mycompany.CRMFly.entities.Organisations;
import com.mycompany.CRMFly.entities.ParticipantsInProject;
import com.mycompany.CRMFly.entities.Projects;
import java.util.ArrayList;
import java.util.Date;
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 ProjectsDAOImpl implements ProjectsDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private DailyDAO dailyDAO;

    @Autowired
    private EventsDAO eventsDAO;

    @Autowired
    private EmployeesDAO employeesDAO;

    @Autowired
    private OrganisationsDAO organisationsDAO;

    @Override
    public void addProject(Projects project) {
        sessionFactory.getCurrentSession().save(project);

        List<Daily> temp = project.getTasksConnectedWithProject();
        List<Events> temp2 = project.getEventsConnectedWithProjects();
        List<Employees> temp3 = project.getParticipants();
        List<ParticipantsInProject> temp4 = project.getOrganisationsInProject();

        if (temp != null && temp.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("projects-with-tasks");
            temp = dailyDAO.getFromProxy(temp);
            for (Daily task : temp) {
                task.setProject(project);
                dailyDAO.changeTask(task);
            }

        }

        if (temp2 != null && temp2.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("projects-with-events");
            temp2 = eventsDAO.getFromProxy(temp2);
            for (Events event : temp2) {
                event.getConnectedProjects().add(project);
                eventsDAO.changeEvent(event);
            }

        }

        if (temp3 != null && temp3.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("projects-with-employees");
            temp3 = employeesDAO.getFromProxy(temp3);
            for (Employees employee : temp3) {
                employee.getResponsibleForProjects().add(project);
                employeesDAO.changeEmployee(employee);
            }

        }

        if (temp4 != null && temp4.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("projects-with-organisations");
            for (ParticipantsInProject organisation : temp4) {
                organisation.setProject(project);
                sessionFactory.getCurrentSession().save(organisation);
            }
        }

    }

    public void addTaskConnection(Projects project, Daily task) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-tasks");
        project = (Projects) sess.load(Projects.class, project.getId());
        project.getTasksConnectedWithProject().add(task);
        sess.update(project);
        task = (Daily) sess.load(Daily.class, task.getId());
        task.setProject(project);
        sess.update(task);
    }

    public void addEventConnection(Projects project, Events event) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-events");
        project = (Projects) sess.load(Projects.class, project.getId());
        project.getEventsConnectedWithProjects().add(event);
        sess.update(project);
        event = (Events) sess.load(Events.class, event.getId());
        event.getConnectedProjects().add(project);
        sess.update(event);
    }

    public void addEmployeeConnection(Projects project, Employees employee) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-employees");
        project = (Projects) sess.load(Projects.class, project.getId());
        project.getParticipants().add(employee);
        sess.update(project);
        employee = (Employees) sess.load(Employees.class, employee.getId());
        employee.getParticipate().add(project);
        sess.update(employee);
    }

    public void addOrganisationConnection(Projects project, ParticipantsInProject participant) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-orgs");
        sess.enableFetchProfile("orgs-with-projects");
        project = (Projects) sess.load(Projects.class, project.getId());
        project.getOrganisationsInProject().add(participant);
        sess.update(project);
        sess.save(participant);
        participant = (ParticipantsInProject) sess.load(ParticipantsInProject.class, participant.getId());
        Organisations org = participant.getOrganisation();
        org.getParticipateInProjects().add(participant);
        sess.update(org);
    }

    public void deleteTaskConnection(Projects project, Daily task) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-tasks");
        project = (Projects) sess.load(Projects.class, project.getId());
        project.getTasksConnectedWithProject().remove(task);
        sess.update(project);
        task = (Daily) sess.load(Daily.class, task.getId());
        task.setProject(null);
        sess.update(task);
    }

    public void deleteEventConnection(Projects project, Events event) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-events");
        project = (Projects) sess.load(Projects.class, project.getId());
        project.getEventsConnectedWithProjects().remove(event);
        sess.update(project);
        event = (Events) sess.load(Events.class, event.getId());
        event.getConnectedProjects().remove(project);
        sess.update(event);
    }

    public void deleteEmployeeConnection(Projects project, Employees employee) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-employees");
        project = (Projects) sess.load(Projects.class, project.getId());
        project.getParticipants().remove(employee);
        sess.update(project);
        employee = (Employees) sess.load(Employees.class, employee.getId());
        employee.getParticipate().remove(project);
        sess.update(employee);
    }

    public void deleteOrganisationConnection(Projects project, ParticipantsInProject participant) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-orgs");
        sess.enableFetchProfile("orgs-with-projects");
        project = (Projects) sess.load(Projects.class, project.getId());
        project.getOrganisationsInProject().remove(participant);
        sess.update(project);
        participant = (ParticipantsInProject) sess.load(ParticipantsInProject.class, participant.getId());
        Organisations org = participant.getOrganisation();
        org.getParticipateInProjects().remove(participant);
        sess.update(org);
        sess.delete(participant);
    }

    @Override
    public List<Projects> listProjects() {

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

    @Override
    public void removeProject(Projects project) {
        if (null != project) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            project = (Projects) sess.get(Projects.class, project.getId());
            List<Daily> temp = project.getTasksConnectedWithProject();
            List<Events> temp2 = project.getEventsConnectedWithProjects();
            List<Employees> temp3 = project.getParticipants();
            List<ParticipantsInProject> temp4 = project.getOrganisationsInProject();

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

            }

            if (temp2 != null && temp2.size() != 0) {
                sess.enableFetchProfile("projects-with-events");
                temp2 = eventsDAO.getFromProxy(temp2);
                for (Events event : temp2) {
                    event.getConnectedProjects().remove(project);
                    sess.update(event);
                }

            }

            if (temp3 != null && temp3.size() != 0) {
                sess.enableFetchProfile("projects-with-employees");
                temp3 = employeesDAO.getFromProxy(temp3);
                for (Employees employee : temp3) {
                    employee.getParticipate().remove(project);
                    sess.update(employee);
                }

            }

            if (temp4 != null && temp4.size() != 0) {
                sess.enableFetchProfile("projects-with-organisations");
                for (ParticipantsInProject organisation : temp4) {
                    sess.delete(organisation);
                }
            }
            sess.update(project);
            sessionFactory.getCurrentSession().delete(project);
        }
    }

    @Override
    public void changeProject(Projects project) {
        sessionFactory.getCurrentSession().update(project);
    }

    @Override
    public Projects getProjectForId(Long id) {
        //  return (Projects) sessionFactory.getCurrentSession().
        //          load(Projects.class, id);
        return (Projects) sessionFactory.getCurrentSession().get(Projects.class, id);
    }

    public Projects getProjectForName(String name) {
        return (Projects) sessionFactory.getCurrentSession().createCriteria(Projects.class)
                .add(Restrictions.eq("name", name)).uniqueResult();
    }

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

    @Override
    public List<Daily> getConnectedTasks(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-tasks");
        Projects project = (Projects) sess.get(Projects.class, id);
        return project.getTasksConnectedWithProject();
    }

    @Override
    public List<Employees> getParticipants(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-employees");
        Projects project = (Projects) sess.get(Projects.class, id);
        return project.getParticipants();
    }

    @Override
    public List<Events> getEvents(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-events");
        Projects project = (Projects) sess.get(Projects.class, id);
        return project.getEventsConnectedWithProjects();
    }

    @Override
    public List<ParticipantsInProject> getOrgParticipationInProjects(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-organisations");
        Projects project = (Projects) sess.get(Projects.class, id);
        return project.getOrganisationsInProject();
    }

    public List<Organisations> getOrganisationsForSelectedId(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-organisations");
        Organisations client = (Organisations) sess.get(Organisations.class, id);
        List<Organisations> returnList = new ArrayList<Organisations>();
        List<ParticipantsInProject> partyList = client.getParticipateInProjects();
        for (ParticipantsInProject org : partyList) {
            returnList.add(org.getOrganisation());
        }
        return returnList;
    }

    @Override
    public Organisations getOrganisations(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-orgs");
        ParticipantsInProject project = (ParticipantsInProject) sess.get(ParticipantsInProject.class, id);
        return project.getOrganisation();
    }

    public List<Projects> searchProject(Projects project) {
        Criteria results = sessionFactory.getCurrentSession().createCriteria(Projects.class);
        if (project.getId() != null && project.getId() != 0)
            results.add(Restrictions.eq("id", project.getId()));
        if (project.getName() != null && !project.getName().isEmpty())
            results.add(Restrictions.eq("name", project.getName()));
        if (project.getBegin_date() != null && !project.getBegin_date().toString().isEmpty())
            results.add(Restrictions.eq("begin_date", project.getBegin_date()));
        if (project.getProjectType() != null && !project.getProjectType().toString().isEmpty())
            results.add(Restrictions.eq("projectType", project.getProjectType()));
        return (List<Projects>) results.list();
    }

    @Override
    public ParticipantsInProject getParticipantForId(Long id) {
        return (ParticipantsInProject) sessionFactory.getCurrentSession().get(ParticipantsInProject.class, id);
    }

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

    public List<Projects> getProjectsForEmployee(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-employees");
        return sess.createCriteria(Projects.class).createCriteria("participants").add(Restrictions.eq("id", id))
                .list();
    }

    public List<Projects> getProjectsOnDate(Date date) {
        return sessionFactory.getCurrentSession().createCriteria(Projects.class)
                .add(Restrictions.le("begin_date", date)).add(Restrictions.ge("end_date", date)).list();
    }

    public List<Projects> getProjectsForEmployeeOnDate(Long id, Date date) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("projects-with-employees");
        return sess.createCriteria(Projects.class).add(Restrictions.le("begin_date", date))
                .add(Restrictions.ge("end_date", date)).createCriteria("participants")
                .add(Restrictions.eq("id", id)).list();
    }

}