/
DefaultBatchWindowManager.java
129 lines (111 loc) · 4.9 KB
/
DefaultBatchWindowManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright © 2013-2018 camunda services GmbH and various authors (info@camunda.com)
*
* 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 org.camunda.bpm.engine.impl.jobexecutor.historycleanup;
import java.util.Calendar;
import java.util.Date;
import org.camunda.bpm.engine.impl.cfg.BatchWindowConfiguration;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
/**
* @author Svetlana Dorokhova.
*/
public class DefaultBatchWindowManager implements BatchWindowManager {
public BatchWindow getPreviousDayBatchWindow(Date date, ProcessEngineConfigurationImpl configuration) {
Date previousDay = addDays(date, -1);
return getBatchWindowForDate(previousDay, configuration);
}
private BatchWindow getBatchWindowForDate(Date date, ProcessEngineConfigurationImpl configuration) {
//get configuration for given day of week
BatchWindowConfiguration batchWindowConfiguration = configuration.getHistoryCleanupBatchWindows().get(dayOfWeek(date));
if (batchWindowConfiguration == null && configuration.getHistoryCleanupBatchWindowStartTime() != null) {
batchWindowConfiguration = new BatchWindowConfiguration(configuration.getHistoryCleanupBatchWindowStartTime(), configuration.getHistoryCleanupBatchWindowEndTime());
}
if (batchWindowConfiguration == null) {
return null;
}
Date startTime = updateTime(date, batchWindowConfiguration.getStartTimeAsDate());
Date endTime = updateTime(date, batchWindowConfiguration.getEndTimeAsDate());
if (!endTime.after(startTime)) {
endTime = addDays(endTime, 1);
}
return new BatchWindow(startTime, endTime);
}
private Integer dayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_WEEK);
}
@Override
public BatchWindow getCurrentOrNextBatchWindow(Date date, ProcessEngineConfigurationImpl configuration) {
final BatchWindow previousDayBatchWindow = getPreviousDayBatchWindow(date, configuration);
if (previousDayBatchWindow != null && previousDayBatchWindow.isWithin(date)) {
return previousDayBatchWindow;
}
final BatchWindow currentDayBatchWindow = getBatchWindowForDate(date, configuration);
if (currentDayBatchWindow!= null && (currentDayBatchWindow.isWithin(date) || date.before(currentDayBatchWindow.getStart()))) {
return currentDayBatchWindow;
}
//check next week
for (int i=1; i<=7; i++ ) {
Date dateToCheck = addDays(date, i);
final BatchWindow batchWindowForDate = getBatchWindowForDate(dateToCheck, configuration);
if (batchWindowForDate != null) {
return batchWindowForDate;
}
}
return null;
}
@Override
public BatchWindow getNextBatchWindow(Date date, ProcessEngineConfigurationImpl configuration) {
final BatchWindow currentDayBatchWindow = getBatchWindowForDate(date, configuration);
if (currentDayBatchWindow != null && date.before(currentDayBatchWindow.getStart())) {
return currentDayBatchWindow;
} else {
//check next week
for (int i=1; i<=7; i++ ) {
Date dateToCheck = addDays(date, i);
final BatchWindow batchWindowForDate = getBatchWindowForDate(dateToCheck, configuration);
if (batchWindowForDate != null) {
return batchWindowForDate;
}
}
}
return null;
}
@Override
public boolean isBatchWindowConfigured(ProcessEngineConfigurationImpl configuration) {
return configuration.getHistoryCleanupBatchWindowStartTimeAsDate() != null ||
!configuration.getHistoryCleanupBatchWindows().isEmpty();
}
private static Date updateTime(Date now, Date newTime) {
Calendar c = Calendar.getInstance();
c.setTime(now);
Calendar newTimeCalendar = Calendar.getInstance();
newTimeCalendar.setTime(newTime);
c.set(Calendar.ZONE_OFFSET, newTimeCalendar.get(Calendar.ZONE_OFFSET));
c.set(Calendar.DST_OFFSET, newTimeCalendar.get(Calendar.DST_OFFSET));
c.set(Calendar.HOUR_OF_DAY, newTimeCalendar.get(Calendar.HOUR_OF_DAY));
c.set(Calendar.MINUTE, newTimeCalendar.get(Calendar.MINUTE));
c.set(Calendar.SECOND, newTimeCalendar.get(Calendar.SECOND));
c.set(Calendar.MILLISECOND, newTimeCalendar.get(Calendar.MILLISECOND));
return c.getTime();
}
private static Date addDays(Date date, int amount) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, amount);
return c.getTime();
}
}