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

Java tutorial

Introduction

Here is the source code for com.mycompany.CRMFly.hibernateAccess.CustomersDAOImpl.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.Clients;
import com.mycompany.CRMFly.entities.ContragentsInContract;
import com.mycompany.CRMFly.entities.ContragentsInShipment;
import com.mycompany.CRMFly.entities.Customers;
import com.mycompany.CRMFly.entities.Documents;
import com.mycompany.CRMFly.entities.Payments;
import com.mycompany.CRMFly.entities.PositionsInContract;
import com.mycompany.CRMFly.entities.Requests;
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 CustomersDAOImpl implements CustomersDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private PaymentsDAO paymentsDAO;

    @Autowired
    private DocumentsDAO documentsDAO;

    @Autowired
    private ContractsDAO contractsDAO;

    @Autowired
    private ContractsDAO shipmentsDAO;

    @Autowired
    private RequestsDAO requestsDAO;

    @Override
    public void addCustomer(Customers customer) {
        sessionFactory.getCurrentSession().save(customer);

        List<Documents> temp = customer.getDocuments();
        List<Payments> temp2 = customer.getPaymentsFromClient();
        //List<ContragentsInContract> temp3=customer.getParticipantInContracts();
        //List<ContragentsInShipment> temp4=customer.getParticipantInShipments();
        //List<Requests> temp5 = customer.getRequests();

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

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

        if (customer.getPaymentsToClient() != null) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("clients-with-payments");
            temp2 = paymentsDAO.getFromProxy(customer.getPaymentsToClient());
            for (Payments payment : temp2) {
                payment.setReceiverOfPayment(customer);
                paymentsDAO.changePayment(payment);
            }
        }

    }

    public void addDocumentConnection(Customers customer, Documents document) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-documents");
        customer = (Customers) sess.load(Customers.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(Customers customer, Payments payment) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-paymentsFrom");
        customer = (Customers) sess.load(Customers.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(Customers customer, Payments payment) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-payments");
        customer = (Customers) sess.load(Customers.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 deleteDocumentConnection(Customers customer, Documents document) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-documents");
        customer = (Customers) sess.load(Customers.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(Customers customer, Payments payment) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-paymentsFrom");
        customer = (Customers) sess.load(Customers.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(Customers customer, Payments payment) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-payments");
        customer = (Customers) sess.load(Customers.class, customer.getId());
        customer.getPaymentsToClient().remove(payment);
        sess.update(customer);
        payment = (Payments) sess.load(Payments.class, payment.getId());
        payment.setReceiverOfPayment(null);
        paymentsDAO.removePayment(payment);
    }

    @Override
    public List<Customers> listCustomers() {

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

    @Override
    public void removeCustomer(Customers customer) {
        if (null != customer) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            customer = (Customers) sess.get(Customers.class, customer.getId());
            List<Documents> temp = customer.getDocuments();
            List<Payments> temp2 = customer.getPaymentsFromClient();
            List<ContragentsInContract> temp3 = customer.getParticipantInContracts();
            List<ContragentsInShipment> temp4 = customer.getParticipantInShipments();
            List<Requests> temp5 = customer.getRequests();

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

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

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

            if (temp3 != null && temp3.size() != 0) {
                for (ContragentsInContract contragent : temp3) {
                    sess.delete(contragent);
                }
            }
            if (temp4 != null && temp4.size() != 0) {
                for (ContragentsInShipment contragent : temp4) {
                    sess.delete(contragent);
                }
            }
            if (temp5 != null && temp5.size() != 0) {
                sess.enableFetchProfile("clients-with-requests");
                temp5 = requestsDAO.getFromProxy(customer.getRequests());
                for (Requests request : temp5) {
                    request.setRequester(null);
                    sess.update(request);
                }
            }
            sess.update(customer);
            sessionFactory.getCurrentSession().delete(customer);
        }
    }

    @Override
    public void changeCustomer(Customers customer) {
        sessionFactory.getCurrentSession().update(customer);
    }

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

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

    @Override
    public List<Documents> getDocumentsforCustomer(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-documents");
        Clients customer = (Clients) sess.get(Clients.class, id);
        return customer.getDocuments();
    }

    public List<Customers> searchCustomer(Customers customer) {
        Criteria results = sessionFactory.getCurrentSession().createCriteria(Customers.class);
        if (customer.getId() != null && customer.getId() != 0)
            results.add(Restrictions.eq("id", customer.getId()));
        if (customer.getName() != null && !customer.getName().isEmpty())
            results.add(Restrictions.eq("name", customer.getName()));
        if (customer.getTelephone() != null && !customer.getTelephone().isEmpty())
            results.add(Restrictions.eq("telephone", customer.getTelephone()));
        if (customer.getEmail() != null && !customer.getEmail().isEmpty())
            results.add(Restrictions.eq("email", customer.getEmail()));
        if (customer.getCreditCardNumber() != null && !customer.getCreditCardNumber().isEmpty())
            results.add(Restrictions.eq("creditCardNumber", customer.getCreditCardNumber()));
        return (List<Customers>) results.list();
    }

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

}