// from [c/d]:/ptc/Windchill (location wc shell opens to) with java in src/ext/
// ant -f bin/tools.xml class -Dclass.includes=ext/publishing/PublishDRW_04.java
// java ext.publishing.PublishDRW_04>D:\ptc\Windchill\src\ext\output\PublishDRW_04-102713_01.txt

package ext.publishing;

import java.util.*;
import java.rmi.RemoteException;
import java.io.*;

import com.ptc.wvs.server.util.PublishUtils;

import wt.doc.*;
import wt.epm.*;
import wt.part.*;
import wt.util.*;
import wt.fc.ObjectVector;
import wt.fc.ObjectVectorIfc;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.fc.PersistenceServerHelper;
import wt.fc.QueryResult;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.representation.Representation;
import wt.vc.VersionControlHelper;
import wt.content.*;

import wt.httpgw.GatewayAuthenticator;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;

import wt.representation.*;

/*

Add to site.xconf or additional xconf:

<!-- Schedule Publishing No Rep EpmDocs -->

<Property name="schedulejobs21" overridable="true" targetFile="codebase/wvs.properties" value="pubEpmNoRep500"/>
<Property name="pubEpmNoRep500.class" overridable="true" targetFile="codebase/wvs.properties" value="ext.publishing.PublishDRW_04"/>
<Property name="pubEpmNoRep500.description" overridable="true" targetFile="codebase/wvs.properties" value="PublishJobs_EPM_NoRep_500"/>
<Property name="pubEpmNoRep500.method" overridable="true" targetFile="codebase/wvs.properties" value="epm500"/>
<Property name="pubEpmNoRep500.enableOnContainers" overridable="true" targetFile="codebase/wvs.properties" value="true"/>

wvs.properties.xconf:

<!-- Schedule Publishing No Rep EpmDocs -->

<Property default="pubEpmNoRep500" name="schedulejobs21"/>
<Property default="pubEpmNoRep500" name="pubEpmNoRep500.description"/>
<Property default="ext.publishing.PublishDRW_04" name="pubEpmNoRep500.class"/>
<Property default="epm500" name="pubEpmNoRep500.method"/>
<Property default="true" name="pubEpmNoRep500.enableOnContainers"/>

500,ALL
REP,NOREP
DRW,PLT
LATEST,ALLVERS

ALL		NOREP	DRW		ALLVERS
ALL		NOREP	DRW		LATEST

ALL		REP		DRW		ALLVERS
ALL		REP		DRW		LATEST

ALL		NOREP	PLT		ALLVERS
ALL		NOREP	PLT		LATEST

ALL		REP		PLT		ALLVERS
ALL		REP		PLT		LATEST

500		NOREP	DRW		ALLVERS
500		NOREP	DRW		LATEST

500		REP		DRW		ALLVERS
500		REP		DRW		LATEST

500		NOREP	PLT		ALLVERS
500		NOREP	PLT		LATEST

500		REP		PLT		ALLVERS
500		REP		PLT		LATEST

*/

public class PublishDRW_04 {

	public static QueryResult drwAllRepLat()		{ QueryResult result = DRW_Run(-1,"drw","rep","latest"); return result; }
	public static QueryResult drw500RepLat()		{ QueryResult result = DRW_Run(500,"drw","rep","latest"); return result; }
	public static QueryResult drwAllNoRepLat()		{ QueryResult result = DRW_Run(-1,"drw","norep","latest"); return result; }
	public static QueryResult drw500NoRepLat()		{ QueryResult result = DRW_Run(500,"drw","norep","latest"); return result; }

	public static QueryResult drwAllRepVers()		{ QueryResult result = DRW_Run(-1,"drw","rep","version"); return result; }
	public static QueryResult drw500RepVers()		{ QueryResult result = DRW_Run(500,"drw","rep","version"); return result; }
	public static QueryResult drwAllNoRepVers()		{ QueryResult result = DRW_Run(-1,"drw","norep","version"); return result; }
	public static QueryResult drw500NoRepVers()		{ QueryResult result = DRW_Run(500,"drw","norep","version"); return result; }

	public static QueryResult pltAllRepLat()		{ QueryResult result = DRW_Run(-1,"plt","rep","latest"); return result; }
	public static QueryResult plt500RepLat()		{ QueryResult result = DRW_Run(500,"plt","rep","latest"); return result; }
	public static QueryResult pltAllNoRepLat()		{ QueryResult result = DRW_Run(-1,"plt","norep","latest"); return result; }
	public static QueryResult plt500NoRepLat()		{ QueryResult result = DRW_Run(500,"plt","norep","latest"); return result; }

