-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathBaseArachneJobDTOToArachneJobConverter.java
More file actions
145 lines (132 loc) · 6.08 KB
/
Copy pathBaseArachneJobDTOToArachneJobConverter.java
File metadata and controls
145 lines (132 loc) · 6.08 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.odysseusinc.scheduler.api.v1.converter;
import com.cronutils.builder.CronBuilder;
import com.cronutils.model.Cron;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.field.expression.FieldExpression;
import com.cronutils.model.field.expression.FieldExpressionFactory;
import com.cronutils.model.field.expression.On;
import com.cronutils.model.field.value.IntegerFieldValue;
import com.odysseusinc.arachne.commons.converter.BaseConvertionServiceAwareConverter;
import com.odysseusinc.scheduler.api.v1.dto.ArachneJobDTO;
import com.odysseusinc.scheduler.model.ArachneJob;
import com.odysseusinc.scheduler.model.JobExecutingType;
import java.time.DayOfWeek;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static com.cronutils.model.field.expression.FieldExpressionFactory.always;
import static com.cronutils.model.field.expression.FieldExpressionFactory.every;
import static com.cronutils.model.field.expression.FieldExpressionFactory.on;
import static com.cronutils.model.field.expression.FieldExpressionFactory.questionMark;
public abstract class BaseArachneJobDTOToArachneJobConverter<S extends ArachneJobDTO, T extends ArachneJob> extends BaseConvertionServiceAwareConverter<S, T> {
protected final CronDefinition cronDefinition;
protected BaseArachneJobDTOToArachneJobConverter(CronDefinition cronDefinition) {
this.cronDefinition = cronDefinition;
}
@Override
protected final void convert(final S source, T target) {
final Date startDate = source.getStartDate();
final JobExecutingType frequency = source.getFrequency();
final List<DayOfWeek> weekDays = source.getWeekDays();
final String cron = createCron(startDate, frequency, weekDays);
target.setCron(cron);
target.setId(source.getId());
target.setEnabled(source.isEnabled());
target.setFrequency(frequency);
target.setRecurringTimes(Optional.ofNullable(source.getRecurringTimes()).orElse(0));
target.setRecurringUntilDate(source.getRecurringUntilDate());
target.setStartDate(source.getStartDate());
target.setWeekDays(weekDays);
convertJob(source, target);
}
protected abstract void convertJob(final S source, T target);
protected String createCron(Date startDate, JobExecutingType frequency, List<DayOfWeek> weekDays) {
final Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
final int second = calendar.get(Calendar.SECOND);
final int minute = calendar.get(Calendar.MINUTE);
final int hour = calendar.get(Calendar.HOUR_OF_DAY);
final int day = calendar.get(Calendar.DAY_OF_MONTH);
final int month = calendar.get(Calendar.MONTH) + 1;
final int year = calendar.get(Calendar.YEAR);
Cron cron;
switch (frequency) {
case ONCE:
cron = CronBuilder.cron(cronDefinition)
.withDoM(on(day))
.withMonth(on(month))
.withDoW(questionMark())
.withHour(on(hour))
.withMinute(on(minute))
.withSecond(on(second))
.instance();
break;
case HOURLY:
cron = CronBuilder.cron(cronDefinition)
.withDoM(always())
.withMonth(always())
.withDoW(questionMark())
.withHour(always())
.withMinute(on(minute))
.withSecond(on(second))
.instance();
break;
case DAILY: {
cron = CronBuilder.cron(cronDefinition)
.withDoM(every(1))
.withMonth(every(1))
.withDoW(questionMark())
.withHour(on(hour))
.withMinute(on(minute))
.withSecond(on(second))
.instance();
break;
}
case WEEKLY: {
if (weekDays == null || weekDays.isEmpty()) {
final String message = String.format("Execution period %s must have at least 1 day of execute", frequency);
throw new IllegalArgumentException(message);
}
final List<FieldExpression> collect = weekDays.stream().map(
dayOfWeek -> (FieldExpression) new On(new IntegerFieldValue(dayOfWeek.getValue()))).collect(Collectors.toList());
cron = CronBuilder.cron(cronDefinition)
.withDoM(questionMark())
.withMonth(always())
.withDoW(FieldExpressionFactory.and(collect))
.withHour(on(hour))
.withMinute(on(minute))
.withSecond(on(second))
.instance();
break;
}
case MONTHLY: {
cron = CronBuilder.cron(cronDefinition)
.withDoM(on(day))
.withMonth(always())
.withDoW(questionMark())
.withHour(on(hour))
.withMinute(on(minute))
.withSecond(on(second))
.instance();
break;
}
case YEARLY: {
cron = CronBuilder.cron(cronDefinition)
.withDoM(on(day))
.withMonth(on(month))
.withDoW(questionMark())
.withHour(on(hour))
.withMinute(on(minute))
.withSecond(on(second))
.instance();
break;
}
default: {
throw new IllegalArgumentException("Unsupported period: " + frequency);
}
}
return cron.asString();
}
}