task.java

来自「OBPM是一个开源」· Java 代码 · 共 355 行

JAVA
355
字号
package cn.myapps.core.task.ejb;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import cn.myapps.base.dao.ValueObject;
import cn.myapps.core.dynaform.dts.exp.Export;
import cn.myapps.core.dynaform.dts.exp.mappingconfig.ejb.MappingConfig;
import cn.myapps.core.macro.runner.JavaScriptRunner;
import cn.myapps.util.DateUtil;

/**
 * @hibernate.class table="T_TASK"
 * @author nicholas
 */
public class Task extends ValueObject {

	private int type; // 类型

	private String id;

	private String name; // 名称

	private String description; // 描述

	private Date runningTime; // 开始时间

	private int period; // 重复类型

	private int runtimes; // 运行次数

	private String terminateScript; // 停止条件

	private String taskScript; // 任务内容

	private Date modifyTime; // 修改时间

	private String creator; // 创建人

	private int totalRuntimes; // 总运行次数

	private int state; // 状态

	private int startupType; // 启动类型

	private int dayOfWeek; // 一周中的第几天

	private int dayOfMonth; // 一个月中的第几天

	private Collection mappingConfigs;

	private static int docsnum = -1;

	/**
	 * @hibernate.property column="STARTUPTYPE"
	 * @return
	 */
	public int getStartupType() {
		return startupType;
	}

	public void setStartupType(int startupType) {
		this.startupType = startupType;
	}

	/**
	 * @hibernate.property column="STATE"
	 * @return
	 */
	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	/**
	 * @hibernate.property column="CREATOR"
	 * @return
	 */
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	/**
	 * @hibernate.property column="DESCRIPTION" length="1000"
	 * @return
	 */
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @hibernate.property column="RUNNINGTIME"
	 * @return
	 */
	public Date getRunningTime() {
		return runningTime;
	}

	public void setRunningTime(Date runningTime) {
		this.runningTime = runningTime;
	}

	/**
	 * @hibernate.id column="ID" generator-class="assigned"
	 */
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @hibernate.property column="MODIFY_TIME"
	 * @return
	 */
	public Date getModifyTime() {
		return modifyTime;
	}

	public String getModifyTiemStr() {
		return DateUtil.getDateStr(modifyTime);
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = new Date();
	}

	/**
	 * @hibernate.property column="NAME"
	 * @return
	 */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @hibernate.property column="TASK_SCRIPT" type = "text"
	 * @return
	 */
	public String getTaskScript() {
		return taskScript;
	}

	public void setTaskScript(String taskScript) {
		this.taskScript = taskScript;
	}

	/**
	 * @hibernate.property column="TERMINATE_SCRIPT" type = "text"
	 * @return
	 */
	public String getTerminateScript() {
		return terminateScript;
	}

	public void setTerminateScript(String terminateScript) {
		this.terminateScript = terminateScript;
	}

	/**
	 * @hibernate.property column="RUNTIMES"
	 * @return
	 */
	public int getRuntimes() {
		return runtimes;
	}

	public void setRuntimes(int runtimes) {
		this.runtimes = runtimes;
	}

	/**
	 * @hibernate.property column="TOTAL_RUNTIMES"
	 * @return
	 */
	public int getTotalRuntimes() {
		return totalRuntimes;
	}

	public void setTotalRuntimes(int totalRuntimes) {
		this.totalRuntimes = totalRuntimes;
	}

	/**
	 * @hibernate.property column="TYPE"
	 * @return
	 */
	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	/**
	 * @hibernate.property column="DAYOFMONTH"
	 * @return
	 */
	public int getDayOfMonth() {
		return dayOfMonth;
	}

	public void setDayOfMonth(int dayOfMonth) {
		this.dayOfMonth = dayOfMonth;
	}

	/**
	 * @hibernate.property column="DAYOFWEEK"
	 * @return
	 */
	public int getDayOfWeek() {
		return dayOfWeek;
	}

	public void setDayOfWeek(int dayOfWeek) {
		this.dayOfWeek = dayOfWeek;
	}

	/**
	 * @hibernate.property column="PERIOD"
	 * @return
	 */
	public int getPeriod() {
		return period;
	}

	public void setPeriod(int period) {
		this.period = period;
	}

	/**
	 * @hibernate.set lazy="false" name="mappingconfigs"
	 *                table="T_TASK_MAPPING_SET" cascade="save-update"
	 * 
	 * @hibernate.collection-key column="TASK"
	 * 
	 * @hibernate.collection-many-to-many class="cn.myapps.core.dynaform.dts.exp.mappingconfig.ejb.MappingConfig"
	 *                                    column="MAPPINGCONFIG"
	 */
	public Collection getMappingConfigs() {
		synchronized (this) {
			return mappingConfigs;
		}
	}

	public void setMappingConfigs(Collection mappingConfigs) {
		synchronized (this) {
			this.mappingConfigs = mappingConfigs;
		}
	}

	public boolean isExpired() { // 是否到期
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, getRunningTime().getHours());// 到期的小时
		calendar.set(Calendar.MINUTE, getRunningTime().getMinutes());// 到期的分钟
		int oneminute = 60 * 1 * 1000;

		// 当前时间与到期时间比较
		long difference0 = new Date().getTime()
				- this.getRunningTime().getTime();
		// System.out.println("no repeat->" + difference0);

		long difference1 = new Date().getTime() - calendar.getTime().getTime();
		// System.out.println("difference->" + difference1 + " Task Name:"
		// + this.getName());

		switch (period) {
		case (TaskConstants.REPEAT_TYPE_NONE):
			if (difference0 <= oneminute && difference0 > 0) {
				return true;
			}
			break;

		case (TaskConstants.REPEAT_TYPE_DAILY):
			if (difference1 <= oneminute && difference1 > 0) {
				return true;
			}
			break;

		case (TaskConstants.REPEAT_TYPE_WEEKLY):
			if (calendar.get(Calendar.DAY_OF_WEEK) == this.getDayOfWeek()
					&& (difference1 <= oneminute && difference1 > 0)) {
				return true;
			}
			break;

		case (TaskConstants.REPEAT_TYPE_MONTHLY):
			if (calendar.get(Calendar.DAY_OF_MONTH) == this.getDayOfMonth()
					&& (difference1 <= oneminute && difference1 > 0)) {
				return true;
			}
			break;
		}
		return false;
	}

	public void execute() throws Exception { // 执行任务
		System.out.println("************Execute Start*************");
		switch (type) {

		case TaskConstants.TASK_TYPE_SCRIPT:
			try {
				JavaScriptRunner runner = JavaScriptRunner.getInstance();
				runner.initBSFManager(null, null, null, null, this.getApplicationid());
				runner.run(this.getTaskScript());
			} catch (Exception e) {
				System.out.println("Run TaskScript Error:" + e.getMessage());
			}
			

			break;

		case TaskConstants.TASK_TYPE_EXPDATA:

			Collection colls = this.getMappingConfigs();

			for (Iterator iter = colls.iterator(); iter.hasNext();) {
				MappingConfig mappcfg = (MappingConfig) iter.next();
				String mappcfgId = mappcfg.getId();
				String result = Export.exprotDocument(mappcfgId, true, getApplicationid());
				if (result != null && !result.equals(Export._ERROR_TYPE_02)
						&& !result.equals(Export._ERROR_TYPE_03)
						&& !result.equals(Export._ERROR_TYPE_04)) {
					docsnum += Integer.parseInt(result);
				}
			}
			if (docsnum > -1)
				docsnum += 1;

			break;
		}
		System.out.println("************Execute End*************");
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?