package cn.com.origin.autocode.saveas;

import java.awt.Frame;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.swt.widgets.Display;

import cn.com.origin.autocode.newitem.generatcode.NewCodeItemDialog;
import cn.com.origin.autocode.newitem.origin.ORSaveObjectDescOperation;
import cn.com.origin.autocodemanager.common.PackCodeDescInfos;
import cn.com.origin.autocodemanager.common.TCPreferenceUitl;

import com.teamcenter.rac.aif.AIFDesktop;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.commands.saveas.SaveItemAsDialog;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemType;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.log.Debug;
import com.teamcenter.services.rac.core.DataManagementService;
import com.teamcenter.soa.client.model.ErrorStack;
public class ORSaveItemAsDialog extends SaveItemAsDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String origin_codeSaveAs_needLoadItemTypes = "origin_codeSaveAs_needLoadItemTypes";
	private String itemID = "";
	private String itemRev = "";
	private String itemName = "";
	PackCodeDescInfos packCodeDescInfos = new PackCodeDescInfos();

	public String getItemID() {
		return itemID;
	}

	public void setItemID(String itemID) {
		this.itemID = itemID;
	}

	public String getItemRev() {
		return itemRev;
	}

	public void setItemRev(String itemRev) {
		this.itemRev = itemRev;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public ORSaveItemAsDialog(Frame frame, InterfaceAIFComponent interfaceaifcomponent) {
		super(frame, interfaceaifcomponent);
	}

	public PackCodeDescInfos getPackCodeDescInfos() {
		return packCodeDescInfos;
	}

	public void setPackCodeDescInfos(PackCodeDescInfos packCodeDescInfos) {
		this.packCodeDescInfos = packCodeDescInfos;
	}

	public void addAssignToProjectTab() {
	}

	@SuppressWarnings("unchecked")
	protected void assignID() {
		boolean flag;
		boolean flag1;
		int i = 0;
		flag = true;
		flag1 = true;
		try {
			String s;
			TCComponentItemType tccomponentitemtype;
			tccomponentitemtype = (TCComponentItemType) session.getTypeComponent(currentComponent.getType());

			DataManagementService localDataManagementService = DataManagementService.getService(this.session);
			com.teamcenter.services.rac.core._2008_06.DataManagement.TypeAndItemRevInfo[] arrayOfTypeAndItemRevInfo = new com.teamcenter.services.rac.core._2008_06.DataManagement.TypeAndItemRevInfo[1];
			final com.teamcenter.services.rac.core._2008_06.DataManagement.TypeAndItemRevInfo localTypeAndItemRevInfo = new com.teamcenter.services.rac.core._2008_06.DataManagement.TypeAndItemRevInfo();
			localTypeAndItemRevInfo.typeName = this.currentComponent.getType();
			localTypeAndItemRevInfo.itemRev = null;
			arrayOfTypeAndItemRevInfo[0] = localTypeAndItemRevInfo;
			this.revResponse = localDataManagementService.getRevNRAttachDetails(arrayOfTypeAndItemRevInfo);
			if (this.revResponse.initRevDetails.length != 0)
				i = this.revResponse.initRevDetails[0].revFormat;
			if (this.id.getText().length() == 0) {
				// ====================tyl 20110810=======================//
				if (TCPreferenceUitl.isSelectExsitToTCPreferenceValues(session, origin_codeSaveAs_needLoadItemTypes, localTypeAndItemRevInfo.typeName)) {
					try {
						Display.getDefault().asyncExec(new Runnable() {
							public void run() {
								new NewCodeItemDialog(ORSaveItemAsDialog.this, AIFDesktop.getActiveDesktop().getShell(), localTypeAndItemRevInfo.typeName).open();
								id.setText(itemID);
								rev.setText(itemRev);
								nameField.setText(itemName);
								nameField.requestFocusInWindow();
								id.setEnabled(false);
								if ((itemID.length() > 0) && (itemRev.length() > 0) && (itemRev.length() > 0)) {
									okButton.setEnabled(true);
									applyButton.setEnabled(true);
								}
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}
					// ====================tyl 20110810=======================//

				} else {
					DataManagementService datamanagementservice = DataManagementService.getService(session);
					com.teamcenter.services.rac.core._2006_03.DataManagement.GenerateItemIdsAndInitialRevisionIdsResponse generateitemidsandinitialrevisionidsresponse;
					com.teamcenter.services.rac.core._2006_03.DataManagement.GenerateItemIdsAndInitialRevisionIdsProperties agenerateitemidsandinitialrevisionidsproperties[] = new com.teamcenter.services.rac.core._2006_03.DataManagement.GenerateItemIdsAndInitialRevisionIdsProperties[1];
					agenerateitemidsandinitialrevisionidsproperties[0] = new com.teamcenter.services.rac.core._2006_03.DataManagement.GenerateItemIdsAndInitialRevisionIdsProperties();
					agenerateitemidsandinitialrevisionidsproperties[0].item = null;
					agenerateitemidsandinitialrevisionidsproperties[0].itemType = currentComponent.getType();
					agenerateitemidsandinitialrevisionidsproperties[0].count = 1;
					generateitemidsandinitialrevisionidsresponse = datamanagementservice.generateItemIdsAndInitialRevisionIds(agenerateitemidsandinitialrevisionidsproperties);

					if (generateitemidsandinitialrevisionidsresponse.serviceData.sizeOfPartialErrors() > 0) {
						id.setText("");
						id.requestFocusInWindow();
						ErrorStack errorstack = generateitemidsandinitialrevisionidsresponse.serviceData.getPartialError(0);
						int ai[] = errorstack.getLevels();
						int ai1[] = errorstack.getCodes();
						String as[] = errorstack.getMessages();
						TCException tcexception1 = new TCException(ai, ai1, as);
						MessageBox.post(parentFrame, tcexception1);
						return;
					}
					String s1 = null;
					Map map = generateitemidsandinitialrevisionidsresponse.outputItemIdsAndInitialRevisionIds;
					Iterator iterator = map.entrySet().iterator();
					if (iterator.hasNext()) {
						java.util.Map.Entry entry = (java.util.Map.Entry) iterator.next();
						com.teamcenter.services.rac.core._2006_03.DataManagement.ItemIdsAndInitialRevisionIds aitemidsandinitialrevisionids[] = (com.teamcenter.services.rac.core._2006_03.DataManagement.ItemIdsAndInitialRevisionIds[]) (com.teamcenter.services.rac.core._2006_03.DataManagement.ItemIdsAndInitialRevisionIds[]) entry
								.getValue();
						com.teamcenter.services.rac.core._2006_03.DataManagement.ItemIdsAndInitialRevisionIds itemidsandinitialrevisionids = aitemidsandinitialrevisionids[0];
						s1 = itemidsandinitialrevisionids.newItemId;
						if (i != 0) {
							assignRevIdWithRevisionRule();
						} else {
							String s2 = itemidsandinitialrevisionids.newRevId;
							rev.setText(s2);
						}
						flag = itemidsandinitialrevisionids.isItemModify;
						flag1 = itemidsandinitialrevisionids.isRevModify;
					}

					id.setText(s1);

					if (!flag)
						id.setEditable(false);
					if (!flag1)
						rev.setEditable(false);
					nameField.requestFocusInWindow();
					return;
				}
				if (i != 0) {
					assignRevIdWithRevisionRule();
				} else {
					s = tccomponentitemtype.getNewRev(null);
					flag1 = tccomponentitemtype.isNewRevModifiable(null, s);
					rev.setText(s);
				}
				if (!flag1)
					rev.setEditable(false);
				nameField.requestFocusInWindow();
			}
		} catch (TCException localTCException) {
			if (Debug.isOn("NewItem"))
				localTCException.printStackTrace();
			MessageBox.post(this.parentFrame, localTCException);
		}
	}

	public void postSaveComponentAs(TCComponentItem tccomponentitem) {
		ORSaveObjectDescOperation orSaveObjectDescOperation = new ORSaveObjectDescOperation(tccomponentitem);
		orSaveObjectDescOperation.saveObject_Desc(packCodeDescInfos);
	}

}
