com.clustercontrol.calendar.factory.SelectCalendar.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.calendar.factory.SelectCalendar.java

Source

/*
    
Copyright (C) 2006 NTT DATA Corporation
    
This program is free software; you can redistribute it and/or
Modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
    
This program is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU General Public License for more details.
    
 */

package com.clustercontrol.calendar.factory;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.clustercontrol.calendar.model.CalendarDetailInfo;
import com.clustercontrol.calendar.model.CalendarInfo;
import com.clustercontrol.calendar.model.CalendarPatternInfo;
import com.clustercontrol.calendar.model.YMD;
import com.clustercontrol.calendar.util.CalendarCache;
import com.clustercontrol.calendar.util.CalendarPatternCache;
import com.clustercontrol.calendar.util.CalendarUtil;
import com.clustercontrol.calendar.util.QueryUtil;
import com.clustercontrol.fault.CalendarNotFound;
import com.clustercontrol.fault.InvalidRole;
import com.clustercontrol.util.HinemosTime;

/**
 * ?<BR>
 *
 * @version 4.1.0
 * @since 2.0.0
 */
public class SelectCalendar {

    private static Log m_log = LogFactory.getLog(SelectCalendar.class);

    private static final long TIMEZONE = HinemosTime.getTimeZoneOffset();
    private static final long HOUR24 = 24 * 60 * 60 * 1000;

    /**
     * ????
     * 
     * @param id
     * @return 
     * @throws CalendarNotFound
     */
    public CalendarInfo getCalendarFromCache(String id) throws CalendarNotFound {
        CalendarInfo ret = null;
        if (id != null && !"".equals(id)) {
            ret = CalendarCache.getCalendarInfo(id);
        }
        return ret;
    }

    /**
     * ????
     * 
     * @param id
     * @return 
     * @throws CalendarNotFound
     * @throws InvalidRole
     */
    public CalendarInfo getCalendar(String id) throws CalendarNotFound, InvalidRole {
        CalendarInfo ret = null;
        if (id != null && !id.isEmpty()) {
            //?
            ret = QueryUtil.getCalInfoPK(id);
        }

        return ret;
    }

    /**
     * ????
     * @param id
     * @return ?
     */
    public ArrayList<CalendarDetailInfo> getCalDetailList(String id) {
        //ID??
        return new ArrayList<>(QueryUtil.getCalDetailByCalendarId(id));
    }

    /**
     * 
     * @param calendarId
     * @return
     * @throws CalendarNotFound
     * @throws InvalidRole
     */
    public CalendarInfo getCalendarFull(String calendarId) throws CalendarNotFound, InvalidRole {
        CalendarInfo info = CalendarCache.getCalendarInfo(calendarId);
        if (info == null) {
            return null;
        }
        for (CalendarDetailInfo detail : info.getCalendarDetailList()) {
            String calPatternId = detail.getCalPatternId();
            if (calPatternId == null || calPatternId.length() == 0) {
                continue;
            }
            //??
            CalendarPatternInfo calPatternInfo = CalendarPatternCache.getCalendarPatternInfo(calPatternId);
            m_log.debug("getCalendarFull() : calPatternInfo=" + calPatternInfo);
            detail.setCalPatternInfo(calPatternInfo);
        }
        return info;
    }

    /**
     * ????
     * 
     * @return ?
     */
    public ArrayList<CalendarInfo> getAllCalendarList(String ownerRoleId) {
        List<CalendarInfo> ct = null;
        if (ownerRoleId == null || ownerRoleId.isEmpty()) {
            //?
            ct = QueryUtil.getAllCalInfo();
        } else {
            // ID?????
            ct = QueryUtil.getAllCalInfo_OR(ownerRoleId);
        }
        return new ArrayList<>(ct);
    }

    /**
     * ID????<BR>
     * 
     * @return ID
     */
    public ArrayList<String> getCalendarIdList() {
        ArrayList<String> list = new ArrayList<String>();

        //?
        List<CalendarInfo> ct = QueryUtil.getAllCalInfo();
        for (CalendarInfo cal : ct) {
            list.add(cal.getCalendarId());
        }
        return list;
    }

    /**
     * ??ID??
     * ??????
     * @param id
     * @param year
     * @param month
     * @return
     * @throws CalendarNotFound
     * @throws InvalidRole
     */
    public ArrayList<Integer> getCalendarMonth(String id, Integer year, Integer month)
            throws CalendarNotFound, InvalidRole {
        return getCalendarMonth(getCalendarFull(id), year, month);
    }

