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 service; import support.AuthManager; import entity.Order; import entity.Payment; import entity.PaymentFile; import entity.User; import java.io.IOException; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import persistence.OrderDao; import persistence.PaymentDao; import persistence.UserDao; import service.parent.PrimService; import support.ServiceResult; /** * * @author Rice Pavel */ @Service @Transactional public class PaymentService extends PrimService { @Autowired private PaymentDao paymentDao; @Autowired private OrderDao orderDao; @Autowired private UserDao userDao; public ServiceResult save(Payment payment, Long orderId, MultipartFile[] files) throws IOException { Order order = orderDao.find(orderId); String username = AuthManager.getUserName(); User user = userDao.getUserByLogin(username); payment.setUser(user); payment.setPaymentDate(new Date()); payment.setOrder(order); payment.setFinalPayment(isFinalPayment(payment, order)); ServiceResult result = validateSave(payment, paymentDao); if (!result.hasErrors()) { if (files != null) { for (MultipartFile file : files) { if (file != null && !file.isEmpty()) { PaymentFile fileEntity = new PaymentFile(); fileEntity.setPayment(payment); saveFile(fileEntity, file); } } } } return result; } public boolean isFinalPayment(Payment payment, Order order) { Double cost = (order.getCost() != null ? order.getCost() : 0.0); Double paymentSum = (order.getPaymentSum() != null ? order.getPaymentSum() : 0.0); Double amount = (payment.getAmount() != null ? payment.getAmount() : 0.0); Double finalPaymentSum = paymentSum + amount; return (finalPaymentSum >= cost); } public List<Payment> getAll() { return paymentDao.getAll(); } public List<Payment> getByOrderId(Long orderId) { return paymentDao.getByOrderId(orderId); } public ServiceResult update(Payment obj) { return validateUpdate(obj, paymentDao); } public void delete(Payment obj) { paymentDao.delete(obj); } public Payment getOne(Long id) { return paymentDao.find(id); } public void delete(Long id) { _delete(id, paymentDao); } }