com.caiyunworks.crm.business.service.impl.HolidayServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.caiyunworks.crm.business.service.impl.HolidayServiceImpl.java

Source

package com.caiyunworks.crm.business.service.impl;

import com.caiyunworks.crm.business.exception.OutOfDateRecordException;
import com.caiyunworks.crm.business.exception.RecordAlreadyExistException;
import com.caiyunworks.crm.business.exception.RecordNotExistException;
import com.caiyunworks.crm.business.service.HolidayService;
import com.caiyunworks.crm.persist.model.Holiday;
import com.caiyunworks.crm.persist.repo.HolidayRepository;
import com.google.common.collect.Iterables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * Copyright (c) 2015, All Right Reserved, http://www.caiyunworks.com/
 * <br/>
 * This source is subject to the CaiYunWorks Software License.
 * Please see the License.txt file for more information.
 * All other rights reserved.
 * <br/>
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE
 * <br/>
 * Created on 9/3/15.
 *
 * @author Tony Wang (ziscloud@gmail.com)
 */
@Service
public class HolidayServiceImpl implements HolidayService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private HolidayRepository holidayRepository;

    @Override
    public Iterable<Holiday> getDeleted() {
        return holidayRepository.findByDeleted(true);
    }

    @Override
    public int delete(Holiday record) throws RecordNotExistException, OutOfDateRecordException {
        Holiday holiday = holidayRepository.findById(record.getId());
        if (null == holiday) {
            if (logger.isWarnEnabled()) {
                logger.warn("try to delete holiday {}, but it does not exist in DB.", record.getId());
            }
            throw new RecordNotExistException();
        } else {
            if (holiday.getVersionNumber().equals(record.getVersionNumber())) {
                return holidayRepository.delete(record);
            } else {
                if (logger.isWarnEnabled()) {
                    logger.warn("holiday record is out of date, version {}, latest version {}",
                            record.getVersionNumber(), holiday.getVersionNumber());
                }
                throw new OutOfDateRecordException();
            }
        }
    }

    @Override
    public void create(Holiday record) throws UnsupportedOperationException, RecordAlreadyExistException {
        Iterable<Holiday> holiday = holidayRepository.findByNameOrDateRange(record.getName(), record.getStartDate(),
                record.getEndDate());
        if (null == holiday || Iterables.isEmpty(holiday)) {
            holidayRepository.create(record);
        } else {
            if (logger.isWarnEnabled()) {
                logger.warn("try to create holiday {}, but it is already exist in DB.", record);
            }
            throw new RecordAlreadyExistException();
        }
    }

    @Override
    public int update(Holiday record) throws UnsupportedOperationException, RecordNotExistException,
            OutOfDateRecordException, RecordAlreadyExistException {
        Holiday holiday = holidayRepository.findById(record.getId());
        if (null == holiday) {
            if (logger.isWarnEnabled()) {
                logger.warn("try to update holiday {}, but it does not exist in DB.", record.getId());
            }
            throw new RecordNotExistException();
        } else {
            if (!holiday.getVersionNumber().equals(record.getVersionNumber())) {
                if (logger.isWarnEnabled()) {
                    logger.warn("holiday record is out of date, version {}, latest version {}",
                            record.getVersionNumber(), holiday.getVersionNumber());
                }
                throw new OutOfDateRecordException();
            }

            Iterable<Holiday> holidays = holidayRepository.findByNameOrDateRange(record.getName(),
                    record.getStartDate(), record.getEndDate());
            if (null == holidays || Iterables.isEmpty(holidays)) {
                return holidayRepository.update(record);
            } else {
                if (Iterables.size(holidays) == 1 && Iterables.get(holidays, 0).getId() == record.getId()) {
                    return holidayRepository.update(record);
                } else {
                    if (logger.isWarnEnabled()) {
                        logger.warn("try to update holiday {}, but it is already exist in DB.", record);
                    }
                    throw new RecordAlreadyExistException();
                }
            }
        }
    }

    @Override
    public Holiday get(int id) {
        return holidayRepository.findById(id);
    }

    @Override
    public Iterable<Holiday> getAll() {
        return holidayRepository.findAll();
    }

    /**
     * filter the holidays by the start date
     *
     * @param startDate the date compared to the holiday start date
     * @return the holiday whose start date is greater or equal to the startDate
     */
    @Override
    public Iterable<Holiday> get(LocalDate startDate) {
        return holidayRepository.find(startDate);
    }

    /**
     * filter the holidays by the name, start date and end date
     *
     * @param name      holiday name going to match
     * @param startDate start date
     * @param endDate   end date
     * @return the holidays match the filter
     */
    @Override
    public Iterable<Holiday> get(String name, LocalDate startDate, LocalDate endDate) {
        return holidayRepository.findByNameOrDateRange(name, startDate, endDate);
    }
}