    /**
     * ??????
     * @param info
     * @param year
     * @param month
     * @return
     */
    public ArrayList<Integer> getCalendarMonth(CalendarInfo info, Integer year, Integer month) {
        ArrayList<Integer> ret = new ArrayList<Integer>();
        ArrayList<CalendarDetailInfo> list24 = new ArrayList<CalendarDetailInfo>();
        for (CalendarDetailInfo d : info.getCalendarDetailList()) {
            list24.addAll(CalendarUtil.getDetail24(d));
        }

        long validFrom = info.getValidTimeFrom();
        long validTo = info.getValidTimeTo();

        Calendar cal = HinemosTime.getCalendarInstance();
        cal.set(year, month - 1, 1);
        int lastDate = cal.getActualMaximum(Calendar.DATE) + 1;
        m_log.debug("maxDate=" + year + "/" + month + "/" + lastDate);
        for (int i = 1; i < lastDate; i++) {
            Calendar startCalendar = HinemosTime.getCalendarInstance();
            startCalendar.clear();
            startCalendar.set(year, month - 1, i, 0, 0, 0);
            long dayStartTime = startCalendar.getTimeInMillis();
            Calendar endCalendar = HinemosTime.getCalendarInstance();
            endCalendar.clear();
            endCalendar.set(year, month - 1, i + 1, 0, 0, 0);
            long dayEndTime = endCalendar.getTimeInMillis();
            m_log.debug("i=" + i + " ==== start=" + new Date(dayStartTime) + ",end=" + new Date(dayEndTime));

            // 1????????
            // ???true??????????
            boolean isContainInvalidPeriod = false;
            // ????????
            long dayValidStart;
            if (dayStartTime < validFrom) {
                dayValidStart = validFrom;
                isContainInvalidPeriod = true;
            } else {
                dayValidStart = dayStartTime;
            }
            long dayValidEnd;
            if (validTo < dayEndTime) {
                dayValidEnd = validTo;
                isContainInvalidPeriod = true;
            } else {
                dayValidEnd = dayEndTime;
            }
            // ????????????
            dayValidEnd--;

            // ?????????????????
            if (dayValidStart > dayValidEnd) {
                ret.add(2);
                continue;
            }

            /**
             * ??????
             * ??????????????
             * ?CalendarDetailInfo?From?To???????
             */
            Set<Long> borderTimeSet = new HashSet<Long>();
            borderTimeSet.add(dayValidStart);
            borderTimeSet.add(dayValidEnd);
            // detail
            for (CalendarDetailInfo detail : list24) {
                long detailStart = dayStartTime + detail.getTimeFrom() + TIMEZONE;
                if (dayValidStart < detailStart && detailStart < dayValidEnd) {
                    borderTimeSet.add(detailStart);
                }

                long detailEnd = dayStartTime + detail.getTimeTo() + TIMEZONE;
                if (dayValidStart < detailEnd && detailEnd < dayValidEnd) {
                    borderTimeSet.add(detailEnd);
                }
            }

            /**
             * ?????
             * [0]???????OK
             * [2]???????NG
             * [1]?OK??NG
             * ??
             */
            boolean isAllNG = true; // OK????false??
            boolean isAllOK = true; // NG????false??
            for (Long borderTime : borderTimeSet) {
                // ????????
                // ?????????
                m_log.debug("date:" + new Date(borderTime));
                boolean retRun = CalendarUtil.isRun(info, new Date(borderTime));
                if (retRun) {
                    isAllNG = false;
                } else {
                    isAllOK = false;
                }
                // OKNG???????????????????
                if (isAllNG == false && isAllOK == false) {
                    break;
                }
            }

            if (isAllNG == true) {
                // NG
                ret.add(2);
            } else {
                if (isAllOK == true) {
                    if (isContainInvalidPeriod) {
                        // OKNG ??OK????????????
                        ret.add(1);
                    } else {
                        // ?OK
                        ret.add(0);
                    }
                } else {
                    // OKNG
                    ret.add(1);
                }
            }
        }
        return ret;
    }

    /**
     *  - ??????????
     * ?????
     * CalendarWeekView??
     * @throws CalendarNotFound
     * @throws InvalidRole
     */

    public ArrayList<CalendarDetailInfo> getCalendarWeek(String id, Integer year, Integer month, Integer day)
            throws CalendarNotFound, InvalidRole {

        CalendarInfo info = getCalendarFull(id);
        return getCalendarWeek(info, year, month, day);
    }

