
// from [c/d]:/ptc/Windchill (location wc shell opens to) with java in src/ext
// ant -f bin/tools.xml class -Dclass.includes=ext/epmParentChildList_02.java
// java ext.epmParentChildList_02 %.asm>D:\ptc\Windchill\src\ext\output\epmParentChildList_02-all-.asm-101613_01.txt

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.epm.workspaces.EPMAsStoredConfigSpec;
import wt.enterprise.RevisionControlled;

import wt.part.*;
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 epmParentChildList_02 {
	public static void findEpmNo(String objNo) throws WTException, WTPropertyVetoException {
		QuerySpec qs = new QuerySpec(EPMDocument.class);
		qs.appendWhere(new SearchCondition(EPMDocument.class,EPMDocument.NUMBER,SearchCondition.LIKE,objNo.toUpperCase()));
		qs.appendAnd();
		qs.appendWhere(new SearchCondition(EPMDocument.class, "iterationInfo.latest", "TRUE"));
		final QueryResult qr = PersistenceHelper.manager.find(qs);

		while (qr.hasMoreElements()) {
			epm = null;
			epm = (EPMDocument)qr.nextElement();
			epmTree(epm);
		}
	}

	public static void epmTree(EPMDocument epmDoc) throws WTException {
		try {
			arraylistTopEPM = new ArrayList();

			LMObject = (LifeCycleManaged)epmDoc;
			StStg = (String)LMObject.getLifeCycleState().toString();
			OBID = epmDoc.getPersistInfo().getObjectIdentifier().getId();
			verStg = epmDoc.getVersionIdentifier().getValue();
			iterStg = epmDoc.getIterationIdentifier().getValue();

			isGen = false;
			isInst = false;
			ftStat = 0;

			isGen = epmDoc.isGeneric();
			isInst = epmDoc.isInstance();
			ftStat = epmDoc.getFamilyTableStatus();

			parStg = "Parent\t"+OBID+"\t"+epmDoc.getNumber()+"\t"+epmDoc.getName()+"\t"+StStg+"\t"+verStg+"."+iterStg+"\t"+isGen+"\t"+isInst+"\t"+ftStat;

			EPMAsStoredConfigSpec configSpec = EPMAsStoredConfigSpec.newEPMAsStoredConfigSpec(epmDoc);
			qrChildChecker = EPMStructureHelper.service.navigateUsesToIteration(epmDoc, null, false, configSpec);

			if ( qrChildChecker.size() > 0) {
				for (int x = 0; x < qrChildChecker.size(); x++ ) {
					Persistable apersistable1[] = (Persistable[])(Persistable[])qrChildChecker.nextElement();
					if(apersistable1[1] instanceof EPMDocument) {
						arraylistTopEPM.add( (EPMDocument)apersistable1[1] );
					}
				}

				tmpStg = new String[0];
				tmpStg = new String[arraylistTopEPM.size()];

				for (int y = 0; y < arraylistTopEPM.size(); y++ ) {
					isQty = true;
					epmChild = null;
					epmChildp1= null;
					epmChild = (EPMDocument)arraylistTopEPM.get(y);

					QueryResult qrEPMMemberLink = findEPMMemberLink(epmDoc, (EPMDocumentMaster)epmChild.getMaster());
					if(qrEPMMemberLink != null) {
						Object[] objectArray = (Object[])qrEPMMemberLink.nextElement();
						EPMMemberLink epmmemberlink = (EPMMemberLink)objectArray[1];

						System.out.println( "\n2) EPMMemberLink Type: "+ epmmemberlink.getDepType());

						stringbuilder = new StringBuilder();
						stringbuilder.append('[').append(epmmemberlink.isRequired() ? "(Required)" : "(O)").append(epmmemberlink.isSuppressed() ? "(Suppressed)" : "").append(epmmemberlink.isSubstitute() ? "(Substitute)" : "").append(epmmemberlink.isPlaced() ? "(Placed)" : "").append(epmmemberlink.isAnnotated() ? "(Annotated)" : "").append(epmmemberlink.isFabricated() ? "(Fabricated)" : "").append(']');

						System.out.println( stringbuilder.toString() );
					}

					LMObject = (LifeCycleManaged)epmChild;
					StStg = (String)LMObject.getLifeCycleState().toString();
					OBID = epmChild.getPersistInfo().getObjectIdentifier().getId();
					verStg = epmChild.getVersionIdentifier().getValue();
					iterStg = epmChild.getIterationIdentifier().getValue();

					isGen = false;
					isInst = false;
					ftStat = 0;

					isGen = epmChild.isGeneric();
					isInst = epmChild.isInstance();
					ftStat = epmChild.getFamilyTableStatus();

					tmpStg[y] = epmChild.getNumber()+"|"+parStg+"\tChild\t"+OBID+"\t"+epmChild.getNumber()+"\t"+epmChild.getName()+"\t"+StStg+"\t"+verStg+"."+iterStg+"\t"+isGen+"\t"+isInst+"\t"+ftStat;
				}

				Arrays.sort(tmpStg);
				int qtyNo = 1;

				for (int z = 0; z < (tmpStg.length-1); z++ ) {
					tmpStgCur = tmpStg[z].split("\\|");
					tmpStgCur2 = tmpStg[z+1].split("\\|");

					if (tmpStgCur[0].equals(tmpStgCur2[0]) ) {
						qtyNo++;
						isQty = false;
						fullStg = tmpStgCur[1]+"\t"+qtyNo;
					}
					else {
						fullStg = tmpStgCur[1]+"\t"+qtyNo;
						qtyNo = 1;
						isQty = true;
					}

					if (isQty) { System.out.println(fullStg); }
				}

				try {
					tmpStgCur = tmpStg[(tmpStg.length-1)].split("\\|");
					fullStg = tmpStgCur[1]+"\t"+qtyNo;
					System.out.println(fullStg);
				} catch (ArrayIndexOutOfBoundsException e) { }
			}
			else {
				System.out.println(parStg+"\tCHILDREN:NONE");
			}
		}
		catch(Exception exception) { exception.printStackTrace(); }
	}

	public static QueryResult findEPMMemberLink(EPMDocument parentEPMDocument, EPMDocumentMaster childEPMDocumentMaster) {
		try{
			QuerySpec qsEPMMemberLink = new QuerySpec();
			SearchCondition searchCondition = new SearchCondition();
			int indexParent			= qsEPMMemberLink.appendClassList(EPMDocument.class, true);
			int indexLink			= qsEPMMemberLink.appendClassList(EPMMemberLink.class, true);
			int indexChildMaster	= qsEPMMemberLink.appendClassList(EPMDocumentMaster.class, true);

			searchCondition = new SearchCondition(EPMDocument.class, "thePersistInfo.theObjectIdentifier.id", SearchCondition.EQUAL, parentEPMDocument.getPersistInfo().getObjectIdentifier().getId());
			qsEPMMemberLink.appendWhere(searchCondition, indexParent);

			qsEPMMemberLink.appendAnd();
			searchCondition = new SearchCondition(EPMMemberLink.class, "roleAObjectRef.key.id", EPMDocument.class, "thePersistInfo.theObjectIdentifier.id");
			qsEPMMemberLink.appendWhere(searchCondition, indexLink, indexParent);

			qsEPMMemberLink.appendAnd();
			searchCondition = new SearchCondition(EPMDocumentMaster.class, "thePersistInfo.theObjectIdentifier.id", EPMMemberLink.class, "roleBObjectRef.key.id");
			qsEPMMemberLink.appendWhere(searchCondition, indexChildMaster, indexLink);

			qsEPMMemberLink.appendAnd();
			searchCondition = new SearchCondition(EPMDocumentMaster.class, "thePersistInfo.theObjectIdentifier.id", SearchCondition.EQUAL, childEPMDocumentMaster.getPersistInfo().getObjectIdentifier().getId());
			qsEPMMemberLink.appendWhere(searchCondition, indexChildMaster);

			return PersistenceHelper.manager.find(qsEPMMemberLink);
		}
		catch(WTException wte) {
			wte.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		RemoteMethodServer rms = RemoteMethodServer.getDefault();
		GatewayAuthenticator auth = new GatewayAuthenticator();
		auth.setRemoteUser("wcadmin");
		rms.setAuthenticator(auth);

		try {
			if(args.length < 1) {
				System.out.println(Usage);
				System.exit(0);
			}
			else {
				QueStg = args[0];

				System.out.println("Parent\tparentOBID\t Number\tname\tlcState\tversion\titeration\tisGen\tisInst\tftStat\tChild\tchildOBID\t Number\tname\tlcState\tversion\titeration\tisGen\tisInst\tFamilyTableStatus\tQuantity");

				findEpmNo(QueStg);
				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.custromization.epmParentChildList_02 %<epmdoc number search string>%\n\n"+
	"1st Argument = %<object number string>%\n\n"+
	"Should include % where % = * = wild card.\n";

	static StringBuilder stringbuilder;
	static String[] tmpStg, tmpStgCur, tmpStgCur2;
	static String OT, QueStg, StStg, epmNo, epmNmIn, epmNoIn, epmNoLg, epmNoSh, parStg, fullStg, childStg;
	static String oid, oidEpm, linkEpmStg, oidCh, oidPar, verStg, iterStg;
	static long OBID;
	static boolean isGen, isInst, isQty;
	static int ftStat, z;
	static EPMDocument epm, epm1, epmChild, epmChildp1, epmTop;
	static LifeCycleManaged LMObject;
	static QueryResult qrUses, qrChildChecker, qr;
	static ArrayList arraylistTopEPM;
	static ConfigSpec configSpec;
}
