com.mycompany.CRMFly.serviceLayer.ProductsServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.CRMFly.serviceLayer.ProductsServiceImpl.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.serviceLayer;

import com.mycompany.CRMFly.entities.Contracts;
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 com.mycompany.CRMFly.hibernateAccess.ContractsDAO;
import com.mycompany.CRMFly.hibernateAccess.ProductsDAO;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author ??
 */
@Service
public class ProductsServiceImpl implements ProductsService {

    @Autowired
    private ProductsDAO productsDAO;

    @Autowired
    private ContractsDAO contractsDAO;

    @Transactional
    public void addProduct(Products product) {
        productsDAO.addProduct(product);
    }

    @Transactional
    public List<Products> listProducts() {

        return productsDAO.listProducts();
    }

    @Transactional
    public void removeProduct(Products product) {
        productsDAO.removeProduct(product);
    }

    @Transactional
    public void changeProduct(Products product) {
        productsDAO.changeProduct(product);
    }

    @Transactional
    public Products getProductForId(Long id) {
        return productsDAO.getProductForId(id);
    }

    @Transactional
    public List<Products> getAllProducts() {
        return productsDAO.getAllProducts();
    }

    @Transactional
    public List<PositionsInContract> getContractPositionsForProduct(Long id) {

        return productsDAO.getContractPositionsForProduct(id);
    }

    @Transactional
    public List<PositionsInShipment> getShipmentPositionsForProduct(Long id) {

        return productsDAO.getShipmentPositionsForProduct(id);
    }

    @Transactional
    public Contracts getContractForPosition(Long id) {

        return productsDAO.getContractForPosition(id);
    }

    @Transactional
    public Shipments getShipmentForPosition(Long id) {

        return productsDAO.getShipmentForPosition(id);
    }

    @Transactional
    public List<Organisations> getManufacturersOfProduct(Long id) {

        return productsDAO.getManufacturersOfProduct(id);
    }

    @Transactional
    public List<Products> searchProduct(Products product) {
        return productsDAO.searchProduct(product);
    }

    @Transactional
    public List<Products> getFromProxy(List<Products> proxy) {
        return productsDAO.getFromProxy(proxy);
    }

    @Transactional
    public List<Contracts> getContractsForProduct(Long id) {
        return productsDAO.getContractsForProduct(id);
    }

    @Transactional
    public List<Shipments> getShipmentsForProduct(Long id) {
        return productsDAO.getShipmentsForProduct(id);
    }

    @Transactional
    public void addManufacturerConnection(Products product, Organisations organisation) {

        productsDAO.addManufacturerConnection(product, organisation);
    }

    @Transactional
    public void deleteManufacturerConnection(Products product, Organisations organisation) {

        productsDAO.deleteManufacturerConnection(product, organisation);
    }

    @Transactional
    public Map<Date, Double> getSumsFotDates(Products product) {
        Long id = product.getId();

        Calendar calendar = new GregorianCalendar();
        Integer monthDifference = 0;
        if (calendar.get(Calendar.MONTH) == Calendar.JANUARY || calendar.get(Calendar.MONTH) == Calendar.APRIL
                || calendar.get(Calendar.MONTH) == Calendar.JULY
                || calendar.get(Calendar.MONTH) == Calendar.OCTOBER) {
            monthDifference = 0;
        } else if (calendar.get(Calendar.MONTH) == Calendar.FEBRUARY || calendar.get(Calendar.MONTH) == Calendar.MAY
                || calendar.get(Calendar.MONTH) == Calendar.AUGUST
                || calendar.get(Calendar.MONTH) == Calendar.NOVEMBER) {
            monthDifference = -1;
        } else if (calendar.get(Calendar.MONTH) == Calendar.MARCH || calendar.get(Calendar.MONTH) == Calendar.JUNE
                || calendar.get(Calendar.MONTH) == Calendar.SEPTEMBER
                || calendar.get(Calendar.MONTH) == Calendar.DECEMBER)
            monthDifference = -2;
        Integer dayDifference = -calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.MONTH, monthDifference);
        calendar.add(Calendar.DAY_OF_MONTH, dayDifference - 1);

        Date beginningOfQuarter = calendar.getTime();
        Double thisQuarterSum = 0.0;
        calendar.add(Calendar.MONTH, -3);
        Date QuarterBackOne = calendar.getTime();
        Double QuarterBackOneSum = 0.0;
        calendar.add(Calendar.MONTH, -3);
        Date QuarterBackTwo = calendar.getTime();
        Double QuarterBackTwoSum = 0.0;
        calendar.add(Calendar.MONTH, -3);
        Date QuarterBackThree = calendar.getTime();
        Double QuarterBackThreeSum = 0.0;

        List<PositionsInContract> positions = productsDAO.getContractPositionsForProduct(id);
        HashMap<Date, Double> sums = new HashMap<Date, Double>();
        for (PositionsInContract position : positions) {
            Long posID = position.getId();
            Contracts contract = productsDAO.getContractForPosition(posID);
            Date date = contract.getBegin_date();
            if (date.after(beginningOfQuarter))
                thisQuarterSum += position.getPrice() * position.getQuantity();
            else if (date.after(QuarterBackOne))
                QuarterBackOneSum += position.getPrice() * position.getQuantity();
            else if (date.after(QuarterBackTwo))
                QuarterBackTwoSum += position.getPrice() * position.getQuantity();
            else if (date.after(QuarterBackThree))
                QuarterBackThreeSum += position.getPrice() * position.getQuantity();
        }
        sums.put(beginningOfQuarter, thisQuarterSum);
        sums.put(QuarterBackOne, QuarterBackOneSum);
        sums.put(QuarterBackTwo, QuarterBackTwoSum);
        sums.put(QuarterBackThree, QuarterBackThreeSum);
        return sums;
    }
}