    public ArrayList<CalendarDetailInfo> getCalendarWeek(CalendarInfo info, Integer year, Integer month,
            Integer day) throws CalendarNotFound {
        m_log.trace("calendarId:" + info.getCalendarId() + ", year:" + year + ", month:" + month + ", day:" + day);
        long validFrom = info.getValidTimeFrom();
        long validTo = info.getValidTimeTo();
        ArrayList<CalendarDetailInfo> ret = new ArrayList<CalendarDetailInfo>();
        Calendar startCalendar = HinemosTime.getCalendarInstance();
        startCalendar.clear();
        startCalendar.set(year, month - 1, day, 0, 0, 0);
        long startTime = startCalendar.getTimeInMillis();
        Calendar endCalendar = HinemosTime.getCalendarInstance();
        endCalendar.clear();
        endCalendar.set(year, month - 1, day + 1, 0, 0, 0);
        long endTime = endCalendar.getTimeInMillis();

        if (startTime <= validFrom && endTime <= validFrom) {
            return ret;
        }
        if (validTo <= startTime && validTo <= endTime) {
            return ret;
        }
        if (startTime < validFrom && validFrom < endTime) {
            CalendarDetailInfo detail = new CalendarDetailInfo();
            detail.setTimeFrom(0 - TIMEZONE);
            detail.setTimeTo(validFrom - startTime - TIMEZONE);
            detail.setOperateFlg(false);
            detail.setOrderNo(-2);// ?
            m_log.trace("start<validfrom && validfrom<endttime add");
            ret.add(detail);
        }
        if (startTime < validTo && validTo < endTime) {
            CalendarDetailInfo detail = new CalendarDetailInfo();
            detail.setTimeFrom(validTo - startTime - TIMEZONE);
            detail.setTimeTo(HOUR24 - TIMEZONE);
            detail.setOperateFlg(false);
            detail.setOrderNo(-1);// ?
            m_log.trace("start<validto && validto<endttime add");
            ret.add(detail);
        }

        // client?????CalendarDetailList???
        // ?????????
        startCalendar.clear();
        startCalendar.set(year, month - 1, day, 0, 0, 0);
        ArrayList<Date> checkDateList = new ArrayList<>();
        int onesec = 1 * 1000;
        Long startLong = startCalendar.getTime().getTime();
        ArrayList<CalendarDetailInfo> substituteList = new ArrayList<>();

        for (CalendarDetailInfo detailInfo : info.getCalendarDetailList()) {
            int timezone = HinemosTime.getTimeZoneOffset();
            // start_time
            checkDateList.add(new Date(startLong + detailInfo.getTimeFrom() + timezone));
            checkDateList.add(new Date(startLong + detailInfo.getTimeFrom() - onesec + timezone));// 1second??
            checkDateList.add(new Date(startLong + detailInfo.getTimeFrom() + onesec + timezone));// 1second?
            // end_time
            checkDateList.add(new Date(startLong + detailInfo.getTimeTo() + timezone));
            checkDateList.add(new Date(startLong + detailInfo.getTimeTo() - onesec + timezone));
            checkDateList.add(new Date(startLong + detailInfo.getTimeTo() + onesec + timezone));
            if (detailInfo.isSubstituteFlg()) {
                // ?????????
                substituteList.add(detailInfo);
            }
        }

        // getDetail24???????
        ArrayList<CalendarDetailInfo> detailList = new ArrayList<>();
        for (CalendarDetailInfo detail : info.getCalendarDetailList()) {
            detailList.addAll(CalendarUtil.getDetail24(detail));
        }
        for (CalendarDetailInfo detailInfo : detailList) {
            int timezone = HinemosTime.getTimeZoneOffset();
            // start_time
            checkDateList.add(new Date(startLong + detailInfo.getTimeFrom() + timezone));
            checkDateList.add(new Date(startLong + detailInfo.getTimeFrom() - onesec + timezone));// 1second??
            checkDateList.add(new Date(startLong + detailInfo.getTimeFrom() + onesec + timezone));// 1second?
            // end_time
            checkDateList.add(new Date(startLong + detailInfo.getTimeTo() + timezone));
            checkDateList.add(new Date(startLong + detailInfo.getTimeTo() - onesec + timezone));
            checkDateList.add(new Date(startLong + detailInfo.getTimeTo() + onesec + timezone));
        }

        // ?????????????
        ArrayList<Date> checkDateListSub = new ArrayList<>();
        for (Date checkDate : checkDateList) {
            for (CalendarDetailInfo detailInfo : substituteList) {
                Long checkDateLong = checkDate.getTime();
                checkDateListSub
                        .add(new Date(checkDateLong - (CalendarUtil.parseDate(detailInfo.getSubstituteTime())
                                + HinemosTime.getTimeZoneOffset())));
            }
        }
        checkDateList.addAll(checkDateListSub);

        // ?valid_time_from?valid_time_to
        checkDateList.add(new Date(info.getValidTimeFrom()));
        checkDateList.add(new Date(info.getValidTimeTo()));
        // list??????
        checkDateList = new ArrayList<>(new HashSet<>(checkDateList));
        // list?
        Collections.sort(checkDateList);
        m_log.trace("checkDateList.size:" + checkDateList.size());

        ArrayList<CalendarDetailInfo> detailSubsList = new ArrayList<>();
        for (Date targetDate : checkDateList) {
            Calendar targetCal = Calendar.getInstance();
            targetCal.setTime(targetDate);
            // ????????????
            if (startCalendar.get(Calendar.YEAR) != targetCal.get(Calendar.YEAR)
                    || startCalendar.get(Calendar.MONTH) != targetCal.get(Calendar.MONTH)
                    || startCalendar.get(Calendar.DATE) != targetCal.get(Calendar.DATE)) {
                continue;
            }
            m_log.trace("startCalendar:" + targetDate);
            ArrayList<CalendarDetailInfo> detailList2 = new ArrayList<>();
            Date substituteDate = new Date(targetDate.getTime());

            // detailList2????CalendarDetailInfo?
            Object[] retObjArr = CalendarUtil.getCalendarRunDetailInfo(info, targetDate, detailList2);
            boolean isrun = (Boolean) retObjArr[0];
            // ?2??????
            if (retObjArr.length == 2) {
                substituteDate.setTime(((Date) retObjArr[1]).getTime());
            }
            m_log.trace("id:" + info.getCalendarId() + ", startCalendar:" + targetDate + ", detailList2.size:"
                    + detailList2.size() + ", isrun:" + isrun + ", substituteDate:" + substituteDate.toString());

            // ?CalendarDetailInfo????????????getDetail24?????
            for (CalendarDetailInfo detailInfo : detailList2) {
                ArrayList<CalendarDetailInfo> calendarDetailList = CalendarUtil.getDetail24(detailInfo);
                for (CalendarDetailInfo detail24 : calendarDetailList) {
                    if (CalendarUtil.isRunByDetailDateTime(detail24, substituteDate)
                            && !detailSubsList.contains(detail24)) {
                        detailSubsList.add(detail24);
                        m_log.trace("add to ret. orderNo:" + detail24.getOrderNo() + ", description:"
                                + detail24.getDescription() + ", isOperation:" + detail24.isOperateFlg() + ", from:"
                                + detail24.getTimeFrom() + ", to:" + detail24.getTimeTo());
                    }
                }
            }
        }
        ret.addAll(detailSubsList);
        Collections.sort(ret, new Comparator<CalendarDetailInfo>() {
            public int compare(CalendarDetailInfo obj0, CalendarDetailInfo obj1) {
                int order1 = obj0.getOrderNo();
                int order2 = obj1.getOrderNo();
                int ret = order1 - order2;
                return ret;
            }
        });

        if (m_log.isDebugEnabled()) {
            for (CalendarDetailInfo detail : ret) {
                m_log.debug("detail=" + detail);
            }
        }
        m_log.trace("ret.size:" + ret.size());
        return ret;
    }