	public static QueryResult pltAllRepVers()		{ QueryResult result = DRW_Run(-1,"plt","rep","version"); return result; }
	public static QueryResult plt500RepVers()		{ QueryResult result = DRW_Run(500,"plt","rep","version"); return result; }
	public static QueryResult pltAllNoRepVers()		{ QueryResult result = DRW_Run(-1,"plt","norep","version"); return result; }
	public static QueryResult plt500NoRepVers()		{ QueryResult result = DRW_Run(500,"plt","norep","version"); return result; }

	public static QueryResult ljett()				{ QueryResult result = DRW_Run(500,"ljett","rep","version"); return result; }

	public static QueryResult DRW_Run( int qtyRun, String repType, String Rep, String Vers ) {
		ObjectVector objVector = new ObjectVector();
		int qrCnt=0;

		System.out.println("Run NO: "+qtyRun+" - repType: "+repType+" - Rep: "+Rep+" - Vers:"+Vers);

		try {
			QuerySpec qs = new QuerySpec(wt.epm.EPMDocument.class);

			if ( repType.equals("ljett") ) {	// LJETT DRW'S
				qs.appendSearchCondition(new SearchCondition(wt.epm.EPMDocument.class, wt.epm.EPMDocument.NUMBER, SearchCondition.LIKE, "LJETT%"));
			}
			else {
				qs.appendSearchCondition(new SearchCondition(wt.epm.EPMDocument.class, wt.epm.EPMDocument.NUMBER, SearchCondition.LIKE, "%"));
			}

			if ( Vers.equals("latest") ) {	// LATEST or VERS
				qs.appendAnd();
				qs.appendWhere(new SearchCondition(EPMDocument.class, "iterationInfo.latest", "TRUE"));
			}

			QueryResult qr = PersistenceHelper.manager.find(qs);

			while (qr.hasMoreElements()) {
				EPMDocument epm = (EPMDocument)qr.nextElement();
				EPMDocumentType epmTyp = epm.getDocType();
				String epmTypStg = epmTyp.toString();

				System.out.println("qtyRun: "+qtyRun+"|Publishing - EPMDoc_NO:"+ epm.getNumber()+"|Vers:" + epm.getVersionIdentifier().getValue()+"."+epm.getIterationIdentifier().getValue()+"|RepVers:"+PublishUtils.getIterationIdentifier(epm));

				if(epmTypStg.equals("CADDRAWING")){
					Representation defaultRep=RepresentationHelper.service.getDefaultRepresentation(epm);
					ContentHolder holder=ContentHelper.service.getContents(defaultRep);
					java.util.Vector contents=null;

					//System.out.println( "\n===========\n1)NO of content items = " +contents.size());

					contents=ContentHelper.getContentListAll(holder);
					ApplicationData data=null;

					System.out.println( "2)NO of content items = " +contents.size());

					for (int i=0;i<contents.size();i++) {
						if (contents.get(i) instanceof ApplicationData) {
							data=(ApplicationData)contents.get(i);
							if (data!=null) {
								System.out.println( "data.getFileName(): "+data.getFileName());
								if ( qtyRun > 0 && qrCnt<qtyRun ) {
									if ( data.getFileName().endsWith("plt") && repType.equals("plt") ) {
										objVector.addElement(epm);
										System.out.println( "data.getFileName(): "+data.getFileName());
										qrCnt++;
									}
									else if ( data.getFileName().endsWith("pdf") && repType.equals("drw") ) {
										objVector.addElement(epm);
										System.out.println( "data.getFileName(): "+data.getFileName());
										qrCnt++;
									}
									System.out.println( "1)qrCnt: "+qrCnt+"\n===========\n");
								}
								else {
									objVector.addElement(epm);
								}
							}
						}
					}
					System.out.println( "2)qrCnt: "+qrCnt+"\n===========\n");
				}
			}
		}
		catch(Exception exception) { objVector = new ObjectVector(); }
		System.out.println( "objVector.size(): "+objVector.size());
		return new QueryResult(objVector);
	}

	public static void main(String[] args) {
		try {
			RemoteMethodServer rms = RemoteMethodServer.getDefault();
			GatewayAuthenticator auth = new GatewayAuthenticator();
			auth.setRemoteUser("wcadmin");
			rms.setAuthenticator(auth);

			//QueryResult qrTest = PublishDRW_04.drwAllRepLat();
			QueryResult qrTest = PublishDRW_04.ljett();
			System.out.println("qrTest.size(): "+qrTest.size());
		}
		catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	public static String Usage = "\nUsage:\n\njava/windchill [-mx1024m] "+
	"ext.LCS_CHG %<wtobject number search string>%\n\n"+
	"1st Argument = %<object number string>%\n\n"+"Should include % where % = * = wild card.\n";
}
