在任務管理流程中,流程的發起是通過模版而進行的,此模板是對擬文的一種持久化,可以重複使用它來發起工作任務管理流程。由於有了模版的存在流程的發起便有了兩種發起方式:一種是手動發起流程,另一種是自動發起流程。本文所講內容涉及的是自動發起流程。
模版的定製過程首先指定其為手動還是自動發起流程,而後指定其發起周期,明確該任務的計劃完成時間以及提醒時間。其中模版的發起周期有五種:周度、月度、季度、年度和定時。提醒時間指在計劃完成時間之前的天數,在提醒時間的當天發起該模版指定的流程,同時對任務的接收人發送郵件加以說明。
自動執行程式每天淩晨零點開始執行,檢查所有模版,根據計劃完成時間(日期)和提醒時間(天數)來計算自動執行程式啟動並執行當天是否為提醒時間和計劃完成時間指定的發起模版流程的日期,如果是則發起流程發送說明性郵件。
在模版的周期下指定相應類別中的具體一天,自動執行程式即會在每個周期的這一天發起此模板對應的流程。其中周度指周一到周日,月度指1號到31號,季度指第一季度到第四季度(一季度是1到3月中的一天,二季度是4到6月中的一天…),年度指1月1日到12月31,定時指任意一個具體時間如2012-5-31。
對比上述五類周期,可以分為兩種,一種是周度和月度,沒有指定出具體計劃完成時間的日期而是指定周幾或幾號完成,需要自己手工計算,剩下的三種則指定了。前四種可以說是真正的周期,而定時則是一個具體時間,即僅會執行一次。前四種的計劃完成時間則涉及到是處於當前周期內,還是下一個周期中,以此加上提醒時間來計算提醒日期,決定Bot運行當天是否需要發起此模版。針對此描述示意代碼如下所示:
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Iterator;import java.util.List;public void doTask () throws VWException {Date currentDate = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");try {currentDate = sdf.parse(sdf.format(currentDate));} catch (ParseException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}Calendar calendar = Calendar.getInstance();calendar.setTime(currentDate);System.out.println("目前時間:" + calendar.get(Calendar.YEAR) + " 年 " + (calendar.get(Calendar.MONTH) + 1) + " 月 " + calendar.get(Calendar.DAY_OF_MONTH) + " 日星期 " + ((calendar.get(Calendar.DAY_OF_WEEK) == 1) ? 7 : (calendar.get(Calendar.DAY_OF_WEEK) - 1)));List<TaskSupervision> taskSupervisionList = taskSupervisionBusinessService.getAll();if(taskSupervisionList != null){Iterator<TaskSupervision> iterator = taskSupervisionList.iterator();while(iterator.hasNext()){calendar.setTime(currentDate);TaskSupervision next = iterator.next();String period = next.getPeriod();System.out.println("周期 : " + period);if("周度".equalsIgnoreCase(period)){//今天是一周中的第幾天int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);String planCompleteDate = next.getWeekPlanCompleteDate();int dividing = 0;if("周一".equals(planCompleteDate)){//周一是一周中的第二天dividing = 2;}else if("周二".equals(planCompleteDate)){dividing = 3;}else if("周三".equals(planCompleteDate)){dividing = 4;}else if("周四".equals(planCompleteDate)){dividing = 5;}else if("周五".equals(planCompleteDate)){dividing = 6;}else if("周六".equals(planCompleteDate)){dividing = 7;}else if("周日".equals(planCompleteDate)){//周日是一周中的第一天dividing = 1;}//如果完成日期的周數在今天所處周數之後,則本周為其計劃完成日期,如果在其周數之前,則下周為其完成日期,依次計算機提醒時間int interval = 0;if(dividing >= dayOfWeek){interval = dividing - dayOfWeek;//計劃完成日期calendar.add(Calendar.DATE, interval);}else{interval = dayOfWeek - dividing;//計劃完成日期calendar.add(Calendar.DATE, 7-interval);}//擷取提醒天數,計算提醒日期int reminderTime = next.getReminderTime();calendar.add(Calendar.DATE, -reminderTime);int dayOfWeekToPlan = calendar.get(Calendar.DAY_OF_WEEK);//如果今日為提醒時間,則寄件提醒try {Date reminderDate = sdf.parse(sdf.format(calendar.getTime()));if(reminderDate.getTime() == currentDate.getTime()){}} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}} else if("月度".equalsIgnoreCase(period)) {//當天日int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);//計劃完成日期的日String planCompleteDate = next.getMonthPlanCompleteDate();int intPlanComDay = Integer.parseInt(planCompleteDate);int interval = 0;if(intPlanComDay >= dayOfMonth){interval = intPlanComDay - dayOfMonth;//計劃完成日期calendar.add(Calendar.DATE, interval);}else{interval = dayOfMonth - intPlanComDay;//計劃完成日期calendar.add(Calendar.MONTH, 1);calendar.add(Calendar.DATE, -interval);}//擷取提醒天數,計算提醒日期int reminderTime = next.getReminderTime();calendar.add(Calendar.DATE, -reminderTime);//如果今日為提醒時間,則發起流程,並寄件提醒try {Date reminderDate = sdf.parse(sdf.format(calendar.getTime()));if(reminderDate.getTime() == currentDate.getTime()){}} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}} else if("季度".equalsIgnoreCase(period)) {//檢測是否需要發送郵件try {//提醒天數int reminderTime = next.getReminderTime();//第一季度String planCompleteDate = next.getFirstQuarterPlanCompleteDate();Date reminderDate = null;//擷取提醒時間Calendar calendarMonth = Calendar.getInstance();calendarMonth.setTime(sdf.parse(calendar.get(Calendar.YEAR) + "-" + planCompleteDate));calendarMonth.add(Calendar.DATE, -reminderTime);reminderDate = sdf.parse(sdf.format(calendarMonth.getTime()));//發起流程、寄件提醒if(currentDate.getTime() == reminderDate.getTime()){}//第二季度planCompleteDate = next.getSecondQuarterCompleteDate();reminderDate = null;//擷取提醒時間calendarMonth = Calendar.getInstance();calendarMonth.setTime(sdf.parse(calendar.get(Calendar.YEAR) + "-" + planCompleteDate));calendarMonth.add(Calendar.DATE, -reminderTime);reminderDate = sdf.parse(sdf.format(calendarMonth.getTime()));//發起流程、寄件提醒if(currentDate.getTime() == reminderDate.getTime()){}//第三季度planCompleteDate = next.getThirdQuarterPlanCompleteDate();reminderDate = null;//擷取提醒時間calendarMonth = Calendar.getInstance();calendarMonth.setTime(sdf.parse(calendar.get(Calendar.YEAR) + "-" + planCompleteDate));calendarMonth.add(Calendar.DATE, -reminderTime);reminderDate = sdf.parse(sdf.format(calendarMonth.getTime()));//發起流程、寄件提醒if(currentDate.getTime() == reminderDate.getTime()){}//第四季度planCompleteDate = next.getForthQuarterPlanCompleteDate();reminderDate = null;//擷取提醒時間calendarMonth = Calendar.getInstance();calendarMonth.setTime(sdf.parse(calendar.get(Calendar.YEAR) + "-" + planCompleteDate));calendarMonth.add(Calendar.DATE, -reminderTime);reminderDate = sdf.parse(sdf.format(calendarMonth.getTime()));//發起流程、寄件提醒if(currentDate.getTime() == reminderDate.getTime()){}} catch (ParseException e) {e.printStackTrace();}} else if("年度".equalsIgnoreCase(period)) {//檢測是否需要發送郵件try {String planCompleteDate = next.getYearPlanCompleteDate();int reminderTime = next.getReminderTime();Date reminderDate = null;//擷取提醒時間Calendar calendarMonth = Calendar.getInstance();calendarMonth.setTime(sdf.parse(calendar.get(Calendar.YEAR) + "-" + planCompleteDate));calendarMonth.add(Calendar.DATE, -reminderTime);reminderDate = sdf.parse(sdf.format(calendarMonth.getTime()));//發起流程、寄件提醒if(currentDate.getTime() == reminderDate.getTime()){}} catch (ParseException e) {e.printStackTrace();}} else if("定時".equalsIgnoreCase(period)){//Nothing//檢測是否需要發送郵件try {String planCompleteDate = next.getTimePlanCompleteDate();int reminderTime = next.getReminderTime();Date reminderDate = null;//擷取提醒時間Calendar calendarMonth = Calendar.getInstance();calendarMonth.setTime(sdf.parse(planCompleteDate));calendarMonth.add(Calendar.DATE, -reminderTime);reminderDate = sdf.parse(sdf.format(calendarMonth.getTime()));//發起流程、寄件提醒if(currentDate.getTime() == reminderDate.getTime()){}} catch (ParseException e) {e.printStackTrace();}}}}}
定期執行、周期類別、計劃完成時間和提醒時間等關鍵便構成了這個小需求,而其中的對計劃完成時間和提醒時間的業務語意的不明確性便導致了代碼的重複性修改,這些問題是可以再明確些,或者是需求或者是演算法囊括所有情況,後續繼續瞭解。