/*
#=======================================================================================================
#
#                   Copyright (c) 2007 Origin Enterprise Solution LTD.
#
#=======================================================================================================
# File description:  ComparatorTask.java
#=======================================================================================================
#	 Date	       time   	   Name	 Action	   Description of Change
#	2011-1-17      下午03:42:41    tyl  	Ini	            初始化文件
#=======================================================================================================
 */
package cust.com.rac.workflow;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;

public class ComparatorTask implements Comparator<Object> {
	private int	count	= 0;

	public int compare(Object obj1, Object obj2) {
		// Object : id$$$processDate1$$$taskDate1$$$taskFinishDate1
		// id$$$processDate1$$$processName$$$previousTaskFinishDate1
		int returnvalue = 0;
		if (obj1 instanceof String) {
			String str1 = (String) obj1;
			String str2 = (String) obj2;
			if (str1.indexOf("$$$") > 0) {
				count = 0;
				getNum(str1);
				if (count == 3) {
					// String id1 = str1.substring(0, str1.indexOf("$$$"));
					String sep1 = str1.substring(str1.indexOf("$$$") + 3);
					String processDate1 = sep1.substring(0, sep1.indexOf("$$$")).trim();
					String processName1 = sep1.substring(sep1.indexOf("$$$") + 3, sep1.lastIndexOf("$$$")).trim();
					String taskCreateDate1 = sep1.substring(sep1.lastIndexOf("$$$") + 3).trim();

					// String id2 = str2.substring(0, str2.indexOf("$$$"));
					String sep2 = str2.substring(str2.indexOf("$$$") + 3);
					String processDate2 = sep2.substring(0, sep2.indexOf("$$$")).trim();
					String processName2 = sep2.substring(sep2.indexOf("$$$") + 3, sep2.lastIndexOf("$$$")).trim();
					String taskCreateDate2 = sep2.substring(sep2.lastIndexOf("$$$") + 3).trim();

					// 先流程创建时间降序，再流程名称排，再任务上步骤完成时间降序
					returnvalue = (compareDate(processDate1, processDate2) == 0 ? (compareStr(processName1, processName2) == 0 ? (compareDate(taskCreateDate1, taskCreateDate2)) : compareStr(processName1, processName2)) : compareDate(processDate2, processDate1));
				}
			}
		}
		return returnvalue;
	}

	/**
	 * compareStr：对String类型排序
	 * @param @return 设定文件
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public int compareStr(String o1, String o2) {
		String s1 = (String) o1;
		String s2 = (String) o2;
		if ((s1 == null) || (s1.length() == 0) || (s2 == null) || (s2.length() == 0)) { return 0; }
		int len1 = s1.length();
		int len2 = s2.length();
		int n = Math.min(len1, len2);
		char v1[] = s1.toCharArray();
		char v2[] = s2.toCharArray();
		int pos = 0;
		while (n-- != 0) {
			char c1 = v1[pos];
			char c2 = v2[pos];

			if (c1 != c2) { return c1 - c2; }
			pos++;
		}
		return len1 - len2;
	}

	public int compareDate(String strDate1, String strDate2) {
		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date1;
		try {
			date1 = simpleDate.parse(strDate1);
			Date date2 = simpleDate.parse(strDate2);
			if (date1.getTime() > date2.getTime()) {
				return 1;
			} else {
				if (date1.getTime() == date2.getTime()) {
					return 0;
				} else {
					return -1;
				}
			}
		}
		catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}
	}

	public static void main(String[] args) {
		new ComparatorTask().compareDate("01-3-2011 11:17", "02-3-2011 11:17");
	}

	/**
	 * compareInt：对int类型排序
	 * @param @return 设定文件
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public int compareInt(Integer o1, Integer o2) {
		int val1 = o1.intValue();
		int val2 = o2.intValue();
		return (val1 < val2 ? -1 : (val1 == val2 ? 0 : 1));
	}

	/**
	 * compareBoolean：对Boolean类型排序
	 * @param @return 设定文件
	 * @return String 对象类型
	 * @throws
	 * @since CodingExample　Ver 1.1
	 */
	public int compareBoolean(Boolean o1, Boolean o2) {
		return (o1.equals(o2) ? 0 : (o1.booleanValue() == true ? 1 : -1));
	}

	private void getNum(String name) {
		if (name.contains("$$$")) {
			count++;
			getNum(name.substring(name.indexOf("$$$") + 3));
		} else {
			return;
		}
	}
}
