package cn.com.origin.autocode.newitem.generatcode;

import java.math.BigInteger;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.teamcenter.rac.kernel.SoaUtil;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevisionType;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCExceptionPartial;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.schemas.soa._2006_03.exceptions.ServiceException;
import com.teamcenter.services.rac.core.DataManagementService;
import com.teamcenter.services.rac.core._2008_06.DataManagement.CreateInput;

public class NewCodeItemUtil {
	
/**
  * 通过2008 SOA创建对象
  * 该方法可以调用IRDC模板
  * @param itemID
  * @param itemRev
  * @param itemType
  * @param itemName
  * @param itemDesc
  * @param path
 * @throws Exception 
  */
 public static void createNewItemSoa(TCSession session , String itemID, String itemRev,
			String itemType, String itemName, String itemDesc, String path,
			HashMap<String, Object> itemPropMap,
			HashMap<String, Object> revPropMap,
			HashMap<String, Object> revFormPropMap) throws Exception {
		 
		//TODO 新添加逻辑
	
		System.out.println("新的方法创建ITEM对象");

	
		TCComponentItem newComp =null;
		Set<Entry<String, Object>> setEntry = null;
		Map<String, String> stringMap = null;
		Map<String, BigInteger> intMap = null;
		Map<String, Boolean> boolMap = null;
		Map<String, Calendar> dateMap = null;
		
		com.teamcenter.services.rac.core._2008_06.DataManagement.CreateInput itemInput = new com.teamcenter.services.rac.core._2008_06.DataManagement.CreateInput();
		itemInput.boName = itemType;
		//Map<String, String> itemStringMap = new HashMap();
		
		//itemInput.stringProps = itemStringMap;

		Map<String, CreateInput[]> revFormMap = new HashMap();
		CreateInput[] revFormInputS = new CreateInput[1];
		revFormInputS[0] = new CreateInput();
		String revFormType = itemType + "RevisionMaster";
		if(session.getTypeComponent(revFormType) == null){
			revFormType = itemType + " RevisionMaster";
			if(session.getTypeComponent(revFormType) == null){
				revFormType = itemType + " Revision Master";
				if(session.getTypeComponent(revFormType) == null){
					 throw new Exception("没有找到版本表单，请检查传递参数是否正确!");
					
				}
			}
		}
		revFormInputS[0].boName = revFormType;
		
		stringMap = new HashMap();
		intMap = new HashMap();
		boolMap = new HashMap();
		dateMap = new HashMap();
		
		if(revFormPropMap!=null){
			setEntry = revFormPropMap.entrySet();
			for(Entry<String,Object> entry : setEntry){
				String propName = entry.getKey();
				Object value = entry.getValue();
				System.out.println(" 版本表单：key ="+ propName+"| value ="+ value);
				if(value instanceof String){
					stringMap.put(propName, (String) value);
				}else if(value instanceof Integer){
					intMap.put(propName,   (BigInteger) value);
				}else if(value instanceof Boolean){
					boolMap.put(propName,  (Boolean) value);
				}else if(value instanceof Date){
					dateMap.put(propName,   (Calendar) value);
				}
			}
		}
	
		revFormInputS[0].stringProps = stringMap;
		revFormInputS[0].intProps = intMap;
		revFormInputS[0].boolProps = boolMap;
		revFormInputS[0].dateProps = dateMap;
		revFormMap.put("IMAN_master_form_rev", revFormInputS);
		
		Map<String, CreateInput[]> revMap = new HashMap();
		CreateInput[] revInputS = new CreateInput[1];
		revInputS[0] = new CreateInput();
		String revType = itemType + "Revision";
		if(session.getTypeComponent(revType)==null){
			revType = itemType + " Revision";
			if(session.getTypeComponent(revType)==null){
				throw new Exception("没有找到版本类型，请检查传递参数是否正确!");
			}
		}
		revInputS[0].boName = revType;
		try {
			TCComponentItemRevisionType revTypeComp = (TCComponentItemRevisionType) session.getTypeComponent(revType);
			if(revTypeComp.getRevisionNamingRule()!=null)
				itemRev = revTypeComp.getRevisionNamingRule().getStartingRevision();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		//设置REV的属性
		stringMap = new HashMap();
		intMap = new HashMap();
		boolMap = new HashMap();
		dateMap = new HashMap();	
		stringMap.put("item_revision_id", itemRev);
		if(revPropMap!=null){
			setEntry = revPropMap.entrySet();
			for(Entry<String,Object> entry : setEntry){
				String propName = entry.getKey();
				Object value = entry.getValue();
				System.out.println(" 版本：key ="+ propName+"| value ="+ value);
				if(value instanceof String){
					stringMap.put(propName, (String) value);
				}else if(value instanceof Integer){
					intMap.put(propName,   (BigInteger) value);
				}else if(value instanceof Boolean){
					boolMap.put(propName,  (Boolean) value);
				}else if(value instanceof Date){
					dateMap.put(propName,   (Calendar) value);
				}
			}
		}
	
		revInputS[0].stringProps = stringMap;
		revInputS[0].intProps = intMap;
		revInputS[0].boolProps = boolMap;
		revInputS[0].dateProps = dateMap;
		revInputS[0].compoundCreateInput = revFormMap;

		revMap.put("revision", revInputS);
		
		itemInput.compoundCreateInput = revMap;
		
		//设置ITEM的属性
		stringMap = new HashMap();
		intMap = new HashMap();
		boolMap = new HashMap();
		dateMap = new HashMap();
		if(itemPropMap!=null){
			setEntry = itemPropMap.entrySet();
			for(Entry<String,Object> entry : setEntry){
				String propName = entry.getKey();
				Object value = entry.getValue();
				System.out.println(" 对象：key ="+ propName+"| value ="+ value);
				if(value instanceof String){
					stringMap.put(propName, (String) value);
				}else if(value instanceof Integer){
					intMap.put(propName,   (BigInteger) value);
				}else if(value instanceof Boolean){
					boolMap.put(propName,  (Boolean) value);
				}else if(value instanceof Date){
					dateMap.put(propName,   (Calendar) value);
				}
			}
		}
		stringMap.put("object_name", itemName);
		stringMap.put("item_id", itemID);
		stringMap.put("object_desc", itemDesc);
		itemInput.stringProps= stringMap;
		itemInput.intProps = intMap;
		itemInput.boolProps = boolMap;
		itemInput.dateProps = dateMap;
		
		DataManagementService datamanagementservice = DataManagementService
				.getService(session);
		com.teamcenter.services.rac.core._2008_06.DataManagement.CreateIn createin = new com.teamcenter.services.rac.core._2008_06.DataManagement.CreateIn();
		createin.clientId = "Connor";
		createin.data = itemInput;
		com.teamcenter.services.rac.core._2008_06.DataManagement.CreateResponse createresponse = datamanagementservice
				.createObjects(new com.teamcenter.services.rac.core._2008_06.DataManagement.CreateIn[] { createin });
		SoaUtil.handlePartialErrors(createresponse.serviceData, null, null,
				true);
		com.teamcenter.services.rac.core._2008_06.DataManagement.CreateOut acreateout[] = createresponse.output;
		if (acreateout != null && acreateout.length > 0) {
			//ArrayList arraylist = new ArrayList(0);
			com.teamcenter.services.rac.core._2008_06.DataManagement.CreateOut acreateout1[];
			int j = (acreateout1 = acreateout).length;
			for (int i = 0; i < j; i++) {
				com.teamcenter.services.rac.core._2008_06.DataManagement.CreateOut createout = acreateout1[i];
				TCComponent atccomponent[] = createout.objects;
				if (atccomponent != null && atccomponent.length > 0) {
					//arraylist.addAll(Arrays.asList(atccomponent));
					
					newComp = (TCComponentItem) atccomponent[0];
					//sendToPlace(itemID,newComp,this.selectItemType.getBean().getLocation());
					//MessageBox.post("创建成功", "成功", MessageBox.INFORMATION);
				}else{
					//MessageBox.post("创建失败", "失败", MessageBox.ERROR);
				}
			}
		}
			 
	 }

}
