001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.kfs.batch;
017:
018: import java.util.Date;
019: import java.util.LinkedHashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.kuali.core.bo.TransientBusinessObjectBase;
024: import org.kuali.kfs.context.SpringContext;
025: import org.kuali.kfs.service.SchedulerService;
026: import org.quartz.JobDetail;
027:
028: public class BatchJobStatus extends TransientBusinessObjectBase {
029:
030: // private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BatchJobStatus.class);
031:
032: private JobDescriptor jobDescriptor;
033:
034: private JobDetail jobDetail;
035:
036: private static SchedulerService schedulerService;
037:
038: private SchedulerService getSchedulerService() {
039: if (schedulerService == null) {
040: schedulerService = SpringContext
041: .getBean(SchedulerService.class);
042: }
043: return schedulerService;
044: }
045:
046: // for DD purposes only
047: public BatchJobStatus() {
048: }
049:
050: public BatchJobStatus(JobDescriptor jobDescriptor,
051: JobDetail jobDetail) {
052: this .jobDescriptor = jobDescriptor;
053: this .jobDetail = jobDetail;
054: }
055:
056: public String getName() {
057: return jobDetail.getName();
058: }
059:
060: public String getGroup() {
061: return jobDetail.getGroup();
062: }
063:
064: public String getFullName() {
065: return jobDetail.getGroup() + "." + jobDetail.getName();
066: }
067:
068: public Map<String, String> getDependencies() {
069: return jobDescriptor.getDependencies();
070: }
071:
072: public List<Step> getSteps() {
073: return jobDescriptor.getSteps();
074: }
075:
076: public String getStatus() {
077: if (isRunning()) {
078: return SchedulerService.RUNNING_JOB_STATUS_CODE;
079: }
080: String tempStatus = schedulerService.getStatus(jobDetail);
081: if (tempStatus == null) {
082: if (getNextRunDate() != null) {
083: return SchedulerService.SCHEDULED_JOB_STATUS_CODE;
084: } else if (getGroup().equals(
085: SchedulerService.SCHEDULED_GROUP)) {
086: return SchedulerService.PENDING_JOB_STATUS_CODE;
087: }
088: }
089: return tempStatus;
090: }
091:
092: public String getDependencyList() {
093: StringBuffer sb = new StringBuffer(200);
094: for (Map.Entry<String, String> entry : getDependencies()
095: .entrySet()) {
096: sb
097: .append(entry.getKey() + " (" + entry.getValue()
098: + ") \n");
099: }
100: return sb.toString();
101: }
102:
103: public String getStepList() {
104: StringBuffer sb = new StringBuffer(200);
105: for (Step step : getSteps()) {
106: sb.append(step.getName() + " \n");
107: }
108: return sb.toString();
109: }
110:
111: public int getNumSteps() {
112: return getSteps().size();
113: }
114:
115: @Override
116: protected LinkedHashMap toStringMapper() {
117: LinkedHashMap m = new LinkedHashMap();
118:
119: m.put("name", getName());
120: m.put("group", getGroup());
121: m.put("status", getStatus());
122: for (Object key : jobDetail.getJobDataMap().keySet()) {
123: m.put("jobDataMap." + key, jobDetail.getJobDataMap().get(
124: key));
125: }
126:
127: return m;
128: }
129:
130: public boolean isScheduled() {
131: // is this instance in the scheuled group?
132: if (getGroup().equals(SchedulerService.SCHEDULED_GROUP)) {
133: return true;
134: }
135: // does this job exist in the scheduled group?
136: if (getSchedulerService().getJob(
137: SchedulerService.SCHEDULED_GROUP, getName()) != null) {
138: return true;
139: }
140: return false;
141: }
142:
143: public boolean isRunning() {
144: return getSchedulerService().isJobRunning(getName());
145: }
146:
147: public void runJob(String requestorEmailAddress) {
148: getSchedulerService().runJob(getName(), requestorEmailAddress);
149: }
150:
151: public void runJob(int startStep, int endStep, Date startTime,
152: String requestorEmailAddress) {
153: getSchedulerService().runJob(getName(), startStep, endStep,
154: startTime, requestorEmailAddress);
155: }
156:
157: public void interrupt() {
158: getSchedulerService().interruptJob(getName());
159: }
160:
161: public void schedule() {
162: // if not already in scheduled group
163: if (!isScheduled()) {
164: // make a copy and add to the scheduled group
165: getSchedulerService().addScheduled(jobDetail);
166: }
167: }
168:
169: public void unschedule() {
170: // if in scheduled group and scheduled group, remove it
171: List<BatchJobStatus> jobs = getSchedulerService().getJobs(
172: SchedulerService.UNSCHEDULED_GROUP);
173: boolean inUnscheduledGroup = false;
174: for (BatchJobStatus detail : jobs) {
175: if (detail.getName().equals(getName())) {
176: inUnscheduledGroup = true;
177: }
178: }
179:
180: // if only in scheduled group, move it
181: if (!inUnscheduledGroup) {
182: getSchedulerService().addUnscheduled(jobDetail);
183: }
184: getSchedulerService().removeScheduled(getName());
185: }
186:
187: public Date getNextRunDate() {
188: return getSchedulerService().getNextStartTime(this);
189: }
190: }
|