
package lj_cust;

import wt.content.ApplicationData;
import wt.content.HolderToContent;
import wt.fc.ObjectIdentifier;
import wt.fc.ObjectReference;
import wt.fc.PersistInfo;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.folder.Cabinet;
import wt.folder.Foldered;
import wt.folder.FolderingInfo;
import wt.httpgw.GatewayAuthenticator;
import wt.method.RemoteMethodServer;
import wt.pds.StatementSpec;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTException;
import wt.part.WTPartMaster;
import wt.part.WTPartAlternateLink;

public class NewTempQuery {

	public static void main(String[] args) {
		try {
			RemoteMethodServer rms = RemoteMethodServer.getDefault();
			GatewayAuthenticator auth = new GatewayAuthenticator();
			auth.setRemoteUser("wcadmin");
			rms.setAuthenticator(auth);

			QueryResult ads = queryAD();
			long sizeInKB = 0;
			String FNM = "---";
			while(ads.hasMoreElements()) {
				System.out.println("sizeInKB: " + sizeInKB);
				sizeInKB += ((WTPartMaster)(((Object[])ads.nextElement())[0])).getFileSizeKB();
				FNM = ((WTPartMaster)(((Object[])ads.nextElement())[0])).getFileName();
				System.out.println("File Name: " + FNM);
			}
			System.out.println("sizeInKB: " + sizeInKB);

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getLocalizedMessage());
		}
		System.exit(0);
	}

	public static QueryResult queryAD() throws WTException {
		QuerySpec qs = new QuerySpec();
		int partIndex = qs.appendClassList(wt.part.WTPartMaster.class, false);
		int alternatePartIndex = qs.appendClassList(wt.part.WTPartMaster.class, false);
		int linkIndex = qs.appendClassList(wt.part.WTPartAlternateLink.class, false);

		// Define the attributes in the query
		ClassAttribute partName =
		new ClassAttribute(wt.part.WTPartMaster.class, wt.part.WTPartMaster.NAME);

		ClassAttribute alternatePartName =
		new ClassAttribute(wt.part.WTPartMaster.class, wt.part.WTPartMaster.NAME);

		ClassAttribute partNumber =
		new ClassAttribute(wt.part.WTPartMaster.class, wt.part.WTPartMaster.NUMBER);

		ClassAttribute alternatePartNumber =
		new ClassAttribute(wt.part.WTPartMaster.class, wt.part.WTPartMaster.NUMBER);

		// Define constants used in the criteria
		ConstantExpression subStringStart = new ConstantExpression(new Long(2));
		ConstantExpression subStringEnd = new ConstantExpression(new Long(4));
		ConstantExpression wildcardExpression = new ConstantExpression("E% [ ]");

		// Add items to the select and join the classes
		qs.appendSelect(partName, new int[] { 0 }, false);
		qs.appendSelect(alternatePartName, new int[] { 1 }, false);
		qs.appendJoin(linkIndex, wt.part.WTPartAlternateLink.ALTERNATES_ROLE, partIndex);
		qs.appendJoin(linkIndex, wt.part.WTPartAlternateLink.ALTERNATE_FOR_ROLE,alternatePartIndex);

		return PersistenceHelper.manager.find((StatementSpec)qs);
	}
}
