package com.connor.jd.derive;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.connor.jd.erp.erpIntegrationOpertaion;
import com.teamcenter.rac.aif.AbstractAIFApplication;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.common.lov.testingservice.LOVTestingService;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

public class deriveCenterHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		// TODO Auto-generated method stub
		// 获取当前系统活动
		AbstractAIFApplication app = AIFUtility.getCurrentApplication();
		TCSession session = (TCSession) app.getSession();
		// 获取tc当前选择的操作
		InterfaceAIFComponent target = app.getTargetComponent();
		if (target instanceof TCComponentItemRevision) {
			try {
				String[] value = session.getPreferenceService().getStringValues("JD_SERIVECENT");
				List<String> list = new ArrayList<>(Arrays.asList(value));
				List<String> typeList = new ArrayList<>(Arrays.asList(value));
				Map<String, String> typeMap = new HashMap<String, String>();
				for(String string : list) {
					System.out.println("首选项配置属性："+string);
					System.out.println("开始分割属性");
					String[] split = string.split("-");
					typeList.add(split[0]);
					typeMap.put(split[0], split[1]);
				}
				
				String stringProperty = ((TCComponentItemRevision) target).getItem().getStringProperty("object_type");
				System.out.println("选中对象item类型："+stringProperty);
				if (typeList.contains(((TCComponentItemRevision) target).getItem().getStringProperty("object_type"))) {
					String createType = typeMap.get(stringProperty);
					TCComponentItemRevision f = (TCComponentItemRevision) target;
					deriveOpertion operation = new deriveOpertion(session, f, 1,createType);
					session.queueOperation(operation);
				} else {
					MessageBox.post("请选择中间件版本执行操作", "提示", MessageBox.INFORMATION);
					return null;
				}
			} catch (TCException e) {
				e.printStackTrace();
			}		
		}
		return null;
	}

}
