
package lj_cust;

import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.io.Serializable;
import java.util.Vector;

import wt.util.WTContext;
import wt.method.RemoteMethodServer;
import wt.method.RemoteAccess;

import wt.fc.QueryResult;
import wt.fc.PersistenceHelper;
import wt.fc.PersistenceManager;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.query.QuerySpec;
import wt.query.SearchCondition;

public class Que_Test_03 {

	// Class name of our inner class that runs in the server
	private static final String SERVER_CLASS = Que_Test_03.class.getName() + "$Server";

	public static void main(String[] args) {

		Vector results = null;
		String like = "% [ ]"+name.toUpperCase()+"% [ ]";
		int count = 0;

		String name;
		String partNumber;

		try
		{
			Integer cnt = null;
			try {
				cnt = new Integer(count);
			}
			catch (Exception e) {
				try {
					cnt = new Integer(5); // this will work
				}
				catch (Exception e2){}
			}

			// construct arguments for call
			Class [] argTypes = { String.class, Integer.TYPE };
			Object [] Oargs = { like, cnt };

			results = (Vector) RemoteMethodServer.getDefault().invoke("doSomeWork", SERVER_CLASS, null, argTypes, Oargs);

			// display results in text area
			for (int i=0;i < results.size(); i++) {
				PartMasterInfo pmi = (PartMasterInfo)results.elementAt(i);
				System.out.println("> "+pmi.getName()+" # "+pmi.getNumber()+"\n");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getLocalizedMessage());
		}

		int i=0;
		Vector parts = new Vector(count);
		WTPartMaster wtpm;

		try {
			// Use feedback mechanism to send progress updates
			// to the user and of course be sure to Localize it
			QuerySpec queryspec = new QuerySpec(WTPartMaster.class);
			queryspec.appendSearchCondition(
			new SearchCondition(WTPartMaster.class,WTPartMaster.NAME,SearchCondition.LIKE,name) );

			QueryResult queryresult = PersistenceHelper.manager.find(queryspec);

			// create a vector of PartMasterInfo object to return to the client
			while (queryresult.hasMoreElements()) {
				wtpm = (WTPartMaster)queryresult.nextElement();
				parts.addElement(new PartMasterInfo(wtpm.getName(), wtpm.getNumber()));
				if (++i >= count)
				break;
			}
		}
		catch (Exception e) {
			parts.addElement(new PartMasterInfo(e.toString(),"-1"));
			e.printStackTrace();
		}
		//return parts;
	}

	public static class PartMasterInfo implements Serializable {

		public PartMasterInfo( String name, String number ) {
			this.name = name;
			partNumber = number;
		}

		public String getName() { return name; }
		public String getNumber() { return partNumber; }
	}


	//System.exit(0);
}
