package cn.com.origin.autocode.common.operations;

import java.util.HashMap;
import cn.com.origin.autocodemanager.common.TCPreferenceUitl;

import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentFolder;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.TCUserService;

public class CreateNewItemOperation {
	private TCSession session = null;
	private String origin_newItemDatasetTemplateItem = "origin_newItemDatasetTemplateItem";
	private String origin_newItemDatasetName = "origin_newItemDatasetName";

	public CreateNewItemOperation() {
		session = (TCSession) AIFUtility.getCurrentApplication().getSession();
	}

	public String getTemplateItemID() {
		String TemplateItemID = "";
		try {
			HashMap<String, String> cfghm = TCPreferenceUitl.getPreferenceHashMap(session, TCPreferenceService.TC_preference_site, origin_newItemDatasetTemplateItem);
			if (cfghm == null) {
				// MessageBox.post("没有配置首选项【" +
				// origin_newItemDatasetTemplateItem + "】!", "INFO",
				// MessageBox.INFORMATION);
				return "";
			}
			TemplateItemID = (cfghm.get("TemplateItemID") + "").trim();
			if ((TemplateItemID == null) || (TemplateItemID.length() == 0)) {
				// MessageBox.post("请配置首选项【" + origin_newItemDatasetTemplateItem
				// + "】!", "INFO", MessageBox.INFORMATION);
				return "";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return TemplateItemID;
	}

	public String getDatasetName(String itemType) {
		String TemplateItemID = "";
		try {
			HashMap<String, String> cfghm = TCPreferenceUitl.getPreferenceHashMap(session, TCPreferenceService.TC_preference_site, origin_newItemDatasetName);
			if (cfghm == null) {
				// MessageBox.post("没有配置首选项【" + origin_newItemDatasetName +
				// "】!", "INFO", MessageBox.INFORMATION);
				return "";
			}
			TemplateItemID = (cfghm.get(itemType) + "").trim();
			if ((TemplateItemID == null) || (TemplateItemID.length() == 0)) {
				// MessageBox.post("请配置首选项【" + origin_newItemDatasetName + "】!",
				// "INFO", MessageBox.INFORMATION);
				return "";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return TemplateItemID;
	}

	public TCComponent createNewItem(String itemID, String itemRev, String itemType, String itemName, String itemDesc, TCComponent targetFolder) {
		TCComponentItem newItem = null;
		try {
			// System.out.println("itemID============" + itemID);
			// System.out.println("itemRev============" + itemRev);
			// System.out.println("itemType============" + itemType);
			// System.out.println("itemName============" + itemName);
			// System.out.println("itemDesc============" + itemDesc);

			String templateItemID = getTemplateItemID();
			TCUserService userService = (TCUserService) session.getUserService();
			Object[] params = { itemID, itemName, itemType, itemRev, new TCComponentFolder(), templateItemID, getDatasetName(itemType) };
			newItem = (TCComponentItem) userService.call("createNewItem", params);
			if (newItem != null) {
				newItem.refresh();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newItem;
	}
}
