package com.connor.jd.derive;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.jdesktop.swingx.MultiSplitLayout.Split;

import com.connor.ml.tcm.outfile.util.MethodUtil;
import com.teamcenter.rac.aif.AbstractAIFOperation;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentFolder;
import com.teamcenter.rac.kernel.TCComponentForm;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCComponentItemType;
import com.teamcenter.rac.kernel.TCComponentType;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCProperty;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

public class deriveOpertion extends AbstractAIFOperation{
	private TCSession session;
	private TCComponentItemRevision rev;
	private int i;
	private String createType;
	public deriveOpertion(TCSession session, TCComponentItemRevision f, int i, String createType) {
		// TODO Auto-generated constructor stub
		this.session = session;
		this.rev = f;
		this.i = i;
		this.createType = createType;
	}

	@Override
	public void executeOperation() throws Exception {
		// TODO Auto-generated method stub	
		System.out.println("根据属性值创建对象");
		//根据属性值创建对象
		TCComponentItemType tccomponentitemtype = (TCComponentItemType)session.getTypeComponent(createType);
		//获取流水码
		String itemId = tccomponentitemtype.getNewID(); 
		//获取版本号
		String itemRev = tccomponentitemtype.getNewRev(null); 
		//Item的id，Item的版本号，Item的类型，零组件的名称，零组件的描述，零组件的单位(可以为null)
		TCComponentItem newitem = tccomponentitemtype.create(itemId,itemRev,createType,"","",null); 
		System.out.println("创建成功，开始同步属性");
		//获取item
		TCComponentItem oldItem = rev.getItem();
		//给rev赋值
		TCComponentItemRevision newItemRevision = newitem.getLatestItemRevision();
		//给表单赋值
		TCComponent[] oldreferenceListProperty = rev.getReferenceListProperty("IMAN_master_form_rev");
		TCComponentForm oldform = (TCComponentForm) oldreferenceListProperty[0];
		TCComponent[] newreferenceListProperty1 = newItemRevision.getReferenceListProperty("IMAN_master_form_rev");
		TCComponentForm newform = (TCComponentForm) newreferenceListProperty1[0];
		//开始赋值属性
		System.out.println("获取首选项配置，开始赋值");
		String[] value = null;
		if(i==1) {
			value = session.getPreferenceService().getStringValues("JD_J6_ZX_Copy_Properties");
		}else {
			value = session.getPreferenceService().getStringValues("JD_J6_MPRODUCT_Copy_Properties");
		}
		
		for(int i=0;i<value.length;i++) {
			String[] getValueNums = value[i].split(";");
			System.out.println("获取首选项的值，根据分号分割");
			for(String getvalString:getValueNums) {
				System.out.println(getvalString);
			}
			if(getValueNums.length>0) {
				for(int j=0;j<getValueNums.length;j++) {
					System.out.println("根据等号分割");
					String[] getVal = getValueNums[j].split("=");					
					System.out.println("左边部分分割");
					String[] getValues = getVal[0].split("\\.");
					System.out.println("右边部分分割");
					String[] setValues = getVal[1].split("\\.");
					Object val = "";
					System.out.println("转移"+getValues[0]+"上的属性"+getValues[1]+"去"+setValues[0]+"的"+getValues[1]+"上");
					if(getValues[0].equals("item")) {
			            val = MethodUtil.getTCPropertyValue(oldItem, getValues[1]);
			            //val = item.getProperty(getValues[1]);
			        }else if(getValues[0].equals("rev")) {
			            val = MethodUtil.getTCPropertyValue(rev, getValues[1]);
			            //val = rev.getProperty(getValues[1]);
			        }else if(getValues[0].equals("revmaster")) {
			            val = MethodUtil.getTCPropertyValue(oldform, getValues[1]);
			            //val = form.getProperty(getValues[1]);
			        }
			        System.out.println("val:"+val);
			        if(setValues[0].equals("item")) {
			            MethodUtil.setTCPropertyValue(newitem, setValues[1], val);
			        }else if(setValues[0].equals("rev")) {
			            MethodUtil.setTCPropertyValue(newItemRevision, setValues[1], val);
			        }else if(setValues[0].equals("revmaster")) {
			            MethodUtil.setTCPropertyValue(newform, setValues[1], val);
			        }
			        
			        
			        
//					if("item".equals(getValues[0])) {
//						stringProperty = newitem.getProperty(getValues[1]);
//					}else if("rev".equals(getValues[0])){
//						stringProperty = rev.getProperty(getValues[1]);
//					}else if("revmaster".equals(getValues[0])) {
//						stringProperty = oldform.getProperty(getValues[1]);
//					} 
					
					
//					if("item".equals(setValues[0])) {
//						newitem.setStringProperty(setValues[1], stringProperty);
//					}else if("rev".equals(setValues[0])){
//						if(setValues[1].equals("j6_weight")) {
//							newItemRevision.setProperty(setValues[1], stringProperty);
//						}else {
//							newItemRevision.setStringProperty(setValues[1], stringProperty);
//						}
//						
//					}else if("revmaster".equals(setValues[0])) {
//						newform.setStringProperty(setValues[1], stringProperty);
//					} 
//					
					
				}
				
			}else {
				MessageBox.post("首选项设置有误，清仔细检查", "提示", MessageBox.INFORMATION);
			}
		}
		
		System.out.println("开始挂载");
		// 放到newstuff
		TCComponentFolder newStuffFolder = session.getUser().getNewStuffFolder();
		newStuffFolder.add("contents", newitem);
		MessageBox.post("创建成功,新对象在newstuff文件夹下", "提示", MessageBox.INFORMATION);
		System.out.println("执行结束");
		
	}

}
