service.WorkReportService.java Source code

Java tutorial

Introduction

Here is the source code for service.WorkReportService.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 datastructure.WorkReportData;
import entity.Notice;
import entity.OrderLog;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.parent.PrimService;

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

    /**
     *   -   ?? ??
     */
    public static final String SHOW_SYSTEM_REPORT_TYPE = "showSystem";

    /**
     *   -   ?? 
     */
    public static final String SHOW_AUTHORS_REPORT_TYPE = "showAuthors";

    /**
     *   -   ?? 
     */
    public static final String SHOW_USERS_REPORT_TYPE = "showUsers";

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private OrderLogService orderLogService;

    public List<WorkReportData> getReport(String type, Date dateFrom, Date dateTo) {
        List<Notice> notices = new ArrayList();
        if (type != null) {
            if (type.equals(SHOW_USERS_REPORT_TYPE)) {
                notices = noticeService.getUsers(dateFrom, dateTo);
            } else if (type.equals(SHOW_SYSTEM_REPORT_TYPE)) {
                notices = noticeService.getAllNotAuthors(dateFrom, dateTo);
            } else if (type.equals(SHOW_AUTHORS_REPORT_TYPE)) {
                notices = noticeService.getSystemOnlyAuthors(dateFrom, dateTo);
            }
        }
        List<OrderLog> logs = new ArrayList();
        if (!(type != null && type.equals(SHOW_AUTHORS_REPORT_TYPE))) {
            logs = orderLogService.getAll(dateFrom, dateTo);
        }
        List<WorkReportData> dataList = createDataList(notices, logs);
        sortDataList(dataList);
        return dataList;
    }

    private List<WorkReportData> createDataList(List<Notice> notices, List<OrderLog> logs) {
        List<WorkReportData> dataList = new ArrayList();
        for (Notice notice : notices) {
            dataList.add(new WorkReportData(notice));
        }
        for (OrderLog log : logs) {
            dataList.add(new WorkReportData(log));
        }
        return dataList;
    }

    private void sortDataList(List<WorkReportData> dataList) {
        Collections.sort(dataList, new Comparator<WorkReportData>() {

            @Override
            public int compare(WorkReportData o1, WorkReportData o2) {
                Date createDate1 = getDate(o1);
                Date createDate2 = getDate(o2);
                return -(createDate1.compareTo(createDate2));
            }

            private Date getDate(WorkReportData data) {
                if (data.getNotice() != null) {
                    return data.getNotice().getNoticeDate();
                } else {
                    return data.getLog().getCreateDate();
                }
            }

        });
    }

}