
package ext;

import java.util.*;
import java.rmi.RemoteException;
import java.io.*;

import wt.util.*;
import wt.fc.*;
import wt.vc.*;
import wt.vc.wip.*;
import wt.vc.config.*;
import wt.vc.struct.StructHelper;
import wt.vc.struct.StructService;

import wt.query.*;
import wt.epm.*;
import wt.epm.build.*;
import wt.epm.structure.*;
import wt.enterprise.RevisionControlled;

import wt.part.*;
import wt.folder.*;
import wt.lifecycle.*;
import wt.lifecycle.State;
import wt.inf.container.*;

import wt.httpgw.GatewayAuthenticator;
import wt.method.RemoteMethodServer;
import com.ptc.windchill.cadx.common.util.AssociateUtilities;
import com.ptc.windchill.cadx.common.util.GenericUtilities;
import com.ptc.windchill.cadx.common.util.ObjectDependencyUtility;

public class epmList_39 {

	public static void findEpmNo(String QueType, String objNo) throws WTException, WTPropertyVetoException {
		configSpec = new LatestConfigSpec();

		QuerySpec qs = new QuerySpec(EPMDocument.class);
		qs.appendWhere(new SearchCondition(EPMDocument.class,EPMDocument.NUMBER,SearchCondition.LIKE,objNo));
		qs.appendAnd();
		qs.appendWhere(new SearchCondition(EPMDocument.class, "iterationInfo.latest", "TRUE"));

		qs = configSpec.appendSearchCriteria(qs);
		qr = PersistenceHelper.manager.find(qs);
		qr = configSpec.process(qr);

		System.out.println( "\nSQL (findEpmNo): \n"+qs.toString()+"\n");

		while (qr.hasMoreElements()) {
			epm = null;
			epm = (EPMDocument)qr.nextElement();
			epmm = (EPMDocumentMaster)epm.getMaster();

			epmNo = epm.getNumber();
			epmCont = epm.getContainer();
			epmFolder = wt.folder.FolderHelper.getFolder(epm);

			LMObject = (LifeCycleManaged)epm;
			StStg = (String)LMObject.getLifeCycleState().toString();

			System.out.println( "Running Tree For EPMDocument Number: "+epmNo+"\n");
			epmTree(QueType,epm);
		}
	}

