
package cust.report;

import java.io.PrintStream;
import java.rmi.RemoteException;
import wt.epm.EPMTypeDefinitionEventsHandler;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;

import com.ptc.core.meta.common.impl.WCTypeIdentifier;
import com.ptc.core.meta.descriptor.common.DefinitionDescriptor;
import com.ptc.core.meta.descriptor.common.DefinitionDescriptorFactory;
import com.ptc.core.meta.type.admin.common.impl.TypeAdminHelper;
import com.ptc.core.meta.type.admin.common.impl.TypeAdminServiceIfc;
import com.ptc.core.meta.type.mgmt.common.TypeDefinitionDefaultView;
import com.ptc.core.meta.type.mgmt.server.*;
import com.ptc.core.meta.type.mgmt.server.impl.*;
import com.ptc.core.meta.type.mgmt.server.impl.service.TypeManagementConstraintDBService;
import com.ptc.core.meta.type.mgmt.server.impl.service.WTTypeManagementDBService;

import java.io.PrintStream;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.Locale;

import wt.access.NotAuthorizedException;
import wt.admin.AdministrativeDomainHelper;
import wt.epm.util.EPMSoftTypeServerUtilities;
import wt.epm.EPMTypeDefinitionEventsHandler;
import wt.fc.*;
import wt.fc.collections.WTCollection;
import wt.inf.container.*;
import wt.method.MethodContext;
import wt.org.OrganizationServicesHelper;
import wt.org.OrganizationServicesManager;
import wt.pom.Transaction;
import wt.query.*;
import wt.services.applicationcontext.implementation.DefaultServiceProvider;
import wt.session.SessionHelper;
import wt.session.SessionManager;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;
import wt.vc.IterationInfo;
import wt.vc.VersionControlHelper;
import wt.vc.wip.WorkInProgressHelper;

import wt.method.RemoteMethodServer;
import wt.httpgw.GatewayAuthenticator;

public class UpgradeSoftTypes_01b {

	public static void main(String[] args) throws Exception {
		RemoteMethodServer rms = RemoteMethodServer.getDefault();
		GatewayAuthenticator auth = new GatewayAuthenticator();
		auth.setRemoteUser("wcadmin");
		rms.setAuthenticator(auth);

		new UpgradeSoftTypes_01b();
	}

	public UpgradeSoftTypes_01b() throws Exception {

		String SERVER_CLASS = (wt.epm.util.UpgradeSoftTypes.class).getName() + "$Server";
		System.out.println("SERVER_CLASS: "+SERVER_CLASS);

		TypeManagementService theTypeManagementDBService = new WTTypeManagementDBService();
		WTTypeDefinition wttypedefinition;
		TypeDefinitionDefaultView atypedefinitiondefaultview[];
		Transaction transaction;

		wttypedefinition = (WTTypeDefinition)theTypeManagementDBService.refreshOriginalTypeDefinition((wt.epm.EPMDocument.class).getName());
		TypeDefinitionDefaultView typedefinitiondefaultview = getRootTypeDefinitionDefaultView(wttypedefinition);
		atypedefinitiondefaultview = getAllChildTypeDefinitionsIds(typedefinitiondefaultview);

		System.out.println("atypedefinitiondefaultview.length: "+atypedefinitiondefaultview.length);

		try {
			String s = TypeDomainHelper.getExchangeDomain();
			String s1 = s;
			String s2 = null;
			s1 = s1 + "." + "DynamicDocument";
			s2 = "wt.epm.DynamicDocument";
			String s3 = getDisplayName(wttypedefinition.getName() + "|" + s2);
			if(s3 != null)
				s2 = s3;


			//WTTypeDefinition wttypedefinition1 = createType(wttypedefinition, s1, s2);
			//addSingleValuedConstraint(wttypedefinition1, flag);
			//createBurstConfiguration(wttypedefinition1, flag);
			//if(flag)
			//	theTypeManagementDBService.checkinTypeDefinition(wttypedefinition1);
		}
		catch(Exception exception)
		{
			if(exception instanceof WTException)
				throw (WTException)exception;
			else
				throw new WTException(exception);
		}

		try {
			ArrayExpression arrayexpression = new ArrayExpression(
				new String[] {
					(wt.epm.EPMDocument.class).getName(),
					(wt.epm.EPMDocumentMaster.class).getName(),
					(wt.epm.structure.EPMMemberLink.class).getName(),
					(wt.epm.structure.EPMReferenceLink.class).getName()
				}
			);

			QuerySpec queryspec = new QuerySpec(com.ptc.core.meta.type.mgmt.server.impl.WTTypeDefinition.class);
			queryspec.appendWhere(new SearchCondition(new ClassAttribute(com.ptc.core.meta.type.mgmt.server.impl.WTTypeDefinition.class, "name"), "IN", arrayexpression));
			queryspec.appendAnd();
			queryspec.appendWhere(VersionControlHelper.getSearchCondition(com.ptc.core.meta.type.mgmt.server.impl.WTTypeDefinition.class, true));
			QueryResult queryresult = PersistenceHelper.manager.find(queryspec);

			do {
				if(!queryresult.hasMoreElements())
					break;
				WTTypeDefinition wttypedefinition2 = (WTTypeDefinition)queryresult.nextElement();
				String s = getDisplayName(wttypedefinition2.getName());
				if(!((WTTypeDefinitionMaster)wttypedefinition2.getMaster()).getDisplayNameKey().equals(s))
				{
					System.out.println("Changing " + wttypedefinition2.getName() + " display name to " + s);
					((WTTypeDefinitionMaster)wttypedefinition2.getMaster()).setDisplayNameKey(s);
					((WTTypeDefinitionMaster)wttypedefinition2.getMaster()).setHierarchyDisplayNameKey(s);
					PersistenceHelper.manager.save(wttypedefinition2.getMaster());
				}
			} while(true);
		}
		catch(WTPropertyVetoException wtpropertyvetoexception)
		{
			throw new WTException(wtpropertyvetoexception);
		}
	}

