Java tutorial
/* * 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.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 com.mycompany.CRMFly.hibernateAccess.ClientsDAO; 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 ClientsServiceImpl implements ClientsService { @Autowired private ClientsDAO clientsDAO; @Transactional public void addClient(Clients client) { clientsDAO.addClient(client); } @Transactional public List<Clients> listClients() { return clientsDAO.listClients(); } @Transactional public void removeClient(Clients client) { clientsDAO.removeClient(client); } @Transactional public void changeClient(Clients client) { clientsDAO.changeClient(client); } @Transactional public Clients getClientForId(Long id) { return clientsDAO.getClientForId(id); } @Transactional public List<Clients> getAllClients() { return clientsDAO.getAllClients(); } @Transactional public List<Documents> getDocumentsforClient(Long id) { return clientsDAO.getDocumentsforClient(id); } @Transactional public List<Payments> getPaymentsToClient(Long id) { return clientsDAO.getPaymentsToClient(id); } @Transactional public List<Payments> getPaymentsFromClient(Long id) { return clientsDAO.getPaymentsFromClient(id); } @Transactional public List<ContragentsInContract> getParticipationInContract(Long id) { return clientsDAO.getParticipationInContract(id); } @Transactional public List<ContragentsInShipment> getParticipationInShipments(Long id) { return clientsDAO.getParticipationInShipments(id); } @Transactional public Contracts getContractForPosition(Long id) { return clientsDAO.getContractForPosition(id); } @Transactional public Shipments getShipmentForPosition(Long id) { return clientsDAO.getShipmentForPosition(id); } @Transactional public List<Shipments> getShipmentsForClient(Long id) { return clientsDAO.getShipmentsForClient(id); } @Transactional public List<Requests> getRequestsFromClient(Long id) { return clientsDAO.getRequestsFromClient(id); } @Transactional public List<Contracts> getContractsForClient(Long id) { return clientsDAO.getContractsForClient(id); } @Transactional public Map<Date, Double> getSumsFotDates(Clients client) { Long id = client.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.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); // 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<Contracts> contracts = clientsDAO.getContractsForClient(id); HashMap<Date, Double> sums = new HashMap<Date, Double>(); for (Contracts contract : contracts) { Date date = contract.getBegin_date(); if (date.after(beginningOfQuarter)) thisQuarterSum += contract.getTotalSum(); else if (date.after(QuarterBackOne)) QuarterBackOneSum += contract.getTotalSum(); else if (date.after(QuarterBackTwo)) QuarterBackTwoSum += contract.getTotalSum(); else if (date.after(QuarterBackThree)) QuarterBackThreeSum += contract.getTotalSum(); } sums.put(beginningOfQuarter, thisQuarterSum); sums.put(QuarterBackOne, QuarterBackOneSum); sums.put(QuarterBackTwo, QuarterBackTwoSum); sums.put(QuarterBackThree, QuarterBackThreeSum); return sums; } }