churashima.action.manage.WorkingAction.java Source code

Java tutorial

Introduction

Here is the source code for churashima.action.manage.WorkingAction.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.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.framework.container.SingletonS2Container;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ActionMessagesUtil;
import org.seasar.struts.util.RequestUtil;

import churashima.dao.SubjectDao;
import churashima.dao.UserInfoDao;
import churashima.dao.WorkDao;
import churashima.dto.LoginInfoDto;
import churashima.entity.Subject;
import churashima.entity.Work;
import churashima.form.WorkingForm;
import churashima.service.Const;
import churashima.service.WorkUtil;

public class WorkingAction {

    @ActionForm
    @Resource
    public WorkingForm workingForm;

    @Resource
    public LoginInfoDto loginInfoDto;

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

        return "search";
    }

    @Execute(validator = false)
    public String search() {
        WorkDao workDao = SingletonS2Container.getComponent(WorkDao.class);

        Long searchUserId = null;
        if (workingForm.searchUserId != null && !"all".equals(workingForm.searchUserId)) {
            searchUserId = Long.valueOf(workingForm.searchUserId);
        }

        Long searchSubjectId = null;
        if (workingForm.searchSubjectId != null && !"all".equals(workingForm.searchSubjectId)) {
            searchSubjectId = Long.valueOf(workingForm.searchSubjectId);
        }

        String searchKind = null;
        if (workingForm.searchKind != null && !"all".equals(workingForm.searchKind)) {
            searchKind = workingForm.searchKind;
        }

        String searchAuth = null;
        if (workingForm.searchAuth != null && !"all".equals(workingForm.searchAuth)) {
            searchAuth = workingForm.searchAuth;
        }

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

        List<Work> workList = workDao.selectForSearch(searchUserId, searchSubjectId, searchKind, searchAuth,
                dateFrom, dateTo, null);

        workingForm.workList = workList;

        // ?
        BigDecimal hour = new BigDecimal(0);
        for (Work work : workList) {
            hour = hour.add(work.workHour);
        }
        workingForm.workHourTotal = hour;

        // ??
        UserInfoDao userInfoDao = SingletonS2Container.getComponent(UserInfoDao.class);
        SubjectDao subjectDao = SingletonS2Container.getComponent(SubjectDao.class);

        workingForm.userInfoList = userInfoDao.selectForSearch(null, null);
        workingForm.subjectList = subjectDao.selectForSearch(null, null);

        return "workList.jsp";
    }

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

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Date today = new Date();
        workingForm.date = sdf.format(today);

        workingForm.subjectIdList = new ArrayList<String>();
        workingForm.subjectIdList.add("");
        workingForm.startHour = new ArrayList<String>();
        workingForm.startHour.add("8");
        workingForm.endHour = new ArrayList<String>();
        workingForm.endHour.add("17");
        workingForm.startMinute = new ArrayList<String>();
        workingForm.startMinute.add("00");
        workingForm.endMinute = new ArrayList<String>();
        workingForm.endMinute.add("30");
        for (int i = 1; i < Const.WORK_ENTRY_MAX_COUNT; i++) {
            workingForm.subjectIdList.add(null);
            workingForm.startHour.add(null);
            workingForm.endHour.add(null);
            workingForm.startMinute.add(null);
            workingForm.endMinute.add(null);
        }
        workingForm.kind = new ArrayList<String>();

        UserInfoDao userInfoDao = SingletonS2Container.getComponent(UserInfoDao.class);
        SubjectDao subjectDao = SingletonS2Container.getComponent(SubjectDao.class);

        workingForm.userInfoList = userInfoDao.selectForSearch(null, null);
        workingForm.subjectList = subjectDao.selectForSearch(null, Const.STATUS_STARTING);

        return "workEntry.jsp";
    }

    @Execute(input = "workEntry.jsp")
    public String entryExecute() {

        WorkDao workDao = SingletonS2Container.getComponent(WorkDao.class);
        UserInfoDao userInfoDao = SingletonS2Container.getComponent(UserInfoDao.class);
        SubjectDao subjectDao = SingletonS2Container.getComponent(SubjectDao.class);

        // ??
        workingForm.userInfoList = userInfoDao.selectForSearch(null, null);
        workingForm.subjectList = subjectDao.selectForSearch(null, null);

        //      List<Work> workList = workDao.selectForSearch(workingForm.name);
        //      
        //      if (workList != null && workList.size() >= 1) {
        //         ActionMessages errors = new ActionMessages();
        //         errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
        //            "errors.already.entried", //??????
        //            new Object[] { workingForm.name }));
        //         ActionMessagesUtil.addErrors(RequestUtil.getRequest(), errors);
        //         return "workEntry.jsp";
        //      }

        Work work = new Work();
        try {
            work.date = new SimpleDateFormat("yyyy/MM/dd").parse(workingForm.date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        work.userId = Long.valueOf(workingForm.userId);

        Timestamp now = new Timestamp(System.currentTimeMillis());
        work.deleted = false;
        work.insDate = now;
        work.insId = Long.valueOf(0);
        work.updDate = now;
        work.updId = Long.valueOf(0);

        List<String> subjectIdList = workingForm.subjectIdList;
        List<String> startHourList = workingForm.startHour;
        List<String> endHourList = workingForm.endHour;
        List<String> startMinuteList = workingForm.startMinute;
        List<String> endMinuteList = workingForm.endMinute;
        List<String> kindList = workingForm.kind;

        String subjectId = null;
        String startHour = null;
        String startMinute = null;
        String endHour = null;
        String endMinute = null;
        String kind = null;

        int endHourBefore = -1;
        int endMinuteBefore = -1;

        for (int index = 0; index < subjectIdList.size(); index++) {
            subjectId = subjectIdList.get(index);
            startHour = startHourList.get(index);
            startMinute = startMinuteList.get(index);
            endHour = endHourList.get(index);
            endMinute = endMinuteList.get(index);
            kind = kindList.get(index);

            if (StringUtils.isEmpty(subjectId)) {
                // subjectId??????
                continue;
            }

            /*** ? start ***/
            if (StringUtils.isEmpty(startHour) || StringUtils.isEmpty(startMinute) || StringUtils.isEmpty(endHour)
                    || StringUtils.isEmpty(endMinute)) {

                // ??????
                Subject subject = subjectDao.selectById(Long.valueOf(subjectId));
                ActionMessages errors = new ActionMessages();
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.empty.work.time", //??????
                        new Object[] { subject.name }));
                ActionMessagesUtil.addErrors(RequestUtil.getRequest(), errors);
                return "workEntry.jsp";
            }
            int startHourInt = Integer.parseInt(startHour);
            int startMinuteInt = Integer.parseInt(startMinute);
            int endHourInt = Integer.parseInt(endHour);
            int endMinuteInt = Integer.parseInt(endMinute);

            if (startHourInt > endHourInt || (startHourInt == endHourInt && startMinuteInt >= endMinuteInt)) {
                // ????????
                ActionMessages errors = new ActionMessages();
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.not.correct.work.time", //??????
                        new Object[] { index + 1 }));
                ActionMessagesUtil.addErrors(RequestUtil.getRequest(), errors);
                return "workEntry.jsp";
            }

            // 2????
            if (endHourBefore >= 0) {

                if (endHourBefore > startHourInt
                        || (endHourBefore == startHourInt && endMinuteBefore > startMinuteInt)) {
                    // ????????
                    ActionMessages errors = new ActionMessages();
                    errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.overlap.work.time", //??????
                            new Object[] { index + 1 }));
                    ActionMessagesUtil.addErrors(RequestUtil.getRequest(), errors);
                    return "workEntry.jsp";
                }
            }
            /*** ? end ***/

            // 
            Calendar startCal = Calendar.getInstance();
            startCal.setTime(work.date);
            startCal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startHour));
            startCal.set(Calendar.MINUTE, Integer.parseInt(startMinute));
            work.startTime = new Timestamp(startCal.getTimeInMillis());

            // 
            Calendar endCal = Calendar.getInstance();
            endCal.setTime(work.date);
            endCal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(endHour));
            endCal.set(Calendar.MINUTE, Integer.parseInt(endMinute));
            work.endTime = new Timestamp(endCal.getTimeInMillis());

            // 
            double diff = endCal.getTimeInMillis() - startCal.getTimeInMillis();

            // 12:0013:00
            BigDecimal lunchRestHour = WorkUtil.getHour(startCal, endCal, Const.REST_HOUR, Const.REST_MINUTE,
                    Const.REST_TERM);

            // ?15:0015:30
            BigDecimal retHour = WorkUtil.getHour(startCal, endCal, Const.LUNCH_REST_HOUR, Const.LUNCH_REST_MINUTE,
                    Const.LUNCH_REST_TERM);

            work.workHour = (new BigDecimal(diff / 1000 / 60 / 60)).subtract(lunchRestHour);
            work.workHour = work.workHour.subtract(retHour);

            // ?
            BigDecimal morningHour = WorkUtil.getHour(startCal, endCal, Const.MORNING_OVER_HOUR,
                    Const.MORNING_OVER_MINUTE, Const.MORNING_OVER_TERM);

            // 
            BigDecimal eveningHour = WorkUtil.getHour(startCal, endCal, Const.EVENING_OVER_HOUR,
                    Const.EVENING_OVER_MINUTE, Const.EVENING_OVER_TERM);

            // 
            BigDecimal nightHour = WorkUtil.getHour(startCal, endCal, Const.NIGHT_OVER_HOUR,
                    Const.NIGHT_OVER_MINUTE, Const.NIGHT_OVER_TERM);

            work.overHourMorning = morningHour;
            work.overHourEvening = eveningHour;
            work.overHourNight = nightHour;
            work.overHour = morningHour.add(eveningHour).add(nightHour);

            work.subjectId = Long.valueOf(subjectId);
            work.kind = kind;

            workDao.insert(work);

            endHourBefore = endHourInt;
            endMinuteBefore = endMinuteInt;
        }

        return "search";
    }

    @Execute(validator = false, urlPattern = "delete/{workId}")
    public String delete() {

        WorkDao workDao = SingletonS2Container.getComponent(WorkDao.class);
        Work work = workDao.selectById(Long.valueOf(workingForm.workId));

        Timestamp now = new Timestamp(System.currentTimeMillis());
        work.deleted = true;
        work.updDate = now;
        work.updId = Long.valueOf(0);
        workDao.update(work);

        return "search";
    }

    @Execute(validator = false, urlPattern = "editInit/{workId}")
    public String editInit() {

        WorkDao workDao = SingletonS2Container.getComponent(WorkDao.class);
        Work workBase = workDao.selectById(Long.valueOf(workingForm.workId));

        if (workBase == null) {
            // ??????
            ActionMessages errors = new ActionMessages();
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.try.again", //??????
                    new Object[] {}));
            ActionMessagesUtil.addErrors(RequestUtil.getRequest(), errors);
            return "search";
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        workingForm.date = sdf.format(workBase.date);
        workingForm.userId = String.valueOf(workBase.userId);
        workingForm.subjectId = String.valueOf(workBase.subjectId);

        // ??
        List<Work> workList = workDao.selectForSearch(workBase.userId, null, null, null, null, null, workBase.date);

        List<String> subjectIdList = new ArrayList<String>();
        List<String> startHourList = new ArrayList<String>();
        List<String> endHourList = new ArrayList<String>();
        List<String> startMinuteList = new ArrayList<String>();
        List<String> endMinuteList = new ArrayList<String>();
        List<String> kindList = new ArrayList<String>();
        Calendar cal = Calendar.getInstance();
        int index = 0;
        for (Work work : workList) {

            subjectIdList.add(String.valueOf(work.subjectId));
            cal.setTimeInMillis(work.startTime.getTime());
            if (cal.get(Calendar.HOUR_OF_DAY) == 0) {
                startHourList.add("24");
            } else if (cal.get(Calendar.HOUR_OF_DAY) == 1) {
                startHourList.add("25");
            } else if (cal.get(Calendar.HOUR_OF_DAY) == 2) {
                startHourList.add("26");
            } else if (cal.get(Calendar.HOUR_OF_DAY) == 3) {
                startHourList.add("27");
            } else if (cal.get(Calendar.HOUR_OF_DAY) == 4) {
                startHourList.add("28");
            } else if (cal.get(Calendar.HOUR_OF_DAY) == 5) {
                startHourList.add("29");
            } else {
                startHourList.add(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)));
            }
            startMinuteList.add(String.valueOf(cal.get(Calendar.MINUTE)));

            cal.setTimeInMillis(work.endTime.getTime());
            if (cal.get(Calendar.HOUR_OF_DAY) == 0) {
                endHourList.add("24");
            } else if (cal.get(Calendar.HOUR_OF_DAY) == 1) {
                endHourList.add("25");
            } else if (cal.get(Calendar.HOUR_OF_DAY) == 2) {
                endHourList.add("26");
            } else if (cal.get(Calendar.HOUR_OF_DAY) == 3) {
                endHourList.add("27");
            } else if (cal.get(Calendar.HOUR_OF_DAY) == 4) {
                endHourList.add("28");
            } else if (cal.get(Calendar.HOUR_OF_DAY) == 5) {
                endHourList.add("29");
            } else {
                endHourList.add(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)));
            }
            endMinuteList.add(String.valueOf(cal.get(Calendar.MINUTE)));
            kindList.add(work.kind);
            index++;
        }

        for (int i = index; i <= Const.WORK_ENTRY_MAX_COUNT; i++) {
            subjectIdList.add("");
            startHourList.add("");
            endHourList.add("");
            startMinuteList.add("");
            endMinuteList.add("");
            kindList.add("1");
        }
        workingForm.subjectIdList = subjectIdList;
        workingForm.startHour = startHourList;
        workingForm.endHour = endHourList;
        workingForm.startMinute = startMinuteList;
        workingForm.endMinute = endMinuteList;
        workingForm.kind = kindList;

        // ?
        UserInfoDao userInfoDao = SingletonS2Container.getComponent(UserInfoDao.class);
        SubjectDao subjectDao = SingletonS2Container.getComponent(SubjectDao.class);

        workingForm.userInfoList = userInfoDao.selectForSearch(null, null);
        workingForm.subjectList = subjectDao.selectForSearch(null, null);

        return "workEdit.jsp";
    }

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

        // ?
        UserInfoDao userInfoDao = SingletonS2Container.getComponent(UserInfoDao.class);
        SubjectDao subjectDao = SingletonS2Container.getComponent(SubjectDao.class);
        WorkDao workDao = SingletonS2Container.getComponent(WorkDao.class);

        workingForm.userInfoList = userInfoDao.selectForSearch(null, null);
        workingForm.subjectList = subjectDao.selectForSearch(null, null);

        //      List<Work> workList = workDao.selectForSearch(workingForm.name);
        //      
        //      if (workList != null && workList.size() >= 1) {
        //         ActionMessages errors = new ActionMessages();
        //         errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
        //            "errors.already.entried", //??????
        //            new Object[] { workingForm.name }));
        //         ActionMessagesUtil.addErrors(RequestUtil.getRequest(), errors);
        //         return "workEntry.jsp";
        //      }
        Work work = workDao.selectById(Long.valueOf(workingForm.workId));

        try {
            work.date = new SimpleDateFormat("yyyy/MM/dd").parse(workingForm.date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        work.userId = Long.valueOf(workingForm.userId);

        Timestamp now = new Timestamp(System.currentTimeMillis());
        work.updDate = now;
        work.updId = Long.valueOf(0);

        List<Work> workDeleteList = workDao.selectForSearch(work.userId, null, null, null, null, null, work.date);

        // 
        for (Work workDel : workDeleteList) {
            workDao.delete(workDel);
        }

        // ?
        work.deleted = false;
        work.insDate = now;
        work.insId = Long.valueOf(0);
        work.updDate = now;
        work.updId = Long.valueOf(0);

        List<String> subjectIdList = workingForm.subjectIdList;
        List<String> startHourList = workingForm.startHour;
        List<String> endHourList = workingForm.endHour;
        List<String> startMinuteList = workingForm.startMinute;
        List<String> endMinuteList = workingForm.endMinute;
        List<String> kindList = workingForm.kind;

        String subjectId = null;
        String startHour = null;
        String startMinute = null;
        String endHour = null;
        String endMinute = null;
        String kind = null;

        int endHourBefore = -1;
        int endMinuteBefore = -1;

        for (int index = 0; index < subjectIdList.size(); index++) {
            subjectId = subjectIdList.get(index);
            startHour = startHourList.get(index);
            startMinute = startMinuteList.get(index);
            endHour = endHourList.get(index);
            endMinute = endMinuteList.get(index);
            kind = kindList.get(index);

            if (StringUtils.isEmpty(subjectId)) {
                // subjectId??????
                continue;
            }

            /*** ? start ***/
            if (StringUtils.isEmpty(startHour) || StringUtils.isEmpty(startMinute) || StringUtils.isEmpty(endHour)
                    || StringUtils.isEmpty(endMinute)) {

                // ??????
                Subject subject = subjectDao.selectById(Long.valueOf(subjectId));
                ActionMessages errors = new ActionMessages();
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.empty.work.time", //??????
                        new Object[] { subject.name }));
                ActionMessagesUtil.addErrors(RequestUtil.getRequest(), errors);
                return "workEdit.jsp";
            }
            int startHourInt = Integer.parseInt(startHour);
            int startMinuteInt = Integer.parseInt(startMinute);
            int endHourInt = Integer.parseInt(endHour);
            int endMinuteInt = Integer.parseInt(endMinute);

            if (startHourInt > endHourInt || (startHourInt == endHourInt && startMinuteInt >= endMinuteInt)) {
                // ????????
                ActionMessages errors = new ActionMessages();
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.not.correct.work.time", //??????
                        new Object[] { index + 1 }));
                ActionMessagesUtil.addErrors(RequestUtil.getRequest(), errors);
                return "workEdit.jsp";
            }

            // 2????
            if (endHourBefore >= 0) {

                if (endHourBefore > startHourInt
                        || (endHourBefore == startHourInt && endMinuteBefore > startMinuteInt)) {
                    // ????????
                    ActionMessages errors = new ActionMessages();
                    errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.overlap.work.time", //??????
                            new Object[] { index + 1 }));
                    ActionMessagesUtil.addErrors(RequestUtil.getRequest(), errors);
                    return "workEdit.jsp";
                }
            }
            /*** ? end ***/

            // 
            Calendar startCal = Calendar.getInstance();
            startCal.setTime(work.date);
            startCal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startHour));
            startCal.set(Calendar.MINUTE, Integer.parseInt(startMinute));
            work.startTime = new Timestamp(startCal.getTimeInMillis());

            // 
            Calendar endCal = Calendar.getInstance();
            endCal.setTime(work.date);
            endCal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(endHour));
            endCal.set(Calendar.MINUTE, Integer.parseInt(endMinute));
            work.endTime = new Timestamp(endCal.getTimeInMillis());

            // 
            double diff = endCal.getTimeInMillis() - startCal.getTimeInMillis();

            // 12:0013:00
            BigDecimal lunchRestHour = WorkUtil.getHour(startCal, endCal, Const.REST_HOUR, Const.REST_MINUTE,
                    Const.REST_TERM);

            // ?15:0015:30
            BigDecimal retHour = WorkUtil.getHour(startCal, endCal, Const.LUNCH_REST_HOUR, Const.LUNCH_REST_MINUTE,
                    Const.LUNCH_REST_TERM);

            work.workHour = (new BigDecimal(diff / 1000 / 60 / 60)).subtract(lunchRestHour);
            work.workHour = work.workHour.subtract(retHour);

            // ?
            BigDecimal morningHour = WorkUtil.getHour(startCal, endCal, Const.MORNING_OVER_HOUR,
                    Const.MORNING_OVER_MINUTE, Const.MORNING_OVER_TERM);

            // 
            BigDecimal eveningHour = WorkUtil.getHour(startCal, endCal, Const.EVENING_OVER_HOUR,
                    Const.EVENING_OVER_MINUTE, Const.EVENING_OVER_TERM);

            // 
            BigDecimal nightHour = WorkUtil.getHour(startCal, endCal, Const.NIGHT_OVER_HOUR,
                    Const.NIGHT_OVER_MINUTE, Const.NIGHT_OVER_TERM);

            work.overHourMorning = morningHour;
            work.overHourEvening = eveningHour;
            work.overHourNight = nightHour;
            work.overHour = morningHour.add(eveningHour).add(nightHour);

            work.subjectId = Long.valueOf(subjectId);
            work.kind = kind;

            workDao.insert(work);
        }

        return "search";
    }

}