churashima.action.manage.ReportAction.java Source code

Java tutorial

Introduction

Here is the source code for churashima.action.manage.ReportAction.java

Source

/*
 * Copyright 2004-2008 the Seasar Foundation and the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package churashima.action.manage;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.seasar.framework.container.SingletonS2Container;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;

import churashima.dao.ExpensesDao;
import churashima.dao.WorkDao;
import churashima.dto.LoginInfoDto;
import churashima.dto.ReportDto;
import churashima.entity.Expenses;
import churashima.form.ReportForm;
import churashima.service.Const;

public class ReportAction {

    @ActionForm
    @Resource
    public ReportForm reportForm;

    @Resource
    public LoginInfoDto loginInfoDto;

    @Execute(validator = false)
    public String index() {
        WorkDao workDao = SingletonS2Container.getComponent(WorkDao.class);
        List<ReportDto> reportDtoList = workDao.selectForReportSubject(null, null, Const.VIEW_ALL);

        reportForm.reportDtoList = reportDtoList;

        return "subjectReport.jsp";
    }

    @Execute(validator = false)
    public String search() {

        WorkDao workDao = SingletonS2Container.getComponent(WorkDao.class);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Date dateFrom = null;
        if (StringUtils.isNotEmpty(reportForm.searchDayFrom)) {
            try {
                dateFrom = sdf.parse(reportForm.searchDayFrom);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Date dateTo = null;
        if (StringUtils.isNotEmpty(reportForm.searchDayTo)) {
            try {
                dateTo = sdf.parse(reportForm.searchDayTo);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        List<ReportDto> reportDtoList = workDao.selectForReportSubject(dateFrom, dateTo, Const.VIEW_ALL);

        reportForm.reportDtoList = reportDtoList;

        return "subjectReport.jsp";
    }

    @Execute(validator = false)
    public String userBySubject() {

        WorkDao workDao = SingletonS2Container.getComponent(WorkDao.class);
        List<ReportDto> reportDtoList = workDao.selectForReportUser(reportForm.subjectId, reportForm.kind,
                Const.VIEW_ALL);

        reportForm.reportDtoList = reportDtoList;

        return "userReport.jsp";
    }

    @Execute(validator = false)
    public String expensesBySubject() {

        ExpensesDao expensesDao = SingletonS2Container.getComponent(ExpensesDao.class);
        List<Expenses> expensesList = expensesDao.selectForSearch(null, reportForm.subjectId, null, null, null,
                null, null, null, null);

        reportForm.expensesList = expensesList;

        return "subjectExpensesReport.jsp";
    }

    @Execute(validator = false)
    public String attendance() {
        String ym = reportForm.ym;

        int year = Integer.parseInt(ym.substring(0, 4));
        int month = Integer.parseInt(ym.substring(4));

        Calendar calFrom = Calendar.getInstance();
        calFrom.set(Calendar.YEAR, year);
        calFrom.set(Calendar.MONTH, month - 1);
        calFrom.set(Calendar.DAY_OF_MONTH, 1);
        Calendar calTo = Calendar.getInstance();
        calTo.set(Calendar.YEAR, year);
        calTo.set(Calendar.MONTH, month);
        calTo.set(Calendar.DAY_OF_MONTH, 1);

        WorkDao workDao = SingletonS2Container.getComponent(WorkDao.class);
        List<ReportDto> reportDtoList = workDao.selectForReportAttendance(reportForm.kind, calFrom.getTime(),
                calTo.getTime());

        BigDecimal workHourTotal = BigDecimal.valueOf(0);
        BigDecimal overHourTotal = BigDecimal.valueOf(0);
        BigDecimal overHourMorningTotal = BigDecimal.valueOf(0);
        BigDecimal overHourEveningTotal = BigDecimal.valueOf(0);
        BigDecimal overHourNightTotal = BigDecimal.valueOf(0);

        for (ReportDto dto : reportDtoList) {
            workHourTotal = workHourTotal.add(dto.workHourTotal);
            overHourTotal = overHourTotal.add(dto.overHourTotal);
            overHourMorningTotal = overHourMorningTotal.add(dto.overHourMorningTotal);
            overHourEveningTotal = overHourEveningTotal.add(dto.overHourEveningTotal);
            overHourNightTotal = overHourNightTotal.add(dto.overHourNightTotal);
        }

        reportForm.workHourTotal = workHourTotal;
        reportForm.overHourTotal = overHourTotal;
        reportForm.overHourMorningTotal = overHourMorningTotal;
        reportForm.overHourEveningTotal = overHourEveningTotal;
        reportForm.overHourNightTotal = overHourNightTotal;

        // ?????
        calFrom.add(Calendar.MONTH, -1);
        calTo.add(Calendar.MONTH, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        int count = workDao.selectForReportExist(reportForm.kind, calFrom.getTime(), calTo.getTime(), null);
        if (count > 0) {
            reportForm.ymBefore = sdf.format(calFrom.getTime());
        } else {
            reportForm.ymBefore = null;
        }

        // ????
        calFrom.add(Calendar.MONTH, +2);
        calTo.add(Calendar.MONTH, +2);
        count = workDao.selectForReportExist(reportForm.kind, calFrom.getTime(), calTo.getTime(), null);
        if (count > 0) {
            reportForm.ymNext = sdf.format(calFrom.getTime());
        } else {
            reportForm.ymNext = null;
        }

        reportForm.reportDtoList = reportDtoList;

        return "attendanceReport.jsp";
    }

    @Execute(validator = false)
    public String attendancePersonal() {
        String ym = reportForm.ym;

        int year = Integer.parseInt(ym.substring(0, 4));
        int month = Integer.parseInt(ym.substring(4));

        Calendar calFrom = Calendar.getInstance();
        calFrom.set(Calendar.YEAR, year);
        calFrom.set(Calendar.MONTH, month - 1);
        calFrom.set(Calendar.DAY_OF_MONTH, 1);
        Calendar calTo = Calendar.getInstance();
        calTo.set(Calendar.YEAR, year);
        calTo.set(Calendar.MONTH, month);
        calTo.set(Calendar.DAY_OF_MONTH, 1);

        WorkDao workDao = SingletonS2Container.getComponent(WorkDao.class);
        List<ReportDto> reportDtoList = workDao.selectForReportAttendancePersonal(reportForm.kind,
                calFrom.getTime(), calTo.getTime(), reportForm.userId);

        BigDecimal workHourTotalPersonal = BigDecimal.valueOf(0);
        BigDecimal overHourTotalPersonal = BigDecimal.valueOf(0);
        BigDecimal overHourMorningTotalPersonal = BigDecimal.valueOf(0);
        BigDecimal overHourEveningTotalPersonal = BigDecimal.valueOf(0);
        BigDecimal overHourNightTotalPersonal = BigDecimal.valueOf(0);

        for (ReportDto dto : reportDtoList) {
            workHourTotalPersonal = workHourTotalPersonal.add(dto.workHourTotal);
            overHourTotalPersonal = overHourTotalPersonal.add(dto.overHourTotal);
            overHourMorningTotalPersonal = overHourMorningTotalPersonal.add(dto.overHourMorningTotal);
            overHourEveningTotalPersonal = overHourEveningTotalPersonal.add(dto.overHourEveningTotal);
            overHourNightTotalPersonal = overHourNightTotalPersonal.add(dto.overHourNightTotal);
        }

        reportForm.workHourTotalPersonal = workHourTotalPersonal;
        reportForm.overHourTotalPersonal = overHourTotalPersonal;
        reportForm.overHourMorningTotalPersonal = overHourMorningTotalPersonal;
        reportForm.overHourEveningTotalPersonal = overHourEveningTotalPersonal;
        reportForm.overHourNightTotalPersonal = overHourNightTotalPersonal;

        // ?????
        calFrom.add(Calendar.MONTH, -1);
        calTo.add(Calendar.MONTH, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        int count = workDao.selectForReportExist(reportForm.kind, calFrom.getTime(), calTo.getTime(),
                reportForm.userId);
        if (count > 0) {
            reportForm.ymBefore = sdf.format(calFrom.getTime());
        } else {
            reportForm.ymBefore = null;
        }

        // ????
        calFrom.add(Calendar.MONTH, +2);
        calTo.add(Calendar.MONTH, +2);
        count = workDao.selectForReportExist(reportForm.kind, calFrom.getTime(), calTo.getTime(),
                reportForm.userId);
        if (count > 0) {
            reportForm.ymNext = sdf.format(calFrom.getTime());
        } else {
            reportForm.ymNext = null;
        }

        reportForm.reportDtoList = reportDtoList;

        return "attendancePersonalReport.jsp";
    }

}