View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.batch;
17  
18  import java.util.Date;
19  import java.util.LinkedHashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.kuali.ole.sys.batch.service.SchedulerService;
24  import org.kuali.ole.sys.context.SpringContext;
25  import org.kuali.rice.krad.bo.TransientBusinessObjectBase;
26  import org.quartz.JobDetail;
27  
28  public class BatchJobStatus extends TransientBusinessObjectBase {
29  
30      // private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BatchJobStatus.class);
31  
32      private JobDescriptor jobDescriptor;
33  
34      private JobDetail jobDetail;
35  
36      private static SchedulerService schedulerService;
37  
38      private SchedulerService getSchedulerService() {
39          if (schedulerService == null) {
40              schedulerService = SpringContext.getBean(SchedulerService.class);
41          }
42          return schedulerService;
43      }
44  
45      // for DD purposes only
46      public BatchJobStatus() {
47      }
48  
49      public BatchJobStatus(JobDescriptor jobDescriptor, JobDetail jobDetail) {
50          this.jobDescriptor = jobDescriptor;
51          this.jobDetail = jobDetail;
52      }
53  
54      public String getName() {
55          return jobDetail.getName();
56      }
57  
58      public String getGroup() {
59          return jobDetail.getGroup();
60      }
61  
62      public String getFullName() {
63          return jobDetail.getGroup() + "." + jobDetail.getName();
64      }
65  
66      public String getNamespaceCode() {
67          if(jobDescriptor == null) return null;
68          return jobDescriptor.getNamespaceCode();
69      }
70  
71      public Map<String, String> getDependencies() {
72          if(jobDescriptor == null) return null;
73          return jobDescriptor.getDependencies();
74      }
75  
76      public List<Step> getSteps() {
77          if(jobDescriptor == null) return null;
78          return jobDescriptor.getSteps();
79      }
80  
81      public String getStatus() {
82          if (isRunning()) {
83              return SchedulerService.RUNNING_JOB_STATUS_CODE;
84          }
85          String tempStatus = schedulerService.getStatus(jobDetail);
86          if (tempStatus == null) {
87              if (getNextRunDate() != null) {
88                  return SchedulerService.SCHEDULED_JOB_STATUS_CODE;
89              }
90              else if (getGroup().equals(SchedulerService.SCHEDULED_GROUP)) {
91                  return SchedulerService.PENDING_JOB_STATUS_CODE;
92              }
93          }
94          return tempStatus;
95      }
96  
97      public String getDependencyList() {
98          StringBuffer sb = new StringBuffer(200);
99          for (Map.Entry<String, String> entry : getDependencies().entrySet()) {
100             sb.append(entry.getKey() + " (" + entry.getValue() + ") \n");
101         }
102         return sb.toString();
103     }
104 
105     public String getStepList() {
106         StringBuffer sb = new StringBuffer(200);
107         for (Step step : getSteps()) {
108             sb.append(step.getName() + " \n");
109         }
110         return sb.toString();
111     }
112 
113     public int getNumSteps() {
114         return getSteps().size();
115     }
116 
117     
118     protected LinkedHashMap toStringMapper_RICE20_REFACTORME() {
119         LinkedHashMap m = new LinkedHashMap();
120 
121         m.put("name", getName());
122         m.put("group", getGroup());
123         m.put("status", getStatus());
124         for (Object key : jobDetail.getJobDataMap().keySet()) {
125             m.put("jobDataMap." + key, jobDetail.getJobDataMap().get(key));
126         }
127 
128         return m;
129     }
130 
131     public boolean isScheduled() {
132         // is this instance in the scheuled group?
133         if (getGroup().equals(SchedulerService.SCHEDULED_GROUP)) {
134             return true;
135         }
136         // does this job exist in the scheduled group?
137         if (getSchedulerService().getJob(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, String requestorEmailAddress) {
152         getSchedulerService().runJob(getName(), startStep, endStep, startTime, requestorEmailAddress);
153     }
154 
155     public void interrupt() {
156         getSchedulerService().interruptJob(getName());
157     }
158 
159     public void schedule() {
160         // if not already in scheduled group
161         if (!isScheduled()) {
162             // make a copy and add to the scheduled group
163             getSchedulerService().addScheduled(jobDetail);
164         }
165     }
166 
167     public void unschedule() {
168         // if in scheduled group and scheduled group, remove it
169         List<BatchJobStatus> jobs = getSchedulerService().getJobs(SchedulerService.UNSCHEDULED_GROUP);
170         boolean inUnscheduledGroup = false;
171         for (BatchJobStatus detail : jobs) {
172             if (detail.getName().equals(getName())) {
173                 inUnscheduledGroup = true;
174             }
175         }
176 
177         // if only in scheduled group, move it
178         if (!inUnscheduledGroup) {
179             getSchedulerService().addUnscheduled(jobDetail);
180         }
181         getSchedulerService().removeScheduled(getName());
182     }
183 
184     public Date getNextRunDate() {
185         return getSchedulerService().getNextStartTime(this);
186     }
187 }