forked from jenkinsci/calendar-view-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalendarView.java
More file actions
436 lines (344 loc) · 14.9 KB
/
CalendarView.java
File metadata and controls
436 lines (344 loc) · 14.9 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
* The MIT License
*
* Copyright (c) 2018 Sven Schoenung
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* Portions of this code copied from the Jenkins Project. See LICENSE.md for notice.
*/
package io.jenkins.plugins.view.calendar;
import hudson.model.*;
import io.jenkins.plugins.view.calendar.event.CalendarEvent;
import io.jenkins.plugins.view.calendar.service.CalendarEventService;
import io.jenkins.plugins.view.calendar.service.CronJobService;
import io.jenkins.plugins.view.calendar.time.Moment;
import io.jenkins.plugins.view.calendar.util.RequestUtil;
import org.apache.commons.text.StringEscapeUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import hudson.Extension;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest2;
import jakarta.servlet.ServletException;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import java.util.regex.Pattern;
import static io.jenkins.plugins.view.calendar.time.MomentRange.range;
import static io.jenkins.plugins.view.calendar.util.FieldUtil.defaultIfNull;
import static io.jenkins.plugins.view.calendar.util.ValidationUtil.*;
@SuppressWarnings({
"PMD.GodClass",
"PMD.ExcessivePublicCount",
"PMD.TooManyFields"
})
@Restricted(NoExternalUse.class)
public class CalendarView extends ListView {
public static enum CalendarViewEventsType {
ALL, BUILDS, POLLINGS;
}
public static enum CalendarViewType {
MONTH, WEEK, DAY;
}
private CalendarViewEventsType calendarViewEventsType;
private CalendarViewType calendarViewType;
private Boolean useCustomFormats;
private Boolean useCustomWeekSettings;
private Boolean useCustomSlotSettings;
private Boolean weekSettingsShowWeekends;
private Boolean weekSettingsShowWeekNumbers;
private Integer weekSettingsFirstDay;
private String monthTitleFormat;
private String monthColumnHeaderFormat;
private String monthTimeFormat;
private String monthPopupBuildTimeFormat;
private String weekTitleFormat;
private String weekColumnHeaderFormat;
private String weekTimeFormat;
private String weekSlotTimeFormat;
private String weekPopupBuildTimeFormat;
private String dayTitleFormat;
private String dayColumnHeaderFormat;
private String dayTimeFormat;
private String daySlotTimeFormat;
private String dayPopupBuildTimeFormat;
private String weekSlotDuration;
private String weekMinTime;
private String weekMaxTime;
private String daySlotDuration;
private String dayMinTime;
private String dayMaxTime;
@DataBoundConstructor
public CalendarView(final String name) {
super(name);
}
public CalendarViewEventsType getCalendarViewEventsType() {
return defaultIfNull(calendarViewEventsType, CalendarViewEventsType.ALL);
}
public void setCalendarViewEventsType(final CalendarViewEventsType calendarViewEventsType) {
this.calendarViewEventsType = calendarViewEventsType;
}
public CalendarViewType getCalendarViewType() {
return defaultIfNull(calendarViewType, CalendarViewType.WEEK);
}
public void setCalendarViewType(final CalendarViewType calendarViewType) {
this.calendarViewType = calendarViewType;
}
public boolean isUseCustomFormats() {
return defaultIfNull(useCustomFormats, false);
}
public void setUseCustomFormats(final boolean useCustomFormats) {
this.useCustomFormats = useCustomFormats;
}
public boolean isUseCustomWeekSettings() {
return defaultIfNull(useCustomWeekSettings, false);
}
public void setUseCustomWeekSettings(final boolean useCustomWeekSettings) {
this.useCustomWeekSettings = useCustomWeekSettings;
}
public boolean isUseCustomSlotSettings() {
return defaultIfNull(useCustomSlotSettings, false);
}
public void setUseCustomSlotSettings(final boolean useCustomSlotSettings) {
this.useCustomSlotSettings = useCustomSlotSettings;
}
public boolean isWeekSettingsShowWeekends() {
return defaultIfNull(weekSettingsShowWeekends, true);
}
public void setWeekSettingsShowWeekends(final boolean weekSettingsShowWeekends) {
this.weekSettingsShowWeekends = weekSettingsShowWeekends;
}
public boolean isWeekSettingsShowWeekNumbers() {
return defaultIfNull(weekSettingsShowWeekNumbers, true);
}
public void setWeekSettingsShowWeekNumbers(final boolean weekSettingsShowWeekNumbers) {
this.weekSettingsShowWeekNumbers = weekSettingsShowWeekNumbers;
}
public int getWeekSettingsFirstDay() {
return defaultIfNull(weekSettingsFirstDay, 1);
}
public void setWeekSettingsFirstDay(final int weekSettingsFirstDay) {
this.weekSettingsFirstDay = weekSettingsFirstDay;
}
public String getMonthTitleFormat() {
return defaultIfNull(monthTitleFormat, "");
}
public void setMonthTitleFormat(final String monthTitleFormat) {
this.monthTitleFormat = monthTitleFormat;
}
public String getMonthColumnHeaderFormat() {
return defaultIfNull(monthColumnHeaderFormat, "");
}
public void setMonthColumnHeaderFormat(final String monthColumnHeaderFormat) {
this.monthColumnHeaderFormat = monthColumnHeaderFormat;
}
public String getMonthTimeFormat() {
return defaultIfNull(monthTimeFormat, "");
}
public void setMonthTimeFormat(final String monthTimeFormat) {
this.monthTimeFormat = monthTimeFormat;
}
public String getMonthPopupBuildTimeFormat() {
return defaultIfNull(monthPopupBuildTimeFormat, "");
}
public void setMonthPopupBuildTimeFormat(final String monthPopupBuildTimeFormat) {
this.monthPopupBuildTimeFormat = monthPopupBuildTimeFormat;
}
public String getWeekTitleFormat() {
return defaultIfNull(weekTitleFormat, "");
}
public void setWeekTitleFormat(final String weekTitleFormat) {
this.weekTitleFormat = weekTitleFormat;
}
public String getWeekColumnHeaderFormat() {
return defaultIfNull(weekColumnHeaderFormat, "");
}
public void setWeekColumnHeaderFormat(final String weekColumnHeaderFormat) {
this.weekColumnHeaderFormat = weekColumnHeaderFormat;
}
public String getWeekTimeFormat() {
return defaultIfNull(weekTimeFormat, "");
}
public void setWeekTimeFormat(final String weekTimeFormat) {
this.weekTimeFormat = weekTimeFormat;
}
public String getWeekSlotTimeFormat() {
return defaultIfNull(weekSlotTimeFormat, "");
}
public void setWeekSlotTimeFormat(final String weekSlotTimeFormat) {
this.weekSlotTimeFormat = weekSlotTimeFormat;
}
public String getWeekPopupBuildTimeFormat() {
return defaultIfNull(weekPopupBuildTimeFormat, "");
}
public void setWeekPopupBuildTimeFormat(final String weekPopupBuildTimeFormat) {
this.weekPopupBuildTimeFormat = weekPopupBuildTimeFormat;
}
public String getDayTitleFormat() {
return defaultIfNull(dayTitleFormat, "");
}
public void setDayTitleFormat(final String dayTitleFormat) {
this.dayTitleFormat = dayTitleFormat;
}
public String getDayColumnHeaderFormat() {
return defaultIfNull(dayColumnHeaderFormat, "");
}
public void setDayColumnHeaderFormat(final String dayColumnHeaderFormat) {
this.dayColumnHeaderFormat = dayColumnHeaderFormat;
}
public String getDayTimeFormat() {
return defaultIfNull(dayTimeFormat, "");
}
public void setDayTimeFormat(final String dayTimeFormat) {
this.dayTimeFormat = dayTimeFormat;
}
public String getDaySlotTimeFormat() {
return defaultIfNull(daySlotTimeFormat, "");
}
public void setDaySlotTimeFormat(final String daySlotTimeFormat) {
this.daySlotTimeFormat = daySlotTimeFormat;
}
public String getDayPopupBuildTimeFormat() {
return defaultIfNull(dayPopupBuildTimeFormat, "");
}
public void setDayPopupBuildTimeFormat(final String dayPopupBuildTimeFormat) {
this.dayPopupBuildTimeFormat = dayPopupBuildTimeFormat;
}
public String getWeekSlotDuration() {
return defaultIfNull(weekSlotDuration, "00:30:00");
}
public void setWeekSlotDuration(final String weekSlotDuration) {
this.weekSlotDuration = weekSlotDuration;
}
public String getDaySlotDuration() {
return defaultIfNull(daySlotDuration, "00:30:00");
}
public void setDaySlotDuration(final String daySlotDuration) {
this.daySlotDuration = daySlotDuration;
}
public String getWeekMinTime() {
return defaultIfNull(weekMinTime, "00:00:00");
}
public void setWeekMinTime(final String weekMinTime) {
this.weekMinTime = weekMinTime;
}
public String getWeekMaxTime() {
return defaultIfNull(weekMaxTime, "24:00:00");
}
public void setWeekMaxTime(final String weekMaxTime) {
this.weekMaxTime = weekMaxTime;
}
public String getDayMinTime() {
return defaultIfNull(dayMinTime, "00:00:00");
}
public void setDayMinTime(final String dayMinTime) {
this.dayMinTime = dayMinTime;
}
public String getDayMaxTime() {
return defaultIfNull(dayMaxTime, "24:00:00");
}
public void setDayMaxTime(final String dayMaxTime) {
this.dayMaxTime = dayMaxTime;
}
@Override
public boolean isAutomaticRefreshEnabled() {
return false;
}
@Override
protected void submit(final StaplerRequest2 req) throws ServletException, Descriptor.FormException, IOException {
this.validate(req);
super.submit(req);
this.updateFields(req);
}
private void validate(final StaplerRequest2 req) throws Descriptor.FormException {
final List<String> validSlotDurations = Collections.unmodifiableList(Arrays.asList(
"00:05:00", "00:10:00", "00:15:00", "00:20:00", "00:30:00", "01:00:00"
));
final Pattern validDateTimePattern = Pattern.compile("(0[0-9]|1[0-9]|2[0-4]):00:00");
validateEnum(req, "calendarViewEventsType", CalendarViewEventsType.class);
validateEnum(req, "calendarViewType", CalendarViewType.class);
validateRange(req, "weekSettingsFirstDay", 0, 7);
validateInList(req, "weekSlotDuration", validSlotDurations);
validatePattern(req, "weekMinTime", validDateTimePattern);
validatePattern(req, "weekMaxTime", validDateTimePattern);
validateInList(req, "daySlotDuration", validSlotDurations);
validatePattern(req, "dayMinTime", validDateTimePattern);
validatePattern(req, "dayMaxTime", validDateTimePattern);
}
private void updateFields(final StaplerRequest2 req) {
setCalendarViewEventsType(CalendarViewEventsType.valueOf(req.getParameter("calendarViewEventsType")));
setCalendarViewType(CalendarViewType.valueOf(req.getParameter("calendarViewType")));
setUseCustomFormats(req.getParameter("useCustomFormats") != null);
setUseCustomWeekSettings(req.getParameter("useCustomWeekSettings") != null);
setUseCustomSlotSettings(req.getParameter("useCustomSlotSettings") != null);
setWeekSettingsShowWeekends(req.getParameter("weekSettingsShowWeekends") != null);
setWeekSettingsShowWeekNumbers(req.getParameter("weekSettingsShowWeekNumbers") != null);
setWeekSettingsFirstDay(Integer.parseInt(req.getParameter("weekSettingsFirstDay")));
setMonthTitleFormat(req.getParameter("monthTitleFormat"));
setMonthColumnHeaderFormat(req.getParameter("monthColumnHeaderFormat"));
setMonthTimeFormat(req.getParameter("monthTimeFormat"));
setMonthPopupBuildTimeFormat(req.getParameter("monthPopupBuildTimeFormat"));
setWeekTitleFormat(req.getParameter("weekTitleFormat"));
setWeekColumnHeaderFormat(req.getParameter("weekColumnHeaderFormat"));
setWeekTimeFormat(req.getParameter("weekTimeFormat"));
setWeekSlotTimeFormat(req.getParameter("weekSlotTimeFormat"));
setWeekPopupBuildTimeFormat(req.getParameter("weekPopupBuildTimeFormat"));
setDayTitleFormat(req.getParameter("dayTitleFormat"));
setDayColumnHeaderFormat(req.getParameter("dayColumnHeaderFormat"));
setDayTimeFormat(req.getParameter("dayTimeFormat"));
setDaySlotTimeFormat(req.getParameter("daySlotTimeFormat"));
setDayPopupBuildTimeFormat(req.getParameter("dayPopupBuildTimeFormat"));
setWeekSlotDuration(req.getParameter("weekSlotDuration"));
setWeekMinTime(req.getParameter("weekMinTime"));
setWeekMaxTime(req.getParameter("weekMaxTime"));
setDaySlotDuration(req.getParameter("daySlotDuration"));
setDayMinTime(req.getParameter("dayMinTime"));
setDayMaxTime(req.getParameter("dayMaxTime"));
}
public List<Job> getJobs() {
final List<TopLevelItem> items = getItems();
final List<Job> jobs = new ArrayList<>(items.size());
for (final TopLevelItem item: items) {
if (item instanceof Job) {
jobs.add((Job)item);
}
}
return jobs;
}
public List<CalendarEvent> getEvents() throws ParseException {
final StaplerRequest2 req = Stapler.getCurrentRequest2();
final Calendar start = RequestUtil.getParamAsCalendar(req, "start");
final Calendar end = RequestUtil.getParamAsCalendar(req, "end");
final Moment now = new Moment();
return new CalendarEventService(now, new CronJobService(now)).getCalendarEvents(getJobs(), range(start, end), getCalendarViewEventsType());
}
public String jsonEscape(final String text) {
return StringEscapeUtils.escapeEcmaScript(text);
}
@Extension
public static final class DescriptorImpl extends ListView.DescriptorImpl {
@Override
public String getDisplayName() {
return Messages.CalendarView_DisplayName();
}
}
}