	public static void epmTree(String QueTypeIN, EPMDocument epmDoc)
		throws WTException
	{
		try {
			arraylistTop = new ArrayList();
			arraylist1 = new ArrayList();

			configSpec = new LatestConfigSpec();

			qrChildChecker = EPMStructureHelper.service.navigateUsesToIteration(epmDoc, null, false, configSpec);

			if ( qrChildChecker.size() > 0) {
				arraylistTop.add(epmDoc);

				arraylist1 = new ArrayList();
				arraylist1 = listSortedChildrenInfo(epmDoc, configSpec);

				System.out.println( "EPMDoc Tree "+epmDoc.getNumber()+" Children/Size (arraylistTop.size()): "+arraylistTop.size()+"\n");

				for (int x = 0; x < arraylistTop.size(); x++ ) {
					EPMDocument epmTop = null;
					epmTop = (EPMDocument)arraylistTop.get(x);
					epmCont = epmTop.getContainer();
					epmFolder = wt.folder.FolderHelper.getFolder(epmTop);

					LMObject = (LifeCycleManaged)epmTop;
					StStg = (String)LMObject.getLifeCycleState().toString();

					System.out.println( "==================================================");
					System.out.println( "EPMDoc Number: "+ epmTop.getNumber() + "| EPMDoc Name: " + epmTop.getName());
					System.out.println( "EPMDoc CADName: "+ epmTop.getCADName() + "| EPMDoc DocType: " + epmTop.getDocType());
					System.out.println( "EPMDoc Container: "+ epmCont.getName() + "| EPMDoc Folder: " + epmFolder);
					System.out.println( "EPMDoc Version: " + epmTop.getVersionIdentifier().getValue() + "."+epmTop.getIterationIdentifier().getValue()+"| LC State: "+StStg+"\n");

					getWtpAssoc(QueTypeIN,epmTop,epmFolder);
				}
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }
	}

	public static void getWtpAssoc(String QueTypeIN, EPMDocument epmIN, Folder epmFolderIN)
		throws WTException
	{
		WTPart wtpListA[] = ObjectDependencyUtility.getAssociated((EPMDocument)epmIN);
		if( wtpListA != null )
			System.out.println( "\tAssociated (.getAssociated) WTPart (wtpListA.length): " + wtpListA.length+"\n");
		else
			System.out.println( "\tFOUND NO Associated (.getAssociated) WTParts: 0\n");

		WTPart wtpListPA[] = ObjectDependencyUtility.getPassiveAssociated(epmIN);
		if( wtpListPA != null )
			System.out.println( "\tPassive Associated (.getPassiveAssociated) WTPart (wtpListPA.length): " + wtpListPA.length+"\n");
		else
			System.out.println( "\tFOUND NO Passive Associated (.getPassiveAssociated) WTParts: 0\n");

		WTPart wtpListAA[] = ObjectDependencyUtility.getActiveAssociated(epmIN);
		if( wtpListAA != null )
			System.out.println( "\tActive Associated (.getActiveAssociated) WTPart (wtpListAA.length): " + wtpListAA.length+"\n");
		else
			System.out.println( "\tFOUND NO Active Associated (.getActiveAssociated) WTParts: 0\n");

		//wtpAssoc = AssociateUtilities.getActiveAssociatedPart((EPMDocument)epmIN);

		QueryResult qrAct = null;
		QueryResult qrPas = null;

		try {
			if(VersionControlHelper.isLatestIteration(epmIN))
				qrAct = PersistenceHelper.manager.navigate(epmIN, "buildTarget", wt.epm.build.EPMBuildRule.class);
			else
				qrAct = PersistenceHelper.manager.navigate(epmIN, "built", wt.build.BuildHistory.class);

			System.out.println( "\tFound Active Associated (build) WTPart (qrAct.size()): " + qrAct.size()+"\n");
		}
		catch(Exception exception) {
			System.out.println( "\tFAILED (Exception) to Find Active Associated (build) WTPart (qrAct.size()): " + qrAct.size()+"\n");
			//exception.printStackTrace();
		}

		try {
			qrPas = StructHelper.service.navigateDescribes(epmIN, true);
			System.out.println( "\tFound Passive Associated (Describes) WTPart (qrPas.size()): " + qrPas.size()+"\n");
		}
		catch(Exception exception) {
			System.out.println( "\tFAILED (Exception) to Find Passive Associated (Describes) WTPart (qrAct.size()): " + qrAct.size()+"\n");
			//exception.printStackTrace();
		}

		if( wtpListA != null ) {	// Found wtpart associated to current epmdoc array element
			for (int y = 0; y < wtpListA.length; y++ ) {	// Loop through epmdoc (epmIN) associated wtpart array
				WTPart wtpAssoc = null;
				wtpAssoc = (WTPart)wtpListA[y];
				wtpNo = wtpAssoc.getNumber();
				wtpCont = wtpAssoc.getContainer();
				wtpFolder = wt.folder.FolderHelper.getFolder(wtpAssoc);

				epmNoIN = "";
				epmNoIN = epmIN.getNumber();

				LMObject = (LifeCycleManaged)wtpAssoc;
				StStg = (String)LMObject.getLifeCycleState().toString();

				System.out.println( "\tAssociated WTPart Info: ("+y+")");
				System.out.println( "\tWTPart Number: "+ wtpAssoc.getNumber() + "| WTPart Name: " + wtpAssoc.getName());
				System.out.println( "\tWTPart Container: "+ wtpCont.getName() + "| WTPart Folder: " + wtpFolder);
				System.out.println( "\tWTPart Version: " + wtpAssoc.getVersionIdentifier().getValue() + "."+wtpAssoc.getIterationIdentifier().getValue()+"| Life Cycle State: "+StStg+"\n");

				// remove wtpart number extension

				if (wtpNo.indexOf(".") > 0 && wtpNo != null ) {
					System.out.println( "\tOrig WTPart String Number: "+wtpNo);
					wtpNo = removeExt(wtpNo);
					System.out.println( "\tFinal WTPart String Number: "+wtpNo+"\n");
				}

				// remove epmdoc number extension

				if (epmNoIN.indexOf(".") > 0 && epmNoIN != null ) {
					System.out.println( "\tOrig EPMDoc String Number: "+epmNoIN);
					epmNoIN = removeExt(epmNoIN);
					System.out.println( "\tFinal EPMDoc String Number: "+epmNoIN+"\n");
				}

				// check for short epmdoc number to be equal to wtpart short number

				if (wtpNo.equals(epmNoIN)) {	// epmdoc short number & wtpart are same number
					System.out.println( "\tEPMDoc: "+epmNoIN+" & WTPart "+ wtpNo + " NUMBERS MATCH: \n");
				}
				else {		// epmdoc short number & wtpart are NOT same number
					System.out.println( "\tEPMDoc: "+epmNoIN+" & WTPart "+ wtpNo + " NUMBERS DO NOT MATCH: \n");
					WTPartMaster wtpMaster = (WTPartMaster)wtpAssoc.getMaster();
					System.out.println("\tFound WTPart Master of "+wtpMaster.getNumber()+"\n");

					if (QueTypeIN.equals("-rw")) {
						try {
							// check for existing wtpart with same number as short epmdoc number

							WTPart newWtp0 = null;
							newWtp0 = findWtpNoEQ(epmNoIN);

							// if found, change wtpart number to be same as long/full epmdoc number

							if (newWtp0 != null) {
								//epmNoIN = epmNoIN+"_wtp";
								epmNoIN = epmIN.getNumber();
								System.out.println( "\tFound Duplicate WTPart Number: "+epmIN.getNumber()+"\n");
								System.out.println( "\tUsing WTPart Number: "+epmNoIN+" instead.\n");
							}

							// change wtpart number to match short or long empdoc number

							wt.fc.IdentificationObject idObj = wtpMaster.getIdentificationObject();
							WTPartMasterIdentity wtpmID = (WTPartMasterIdentity)idObj;
							wtpmID.setNumber(epmNoIN);
							IdentityHelper.service.changeIdentity(wtpMaster, wtpmID);
							System.out.println("\tIdentity Changed to: "+epmNoIN+"\n");
						}
						catch(WTException wte) {
							System.out.println( "\tWTPart Number ( "+epmNoIN+") Change FAILED:\n");
							//wte.printStackTrace();
						}
						catch(WTPropertyVetoException wtp) {
							System.out.println( "\tWTPart Number ( "+epmNoIN+") Change FAILED:\n");
							//wtp.printStackTrace();
						}
					}
				}
			}
		}
		else {
			System.out.println( "\tNO Associated WTPart: \n");

			epmNoIN3 = "";
			epmNoIN3 = epmIN.getNumber();

			try {
				// generate short epmdoc number (without extension)

				if (epmNoIN3.indexOf(".") > 0 && epmNoIN3 != null ) {
					System.out.println( "\tOrig EPMDoc String Number: "+epmNoIN3);
					epmNoIN3 = removeExt(epmNoIN3);
					System.out.println( "\tFinal EPMDoc String Number: "+epmNoIN3+"\n");
				}

				// check for existing wtpart with same number as short epmdoc number

				WTPart newWtp3 = null;
				newWtp3 = findWtpNoEQ(epmNoIN3);

				// Found wtpart with same short number as epmdoc without extension

				if (newWtp3 != null) {
					System.out.println( "\tFound Non Associated WTPart NO: "+newWtp3.getNumber() +"\n\twith same Number as EPMDoc: "+epmNoIN3+"\n");

					// check if found wtpart has associated epmdoc

					EPMDocument epmList3[] = ObjectDependencyUtility.getAssociated((WTPart)newWtp3);

					// found epmdoc associated to wtpart, so exit to next wtpart array element

					if( epmList3 != null && epmList3.length > 0 ) {
						System.out.println( "\tFound Associated EPMDoc length (epmList3.length): " + epmList3.length+"\n");
						for (int y = 0; y < epmList3.length; y++ ) {
							EPMDocument epmTop1 = null;
							epmTop1 = (EPMDocument)epmList3[y];
							System.out.println( "\tFound Existing WTPart Association to ("+y+"): "+epmTop1.getNumber());
						}
						System.out.println( "\n");
					}
					else {
						// check for existing wtpart with same number as long epmdoc number

						epmNoIN4 = "";
						epmNoIN4 = epmIN.getNumber();

						WTPart newWtp4 = null;
						newWtp4 = findWtpNoEQ(epmNoIN4);

						if (newWtp4 != null) {
							System.out.println( "\tFound Non Associated WTPart NO: "+newWtp4.getNumber() +"\n\twith same Number as EPMDoc: "+epmNoIN4+"\n");

							// check if wtpart found has associated epmdoc, if so, leave loop

							EPMDocument epmList4[] = ObjectDependencyUtility.getAssociated((WTPart)newWtp4);

							if( epmList4 != null && epmList4.length > 0 ) {
								System.out.println( "\tFound Associated EPMDoc length (epmList4.length): " + epmList4.length+"\n");
								for (int y = 0; y < epmList4.length; y++ ) {
									EPMDocument epmTop4 = null;
									epmTop4 = (EPMDocument)epmList4[y];
									System.out.println( "\tFound Existing WTPart Association to ("+y+"): "+epmTop4.getNumber());
								}
								System.out.println( "\n");
							}
							else if (QueTypeIN.equals("-rw")) {		// wtpart newWtp4 has no associated epmdoc same number
								try {
									newWtp4 = (WTPart)setCheckoutInfo(newWtp4, WorkInProgressState.WORKING);
									EPMBuildRule epmbuildrule = null;
									epmbuildrule = EPMBuildRule.newEPMBuildRule(epmIN, newWtp4);
									epmbuildrule = (EPMBuildRule)PersistenceHelper.manager.save(epmbuildrule);

									System.out.println( "\tCreated Association between WTPart Number: "+newWtp4.getNumber()+ " & EPMDoc Number: "+epmIN.getNumber()+"\n");
								}
								catch(WTException wtexception) {
									System.out.println( "\tFAILED Link association between WTPart Number: "+newWtp4.getNumber()+ " & EPMDoc Number: "+epmIN.getNumber()+"\n");

									WTPart wtpList5[]  = ObjectDependencyUtility.getAssociated((EPMDocument)epmIN);

									if( wtpList5 != null ) {
										System.out.println( "\tAssociated EPMDoc length (wtpList5.length): " + wtpList5.length+"\n");

										for (int y = 0; y < wtpList5.length; y++ ) {
											WTPart wtpAssoc5 = null;
											wtpAssoc5 = (WTPart)wtpList5[y];
											System.out.println( "\tBut Did Find Existing WTPart Number Association to ("+y+"): "+wtpAssoc5.getNumber());
										}
									}

									EPMDocument epmList5[] = ObjectDependencyUtility.getAssociated((WTPart)newWtp4);

									if( epmList5 != null ) {
										System.out.println( "\tAssociated EPMDoc length: " + epmList5.length+"\n");

										for (int y = 0; y < epmList5.length; y++ ) {
											EPMDocument epmTop5 = null;
											epmTop5 = (EPMDocument)epmList5[y];
											System.out.println( "\tBut Did Find Existing EPMDoc Number Association ("+y+"): "+epmTop5.getNumber());
										}
									}
									System.out.println( "\n");
								}
							}
						}
					}
				}
				else {	// Didnt find epmdoc associated to wtpart & == null or > 0
					if (QueTypeIN.equals("-rw")) {

						epmNoIN2 = "";epmNmIN2 = "";

						// Create and persist new wtpart
						epmNoIN2 = epmIN.getNumber();
						epmNmIN2 = epmIN.getName();
						epmCont = epmIN.getContainer();

						LMObject = (LifeCycleManaged)epmIN;
						StStg = (String)LMObject.getLifeCycleState().toString();

						if (epmNoIN2.indexOf(".") > 0 && epmNoIN2 != null ) {
							System.out.println( "\tOrig EPMDoc String Number: "+epmNoIN2);
							epmNoIN2 = removeExt(epmNoIN2);
							System.out.println( "\tFinal EPMDoc String Number: "+epmNoIN2+"\n");
						}

						if (epmNmIN2.indexOf(".") > 0 && epmNmIN2 != null ) {
							System.out.println( "\tOrig EPMDoc String Number: "+epmNmIN2);
							epmNmIN2 = removeExt(epmNmIN2);
							System.out.println( "\tFinal EPMDoc String Number: "+epmNmIN2+"\n");
						}

						WTPart newWtp2 = null;
						newWtp2 = WTPart.newWTPart(epmNoIN2,epmNmIN2);
						FolderHelper.assignLocation(newWtp2,epmFolderIN);

						try {
							newWtp2.getMaster().setSeries(epmIN.getMaster().getSeries());
							VersionControlHelper.setVersionIdentifier(newWtp2, epmIN.getVersionIdentifier());
						}
						catch(NumberFormatException numberformatexception) { System.out.println( "\tUnable to set the version id of the WTPart Number: "+newWtp2.getNumber()); }

						VersionControlHelper.setIterationIdentifier(newWtp2, IterationIdentifier.newIterationIdentifier());
						//newWtp2 = (WTPart)setLifeCycle(newWtp, StStg, epmCont);
						newWtp2=(WTPart)PersistenceHelper.manager.save(newWtp2);

						System.out.println( "\tCreated WTPart Number: "+newWtp2.getNumber());

						wtpCont = newWtp2.getContainer();
						wtpFolder = wt.folder.FolderHelper.getFolder(newWtp2);

						LMObject = (LifeCycleManaged)newWtp2;
						StStg = (String)LMObject.getLifeCycleState().toString();

						System.out.println( "\tWTPart Number: "+ newWtp2.getNumber() + "| WTPart Name: " + newWtp2.getName());
						System.out.println( "\tWTPart Container: "+ wtpCont.getName() + "| WTPart Folder: " + wtpFolder);
						System.out.println( "\tWTPart Version: " + newWtp2.getVersionIdentifier().getValue() + "."+newWtp2.getIterationIdentifier().getValue()+"| LC State: "+StStg+"\n");

						try {
							newWtp2 = (WTPart)setCheckoutInfo(newWtp2, WorkInProgressState.WORKING);
							EPMBuildRule epmbuildrule = null;
							epmbuildrule = EPMBuildRule.newEPMBuildRule(epmIN, newWtp2);
							epmbuildrule = (EPMBuildRule)PersistenceHelper.manager.save(epmbuildrule);

							System.out.println("\tCreated Association between WTPart NUMBER: "+newWtp2.getNumber()+ " & EPMDoc NUMBER: "+epmIN.getNumber()+"\n");
						}
						catch(WTException wtexception) {
							System.out.println( "\tFAILED Association between WTPart NUMBER: "+newWtp2.getNumber()+ " & EPMDoc NUMBER: "+epmIN.getNumber()+"\n");
							//System.out.println( "\tWTPart Number Deleted: "+newWtp2.getNumber()+"\n");
							//newWtp2 = (WTPart)PersistenceHelper.manager.delete(newWtp);
						}
					}
				}
			}
			catch(Exception exception) { System.out.println( "\tFAILED = Exception: "+exception.toString()+"\n"); }
		}
	}

	public static WTPart findWtpNoEQ(String objNo) throws WTException, WTPropertyVetoException {
		configSpec = new LatestConfigSpec();

		QuerySpec qs = new QuerySpec(WTPart.class);
		qs.appendWhere(new SearchCondition(WTPart.class,WTPart.NUMBER,SearchCondition.EQUAL,objNo));
		qs.appendAnd();
		qs.appendWhere(new SearchCondition(WTPart.class, "iterationInfo.latest", "TRUE"));

		qs = configSpec.appendSearchCriteria(qs);
		qr = PersistenceHelper.manager.find(qs);
		qr = configSpec.process(qr);

		System.out.println( "\nSQL (findWtpNoEQ): \n"+qs.toString()+"\n");

		if(qr.size() == 1) {
			WTPart wtpEQ = (WTPart)qr.nextElement();
			System.out.println( "\tFound EQUAL WTPart for : "+wtpEQ.getNumber()+"\n");
			System.out.println( "\tFound 1 EQUAL WTPart for qr.size() : "+qr.size()+"\n");
			return wtpEQ;
		}
		else { //qr.size() == 0
			System.out.println( "\tFound Other than 1 EQUAL WTPart for qr.size() : "+qr.size()+"\n");
			return null;
		}
	}

	public static void findWtpNo(String QueType, String objNo) throws WTException, WTPropertyVetoException {
		configSpec = new LatestConfigSpec();

		QuerySpec qs = new QuerySpec(WTPart.class);
		qs.appendWhere(new SearchCondition(WTPart.class,WTPart.NUMBER,SearchCondition.LIKE,objNo));
		qs.appendAnd();
		qs.appendWhere(new SearchCondition(WTPart.class, "iterationInfo.latest", "TRUE"));

		qs = configSpec.appendSearchCriteria(qs);
		qr = PersistenceHelper.manager.find(qs);
		qr = configSpec.process(qr);

		System.out.println( "\nSQL (findWtpNo): \n"+qs.toString()+"\n");

		while (qr.hasMoreElements()) {
			wtpOrig = null;
			wtpOrig = (WTPart)qr.nextElement();
			System.out.println( "Running Original WTPart Tree for : "+wtpOrig.getNumber()+"\n");
			wtpTree(wtpOrig);
		}
	}

	public static void wtpTree(WTPart wtp)
		throws WTException
	{
		try{
			arraylistTop = new ArrayList();
			arraylist1 = new ArrayList();
			configSpec = new LatestConfigSpec();

			qrChildChecker = WTPartHelper.service.getUsesWTParts(wtp, configSpec);

			if ( qrChildChecker.size() > 0) {
				arraylistTop.add(wtp);
				arraylist1 = new ArrayList();
				arraylist1 = listSortedChildrenInfo(wtp, configSpec);

				System.out.println( "WTPart Tree "+wtp.getNumber()+" Children/Size (arraylistTop.size()): "+arraylistTop.size()+"\n");

				for (int x = 0; x < arraylistTop.size(); x++ ) {
					WTPart wtpTop = null;
					wtpTop = (WTPart)arraylistTop.get(x);
					wtpCont = wtpTop.getContainer();
					wtpFolder = wt.folder.FolderHelper.getFolder(wtpTop);

					LMObject = (LifeCycleManaged)wtpTop;
					StStg = (String)LMObject.getLifeCycleState().toString();

					System.out.println( "WTPart Number: "+ wtpTop.getNumber() + "| WTPart Name: " + wtpTop.getName());
					System.out.println( "WTPart Container: "+ wtpCont.getName() + "| WTPart Folder: " + wtpFolder);
					System.out.println( "WTPart Version: " + wtpTop.getVersionIdentifier().getValue() + "."+wtpTop.getIterationIdentifier().getValue()+"| LC State: "+StStg+"\n");

					EPMDocument epmList[] = ObjectDependencyUtility.getAssociated((WTPart)wtpTop);

					if( epmList != null ) {
						System.out.println( "\tAssociated EPMDoc (epmList.length): " + epmList.length+"\n");

						for (int y = 0; y < epmList.length; y++ ) {
							EPMDocument epmTop = null;
							epmTop = (EPMDocument)epmList[y];
							epmCont = epmTop.getContainer();
							epmFolder = wt.folder.FolderHelper.getFolder(epmTop);

							LMObject = (LifeCycleManaged)epmTop;
							StStg = (String)LMObject.getLifeCycleState().toString();

							System.out.println( "\tAssociated EPMDoc Info ("+y+"): ");
							System.out.println( "\tEPMDoc Number: "+ epmTop.getNumber() + "| EPMDoc Name: " + epmTop.getName());
							System.out.println( "\tEPMDoc CADName: "+ epmTop.getCADName() + "| EPMDoc DocType: " + epmTop.getDocType());
							System.out.println( "\tEPMDoc Container: "+ epmCont.getName() + "| EPMDoc Folder: " + epmFolder);
							System.out.println( "\tEPMDoc Version: " + epmTop.getVersionIdentifier().getValue() + "."+epmTop.getIterationIdentifier().getValue()+"| LC State: "+StStg+"\n");
						}
					}
				}
			}
		}
		catch(Exception exception) { System.out.println( "\tFAILED = Exception: "+exception.toString()+"\n"); }
	}

	public static Persistable[][] my_getUsesWTParts(WTPart wtpart, ConfigSpec configSpec)
		throws WTException
	{
		ArrayList arraylist2 = new ArrayList();
		QueryResult qrMY = WTPartHelper.service.getUsesWTParts(wtpart, configSpec);
		do
		{
			if(!qrMY.hasMoreElements())
	 			break;
			Persistable apersistable[] = (Persistable[])(Persistable[])qrMY.nextElement();
			if(apersistable[1] instanceof WTPart)
			{
				WTPartUsageLink wtpartusagelink = (WTPartUsageLink)apersistable[0];
	 			if(!wtpartusagelink.dissatisfiesFilterCriteria())
				arraylist2.add(apersistable);

				WTPart wtChild3 = ((WTPart) apersistable[1] );
				arraylistTop.add(wtChild3);
			}
		} while(true);
		return (Persistable[][])(Persistable[][])arraylist2.toArray(new Persistable[arraylist2.size()][]);
	}

	public static Persistable[][] my_getUsesEPMDocs(EPMDocument epmDoc, ConfigSpec configSpec)
		throws WTException
	{
		ArrayList arraylist2 = new ArrayList();
		QueryResult qrMY = EPMStructureHelper.service.navigateUsesToIteration(epmDoc, null, false, configSpec);
		do
		{
			if(!qrMY.hasMoreElements())
	 			break;

			Persistable apersistable[] = (Persistable[])(Persistable[])qrMY.nextElement();
			Persistable persistable = apersistable[0];
			Persistable persistable1 = apersistable[1];

			if((persistable instanceof EPMDependencyLink) && (persistable1 instanceof EPMDocument))
			{
				EPMDependencyLink epmdependencylink = (EPMDependencyLink)persistable;
				epm1 = (EPMDocument)persistable1;
				epmCont = epm1.getContainer();
				epmFolder = wt.folder.FolderHelper.getFolder(epm1);
				epmNo = epm1.getNumber();

				arraylist2.add(apersistable);
				arraylistTop.add(epm1);

				LMObject = (LifeCycleManaged)epm1;
				StStg = (String)LMObject.getLifeCycleState().toString();
			}
		} while(true);
		return (Persistable[][])(Persistable[][])arraylist2.toArray(new Persistable[arraylist2.size()][]);
	}

	public static ArrayList listSortedChildrenInfo(WTPart wtpart, ConfigSpec configSpec)
		throws WTPropertyVetoException, WTException
	{
		ArrayList arraylist = new ArrayList();
		Persistable apersistable[][] = my_getUsesWTParts(wtpart, configSpec);
		if(apersistable.length > 0)
		{
			for(int j = 0; j < apersistable.length; j++)
			{
				WTPart wtChild2 = (WTPart)apersistable[j][1];
				ArrayList arraylist1 = listSortedChildrenInfo(wtChild2, configSpec);
				arraylist.add(arraylist1);
			}
		}
		return arraylist;
	}

	public static ArrayList listSortedChildrenInfo(EPMDocument epmDoc, ConfigSpec configSpec)
		throws WTPropertyVetoException, WTException
	{
		ArrayList arraylist = new ArrayList();
		Persistable apersistable[][] = my_getUsesEPMDocs(epmDoc, configSpec);
		if(apersistable.length > 0)
		{
			for(int j = 0; j < apersistable.length; j++)
			{
				EPMDocument epmDoc2 = (EPMDocument)apersistable[j][1];
				ArrayList arraylist1 = listSortedChildrenInfo(epmDoc2, configSpec);
				arraylist.add(arraylist1);
			}
		}
		return arraylist;
	}

	public static String removeExt(String tmpStg) {
		if ( tmpStg.endsWith(".PRT") || tmpStg.endsWith(".prt") ) { tmpStg = tmpStg.substring(0, tmpStg.length() - 4); }
		if ( tmpStg.endsWith(".DRW") || tmpStg.endsWith(".drw") ) { tmpStg = tmpStg.substring(0, tmpStg.length() - 4); }
		if ( tmpStg.endsWith(".ASM") || tmpStg.endsWith(".asm") ) { tmpStg = tmpStg.substring(0, tmpStg.length() - 4); }

		return tmpStg;
	}

	protected static RevisionControlled setCheckoutInfo(RevisionControlled revCon, WorkInProgressState wipState)
		throws WTException
	{
		try
		{
			CheckoutInfo checkoutinfo = CheckoutInfo.newCheckoutInfo();
			checkoutinfo.setState(wipState);
			revCon.setCheckoutInfo(checkoutinfo);
		}
		catch(WTPropertyVetoException wtp) { System.out.println( "\tFAILED = WTPropertyVetoException: "+wtp.toString()+"\n"); }
		catch(WTException wte) { System.out.println( "\tFAILED = WTException: "+wte.toString()+"\n"); }
		return revCon;
	}

	public static void main(String[] args) {
		RemoteMethodServer rms = RemoteMethodServer.getDefault();
		GatewayAuthenticator auth = new GatewayAuthenticator();
		auth.setRemoteUser("wcadmin");
		rms.setAuthenticator(auth);

		try {
			if(args.length < 2)
			{
				System.out.println(Usage);
				System.exit(0);
			}
			else {
				OT = args[0];
				QueStg = args[1].toUpperCase();

				if ( OT.equals("-qe")||OT.equals("-rw") ) {
					findEpmNo(OT,QueStg);
				}
				else if ( OT.equals("-qw") ) {
					findWtpNo(OT,QueStg);
				}
				else { System.out.println(Usage); }

				System.exit(0);
			}
		}
		catch(WTException wtee) { System.out.println( "\tFAILED = WTException: "+wtee.toString()+"\n"); }
		catch(WTPropertyVetoException wtpe) { System.out.println( "\tFAILED = WTPropertyVetoException: "+wtpe.toString()+"\n"); }
		finally { System.exit(0); }
	}

	public static String Usage = "\nUsage:\n\njava/windchill [-mx1024m] "+
	"ext.epmList -<query/rename> %<epmdoc/wtpart number search string>%\n\n"+
	"1st Argument = <query/rename>:\n\n-qe = query epmdocs only\n-qw = query wtparts only\n"+
	"-rw = query epmdocs & rename wtparts (query 1st)\n\n"+
	"1st Argument should be lower case.\n\n2nd Argument = %<object number string>%\n\n"+
	"Should include % where % = * = wild card.\n";

	static String OT, QueStg, StStg, epmNo, wtpNo;
	static String epmNoIN, epmNoIN2, epmNmIN2, epmNoIN3, epmNoIN4;
	static WTContainer wtpCont, epmCont;
	static Folder wtpFolder, epmFolder;
	static EPMDocumentMaster epmm;
	static EPMDocument epm, epm1, epmOrig;
	static WTPart wtpAssoc, wtpOrig;
	static LifeCycleTemplate lifecycletemplate;
	static LifeCycleManaged LMObject;
	static QueryResult qrUses, qrChildChecker, qr;
	static ArrayList arraylist1, arraylistTop;
	static ConfigSpec configSpec;
}

