
package lj_cust;

import java.util.*;
import wt.util.*;
import wt.fc.*;
import wt.vc.*;
import wt.query.*;
import wt.part.*;
import wt.httpgw.GatewayAuthenticator;
import wt.method.RemoteMethodServer;

public class WtPart_Query8 {
    public static Enumeration findPartsNumbered(String name) throws WTException {

		QuerySpec qs = new QuerySpec(WTPartMaster.class);
		qs.appendSearchCondition(new SearchCondition(WTPartMaster.class,WTPartMaster.NAME,SearchCondition.LIKE,name) );

		final QueryResult qr = PersistenceHelper.manager.find(qs);

		// create a vector of PartMasterInfo object to return to the client
		while (qr.hasMoreElements()) {
			WTPartMaster wtpm = (WTPartMaster)qr.nextElement();
			System.out.println("Name: " + wtpm.getName() + " - Number: " + wtpm.getNumber());
		}

		//qs.appendSearchCondition(new SearchCondition(WTPartMaster.class,WTPartMaster.NAME,SearchCondition.LIKE,number) );

        // the join condition: each part master is associated with its iteration(s)
        //qs.appendSearchCondition(new SearchCondition(WTPartMaster.class, OID, WTPart.class, MASTER), masters, versions);
        //qs.appendAnd();

        // search condition: the part master whose number matches the specified number
        //qs.appendSearchCondition(new SearchCondition(WTPartMaster.class, WTPartMaster.NUMBER, SearchCondition.LIKE, number), masters, NO_JOIN);
        //qs.appendAnd();

        // search condition: the iterations that are the latest of their branch (i.e. it the "versions")
        //qs.appendSearchCondition(new SearchCondition(WTPart.class, Iterated.LATEST_ITERATION, SearchCondition.IS_TRUE), versions, NO_JOIN);

        // the new enumeration of objects constructed from the query result
        //final QueryResult qr = PersistenceHelper.manager.find(qs);

        return new Enumeration() {
            public boolean hasMoreElements() {
                return qr.hasMoreElements();
            }
            public Object nextElement() throws NoSuchElementException {
                return ((Persistable[])qr.nextElement());
            }
        };
    }

    public static void main(String[] args) {
        // test program to query for all versions of a part whose number is specified on the command line and
        // print the identities of these to the console

		RemoteMethodServer rms = RemoteMethodServer.getDefault();
		GatewayAuthenticator auth = new GatewayAuthenticator();
		auth.setRemoteUser("wcadmin");
		rms.setAuthenticator(auth);

        try {
			Enumeration e = findPartsNumbered(args[0]);
			while (e.hasMoreElements()) {
				System.out.println(((WTPart)e.nextElement()).getIdentity());
			}
        } catch (WTException wte) {
            wte.printStackTrace();
        } finally {
            System.exit(0);
        }
    }
}
