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

Java tutorial

Introduction

Here is the source code for com.mycompany.CRMFly.hibernateAccess.ProductsDAOImpl.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.Organisations;
import com.mycompany.CRMFly.entities.PositionsInContract;
import com.mycompany.CRMFly.entities.PositionsInShipment;
import com.mycompany.CRMFly.entities.Products;
import com.mycompany.CRMFly.entities.Shipments;
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 ProductsDAOImpl implements ProductsDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private OrganisationsDAO organisationsDAO;

    @Override
    public void addProduct(Products product) {
        sessionFactory.getCurrentSession().save(product);
        List<Organisations> temp = product.getManufacturers();
        if (temp != null && temp.size() != 0) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            sess.enableFetchProfile("products-with-manufacturers");
            temp = organisationsDAO.getFromProxy(temp);
            for (Organisations organisation : temp) {
                organisation.getProducts().add(product);
                organisationsDAO.changeOrganisation(organisation);
            }

        }
    }

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

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

    @Override
    public List<Products> listProducts() {

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

    @Override
    public void removeProduct(Products product) {
        if (null != product) {
            org.hibernate.Session sess = sessionFactory.getCurrentSession();
            product = (Products) sess.get(Products.class, product.getId());

            List<Organisations> temp = product.getManufacturers();
            if (temp != null && temp.size() != 0) {

                sess.enableFetchProfile("products-with-manufacturers");
                temp = organisationsDAO.getFromProxy(temp);
                for (Organisations organisation : temp) {
                    organisation.getProducts().remove(product);
                    sess.update(organisation);
                }
            }
            sess.update(product);
            sessionFactory.getCurrentSession().delete(product);
        }
    }

    @Override
    public void changeProduct(Products product) {

        sessionFactory.getCurrentSession().update(product);

    }

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

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

    @Override
    public List<PositionsInContract> getContractPositionsForProduct(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("products-with-positionsCont");
        Products product = (Products) sess.get(Products.class, id);
        return product.getPositionsInContract();
    }

    @Override
    public List<PositionsInShipment> getShipmentPositionsForProduct(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("products-with-positionsShip");
        Products product = (Products) sess.get(Products.class, id);
        return product.getPositionsInShipment();
    }

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

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

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

    public List<Contracts> getContractsForProduct(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("products-with-positionsCont");
        sess.enableFetchProfile("products-with-contracts");
        Products product = (Products) sess.get(Products.class, id);
        List<Contracts> returnList = new ArrayList<Contracts>();
        List<PositionsInContract> contrList = product.getPositionsInContract();
        for (PositionsInContract con : contrList) {
            returnList.add(con.getContract());
        }
        return returnList;
    }

    public List<Shipments> getShipmentsForProduct(Long id) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        sess.enableFetchProfile("products-with-positionsShip");
        sess.enableFetchProfile("products-with-shipments");
        Products product = (Products) sess.get(Products.class, id);
        List<Shipments> returnList = new ArrayList<Shipments>();
        List<PositionsInShipment> shipList = product.getPositionsInShipment();
        for (PositionsInShipment ship : shipList) {
            returnList.add(ship.getShipment());
        }
        return returnList;
    }

    public List<Products> searchProduct(Products product) {
        Criteria results = sessionFactory.getCurrentSession().createCriteria(Products.class);
        if (product.getId() != null && product.getId() != 0)
            results.add(Restrictions.eq("id", product.getId()));
        if (product.getName() != null && !product.getName().isEmpty())
            results.add(Restrictions.eq("name", product.getName()));
        if (product.getArticle() != null && !product.getArticle().isEmpty())
            results.add(Restrictions.eq("article", product.getArticle()));
        return (List<Products>) results.list();
    }

    public List<Products> getFromProxy(List<Products> proxy) {
        org.hibernate.Session sess = sessionFactory.getCurrentSession();
        //    sess.enableFetchProfile("products-with-manufacturers");
        Disjunction or = Restrictions.disjunction();
        for (Products pr : proxy) {
            or.add(Restrictions.eq("id", pr.getId()));
        }
        return sessionFactory.getCurrentSession().createCriteria(Products.class).add(or).list();
    }

}