	private TypeDefinitionDefaultView getRootTypeDefinitionDefaultView(WTTypeDefinition wttypedefinition)
		throws WTException
	{
		if(wttypedefinition != null)
		{
			Long long1 = new Long(wttypedefinition.getIterationInfo().getBranchId());
			TypeDefinitionCache typedefinitioncache = EPMSoftTypeServerUtilities.TYPEDEF_CACHE_FACTORY.getTypeDefinitionCache();
			TypeDefinitionCache typedefinitioncache1 = StandardTypeDefinitionCache.getInstance();
			TypeDefinitionDefaultView typedefinitiondefaultview = typedefinitioncache.getTypeDefinition(long1);
			if(typedefinitiondefaultview == null)
				typedefinitiondefaultview = typedefinitioncache1.getTypeDefinition(long1);
			if(typedefinitiondefaultview == null)
				return null;
			Object obj = null;
			do
			{
				if(typedefinitiondefaultview == null || typedefinitiondefaultview.getParentID() == null)
					break;
				TypeDefinitionDefaultView typedefinitiondefaultview1 = typedefinitiondefaultview;
				typedefinitiondefaultview = typedefinitioncache.getParent(new Long(typedefinitiondefaultview1.getBranchId()));
				if(typedefinitiondefaultview == null)
					typedefinitiondefaultview = typedefinitioncache1.getParent(new Long(typedefinitiondefaultview1.getBranchId()));
			} while(true);
			return typedefinitiondefaultview;
		} else
		{
			return null;
		}
	}

	private TypeDefinitionDefaultView[] getAllChildTypeDefinitionsIds(TypeDefinitionDefaultView typedefinitiondefaultview)
		throws WTException
	{
		Long long1 = new Long(typedefinitiondefaultview.getBranchId());
		TypeDefinitionCache typedefinitioncache = StandardTypeDefinitionCache.getInstance();
		TypeDefinitionDefaultView atypedefinitiondefaultview[] = typedefinitioncache.getDescendants(long1, false);
		return atypedefinitiondefaultview;
	}

	private static String getDisplayName(String s)
	{
		DefinitionDescriptorFactory DD_FACTORY;
		DD_FACTORY = (DefinitionDescriptorFactory)DefaultServiceProvider.getService(com.ptc.core.meta.descriptor.common.DefinitionDescriptorFactory.class, "default");

		WCTypeIdentifier wctypeidentifier = null;
		wctypeidentifier = new WCTypeIdentifier(s);
		try
		{
			DefinitionDescriptor definitiondescriptor = DD_FACTORY.get(wctypeidentifier.intern(), null, null);
			s = definitiondescriptor.getFullDisplay();
		}
		catch(Exception exception) { }
		return s;
	}

/**
	private WTTypeDefinition createType(WTTypeDefinition wttypedefinition, String s, String s1)
		throws Exception
	{
		WTTypeDefinition wttypedefinition1;
		wttypedefinition1 = (WTTypeDefinition)theTypeManagementDBService.refreshOriginalTypeDefinition(s);
		if(wttypedefinition1 != null)
			break MISSING_BLOCK_LABEL_122;
		wttypedefinition1 = WTTypeDefinition.newWTTypeDefinition(s, "");
		wttypedefinition1.setDescriptionKey(s1);
		wttypedefinition1.setDisplayNameKey(s1);
		wttypedefinition1.setHierarchyDisplayNameKey(s1);
		wttypedefinition1.setInstantiable(true);
		wttypedefinition1.setUserAttributeable(false);
		wttypedefinition1.setAttributeContainer(null);
		wttypedefinition1.setParent(wttypedefinition);
		Object obj = null;
		MethodContext.getContext().put("CREATING_DEF_TYPE", SECRET_VALUE);
		String s2 = theTypeManagementDBService.createTypeDefinition(wttypedefinition1);
		MethodContext.getContext().remove("CREATING_DEF_TYPE");
		break MISSING_BLOCK_LABEL_122;
		Exception exception;
		exception;
		MethodContext.getContext().remove("CREATING_DEF_TYPE");
		throw exception;
		return wttypedefinition1;
	}
	*/
}
