
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;

public class TempQuery {

	private static final String OID = Persistable.PERSIST_INFO + "." + PersistInfo.OBJECT_IDENTIFIER + "." + ObjectIdentifier.ID;
	private static final String HTC_TO_APP_DATA_REF = HolderToContent.ROLE_BOBJECT_REF + "." + ObjectReference.KEY + "." + ObjectIdentifier.ID;
	private static final String HTC_TO_HOLDER_REF = HolderToContent.ROLE_AOBJECT_REF + "." + ObjectReference.KEY + "." + ObjectIdentifier.ID;
	private static final String CABINET_BASED_TO_CAB_REF = Foldered.FOLDERING_INFO + "." + FolderingInfo.CABINET + "." + ObjectReference.KEY + "." + ObjectIdentifier.ID;

	public static void main(String[] args) {
		try {
			RemoteMethodServer rms = RemoteMethodServer.getDefault();
			GatewayAuthenticator auth = new GatewayAuthenticator();
			auth.setRemoteUser("wcadmin");
			rms.setAuthenticator(auth);

			QueryResult ads = queryAD("Default");
			long sizeInKB = 0;
			String FNM = "---";
			while(ads.hasMoreElements()) {
				System.out.println("sizeInKB: " + sizeInKB);
				sizeInKB += ((ApplicationData)(((Object[])ads.nextElement())[0])).getFileSizeKB();
				FNM = ((ApplicationData)(((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(String cabinetName) throws WTException {
		SearchCondition join;
		SearchCondition where;

		QuerySpec qs = new QuerySpec();

		int adAppendIndex = qs.appendClassList(ApplicationData.class,true);
		int htcAppendIndex = qs.appendClassList(HolderToContent.class,false);
		int chAppendIndex = qs.appendClassList(Foldered.class,false);	//ContentHolder.class
		int cabinetAppendIndex = qs.appendClassList(Cabinet.class,false);

		join = new SearchCondition(ApplicationData.class, OID, HolderToContent.class, HTC_TO_APP_DATA_REF);
		qs.appendWhere(join, new int[] {adAppendIndex, htcAppendIndex});

		qs.appendAnd();
		join = new SearchCondition(Foldered.class, OID, HolderToContent.class, HTC_TO_HOLDER_REF);
		qs.appendWhere(join, new int[] {chAppendIndex, htcAppendIndex});

		qs.appendAnd();
		join = new SearchCondition(Cabinet.class,OID,Foldered.class, CABINET_BASED_TO_CAB_REF);
		qs.appendWhere(join,new int[]{cabinetAppendIndex,chAppendIndex});

		qs.appendAnd();
		where = new SearchCondition(Cabinet.class,Cabinet.NAME,SearchCondition.EQUAL,cabinetName);
		qs.appendWhere(where,new int[]{cabinetAppendIndex});

		return PersistenceHelper.manager.find((StatementSpec)qs);
	}
}
