Created
July 12, 2010 16:33
-
-
Save lenards/472688 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.iplantc.de.client.views.panels; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.iplantc.de.client.I18N; | |
import org.iplantc.de.client.images.Resources; | |
import org.iplantc.de.client.models.Job; | |
import org.iplantc.de.client.models.JsJob; | |
import org.iplantc.de.client.models.UserInfo; | |
import org.iplantc.de.client.services.JobServices; | |
import org.iplantc.de.client.utils.JsonUtil; | |
import org.iplantc.de.client.utils.NotifyInfo; | |
import com.extjs.gxt.ui.client.Style.SelectionMode; | |
import com.extjs.gxt.ui.client.data.ModelData; | |
import com.extjs.gxt.ui.client.event.BaseEvent; | |
import com.extjs.gxt.ui.client.event.Events; | |
import com.extjs.gxt.ui.client.event.GridEvent; | |
import com.extjs.gxt.ui.client.event.Listener; | |
import com.extjs.gxt.ui.client.event.MessageBoxEvent; | |
import com.extjs.gxt.ui.client.store.ListStore; | |
import com.extjs.gxt.ui.client.util.Params; | |
import com.extjs.gxt.ui.client.widget.ContentPanel; | |
import com.extjs.gxt.ui.client.widget.Dialog; | |
import com.extjs.gxt.ui.client.widget.Info; | |
import com.extjs.gxt.ui.client.widget.MessageBox; | |
import com.extjs.gxt.ui.client.widget.button.Button; | |
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig; | |
import com.extjs.gxt.ui.client.widget.grid.ColumnModel; | |
import com.extjs.gxt.ui.client.widget.grid.Grid; | |
import com.extjs.gxt.ui.client.widget.grid.GridViewConfig; | |
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem; | |
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar; | |
import com.google.gwt.core.client.JsArray; | |
import com.google.gwt.user.client.Timer; | |
import com.google.gwt.user.client.Window; | |
import com.google.gwt.user.client.rpc.AsyncCallback; | |
import com.google.gwt.user.client.ui.AbstractImagePrototype; | |
/** | |
* Provides a user interface for communicating the status of a job. | |
*/ | |
public class JobStatusPanel extends ContentPanel | |
{ | |
public static final int JOB_CHECK_INTERVAL = 5000; | |
private Button btnDelete; | |
private Button btnRefresh; | |
private Button btnStop; | |
private Grid<Job> grid; | |
private ListStore<Job> jobStore; | |
private String caption; | |
private JobStatusCheckTimer timer; | |
private Job selectedJob; | |
/** | |
* Indicates the status of a job's state. | |
*/ | |
public enum JOB_STATUS | |
{ | |
UNKNOWN, READY, SUBMITTED, RUNNING, COMPLETED, TIMEOUT, FAILED, STOPPED | |
} | |
public JobStatusPanel(String caption) | |
{ | |
super(); | |
this.caption = caption; | |
jobStore = new ListStore<Job>(); | |
assembleView(); | |
} | |
private void assembleView() | |
{ | |
getJobs(); | |
grid = new Grid<Job>(jobStore, buildColumnModel()); | |
grid.setHeight(300); | |
grid.setWidth(700); | |
grid.getView().setViewConfig(new JobGridViewConfig()); | |
grid.getView().setEmptyText(I18N.DISPLAY.noJobs()); | |
grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); | |
grid.setStripeRows(true); | |
grid.setTitle(I18N.DISPLAY.jobPanelToolTip()); | |
grid.addListener(Events.RowClick,new Listener<BaseEvent>() | |
{ | |
@Override | |
public void handleEvent(BaseEvent be) | |
{ | |
selectedJob = grid.getSelectionModel().getSelectedItem(); | |
} | |
}); | |
this.setHeading(caption); | |
this.setTopComponent(buildGridToolBar()); | |
this.add(grid); | |
monitorJobs(); | |
} | |
private ColumnModel buildColumnModel() | |
{ | |
List<ColumnConfig> configs = new ArrayList<ColumnConfig>(); | |
configs.add(new ColumnConfig("id", I18N.DISPLAY.jobId(), 75)); | |
configs.add(new ColumnConfig("name", I18N.DISPLAY.name(), 125)); | |
configs.add(new ColumnConfig("created", I18N.DISPLAY.createdDate(), 175)); | |
configs.add(new ColumnConfig("status", I18N.DISPLAY.status(), 100)); | |
configs.add(new ColumnConfig("description", I18N.DISPLAY.description(), 200)); | |
return new ColumnModel(configs); | |
} | |
private void monitorJobs() | |
{ | |
timer = new JobStatusCheckTimer(); | |
timer.scheduleRepeating(JOB_CHECK_INTERVAL); | |
} | |
public void cancelJobMonitors() | |
{ | |
timer.cancel(); | |
} | |
private void setStore(ArrayList<Job> jobList) | |
{ | |
jobStore.removeAll(); | |
jobStore.add(jobList); | |
} | |
private ToolBar buildGridToolBar() | |
{ | |
ToolBar t = new ToolBar(); | |
btnDelete = new Button(I18N.DISPLAY.delete()); | |
btnDelete.setIcon(AbstractImagePrototype.create(Resources.ICONS.cancel())); | |
btnDelete.addListener(Events.OnClick, new Listener<BaseEvent>() | |
{ | |
@Override | |
public void handleEvent(BaseEvent be) | |
{ | |
if(selectedJob!=null) | |
{ | |
MessageBox.confirm(I18N.DISPLAY.confirmAction(), I18N.DISPLAY.jobDeleteConfirm(), | |
new Listener<MessageBoxEvent>() | |
{ | |
@Override | |
public void handleEvent(MessageBoxEvent be) | |
{ | |
Button btn = be.getButtonClicked(); | |
if(btn.getText().equalsIgnoreCase(Dialog.YES)) | |
{ | |
deleteJob(selectedJob.getId()); | |
} | |
} | |
}); | |
} | |
else | |
{ | |
MessageBox.alert(I18N.DISPLAY.alert(), I18N.DISPLAY.noJobSelected(), null); | |
} | |
} | |
}); | |
btnStop = new Button(I18N.DISPLAY.stop()); | |
btnStop.setIcon(AbstractImagePrototype.create(Resources.ICONS.stop())); | |
btnStop.addListener(Events.OnClick, new Listener<BaseEvent>() | |
{ | |
@Override | |
public void handleEvent(BaseEvent be) | |
{ | |
if(selectedJob!=null) | |
{ | |
MessageBox.confirm(I18N.DISPLAY.confirmAction(), I18N.DISPLAY.jobStopConfirm(), | |
new Listener<MessageBoxEvent>() | |
{ | |
@Override | |
public void handleEvent(MessageBoxEvent be) | |
{ | |
Button btn = be.getButtonClicked(); | |
if(btn.getText().equalsIgnoreCase(Dialog.YES)) | |
{ | |
stopJob(selectedJob); | |
} | |
} | |
}); | |
} | |
else | |
{ | |
MessageBox.alert(I18N.DISPLAY.alert(), I18N.DISPLAY.noJobSelected(), null); | |
} | |
} | |
}); | |
btnRefresh = new Button(I18N.DISPLAY.refresh()); | |
btnRefresh.setIcon(AbstractImagePrototype.create(Resources.ICONS.refresh())); | |
btnRefresh.addListener(Events.OnClick, new Listener<BaseEvent>() | |
{ | |
@Override | |
public void handleEvent(BaseEvent be) | |
{ | |
getJobs(); | |
} | |
}); | |
t.add(btnDelete); | |
t.add(btnStop); | |
t.add(new FillToolItem()); | |
t.add(btnRefresh); | |
return t; | |
} | |
private void stopJob(final Job job) | |
{ | |
if(job.getStatus().equals(JOB_STATUS.RUNNING.toString()) || job.getStatus().equals(JOB_STATUS.SUBMITTED.toString())) | |
{ | |
JobServices.stopJob(job.getId(), new AsyncCallback<String>() | |
{ | |
@Override | |
public void onFailure(Throwable caught) | |
{ | |
org.iplantc.de.client.ErrorHandler.post(I18N.ERROR.jobStopError()); | |
} | |
@Override | |
public void onSuccess(String result) | |
{ | |
job.set("status", JOB_STATUS.STOPPED); | |
ArrayList<Job> jobs_to_update = new ArrayList<Job>(); | |
jobs_to_update.add(job); | |
updateJobStatus(jobs_to_update); | |
NotifyInfo.notify(I18N.DISPLAY.information(), I18N.DISPLAY.jobStopped(), new Params(job.getName())); | |
} | |
}); | |
} | |
else | |
{ | |
MessageBox.alert(I18N.DISPLAY.alert(), I18N.DISPLAY.jobStopAlert(), null); | |
} | |
} | |
private void deleteJob(String jobid) | |
{ | |
JobServices.deleteJob(jobid, new AsyncCallback<String>() | |
{ | |
@Override | |
public void onFailure(Throwable caught) | |
{ | |
org.iplantc.de.client.ErrorHandler.post(I18N.ERROR.deleteJobError()); | |
} | |
@Override | |
public void onSuccess(String result) | |
{ | |
NotifyInfo.notify(I18N.DISPLAY.information(), I18N.DISPLAY.jobDeleted(), new Params(selectedJob.getName())); | |
jobStore.remove(selectedJob); | |
} | |
}); | |
} | |
private void updateJobStatus(ArrayList<Job> jobstoUpdate) | |
{ | |
for(Job j : jobstoUpdate) | |
{ | |
for(Job k : this.jobStore.getModels()) | |
{ | |
if(j.get("id").equals(k.get("id"))) | |
{ | |
this.jobStore.remove(k); | |
this.jobStore.add(j); | |
} | |
} | |
} | |
} | |
public void getJobs() | |
{ | |
UserInfo uinfo = UserInfo.getInstance(); | |
String workspaceId = uinfo.getWorkspaceId(); | |
JobServices.getJobs(workspaceId, new AsyncCallback<String>() | |
{ | |
@Override | |
public void onSuccess(String result) | |
{ | |
JsArray<JsJob> jobinfos = JsonUtil.asArrayOf(result); | |
Job j = null; | |
ArrayList<Job> jobs = new ArrayList<Job>(); | |
for(int i = 0;i < jobinfos.length();i++) | |
{ | |
j = new Job(jobinfos.get(i).getId(), jobinfos.get(i).getName(), jobinfos.get(i) | |
.getCreationDate(), jobinfos.get(i).getStatus(), jobinfos.get(i).getType(), | |
jobinfos.get(i).getDescription(), jobinfos.get(i).getStartDate(), jobinfos | |
.get(i).getEndDate(), jobinfos.get(i).getSubmitDate()); | |
jobs.add(j); | |
} | |
setStore(jobs); | |
} | |
@Override | |
public void onFailure(Throwable caught) | |
{ | |
org.iplantc.de.client.ErrorHandler.post(I18N.ERROR.getJobsError()); | |
} | |
}); | |
} | |
/** | |
* Checks the status of the given job in scheduled manner | |
* | |
* @author sriram | |
* | |
*/ | |
class JobStatusCheckTimer extends Timer | |
{ | |
JobStatusCheckTimer() | |
{ | |
} | |
@Override | |
public void run() | |
{ | |
UserInfo uinfo = UserInfo.getInstance(); | |
String workspaceId = uinfo.getWorkspaceId(); | |
JobServices.getJobs(workspaceId, new AsyncCallback<String>() | |
{ | |
@Override | |
public void onSuccess(String result) | |
{ | |
JsArray<JsJob> jobinfos = JsonUtil.asArrayOf(result); | |
Job j = null; | |
ArrayList<Job> jobs = new ArrayList<Job>(); | |
for(int i = 0;i < jobinfos.length();i++) | |
{ | |
j = new Job(jobinfos.get(i).getId(), jobinfos.get(i).getName(), jobinfos.get(i) | |
.getCreationDate(), jobinfos.get(i).getStatus(), jobinfos.get(i) | |
.getType(), jobinfos.get(i).getDescription(), jobinfos.get(i) | |
.getStartDate(), jobinfos.get(i).getEndDate(), jobinfos.get(i) | |
.getSubmitDate()); | |
jobs.add(j); | |
} | |
setStore(jobs); | |
} | |
@Override | |
public void onFailure(Throwable caught) | |
{ | |
org.iplantc.de.client.ErrorHandler.post(I18N.ERROR.getJobsError()); | |
} | |
}); | |
} | |
} | |
/** | |
* set cell text color to red for error status | |
* | |
* @author sriram | |
* | |
*/ | |
class JobGridViewConfig extends GridViewConfig | |
{ | |
@Override | |
public String getRowStyle(ModelData model, int rowIndex, ListStore<ModelData> ls) | |
{ | |
if(model.get("status").equals(JobStatusPanel.JOB_STATUS.FAILED.toString())) | |
{ | |
return "iplantc-job-error-status"; | |
} | |
else | |
{ | |
return "iplantc-job-panel"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment