
package ext;

import java.util.*;
import java.rmi.RemoteException;

import wt.util.*;
import wt.fc.*;
import wt.vc.*;
import wt.query.*;
import wt.epm.*;
import wt.lifecycle.*;
import wt.lifecycle.State;
import wt.httpgw.GatewayAuthenticator;
import wt.method.RemoteMethodServer;
import com.ptc.windchill.enterprise.search.server.*;

public class epmList_04 {

	public static Enumeration FindEpmNumbered(String drwNo) throws WTException, WTPropertyVetoException {
		QuerySpec qs = new QuerySpec(EPMDocument.class);
		qs.appendWhere(new SearchCondition(EPMDocument.class,EPMDocument.NUMBER,SearchCondition.LIKE,drwNo));
		qs.appendAnd();
		qs.appendWhere(new SearchCondition(EPMDocument.class, "iterationInfo.latest", "TRUE"));
		qs.appendAnd();
		qs.appendWhere(new SearchCondition(EPMDocument.class, "versionInfo.identifier.versionId", "=", "", false));

		System.out.println( "Now Querying for Doc Using: " + qs.toString() );

		final QueryResult qr = PersistenceHelper.manager.find(qs);

		while (qr.hasMoreElements()) {
			EPMDocument epmm = (EPMDocument)qr.nextElement();

			LifeCycleManaged LMObject = (LifeCycleManaged)epmm;
			String StStg = (String)LMObject.getLifeCycleState().toString();

			String s1 = epmm.getVersionInfo().getIdentifier().getSeries() + "." + epmm.getIterationInfo().getIdentifier().getSeries();

			int revisionNumber = epmm.getRevisionNumber();

			//String version = searchInfo.getCriteriaInfo().getVersion();
			//boolean latestVersion = version != null && version.equalsIgnoreCase("LATEST");

			//versionInfo.identifier.versionId
			//if (StStg.compareTo("RELEASED") == 0) {
				//System.out.println( "Version: "+epmm.getVersionInfo().getIdentifier().getValue());
				//System.out.println( "Revision: "+revisionNumber);
				System.out.println( "s1: "+s1+" & "+epmm.getNumber() + "," + epmm.getVersionIdentifier().getValue() + "."+epmm.getIterationIdentifier().getValue());
				//System.out.println( "Revision: "+epmm.getRevisionIdentifier().getValue());
			//}
		}

		return new Enumeration() {
			public boolean hasMoreElements() {
				return qr.hasMoreElements();
			}
			public Object nextElement() throws NoSuchElementException {
				return ((Persistable[])qr.nextElement());
			}
		};
	}

	public static void main(String[] args) {
		if(args.length < 1)
		{
			System.out.println("Usage: windchill epmList <epmdoc search string>");
			System.exit(0);
		} else
		{
			try {
				RemoteMethodServer rms = RemoteMethodServer.getDefault();
				GatewayAuthenticator auth = new GatewayAuthenticator();
				auth.setRemoteUser("wcadmin");
				rms.setAuthenticator(auth);

				String drwNo = args[0];

				try {
					Enumeration e = FindEpmNumbered(drwNo);
					while (e.hasMoreElements()) {
						System.out.println(((EPMDocument)e.nextElement()).getIdentity());
					}
				}
				catch (WTException wtee) {
					wtee.printStackTrace();
				}
				catch (WTPropertyVetoException wtpe) {
					wtpe.printStackTrace();
				}
				finally {
					System.exit(0);
				}

				System.exit(0);
			}
			catch(Exception exceptionMain)
			{
				exceptionMain.printStackTrace();
			}
		}
	}

    private final transient SearchInfo searchInfo = new SearchInfo();
}