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

Java tutorial

Introduction

Here is the source code for com.mycompany.CRMFly.hibernateAccess.ClientsDAOImpl.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.Contracts;
import com.mycompany.CRMFly.entities.ContragentsInContract;
import com.mycompany.CRMFly.entities.ContragentsInShipment;
import com.mycompany.CRMFly.entities.Documents;
import com.mycompany.CRMFly.entities.Payments;
import com.mycompany.CRMFly.entities.Requests;
import com.mycompany.CRMFly.entities.Shipments;
import java.util.ArrayList;
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 ClientsDAOImpl implements ClientsDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private PaymentsDAO paymentsDAO;

    @Autowired
    private DocumentsDAO documentsDAO;

    @Autowired
    private ContractsDAO contractsDAO;

    @Autowired
    private ContractsDAO shipmentsDAO;

    @Override
    public void addClient(Clients client) {
        List<Documents> temp = client.getDocuments();
        List<Payments> temp2 = client.getPaymentsFromClient();
        sessionFactory.getCurrentSession().save(client);

        //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(client);
                documentsDAO.changeDocument(doc);
            }
        }

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

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

    }

    @Override
    public List<Clients> listClients() {

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

    @Override
    public void removeClient(Clients client) {
        if (null != client) {
            sessionFactory.getCurrentSession().delete(client);
        }
    }

    @Override
    public void changeClient(Clients client) {
        sessionFactory.getCurrentSession().update(client);
    }

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

    }

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

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

    @Override
    public List<Payments> getPaymentsToClient(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-payments");
        Clients client = (Clients) sess.get(Clients.class, id);
        return client.getPaymentsToClient();
    }

    @Override
    public List<Payments> getPaymentsFromClient(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-paymentsFrom");
        Clients client = (Clients) sess.get(Clients.class, id);
        return client.getPaymentsFromClient();
    }

    @Override
    public List<Requests> getRequestsFromClient(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-requests");
        Clients client = (Clients) sess.get(Clients.class, id);
        return client.getRequests();
    }

    public List<Contracts> getContractsForClient(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-rolesCont");
        sess.enableFetchProfile("clients-with-contracts");
        Clients client = (Clients) sess.get(Clients.class, id);
        List<Contracts> returnList = new ArrayList<Contracts>();
        List<ContragentsInContract> contrList = client.getParticipantInContracts();
        for (ContragentsInContract con : contrList) {
            returnList.add(con.getContract());
        }
        return returnList;
    }

    public List<Shipments> getShipmentsForClient(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-rolesShip");
        sess.enableFetchProfile("clients-with-shipments");
        Clients client = (Clients) sess.get(Clients.class, id);
        List<Shipments> returnList = new ArrayList<Shipments>();
        List<ContragentsInShipment> shipList = client.getParticipantInShipments();
        for (ContragentsInShipment con : shipList) {
            returnList.add(con.getShipment());
        }
        return returnList;
    }

    @Override
    public List<ContragentsInContract> getParticipationInContract(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-rolesCont");
        Clients client = (Clients) sess.get(Clients.class, id);
        return client.getParticipantInContracts();
    }

    @Override
    public List<ContragentsInShipment> getParticipationInShipments(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-rolesShip");
        Clients client = (Clients) sess.get(Clients.class, id);
        return client.getParticipantInShipments();
    }

    @Override
    public Contracts getContractForPosition(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-contracts");
        ContragentsInContract client = (ContragentsInContract) sess.get(ContragentsInContract.class, id);
        return client.getContract();
    }

    @Override
    public Shipments getShipmentForPosition(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("clients-with-shipments");
        ContragentsInShipment client = (ContragentsInShipment) sess.get(ContragentsInShipment.class, id);
        return client.getShipment();
    }

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

}