/*
#=======================================================================================================
#
#                   Copyright (c) 2007 Origin Enterprise Solution LTD.
#
#=======================================================================================================
# File description:  QueryTaskOpinionHandler.java
#=======================================================================================================
#	 Date	       time   	   Name	 Action	   Description of Change
#
#=======================================================================================================
*/
package cust.com.rac.workflow;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;

import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCAttachmentType;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentProcessType;
import com.teamcenter.rac.kernel.TCComponentTaskTemplate;
import com.teamcenter.rac.kernel.TCComponentTaskTemplateType;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

public class CustAddReleaseHandler  extends AbstractHandler implements IHandler{

	/* 
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		AbstractAIFUIApplication app = AIFUtility.getCurrentApplication();
		TCSession session = (TCSession) app.getSession();
		try {
			TCPreferenceService preSer = session.getPreferenceService();
			String[] value = preSer.getStringArray(4, "Cust_Release_Templates");
			
			if(value == null || value.length < 1){
				MessageBox.post("Cust_Release_Templates配置不正确,请联系管理员！", "WARNING", 1);
				return null;
			}
			
			TCComponentProcessType procType = (TCComponentProcessType) session.getTypeComponent("Job");
			TCComponentTaskTemplateType taskTempType = (TCComponentTaskTemplateType) session.getTypeComponent("EPMTaskTemplate");
			TCComponentTaskTemplate taskTemp = taskTempType.find(value[0], TCComponentTaskTemplate.PROCESS_TEMPLATE_TYPE);
			
			InterfaceAIFComponent[] selectComps = app.getTargetComponents();
			if(selectComps.length > 0){
				int[] intArr = new int[selectComps.length];
				TCComponent[] comps = new TCComponent[selectComps.length];
				for (int i = 0; i < intArr.length; i++) {
					intArr[i] = TCAttachmentType.TARGET;
					comps[i] = (TCComponent) selectComps[i];
				}
				procType.create("自动发起", "添加状态", taskTemp, comps, intArr, null);
				MessageBox.post("添加状态成功！", "Info", 0);
			}else{
				MessageBox.post("请选择对象执行此功能,谢谢！", "WARNING", 1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
