?? taskproxy.java
字號:
/**
* <p>Title: 卓博營運支撐系統</p>
*
* <p>Description: 為本企業內部運作提供支撐,為企業資源的E化管理提供解決方案</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: 卓博信息科技有限公司</p>
*
* @author Henry
* @version 1.0
*/
package com.jobcn.control;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
import java.awt.Dimension;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;
import com.jobcn.ui.*;
/**
* TaskProxy.java
* function: 任務代理類
* describe: 接受前臺的請求,做具體的任務管理和分配工作
*/
public class TaskProxy {
public TaskProxy(final MainFrame mainFrame) {
this.mainFrame = mainFrame;
this.table = mainFrame.getTable();
this.model = mainFrame.getModel();
this.fileChooser = mainFrame.getFileChooser();
this.taskDialog = mainFrame.getTaskDialog();
this.taskMap = taskDialog.getMap();
if (map == null) {
map = new HashMap<Integer, Object[]>();
}
if (reader == null) {
reader = new SAXReader();
}
if (step == null) {
step = new ArrayList<String[]>();
}
if (attemp == null) {
attemp = new ArrayList<String[]>();
}
}
public void createNewTask(){
taskDialog.setEditable(false);
GeneralPanel gp = taskDialog.getGeneralPanel();
gp.getTaskName().setText("");
gp.getTaskType().setSelectedIndex(0);
gp.getTaskDesc().setText("");
StepPanel sp = taskDialog.getStepPanel();
sp.getStepData().clear();
sp.setID(0);
int rowCount = sp.getTable().getRowCount();
if(rowCount > 0){
for(int i=0;i<rowCount;i++){
sp.getModel().removeRow(0);
}
}
AttempPanel ap = taskDialog.getAttempPanel();
ap.getAttempData().clear();
((AttempNewPanel)ap.getNewAttempDialog()).setID(0);
rowCount = ap.getTable().getRowCount();
if(rowCount > 0){
for(int i=0;i<rowCount;i++){
ap.getModel().removeRow(0);
}
}
NotifyPanel np = taskDialog.getNotifyPanel();
np.getMailCheckBox().setSelected(true);
np.getMailComboBox().setSelectedIndex(0);
np.getMail().setText("");
np.getCc().setText("");
np.getOnlineCheckBox().setSelected(false);
np.getOnlineComboBox().setSelectedIndex(0);
np.getOnlineID().setText("");
taskDialog.setTitle("新建任務");
taskDialog.getTab().setSelectedIndex(0);
taskDialog.setSize(new Dimension(600, 400));
taskDialog.setLocationRelativeTo(null);
taskDialog.setVisible(true);
}
public void loadTaskList(){
boolean haveRunningTask = false;
int rowCount=0;
for (int i=0;i<table.getRowCount();i++) {
if ((Boolean)table.getValueAt(i, table.getColumnCount()-1)) {
haveRunningTask = true;
}
rowCount++;
}
if (haveRunningTask) {
JOptionPane.showMessageDialog(mainFrame, "任務列表中有任務正在運行,請先中止運行中的任務!");
return;
}
fileChooser.setDialogTitle("載入任務列表");
int returnVal = fileChooser.showDialog(mainFrame, "選擇");
if (returnVal == JFileChooser.APPROVE_OPTION) {
taskDialog.getStepPanel().getStepData().clear();
taskDialog.getAttempPanel().getAttempData().clear();
taskDialog.getMap().clear();
if(rowCount > 0){
model.clearRows();
}
HashMap load = loadTaskListFile(fileChooser.getSelectedFile());
if (load == null) {
JOptionPane.showMessageDialog(mainFrame, "載入任務列表失敗!");
return;
}
Set set = load.keySet();
int[] keys = new int[set.size()];
Iterator it = set.iterator();
int count = 0;
while(it.hasNext()){
keys[count++] = ((Integer)it.next()).intValue();
}
Arrays.sort(keys);
for(int i=0;i<keys.length;i++){
String[] general = (String[]) ((Object[]) load.get(keys[i]))[0];
String type = Constant.generalType[Integer.parseInt(general[1])];
model.addRow(new Object[] { keys[i]+1, general[0], type,
general[2], general[3], general[4],
new Boolean(false) });
}
taskDialog.setMap(load);
setTaskMap(load);
setLoadSuccessful(true);
}
}
@SuppressWarnings("unchecked")
public void editTask(int currentRow, int code){
//int currentRow = table.getSelectedRow();
if(currentRow >= table.getRowCount()){
currentRow = -1;
}
if(table.getRowCount() == 0){
JOptionPane.showMessageDialog(mainFrame, "任務列表中未定義任何任務!");
return;
}
if(currentRow == -1){
JOptionPane.showMessageDialog(mainFrame, "請選擇一個任務!");
return;
}
if (((Boolean)table.getValueAt(currentRow, table.getColumnCount()-1)).booleanValue()){
JOptionPane.showMessageDialog(mainFrame, "該任務正在運行中,無法編輯!");
return;
}
Integer ID = (Integer)table.getValueAt(currentRow, 0)-1;
taskDialog.setID(ID);
taskDialog.setEditable(true);
Object[] obj = taskDialog.getMap().get(ID);
String[] general = (String[])((Object[])obj)[0]; // 常規屬性
ArrayList stepData = (ArrayList)((Object[])obj)[1]; // 步驟屬性
ArrayList attempData = (ArrayList)((Object[])obj)[2]; // 調度屬性
String[] notify = (String[])((Object[])obj)[3]; // 通知屬性
GeneralPanel gp = taskDialog.getGeneralPanel();
gp.getTaskName().setText(general[0]);
gp.getTaskType().setSelectedIndex(Integer.parseInt(general[1]));
gp.getTaskDesc().setText(general[2]);
gp.getCreateDate().setText(general[3]);
gp.getLastUptDate().setText(general[4]);
StepPanel sp = taskDialog.getStepPanel();
sp.setID(stepData.size());
sp.setStepData((ArrayList)stepData.clone());
int rowCount = sp.getTable().getRowCount();
if(rowCount > 0){
for(int i=0;i<rowCount;i++){
sp.getModel().removeRow(0);
}
}
String[] tempRows = new String[Constant.stepData.length];
for(int i=0;i<stepData.size();i++){
String[] oneStepData = ((String[])stepData.get(i)).clone();
oneStepData[2] = Constant.stepType[Integer.parseInt(oneStepData[2])];
oneStepData[3] = Constant.stepPack[Integer.parseInt(oneStepData[3])];
for(int j=0;j<tempRows.length;j++){
tempRows[j] = oneStepData[j];
}
sp.getModel().addRow(tempRows);
}
AttempPanel ap = taskDialog.getAttempPanel();
((AttempNewPanel)ap.getNewAttempDialog()).setID(attempData.size());
ap.setAttempData((ArrayList)attempData.clone());
rowCount = ap.getTable().getRowCount();
if(rowCount > 0){
for(int i=0;i<rowCount;i++){
ap.getModel().removeRow(0);
}
}
tempRows = new String[Constant.attempData.length];
for(int i=0;i<attempData.size();i++){
String[] oneAttempData = ((String[])attempData.get(i)).clone();
oneAttempData[2] = Constant.attempType[Integer.parseInt(oneAttempData[2])-1];
for(int j=0;j<tempRows.length;j++){
tempRows[j] = oneAttempData[j];
}
ap.getModel().addRow(tempRows);
}
NotifyPanel np = taskDialog.getNotifyPanel();
boolean boo = Boolean.parseBoolean(notify[0]);
np.getMail().setEnabled(boo);
np.getCc().setEnabled(boo);
np.getMailCheckBox().setSelected(boo);
np.getMailComboBox().setSelectedIndex(Integer.parseInt(notify[1]));
np.getMail().setText(notify[2]);
np.getCc().setText(notify[3]);
boo = Boolean.parseBoolean(notify[4]);
np.getOnlineID().setEnabled(boo);
np.getOnlineCheckBox().setSelected(boo);
np.getOnlineComboBox().setSelectedIndex(Integer.parseInt(notify[5]));
np.getOnlineID().setText(notify[6]);
taskDialog.setTitle("正在編輯任務:"+general[0]);
taskDialog.getTab().setSelectedIndex(code);
taskDialog.setSize(new Dimension(600, 400));
taskDialog.setLocationRelativeTo(null);
taskDialog.setVisible(true);
}
public void delTask(){
int currentRow = table.getSelectedRow();
if(table.getRowCount() == 0){
JOptionPane.showMessageDialog(mainFrame, "任務列表中未定義任何任務!");
return;
}
if(currentRow == -1){
JOptionPane.showMessageDialog(mainFrame, "請選擇一個任務!");
return;
}
if (((Boolean)table.getValueAt(currentRow, table.getColumnCount()-1)).booleanValue()) {
JOptionPane.showMessageDialog(mainFrame, "該任務正在運行,請先停止該運行中的任務!");
return;
}
int n = JOptionPane.showConfirmDialog(
mainFrame, "您確定要刪除該任務嗎?","刪除任務",
JOptionPane.YES_NO_OPTION
);
if (n == JOptionPane.YES_OPTION) {
taskDialog.getMap().remove((Integer)table.getValueAt(currentRow, 0)-1);
model.delRow(currentRow);
}
}
public void saveTaskList(){
int rows = table.getRowCount();
if (rows == 0) {
JOptionPane.showMessageDialog(mainFrame, "任務列表中未定義任何任務!");
return;
}
fileChooser.setDialogTitle("保存為");
fileChooser.setAcceptAllFileFilterUsed(false);
int returnVal = fileChooser.showSaveDialog(mainFrame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
String fileName = file.getName();
if (fileName.lastIndexOf(".xml") == -1
|| fileName.lastIndexOf(".xml") + 4 != fileName.length()) {
fileName += ".xml";
file = new File(Constant.taskConfPath + fileName);
}
if (file.exists() && !mainFrame.taskListName.equals(file.getName())) {
int n = JOptionPane.showConfirmDialog(
mainFrame, "文件<"+file.getName()+">已存在,是否替換?","保存任務列表",
JOptionPane.YES_NO_OPTION
);
if (n != JOptionPane.YES_OPTION) {
return;
}
}
HashMap map = taskDialog.getMap();
if (saveTaskListFile(map, file)) {
JOptionPane.showMessageDialog(mainFrame, "任務列表:"+mainFrame.taskListName+" 已經成功保存為:"+file.getName()+"!");
mainFrame.setTaskListName(file.getName());
} else {
JOptionPane.showMessageDialog(mainFrame, "任務列表:"+mainFrame.taskListName+" 保存失敗!");
}
}
}
public void clearTaskList(){
int rows = table.getRowCount();
if (rows == 0) {
JOptionPane.showMessageDialog(mainFrame, "任務列表中未定義任何任務!");
return;
}
int n = JOptionPane.showConfirmDialog(
mainFrame, "您確定清空任務列表嗎,該操作會刪除所有已設置的任務,是否繼續?","清空任務列表",
JOptionPane.YES_NO_OPTION
);
if (n == JOptionPane.YES_OPTION) {
model.clearRows();
}
}
public boolean runOrStopTask(int rowNo, boolean isRun, int runType) {
setRunType(runType);
boolean result = false;
int runColumn = table.getColumnCount() - 1;
Integer ID = (Integer) table.getValueAt(rowNo, 0) - 1;
String taskName = (String) table.getValueAt(rowNo, 1);
String message;
errorMsg = "";
if (isRun) {
// 啟動任務
if (runTask(ID, taskName)) {
message = "<<"+taskName+">> 任務已經啟動!";
insertInstantInfo(true, message);
JOptionPane.showMessageDialog(mainFrame, message);
result = true;
} else {
JOptionPane.showMessageDialog(mainFrame, errorMsg + "無法啟動任務:"
+ taskName);
model.setValueAt(new Boolean(false), rowNo, runColumn);
if (errorMsg.indexOf("步驟") != -1) {
editTask(rowNo, 1);
} else if (errorMsg.indexOf("調度") != -1
|| errorMsg.indexOf("已到結束時間") != -1
|| errorMsg.indexOf("已過執行時間") != -1) {
editTask(rowNo, 2);
}
}
} else {
// 停止任務
int n = JOptionPane.showConfirmDialog(mainFrame, "任務正在執行中,是否中止執行?",
"中止任務", JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -