-
Notifications
You must be signed in to change notification settings - Fork 1
/
SchedulerDataSource.java
145 lines (128 loc) · 4.62 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
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2011-2019 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.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.application.scheduler.model.JobXmlModel;
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;
/**
*
* A {@link DataProvider} which returns informations about all implementations of {@link ScheduledJob} that where
* registered during application startup
*
* @author Matthias Müller
*
*/
public class SchedulerDataSource implements DataProvider {
private static final String ACTION_CREATE = "create";
private static final String ACTION_DELETE = "delete";
private static final String AVAILABLE_JOB = "availableJob";
private Scheduler scheduler;
public Scheduler getScheduler() {
return scheduler;
}
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
public DataContainer getData(Site site, Application application, Environment environment, 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 {
JobXmlModel jobXmlModel = new JobXmlModel();
if (null != jobId && !"".equals(jobId) && !ACTION_DELETE.equals(actionForm)) {
JobModel job = jobXmlModel.getJob(jobId, scheduler, site);
data.setItem(job);
} else {
if (ACTION_CREATE.equals(actionId)) {
JobModel jobModel = new JobModel();
jobModel.setName("");
jobModel.setDescription("");
jobModel.setCronExpression("");
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 {
List<JobModel> jobs = jobXmlModel.getJobs(scheduler, site);
data.setPage(jobs, fp.getPageable());
}
}
} catch (SchedulerException e) {
throw new ApplicationException("error while retrieving job(s)", e);
}
return data;
}
class NamedJob implements Named<String> {
private String name;
private String id;
NamedJob(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public String getDescription() {
return null;
}
}
}