    /**
     * []????
     * 
     * @param id
     * @return []
     * @throws CalendarNotFound
     * @throws InvalidRole
     */
    public CalendarPatternInfo getCalendarPattern(String id) throws CalendarNotFound, InvalidRole {
        CalendarPatternInfo ret = null;
        if (id != null && !"".equals(id)) {
            ret = CalendarPatternCache.getCalendarPatternInfo(id);
        } else {
            throw new CalendarNotFound("id is null");
        }
        // ?
        if (ret.getYmd() != null) {
            Collections.sort(ret.getYmd(), new Comparator<YMD>() {
                @Override
                public int compare(YMD y1, YMD y2) {
                    Calendar ymd1 = Calendar.getInstance();
                    ymd1.set(y1.getYear(), y1.getMonth() - 1, y1.getDay());
                    Calendar ymd2 = Calendar.getInstance();
                    ymd2.set(y2.getYear(), y2.getMonth() - 1, y2.getDay());
                    return ymd1.getTime().compareTo(ymd2.getTime());
                }
            });
        }
        return ret;
    }

    /**
     * []????
     * 
     * @param ownerRoleId ID
     * @return []?
     * @throws CalendarNotFound
     * @throws InvalidRole
     */
    public ArrayList<CalendarPatternInfo> getCalendarPatternList(String ownerRoleId)
            throws CalendarNotFound, InvalidRole {
        ArrayList<CalendarPatternInfo> list = new ArrayList<CalendarPatternInfo>();
        //?
        ArrayList<String> patternIdList = getCalendarPatternIdList(ownerRoleId);
        for (String id : patternIdList) {
            CalendarPatternInfo info = CalendarPatternCache.getCalendarPatternInfo(id);
            // ?
            if (info.getYmd() != null) {
                Collections.sort(info.getYmd(), new Comparator<YMD>() {
                    @Override
                    public int compare(YMD y1, YMD y2) {
                        Calendar ymd1 = Calendar.getInstance();
                        ymd1.set(y1.getYear(), y1.getMonth() - 1, y1.getDay());
                        Calendar ymd2 = Calendar.getInstance();
                        ymd2.set(y2.getYear(), y2.getMonth() - 1, y2.getDay());
                        return ymd1.getTime().compareTo(ymd2.getTime());
                    }
                });
            }
            list.add(info);
        }
        /*
         * ID?
         */
        Collections.sort(list, new Comparator<CalendarPatternInfo>() {
            @Override
            public int compare(CalendarPatternInfo o1, CalendarPatternInfo o2) {
                return o1.getCalPatternId().compareTo(o2.getCalPatternId());
            }
        });
        return list;
    }

