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

Java tutorial

Introduction

Here is the source code for com.mycompany.CRMFly.hibernateAccess.OrganisationsDAOImpl.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.Contacts;
import com.mycompany.CRMFly.entities.ContragentsInContract;
import com.mycompany.CRMFly.entities.ContragentsInShipment;
import com.mycompany.CRMFly.entities.Documents;
import com.mycompany.CRMFly.entities.Organisations;
import com.mycompany.CRMFly.entities.ParticipantsInProject;
import com.mycompany.CRMFly.entities.Payments;
import com.mycompany.CRMFly.entities.Products;
import com.mycompany.CRMFly.entities.Projects;
import com.mycompany.CRMFly.entities.Requests;
import java.util.ArrayList;
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 OrganisationsDAOImpl implements OrganisationsDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private ContactsDAO contactsDAO;

    @Autowired
    private ProjectsDAO projectsDAO;

    @Autowired
    private ProductsDAO productsDAO;

    @Autowired
    private PaymentsDAO paymentsDAO;

    @Autowired
    private DocumentsDAO documentsDAO;

    @Autowired
    private ContractsDAO contractsDAO;

    @Autowired
    private ContractsDAO shipmentsDAO;

    @Autowired
    private RequestsDAO requestsDAO;

    @Override
    public void addOrganisation(Organisations organisation) {
        sessionFactory.getCurrentSession().save(organisation);

        List<Contacts> temp = organisation.getContacts();
        List<ParticipantsInProject> temp2 = organisation.getParticipateInProjects();
        List<Products> temp3 = organisation.getProducts();
        List<Documents> temp4 = organisation.getDocuments();
        List<Payments> temp5 = organisation.getPaymentsFromClient();
        List<Payments> temp6 = organisation.getPaymentsToClient();
        //    List<ContragentsInContract> temp3=customer.getParticipantInContracts();
        //List<ContragentsInShipment> temp4=customer.getParticipantInShipments();
        // List<Requests> temp5 = customer.getRequests(); 

        //add links to documents
        if (temp4 != null && temp4.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("clients-with-documents");
            temp4 = documentsDAO.getFromProxy(temp4);
            for (Documents doc : temp4) {
                doc.getClients().add(organisation);
                documentsDAO.changeDocument(doc);
            }
        }

        if (temp2 != null && temp2.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("clients-with-payments");
            temp5 = paymentsDAO.getFromProxy(temp5);
            for (Payments payment : temp5) {
                payment.setPayerOfPayment(organisation);
                paymentsDAO.changePayment(payment);
            }
        }

        if (organisation.getPaymentsToClient() != null) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("clients-with-payments");
            temp6 = paymentsDAO.getFromProxy(organisation.getPaymentsToClient());
            for (Payments payment : temp6) {
                payment.setReceiverOfPayment(organisation);
                paymentsDAO.changePayment(payment);
            }
        }
        if (temp != null && temp.size() != 0) {

            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("organisations-with-contacts");
            temp = contactsDAO.getFromProxy(temp);
            for (Contacts contact : temp) {
                contact.setOrganisation(organisation);
                contactsDAO.changeContact(contact);
            }
        }

        if (temp3 != null && temp3.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("organisations-with-products");
            temp3 = productsDAO.getFromProxy(temp3);
            for (Products product : temp3) {
                product.getManufacturers().add(organisation);
                productsDAO.changeProduct(product);
            }

        }

        if (temp2 != null && temp2.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            for (ParticipantsInProject project : temp2) {
                project.setOrganisation(organisation);
            }

        }
    }

    public void addDocumentConnection(Organisations customer, Documents document) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-documents");
        customer = (Organisations) sess.load(Organisations.class, customer.getId());
        customer.getDocuments().add(document);
        sess.update(customer);
        document = (Documents) sess.load(Documents.class, document.getId());
        document.getClients().add(customer);
        sess.update(document);
    }

    public void addPaymentFromConnection(Organisations customer, Payments payment) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-paymentsFrom");
        customer = (Organisations) sess.load(Organisations.class, customer.getId());
        customer.getPaymentsFromClient().add(payment);
        sess.update(customer);
        payment = (Payments) sess.load(Payments.class, payment.getId());
        payment.setPayerOfPayment(customer);
        sess.update(payment);
    }

    public void addPaymentToConnection(Organisations customer, Payments payment) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-payments");
        customer = (Organisations) sess.load(Organisations.class, customer.getId());
        customer.getPaymentsToClient().add(payment);
        sess.update(customer);
        payment = (Payments) sess.load(Payments.class, payment.getId());
        payment.setReceiverOfPayment(customer);
        sess.update(payment);
    }

    public void addProductConnection(Organisations organisation, Products product) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("organisations-with-products");
        organisation = (Organisations) sess.load(Organisations.class, organisation.getId());
        organisation.getProducts().add(product);
        sess.update(organisation);
        product = (Products) sess.load(Products.class, product.getId());
        product.getManufacturers().add(organisation);
        sess.update(product);
    }

    public void addContactConnection(Organisations organisation, Contacts contact) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("organisations-with-contacts");
        organisation = (Organisations) sess.load(Organisations.class, organisation.getId());
        organisation.getContacts().add(contact);
        sess.update(organisation);
        contact = (Contacts) sess.load(Contacts.class, contact.getId());
        contact.setOrganisation(organisation);
        sess.update(contact);
    }

    public void deleteDocumentConnection(Organisations customer, Documents document) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-documents");
        customer = (Organisations) sess.load(Organisations.class, customer.getId());
        customer.getDocuments().remove(document);
        sess.update(customer);
        document = (Documents) sess.load(Documents.class, document.getId());
        document.getClients().add(customer);
        sess.update(document);
    }

    public void deletePaymentFrom(Organisations customer, Payments payment) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-paymentsFrom");
        customer = (Organisations) sess.load(Organisations.class, customer.getId());
        customer.getPaymentsFromClient().remove(payment);
        sess.update(customer);
        payment = (Payments) sess.load(Payments.class, payment.getId());
        payment.setPayerOfPayment(null);
        paymentsDAO.removePayment(payment);
    }

    public void deletePaymentTo(Organisations customer, Payments payment) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-payments");
        customer = (Organisations) sess.load(Organisations.class, customer.getId());
        customer.getPaymentsToClient().remove(payment);
        sess.update(customer);
        payment = (Payments) sess.load(Payments.class, payment.getId());
        payment.setReceiverOfPayment(null);
        paymentsDAO.removePayment(payment);
    }

    public void deleteProductConnection(Organisations organisation, Products product) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("organisations-with-products");
        organisation = (Organisations) sess.load(Organisations.class, organisation.getId());
        organisation.getProducts().remove(product);
        sess.update(organisation);
        product = (Products) sess.load(Products.class, product.getId());
        product.getManufacturers().remove(organisation);
        sess.update(product);
    }

    public void deleteContactConnection(Organisations organisation, Contacts contact) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("organisations-with-contacts");
        organisation = (Organisations) sess.load(Organisations.class, organisation.getId());
        organisation.getContacts().remove(contact);
        sess.update(organisation);
        contact = (Contacts) sess.load(Contacts.class, contact.getId());
        contact.setOrganisation(null);
        sess.update(contact);
    }

    @Override
    public List<Organisations> listOrganisations() {

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

    @Override
    public void removeOrganisation(Organisations organisation) {
        if (null != organisation) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            organisation = (Organisations) sessionFactory.getCurrentSession().get(Organisations.class,
                    organisation.getId());
            List<Contacts> temp = organisation.getContacts();
            List<ParticipantsInProject> temp2 = organisation.getParticipateInProjects();
            List<Products> temp3 = organisation.getProducts();
            List<Documents> temp4 = organisation.getDocuments();
            List<Payments> temp5 = organisation.getPaymentsFromClient();
            List<Payments> temp6 = organisation.getPaymentsToClient();
            List<ContragentsInContract> temp7 = organisation.getParticipantInContracts();
            List<ContragentsInShipment> temp8 = organisation.getParticipantInShipments();
            List<Requests> temp9 = organisation.getRequests();
            List<Payments> temp10 = organisation.getServedPayments();

            if (temp4 != null && temp4.size() != 0) {
                sess.enableFetchProfile("clients-with-documents");
                temp4 = documentsDAO.getFromProxy(temp4);
                for (Documents doc : temp4) {
                    doc.getClients().remove(organisation);
                    sess.update(doc);
                }
            }

            if (temp5 != null && temp5.size() != 0) {
                sess.enableFetchProfile("clients-with-paymentsFrom");
                temp5 = paymentsDAO.getFromProxy(temp5);
                for (Payments payment : temp5) {
                    payment.setPayerOfPayment(null);
                    sess.update(payment);
                }
            }

            if (temp6 != null && temp6.size() != 0) {
                sess.enableFetchProfile("clients-with-payments");
                temp6 = paymentsDAO.getFromProxy(organisation.getPaymentsToClient());
                for (Payments payment : temp6) {
                    payment.setReceiverOfPayment(null);
                    sess.update(payment);
                }
            }
            if (temp != null && temp.size() != 0) {

                sess.enableFetchProfile("organisations-with-contacts");
                temp = contactsDAO.getFromProxy(temp);
                for (Contacts contact : temp) {
                    contact.setOrganisation(null);
                    sess.update(contact);
                }
            }

            if (temp3 != null && temp3.size() != 0) {
                sess.enableFetchProfile("organisations-with-products");
                temp3 = productsDAO.getFromProxy(temp3);
                for (Products product : temp3) {
                    product.getManufacturers().remove(organisation);
                    sess.update(product);
                }

            }

            if (temp2 != null && temp2.size() != 0) {

                for (ParticipantsInProject project : temp2) {
                    sess.delete(project);
                }
            }
            if (temp7 != null && temp7.size() != 0) {
                for (ContragentsInContract contragent : temp7) {
                    sess.delete(contragent);
                }
            }
            if (temp8 != null && temp8.size() != 0) {
                for (ContragentsInShipment contragent : temp8) {
                    sess.delete(contragent);
                }
            }
            if (temp9 != null && temp9.size() != 0) {
                sess.enableFetchProfile("clients-with-requests");
                temp9 = requestsDAO.getFromProxy(organisation.getRequests());
                for (Requests request : temp9) {
                    request.setRequester(null);
                    sess.update(request);
                }
            }

            if (temp10 != null && temp10.size() != 0) {
                sess.enableFetchProfile("organisations-with-servedpay");
                temp10 = paymentsDAO.getFromProxy(organisation.getServedPayments());
                for (Payments payment : temp10) {
                    payment.setBank(null);
                    sess.update(payment);
                }
            }

            sess.update(organisation);
            sessionFactory.getCurrentSession().delete(organisation);
        }
    }

    @Override
    public void changeOrganisation(Organisations organisation) {
        sessionFactory.getCurrentSession().update(organisation);
    }

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

    }

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

    @Override
    public List<Contacts> getContactsWithOrganisation(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("organisations-with-contacts");
        Organisations organisation = (Organisations) sess.get(Organisations.class, id);
        return organisation.getContacts();
    }

    @Override
    public List<Products> getManufacturedProducts(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("organisations-with-products");
        Organisations organisation = (Organisations) sess.get(Organisations.class, id);
        return organisation.getProducts();
    }

    @Override
    public List<ParticipantsInProject> getParticipationInProjects(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        Organisations organisation = (Organisations) sess.get(Organisations.class, id);
        return organisation.getParticipateInProjects();
    }

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

    public List<Projects> getProjectsForOrganisation(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("orgs-with-projects");
        Organisations organisation = (Organisations) sess.get(Organisations.class, id);
        List<Projects> returnList = new ArrayList<Projects>();
        List<ParticipantsInProject> party = organisation.getParticipateInProjects();
        for (ParticipantsInProject par : party) {
            returnList.add(par.getProject());
        }
        return returnList;
    }

    public List<Organisations> searchOrganisation(Organisations organisation) {
        Criteria results = sessionFactory.getCurrentSession().createCriteria(Organisations.class);
        if (organisation.getId() != null && organisation.getId() != 0)
            results.add(Restrictions.eq("ID", organisation.getId()));
        if (organisation.getName() != null && !organisation.getName().isEmpty())
            results.add(Restrictions.eq("name", organisation.getId()));
        if (organisation.getAccount() != null && !organisation.getAccount().isEmpty())
            results.add(Restrictions.eq("account", organisation.getAccount()));
        if (organisation.getLegal_form() != null && !organisation.getLegal_form().isEmpty())
            results.add(Restrictions.eq("legal_form", organisation.getLegal_form()));
        return (List<Organisations>) results.list();
    }

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

    /*public List<Organisations> searchOrganisation(Organisations organisation) {
         
     return sessionFactory.getCurrentSession().
             createQuery("from Clients where )
                
    }*/

}