Java tutorial
/* Copyright (C) 2012 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.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; 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.util.HinemosTime; /** * ????????????????????? * ???????? CalendarDetailInfo?CalendarInfo ?? CalendarPatternInfo ?? * ????????? * ????????????????????????? * ?????ant ??????????????????? * */ public class CalendarUtil { private static Log m_log = LogFactory.getLog(CalendarUtil.class); private static final long HOUR0 = 0l; private static final long HOUR24 = 24 * 60 * 60 * 1000; /** * ?? * @param info * @param date * @return */ public static Integer isRunOrder(CalendarInfo info, Date date) { // ???false if (date.getTime() < info.getValidTimeFrom() || info.getValidTimeTo() < date.getTime()) { return -1; } //??? m_log.trace("DetailInfo=" + info.getCalendarDetailList().size()); int count = 1; for (CalendarDetailInfo detailInfo : info.getCalendarDetailList()) { if (isRunByDetailDateTime(detailInfo, date)) { m_log.trace("CalendarID=" + info.getCalendarId() + "isRun"); return count; } count++; } return -1; } /** * ???/????? * * @param info * @param date * @return true:?false:? */ public static Boolean isRun(CalendarInfo info, Date date) { return getCalendarRunDetailInfo(info, date, new ArrayList<CalendarDetailInfo>()); } /** * ?????CalendarDetailInfo??retDetailList???<br> * ??true???false???<br> * * @param info * @param date * @return */ public static boolean getCalendarRunDetailInfo(CalendarInfo info, Date date, ArrayList<CalendarDetailInfo> retDetailList) { if (info == null) { return true; // ????????true } m_log.trace("Valid_START_Time : " + new Date(info.getValidTimeFrom())); m_log.trace("Valid_END_Time : " + new Date(info.getValidTimeTo())); m_log.trace("This_Time : " + date); Long timeFrom = info.getValidTimeFrom(); Long timeTo = info.getValidTimeTo(); // ???false if (date.getTime() < timeFrom || timeTo < date.getTime()) { return false; } for (CalendarDetailInfo detailInfo3 : info.getCalendarDetailList()) { // ???hit?????? if (isRunByDetailDateTime(detailInfo3, date)) { m_log.trace("???hit?? description:" + detailInfo3.getDescription() + ", operationFlg:" + detailInfo3.isOperateFlg()); m_log.trace("CalendarDetailInfo.toString = " + detailInfo3.toString()); retDetailList.add(detailInfo3); if (detailInfo3.isOperateFlg()) { return true; } else { return false; } } if (detailInfo3.isSubstituteFlg() && detailInfo3.isOperateFlg()) { m_log.trace("????? description:" + detailInfo3.getDescription()); boolean findhikadou = false; for (CalendarDetailInfo detailInfo : info.getCalendarDetailList()) { if (!detailInfo.isSubstituteFlg()) { continue; } for (int limit = 1; limit <= detailInfo.getSubstituteLimit(); limit++) { Date substituteDate = new Date(date.getTime() - (parseDate(detailInfo.getSubstituteTime()) + HinemosTime.getTimeZoneOffset()) * limit); m_log.trace("SubstituteDate:" + substituteDate + ", description:" + detailInfo.getDescription() + ", limit:" + limit); for (CalendarDetailInfo detailInfo2 : info.getCalendarDetailList()) { m_log.trace("CalendarDetailInfo.toString = " + detailInfo2.toString()); if (detailInfo.equals(detailInfo2)) { if (!findhikadou) { m_log.trace( "????????????return false."); return false; } else { if (!isRunByDetailDateTime(detailInfo2, substituteDate)) { m_log.trace( "??????????????????break."); retDetailList.clear(); findhikadou = false; break; } else { if (substituteDate.getTime() < timeFrom || timeTo < substituteDate.getTime()) { m_log.trace( "??????hit????? return false."); return false; } m_log.trace( "??????hit???? return true."); retDetailList.add(detailInfo2); return true; } } } if (isRunByDetailDateTime(detailInfo2, substituteDate) && !detailInfo2.isOperateFlg()) { m_log.trace( "??????????...?"); findhikadou = true; } } } } } } m_log.trace("?hit????? return false. calendarId:" + info.getCalendarId()); return false; } /** * - ????????????? * * detailInfo?timeFrom,timeTo??0-48???? * @param detailInfo * @param date ? * @return */ public static boolean isRunByDetailDateTime(CalendarDetailInfo detailInfo, Date date) { for (CalendarDetailInfo detail : getDetail24(detailInfo)) { if (isRunByDetailDate(detail, date) && isRunByDetailTime(detail, date)) { m_log.trace("True : carry out " + detail.getDescription()); return true; } } return false; } /** * detailInfo?timeFrom,timeTo??0-24???? * @param detailInfo * @param date ? * @return */ public static boolean isRunByDetailDate(CalendarDetailInfo detailInfo, Date date) { // from, to?0-24?????? long timezoneOffset = HinemosTime.getTimeZoneOffset(); long hour24 = 24 * 60 * 60 * 1000 - timezoneOffset; if (detailInfo.getTimeFrom() < (0 - timezoneOffset) || hour24 < (detailInfo.getTimeFrom())) { m_log.trace("detailInfo.getTimeFrom = " + detailInfo.getTimeFrom()); return false; } if ((detailInfo.getTimeTo()) < (0 - timezoneOffset) || hour24 < (detailInfo.getTimeTo())) { m_log.trace("detailInfo.getTimeTo = " + (detailInfo.getTimeTo())); return false; } //null? if (detailInfo.getMonth() == null) { m_log.warn("detailInfo.getMonth() is NULL"); return false; } //? Calendar detailCal = HinemosTime.getCalendarInstance(); //??? detailCal.setTime(new Date(date.getTime() - (long) detailInfo.getAfterday() * 24 * 3600 * 1000)); // int year = detailCal.get(Calendar.YEAR); // int month = detailCal.get(Calendar.MONTH) + 1; // int weekXth = detailCal.get(Calendar.DAY_OF_WEEK_IN_MONTH); // int weekday = detailCal.get(Calendar.DAY_OF_WEEK); // int dateNo = detailCal.get(Calendar.DATE); m_log.debug("detailCal=" + detailCal.getTime()); /** * */ //? ? ?????????0? if (detailInfo.getYear() != year && detailInfo.getYear() != 0) { m_log.trace("Year is false : Schdule year =" + detailInfo.getYear() + ", Current Year =" + year); return false; } m_log.trace("Year is TRUE : Schduler Year =" + detailInfo.getYear() + ", Current Year =" + year); /** * */ //? ? ?????????0? if (detailInfo.getMonth() != month && detailInfo.getMonth() != 0) { m_log.trace("Month is False : Schduler Month =" + detailInfo.getMonth() + ", Current Month =" + month); return false; } m_log.trace("Month is TRUE : Schduler Month =" + detailInfo.getMonth() + ", Current Month =" + month); /** * */ //null? if (detailInfo.getDayType() == null) { m_log.warn("detailInfo.getDayType() is NULL"); return false; } //??? ? ?????? //???? if (detailInfo.getDayType() == 0) { m_log.trace( "DateALL is TRUE : Schduler dayType=" + detailInfo.getDayType() + ", Current Date =" + dateNo); return true; } //??? else if (detailInfo.getDayType() == 1) { //null? if (detailInfo.getDayOfWeekInMonth() == null) { m_log.trace("detailInfo.getDayOfWeekInMonth() is NULL"); return false; } //null? if (detailInfo.getDayOfWeek() == null) { m_log.warn("detailInfo.getDayOfWeek() is NULL"); return false; } if (detailInfo.getDayOfWeekInMonth() == weekXth || detailInfo.getDayOfWeekInMonth() == 0) { m_log.trace("WeekXth is TRUE : Schduler Xth =" + detailInfo.getDayOfWeekInMonth() + ", Current weekXth =" + weekXth); if (detailInfo.getDayOfWeek() == weekday) { m_log.trace("Weekday is TRUE : Schduler Weekday =" + detailInfo.getDayOfWeek() + ", Current Weekday =" + weekday); return true; } } } // ??? else if (detailInfo.getDayType() == 2) { //null? if (detailInfo.getDate() == null) { m_log.trace("detailInfo.getDate() is NULL"); return false; } if (detailInfo.getDate() == dateNo) { m_log.trace("Date is TRUE : Schduler Date =" + detailInfo.getDate() + ", Current Date =" + dateNo); return true; } } //??? else if (detailInfo.getDayType() == 3) { //null? if (detailInfo.getCalPatternId() == null) { m_log.trace("detailInfo.getCalPatternId() is NULL"); return false; } m_log.trace("CalendarPatternID = " + detailInfo.getCalPatternId()); CalendarPatternInfo calPatternInfo = null; calPatternInfo = detailInfo.getCalPatternInfo(); if (calPatternInfo.isRun(year, month, dateNo)) { m_log.trace(year + "/" + month + "/" + dateNo); return true; } } return false; } /** * - ???????? * * @param detailInfo * @param date * @return ??? True */ public static boolean isRunByDetailTime(CalendarDetailInfo detailInfo, Date date) { //? (00:00:00 - 24:00:00) long timezoneOffset = HinemosTime.getTimeZoneOffset(); //null? if (detailInfo.getTimeFrom() == null || detailInfo.getTimeTo() == null) { m_log.warn("detailInfo.getTime is NULL"); return false; } //?????00:00:00? SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); sdf.setTimeZone(HinemosTime.getTimeZone()); String strNow = sdf.format(date); Date dateNow = null; try { dateNow = sdf.parse(strNow); /* * ???????1970/1/1 ??????? * ????????????????? */ long from = detailInfo.getTimeFrom() + timezoneOffset + dateNow.getTime(); long to = detailInfo.getTimeTo() + timezoneOffset + dateNow.getTime(); m_log.trace("this Time " + date); m_log.trace("DetailTimeFrom " + new Date(from)); m_log.trace("DetailTimeTo " + new Date(to)); //???Long Long checkTime = date.getTime(); //?????? if (from <= checkTime && checkTime < to) { return true; } } catch (ParseException e) { e.printStackTrace(); } // ?????????? return false; } /** * CalendarDetailInfo??? * * detailInfo?timeFrom,timeTo??048????? * @param detailInfo * @param detailInfo * @param date * @return ?detailInfo024???detailInfo? */ public static ArrayList<CalendarDetailInfo> getDetail24(CalendarDetailInfo detailInfo) { ArrayList<CalendarDetailInfo> ret = new ArrayList<CalendarDetailInfo>(); long timezoneOffset = HinemosTime.getTimeZoneOffset(); long from = detailInfo.getTimeFrom() + timezoneOffset; long to = detailInfo.getTimeTo() + timezoneOffset; // from??to??024(0)2448(1)?-240(-1)?????(???)? // ???????????setAfterday()??? // ??setTimeFrom()?setTimeTo()????(24??)??? // 2448??????????????1?(???1)??? long correctFrom = 0 <= from ? from / HOUR24 : (from + 1) / HOUR24 - 1; long correctTo = 0 <= to ? (to - 1) / HOUR24 : to / HOUR24 - 1; if (HOUR0 <= from && from <= HOUR24 && HOUR0 <= to && to <= HOUR24) { /* * ???(?????) * * from??to???0?24??(0 <= from & to <= 24) * info = 06:00 18:00 () */ m_log.trace("0 <= from & to <= 24"); ret.add(detailInfo); } else if (correctFrom == correctTo) { /* * ??????(?) * * from??to???-24?0??(-24 <= from & to <= 0) * info =-12:00 -06:00 () * info1 = 12:00 18:00 (?) * * from??to???24?48??(24 <= from & to <= 48) * info = 30:00 36:00 () * info1 = 06:00 12:00 () * * from??to???48?72??(48 <= from & to <= 72) * info = 54:00 60:00 () * info1 = 06:00 12:00 () */ m_log.trace("correctFrom = correctTo:" + correctFrom); CalendarDetailInfo info1 = detailInfo.clone(); info1.setAfterday(detailInfo.getAfterday() + (int) correctFrom); info1.setTimeFrom(info1.getTimeFrom() - HOUR24 * correctFrom); info1.setTimeTo(info1.getTimeTo() - HOUR24 * correctTo); ret.add(info1); } else { /* * 2??????(?) * * from?-24??0??to?0???24??(-24 <= from < 0 < to <= 24) * info =-06:00 06:00 () * info1 = 18:00 24:00 (?) * info2 = 00:00 06:00 () * * from?0??24??to?24???48??(0 <= from < 24 < to <= 48) * info = 18:00 30:00 () * info1 = 18:00 24:00 () * info2 = 00:00 06:00 () * * from?24??48??to?48???72??(24 <= from < 48 < to <= 72) * info = 30:00 54:00 () * info1 = 06:00 24:00 () * info2 = 00:00 -06:00 () */ /* * 3??????(?) * * from?-24??0??to?24???48??(-24 <= from < 0 < 24 < to <= 48) * info =-06:00 30:00 () * info1 = 18:00 24:00 (?) * tmp = 00:00 24:00 () * info2 = 00:00 06:00 () * * from?0??24??to?48???72??(0 <= from < 24 < 48 < to <= 72) * info = 18:00 54:00 () * info1 = 18:00-24:00 () * tmp = 00:00-24:00 () * info2 = 00:00-06:00 () */ m_log.trace("correctFrom:" + correctFrom + "correctTo:" + correctTo); CalendarDetailInfo info1 = detailInfo.clone(); CalendarDetailInfo info2 = detailInfo.clone(); info1.setAfterday(detailInfo.getAfterday() + (int) correctFrom); info1.setTimeFrom(info1.getTimeFrom() - HOUR24 * correctFrom); info1.setTimeTo(HOUR24 - timezoneOffset); ret.add(info1); if (correctTo - correctFrom > 1) { // 3???tmp?(024????) for (long i = correctFrom + 1; i < correctTo; i++) { CalendarDetailInfo tmp = detailInfo.clone(); tmp.setAfterday(detailInfo.getAfterday() + (int) i); tmp.setTimeFrom(HOUR0 - timezoneOffset); tmp.setTimeTo(HOUR24 - timezoneOffset); ret.add(tmp); } } info2.setAfterday(detailInfo.getAfterday() + (int) correctTo); info2.setTimeFrom(HOUR0 - timezoneOffset); info2.setTimeTo(info2.getTimeTo() - HOUR24 * correctTo); ret.add(info2); } return ret; } /** * hour?Date???long????(HinemosClient?TimeStringComverter?) * @param hour * @return */ public static long parseDate(int hour) { long msecTime = 1000 * (hour * 3600); Date dateTime = new Date(msecTime - HinemosTime.getTimeZoneOffset()); return dateTime.getTime(); } }