    /**
     * ID????<BR>
     * 
     * @param ownerRoleId ID
     * @return ?ID
     */
    public ArrayList<String> getCalendarPatternIdList(String ownerRoleId) {
        ArrayList<String> list = new ArrayList<String>();
        //?
        List<CalendarPatternInfo> entityList = QueryUtil.getAllCalPatternInfo();
        if (ownerRoleId == null || ownerRoleId.isEmpty()) {
            entityList = QueryUtil.getAllCalPatternInfo();
        } else {
            entityList = QueryUtil.getAllCalPatternInfo_OR(ownerRoleId);
        }
        for (CalendarPatternInfo entity : entityList) {
            list.add(entity.getCalPatternId());
        }
        //?
        Collections.sort(list);
        return list;
    }

    /**
     * ??????<BR>
     * 
     * ???????????Bool???
     * 
     * @param id
     * @param checkTimestamp
     * @return ?????
     * @throws CalendarNotFound
     * @throws InvalidRole
     */
    public Boolean isRun(String id, Long checkTimestamp) throws CalendarNotFound, InvalidRole {
        CalendarInfo info = null;
        Date date = new Date(checkTimestamp);
        if (id == null) {
            return true;
        }
        info = getCalendarFull(id);

        return CalendarUtil.isRun(info, date);
    }

    /**
     * 
     * @param args
     */
    public static void main(String args[]) {
        monthTest();
    }

    /**
     * 
     */
    public static void monthTest() {
        CalendarInfo info = new CalendarInfo();
        info.setValidTimeFrom(0l);
        info.setValidTimeTo(Long.MAX_VALUE);

        ArrayList<CalendarDetailInfo> detailList = new ArrayList<CalendarDetailInfo>();
        CalendarDetailInfo detail = null;

        detail = new CalendarDetailInfo();
        detail.setYear(2012);
        detail.setMonth(0); // ???0
        detail.setDayType(0);//?
        detail.setDayType(1);//?
        detail.setDayOfWeekInMonth(0);//?0?
        detail.setDayOfWeek(1);//?1?
        //      detail.setTimeFrom(0*3600*1000l - TIMEZONE);
        detail.setTimeFrom(1 * 3600 * 1000l - TIMEZONE);
        detail.setTimeTo(23 * 3600 * 1000l - TIMEZONE);
        //      detail.setTimeTo(24*3600*1000l - TIMEZONE);
        //      detail.setTimeTo(25*3600*1000l - TIMEZONE);
        detail.setOperateFlg(true);
        detailList.add(detail);

        info.setCalendarDetailList(detailList);

        SelectCalendar selectCalendar = new SelectCalendar();
        ArrayList<Integer> list = selectCalendar.getCalendarMonth(info, 2012, 2);
        int j = 0;
        StringBuilder str = new StringBuilder();
        for (Integer i : list) {
            if (j % 7 == 0) {
                str.append("\n");
            }
            str.append(i).append(" ");
            j++;
        }
        m_log.trace("getCalendarMonthInfo=" + str);
    }
}