-
Notifications
You must be signed in to change notification settings - Fork 1
/
SchedulerDataSource.java
134 lines (120 loc) · 4.79 KB
/
SchedulerDataSource.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
130
131
132
133
134
/*
* Copyright 2011-2023 the original author or authors.
*
* 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.appng.application.scheduler.business;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.appng.api.ApplicationException;
import org.appng.api.DataContainer;
import org.appng.api.DataProvider;
import org.appng.api.Environment;
import org.appng.api.FieldProcessor;
import org.appng.api.Options;
import org.appng.api.Request;
import org.appng.api.ScheduledJob;
import org.appng.api.model.Application;
import org.appng.api.model.Named;
import org.appng.api.model.Site;
import org.appng.api.support.OptionGroupFactory;
import org.appng.api.support.OptionGroupFactory.OptionGroup;
import org.appng.application.scheduler.Constants;
import org.appng.application.scheduler.SchedulerUtils;
import org.appng.application.scheduler.model.JobForm;
import org.appng.application.scheduler.model.JobModel;
import org.appng.xml.platform.Label;
import org.appng.xml.platform.Selection;
import org.appng.xml.platform.SelectionType;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* A {@link DataProvider} which returns informations about all implementations of {@link ScheduledJob} that where
* registered during application startup
*
* @author Matthias Müller
*/
@Component("jobs")
public class SchedulerDataSource extends SchedulerAware implements DataProvider {
private static final String ACTION_CREATE = "create";
private static final String ACTION_DELETE = "delete";
private static final String AVAILABLE_JOB = "availableJob";
private final MessageSource messageSource;
public SchedulerDataSource(Scheduler scheduler, MessageSource messageSource) {
super(scheduler);
this.messageSource = messageSource;
}
public DataContainer getData(Site site, Application application, Environment env, Options options, Request request,
FieldProcessor fp) {
DataContainer data = new DataContainer(fp);
String jobId = options.getOptionValue(Constants.OPT_JOB, Constants.ATTR_ID);
String actionId = options.getOptionValue(Constants.OPT_ACTION, Constants.ATTR_ID);
String actionForm = request.getParameter(Constants.FORM_ACTION);
try {
if (StringUtils.isNotBlank(jobId) && !ACTION_DELETE.equals(actionForm)) {
JobModel job = JobModel.getJob(scheduler, messageSource, env.getLocale(), jobId, site);
data.setItem(job);
} else if (ACTION_CREATE.equals(actionId)) {
JobModel jobModel = new JobModel();
jobModel.setName(StringUtils.EMPTY);
jobModel.setDescription(StringUtils.EMPTY);
jobModel.setCronExpression(StringUtils.EMPTY);
JobForm jobForm = new JobForm(jobModel);
data.setItem(jobForm);
Selection selection = new Selection();
selection.setId(AVAILABLE_JOB);
selection.setType(SelectionType.SELECT);
Label label = new Label();
label.setId(AVAILABLE_JOB);
selection.setTitle(label);
OptionGroupFactory optionGroupFactory = new OptionGroupFactory();
for (Application a : site.getApplications()) {
String[] jobClasses = a.getBeanNames(ScheduledJob.class);
if (jobClasses.length > 0) {
List<Named<String>> jobs = new ArrayList<Named<String>>();
for (int i = 0; i < jobClasses.length; i++) {
jobs.add(new NamedJob(jobClasses[i],
a.getName() + SchedulerUtils.JOB_SEPARATOR + jobClasses[i]));
}
OptionGroup applicationJobs = optionGroupFactory.fromNamed(a.getName(), a.getName(), jobs,
(Named<String>) null);
selection.getOptionGroups().add(applicationJobs);
}
}
data.getSelections().add(selection);
} else if ("running".equals(actionId)) {
data.setItems(new SchedulerUtils(scheduler, fp).getRunningTriggers(site));
} else {
List<JobModel> jobs = JobModel.getJobs(scheduler, site, messageSource, env.getLocale());
data.setPage(jobs, fp.getPageable());
}
} catch (SchedulerException e) {
throw new ApplicationException("error while retrieving job(s)", e);
}
return data;
}
@Getter
@AllArgsConstructor
class NamedJob implements Named<String> {
private String name;
private String id;
public String getDescription() {
return null;
}
}
}