⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 taskproxy.java

📁 一个用于监控WEB服务器和数据库服务器的客户端程序。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**
 * <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 + -