service.DelegateMessageService.java Source code

Java tutorial

Introduction

Here is the source code for service.DelegateMessageService.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 service;

import entity.DelegateMessage;
import entity.DelegateMessageFile;
import entity.Order;
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.DelegateMessageDao;
import persistence.OrderDao;
import service.parent.PrimService;
import support.ServiceResult;

/**
 *
 * @author Rice Pavel
 */
@Service
@Transactional
public class DelegateMessageService extends PrimService {

    @Autowired
    private DelegateMessageDao delegateMessageDao;

    @Autowired
    private FileService fileService;

    @Autowired
    private OrderDao orderDao;

    public enum Type {

        FROM_CHILD_TO_PARENT, FROM_PAREN_TO_CHILD;
    }

    public void readyMessages(Long orderId) {
        Order order = orderDao.find(orderId);
        List<DelegateMessage> list = delegateMessageDao.getNotReadyMessagesToOrder(order);
        for (DelegateMessage message : list) {
            message.setReady(true);
            delegateMessageDao.update(message);
        }
    }

    public ServiceResult save(DelegateMessage message, MultipartFile[] files, Long orderIdFrom, Long orderIdTo,
            Type type) throws IOException {
        ServiceResult res = new ServiceResult();
        Order orderFrom = orderDao.find(orderIdFrom);
        Order orderTo = orderDao.find(orderIdTo);
        if (type.equals(Type.FROM_CHILD_TO_PARENT)) {
            orderFrom.setDesiredCost(message.getCost());
            orderDao.update(orderFrom);
        }
        message.setOrderFrom(orderFrom);
        message.setOrderTo(orderTo);
        message.setMessageDate(new Date());
        message.setInsertUser(authManager.getCurrentUser());
        validateSave(message, delegateMessageDao, res);
        if (!res.hasErrors()) {
            if (files != null) {
                for (MultipartFile file : files) {
                    if (file != null && !file.isEmpty()) {
                        DelegateMessageFile fileEntity = new DelegateMessageFile();
                        fileEntity.setMessage(message);
                        fileService.saveNewFile(fileEntity, file);
                    }
                }
            }
        }
        return res;
    }

    public List<DelegateMessage> getByOrder(Order order) {
        return delegateMessageDao.getByOrder(order);
    }

}