
package cust.report;

import java.util.*;
import java.sql.Timestamp;

import wt.viewmarkup.*;
import wt.content.*;
import wt.ownership.*;
import wt.representation.*;

import wt.queue.*;

import wt.locks.Lock;
import wt.locks.Lockable;
import wt.org.WTPrincipal;
import wt.org.WTPrincipalReference;

import wt.inf.container.*;

import wt.fc.*;
import wt.fc.WTObject;
import wt.fc.QueryResult;
import wt.fc.PersistenceHelper;
import wt.fc.PersistenceManager;

import wt.query.QuerySpec;
import wt.query.ArrayExpression;

import com.ptc.wvs.server.cadagent.*;
import com.ptc.wvs.common.ui.*;

import wt.method.RemoteMethodServer;
import wt.httpgw.GatewayAuthenticator;

import wt.util.WTException;

public class pubQue_01e {

	static String div = "\n\n************************************************************\n";
    private static WtQueue selectedQueue = null;
    private static WtQueueEntry selectedEntry = null;

	public static void main(String[] args) throws Exception {
		RemoteMethodServer rms = RemoteMethodServer.getDefault();
		GatewayAuthenticator auth = new GatewayAuthenticator();
		auth.setRemoteUser("wcadmin");
		rms.setAuthenticator(auth);

		new pubQue_01e();
	}

	public pubQue_01e() throws Exception {

		System.out.println("\n");

		// WtQueue.class
		QueryResult resWtQ2=PersistenceHelper.manager.find(new QuerySpec(WtQueue.class));
		while(resWtQ2.hasMoreElements()) {
			WtQueue wtobj=(WtQueue)resWtQ2.nextElement();
			selectedQueue = wtobj;
			showSelectedQueue2();
		}

		// PublishingSummary.class	//wt.viewmarkup.PublishingSummary
		QueryResult resPS=PersistenceHelper.manager.find(new QuerySpec(PublishingSummary.class));
		while(resPS.hasMoreElements()) {

			PublishingSummary wtobj=(PublishingSummary)resPS.nextElement();

			System.out.println("UserName: "+wtobj.getUserName());
			System.out.println("Authoring Application: "+wtobj.getAuthoringApplication());

			System.out.println("Container Name: "+wtobj.getContainerName());
			System.out.println("Container: "+(WTContainer)wtobj.getContainer());
			System.out.println("Container Reference: "+(WTContainerRef)wtobj.getContainerReference());

			System.out.println("Identity: "+wtobj.getIdentity());
			System.out.println("Type: "+wtobj.getType());
			System.out.println("Type Identifier: "+wtobj.getTypeIdentifier());

			System.out.println("PersistInfo: "+(PersistInfo)wtobj.getPersistInfo());

			System.out.println("Job Success: "+(Boolean)wtobj.getJobSuccess());
			System.out.println("Job Submit Time: "+(Timestamp)wtobj.getJobSubmitTime());
			System.out.println("Job End Time: "+(Timestamp)wtobj.getJobEndTime());
			System.out.println("Job Start Time: "+(Timestamp)wtobj.getJobStartTime());
			System.out.println("Job Request Type: "+(Integer)wtobj.getJobRequestType());
			System.out.println("Job Request Source: "+(Integer)wtobj.getJobRequestSource());
			System.out.println("Job Request Queue: "+wtobj.getJobRequestQueue());

			System.out.println("Worker Type: "+wtobj.getWorkerType());
			System.out.println("Worker Name: "+wtobj.getWorkerName());
			System.out.println("Worker Success: "+(Boolean)wtobj.getWorkerSuccess());
			System.out.println("Worker End Exec Time: "+(Timestamp)wtobj.getWorkerEndExecTime());
			System.out.println("Worker End Time: "+(Timestamp)wtobj.getWorkerEndTime());
			System.out.println("Worker Start Exec Time: "+(Timestamp)wtobj.getWorkerStartExecTime());
			System.out.println("Worker Start Time: "+(Timestamp)wtobj.getWorkerStartTime());
			System.out.println("Worker Start Transfer Time: "+(Timestamp)wtobj.getWorkerStartTransferTime());

			System.out.println("\n");
		}
		System.out.println("Number of PublishingSummary Objects: "+resPS.size()+div);

		// WtQueue.class
		QueryResult resWtQ=PersistenceHelper.manager.find(new QuerySpec(WtQueue.class));
		while(resWtQ.hasMoreElements()) {

			WtQueue wtobj=(WtQueue)resWtQ.nextElement();

			System.out.println("Name: "+wtobj.getName());
			System.out.println("Execution Host: "+wtobj.getExecutionHost());
			System.out.println("Queue State: "+wtobj.getQueueState());

			System.out.println("Last State Set: "+wtobj.getLastStateSet());
			System.out.println("Suspend Until: "+(Long)wtobj.getSuspendUntil());
			System.out.println("Suspend Duration: "+(Long)wtobj.getSuspendDuration());
			System.out.println("Failure Notification Email: "+wtobj.getFailureNotificationEmail());
			System.out.println("Last Notification Time: "+(Long)wtobj.getLastNotificationTime());
			System.out.println("Total Entries: "+(Integer)wtobj.getTotalEntries());
			System.out.println("Waiting Entries: "+(Integer)wtobj.getWaitingEntries());
			System.out.println("CacheName: "+wtobj.getCacheName());
			System.out.println("QType: "+wtobj.getQType());

			System.out.println("Enabled: "+(boolean)wtobj.isEnabled());
			System.out.println("Running: "+(boolean)wtobj.isRunning());
			System.out.println("Remove Failed Entires: "+(boolean)wtobj.isRemoveFailedEntires());
			System.out.println("queue Suspended: "+(boolean)wtobj.queueSuspended());
;
			System.out.println("\n");
		}
		System.out.println("Number of WtQueue Objects: "+resWtQ.size()+div);

		// WtQueueEntry.class
		QueryResult resWtQe=PersistenceHelper.manager.find(new QuerySpec(WtQueueEntry.class));
		while(resWtQe.hasMoreElements()) {

			WtQueueEntry wtobj=(WtQueueEntry)resWtQe.nextElement();

			System.out.println("Entry Number: "+wtobj.getEntryNumber());
			System.out.println("Queue Ref: "+wtobj.getQueueRef());
			System.out.println("Entry Owner: "+wtobj.getEntryOwner());
			//System.out.println("User=Principal: "+wtobj.getPrincipal().getName());
			System.out.println("User=Principal: "+(WTPrincipal)wtobj.getPrincipal());

			System.out.println("Target method: "+wtobj.getTargetMethod());
			System.out.println("Target class: "+wtobj.getTargetClass());
			System.out.println("Status: "+wtobj.getStatusInfo().getCode());
			System.out.println("Message: "+wtobj.getStatusInfo().getMessage());
			System.out.println("Reschedule Time: "+wtobj.getStatusInfo().getRescheduleTime());
			System.out.println("Time execution started: "+wtobj.getStartExec());
			System.out.println("Time execution finished: "+wtobj.getEndExec());
			System.out.println("Display String: "+wtobj.getDisplayString());

			System.out.println("\n");
		}
		System.out.println("Number of WtQueueEntry Objects: "+resWtQe.size()+div);

		// ScheduleQueue.class
		QueryResult resSQ=PersistenceHelper.manager.find(new QuerySpec(ScheduleQueue.class));
		while(resSQ.hasMoreElements()) {

			ScheduleQueue wtobj=(ScheduleQueue)resSQ.nextElement();

			System.out.println("Name: "+wtobj.getName());
			//System.out.println("Total Entry Count: "+(Integer)wtobj.getTotalEntryCount());
			//System.out.println("Waiting Entry Count: "+(Integer)wtobj.getWaitingEntryCount());
			System.out.println("Late Entries Count: "+(int)wtobj.getLateEntriesCount());
			System.out.println("Next Process Time: "+(Timestamp)wtobj.getNextProcessTime());

			System.out.println("\n");
		}
		System.out.println("Number of ScheduleQueue Objects: "+resSQ.size()+div);

		// ScheduleQueueEntry.class
		QueryResult resSQE=PersistenceHelper.manager.find(new QuerySpec(ScheduleQueueEntry.class));
		while(resSQE.hasMoreElements()) {

			ScheduleQueueEntry wtobj=(ScheduleQueueEntry)resSQE.nextElement();

			System.out.println("Schedule Time: "+(Timestamp)wtobj.getScheduleTime());
			System.out.println("User=Principal: "+(WTPrincipal)wtobj.getPrincipal());
			System.out.println("Queue: "+(ScheduleQueue)wtobj.getQueue());
			System.out.println("Display String: "+wtobj.getDisplayString());
			System.out.println("Locked: "+(boolean)wtobj.isLocked());

			boolean tstbool = (boolean)wtobj.isLocked();

			if(tstbool) {
				System.out.println("Lock Date: "+(Timestamp)wtobj.getLockDate());
				System.out.println("Lock Note: "+wtobj.getLockNote());
				System.out.println("Locker: "+(WTPrincipalReference)wtobj.getLocker());
				System.out.println("Locker EMail: "+wtobj.getLockerEMail());
				System.out.println("Locker Name: "+wtobj.getLockerName());
				System.out.println("Locker Full Name: "+wtobj.getLockerFullName());
				System.out.println("Lock: "+(Lock)wtobj.getLock());
			}
			System.out.println("\n");
		}
		System.out.println("Number of ScheduleQueueEntry Objects: "+resSQE.size()+div);

		// QueueEntry.class
		QueryResult resQE=PersistenceHelper.manager.find(new QuerySpec(QueueEntry.class));
		while(resQE.hasMoreElements()) {

			QueueEntry wtobj=(QueueEntry)resQE.nextElement();

			System.out.println("Entry Number: "+wtobj.getEntryNumber());
			System.out.println("Queue Ref: "+wtobj.getQueueRef());
			System.out.println("Entry Owner: "+wtobj.getEntryOwner());
			//System.out.println("User=Principal: "+(WTPrincipal)wtobj.getPrincipal());
			System.out.println("User=Principal: "+wtobj.getPrincipal().getName());

			System.out.println("Target method: "+wtobj.getTargetMethod());
			System.out.println("Target class: "+wtobj.getTargetClass());
			System.out.println("Status: "+wtobj.getStatusInfo().getCode());
			System.out.println("Message: "+wtobj.getStatusInfo().getMessage());
			System.out.println("Reschedule Time: "+wtobj.getStatusInfo().getRescheduleTime());
			System.out.println("Time execution started: "+wtobj.getStartExec());
			System.out.println("Time execution finished: "+wtobj.getEndExec());

			//System.out.println("Display String: "+wtobj.getDisplayString());

			System.out.println("Processing Queue: "+(ProcessingQueue)wtobj.getQueue());
			System.out.println("Locked: "+(boolean)wtobj.isLocked());

			boolean tstbool = (boolean)wtobj.isLocked();

			if(tstbool) {
				System.out.println("Lock Date: "+(Timestamp)wtobj.getLockDate());
				System.out.println("Lock Note: "+wtobj.getLockNote());
				System.out.println("Locker: "+(WTPrincipalReference)wtobj.getLocker());
				System.out.println("Locker EMail: "+wtobj.getLockerEMail());
				System.out.println("Locker Name: "+wtobj.getLockerName());
				System.out.println("Locker Full Name: "+wtobj.getLockerFullName());
				System.out.println("Lock: "+(Lock)wtobj.getLock());
			}

			System.out.println("\n");
		}
		System.out.println("Number of QueueEntry Objects: "+resQE.size()+div);
	}

	private static int showQueues2(Class class1, int i) throws WTException {
		int j = i;
		for(Enumeration enumeration = QueueHelper.manager.queues(class1); enumeration.hasMoreElements();) {
			WtQueue wtqueue = (WtQueue)enumeration.nextElement();
			String s = isActive(class1, wtqueue);
			String s1 = wtqueue.getExecutionHost();
			String s2 = "											";
			String s3 = wtqueue.getName();
			//s3 = s3.concat(s2.substring(0, 35 - s3.length()));

			if(class1 == (wt.queue.ProcessingQueue.class)) {
				long l = QueueHelper.manager.getInterval((ProcessingQueue)wtqueue);
				System.out.println(" " + ++j + ". " + s3 + " (" + s + ") execution group (" + (s1 != null ? s1 : "default") + ") interval = " + l);
			} else {
				System.out.println(" " + ++j + ". " + s3 + " (" + s + ") execution group (" + (s1 != null ? s1 : "default") + ")");
			}
		}

		System.out.println("There are no queues to show.");
		return j;
	}

	private static String isActive(Class class1, WtQueue wtqueue) {
		boolean flag;
		try {
			if(class1 == (wt.queue.ProcessingQueue.class))
				flag = QueueHelper.manager.isActive((ProcessingQueue)wtqueue);
			else
				flag = QueueHelper.manager.isActive((ScheduleQueue)wtqueue);
		}
		catch(WTException wtexception) {
			flag = false;
			wtexception.printStackTrace();
		}
		return flag ? "active" : "not active";
	}

	private static void selectQueue2() {
		System.out.println("\n");
		System.out.println("\tSelect queue");
		System.out.println("\t------------\n");
		Vector vector;
		int i;
		int j;
		try {
			vector = getAllQueues();
			Enumeration enumeration = vector.elements();
			i = 0;
			WtQueue wtqueue;
			for(; enumeration.hasMoreElements(); System.out.println(" " + ++i + ". " + wtqueue))
			wtqueue = (WtQueue)enumeration.nextElement();

			if(i == 0) {
				System.out.println("There are no queues from which to select.");
				return;
			}
		}
		catch(WTException wtexception) {
			System.out.println(wtexception.getLocalizedMessage());
			return;
		}
		System.out.println("\n");
		j = getChoice(1, i);
		selectedQueue = (WtQueue)vector.elementAt(j - 1);
	}

	private static void showSelectedQueue2() {
		if(selectedQueue == null) {
			System.out.println("No queue selected.");
			return;
		}
		System.out.println("\n");
		System.out.println("\tSelected queue");
		System.out.println("\t--------------");
		System.out.println("\tName: " + selectedQueue.getName());
		System.out.println("\nEntry Status");
		System.out.println("------------");
		for(int i = 0; i < statusCodeArray.length; i++)
			System.out.println((i + 1) + ". " + statusCodeArray[i]);

		System.out.println("\n");
		int j = getChoice(1, statusCodeArray.length);
		System.out.println("\n");
		System.out.println("Entries");
		System.out.println("-------");
		try {
			Enumeration enumeration;
			if(j == 1) {
				if(selectedQueue instanceof ProcessingQueue)
					enumeration = QueueHelper.manager.queueEntries((ProcessingQueue)selectedQueue);
				else
					enumeration = QueueHelper.manager.queueEntries((ScheduleQueue)selectedQueue);
			} else
			if(selectedQueue instanceof ProcessingQueue)
				enumeration = QueueHelper.manager.queueEntries((ProcessingQueue)selectedQueue, statusCodeArray[j - 1]);
			else
				enumeration = QueueHelper.manager.queueEntries((ScheduleQueue)selectedQueue, statusCodeArray[j - 1]);
			if(!enumeration.hasMoreElements()) {
				System.out.println("No entries to show.");
			} else {
				WtQueueEntry wtqueueentry;
				for(; enumeration.hasMoreElements(); System.out.println(wtqueueentry.getDisplayString()))
					wtqueueentry = (WtQueueEntry)enumeration.nextElement();
			}
		}
		catch(WTException wtexception) {
			System.out.println(wtexception.getLocalizedMessage());
			return;
		}
	}
}