/***************************************************/
/* Description: Custom Publish Methods             */
/***************************************************/
/*
Configure and add to site.xconf:
/*
<Property name="schedulejobs19" overridable="true" targetFile="codebase/wvs.properties" value="publishAllLatestVersionReleasedDrawingsNoRep"/>
<Property name="publishAllLatestVersionReleasedDrawingsNoRep.class" overridable="true" targetFile="codebase/wvs.properties" value="ext.publishing.CustomScheduleJobs"/>
<Property name="publishAllLatestVersionReleasedDrawingsNoRep.description" overridable="true" targetFile="codebase/wvs.properties" value="publishAllLatestVersionReleasedDrawingsNoRep"/>
<Property name="publishAllLatestVersionReleasedDrawingsNoRep.method" overridable="true" targetFile="codebase/wvs.properties" value="allLatestVersionReleasedDrawingsNoRep"/>
<Property name="publishAllLatestVersionReleasedDrawingsNoRep.enableOnContainers" overridable="true" targetFile="codebase/wvs.properties" value="false"/>

compile:
C:\j2sdk1_4\bin\javac -d d:\ptc\windchill\codebase D:\ptc\Windchill\src\ext\publishing\CustomScheduleJobs.java

================================
Replace above

site.xconf

<Property name="schedulejobs19" overridable="true" targetFile="codebase/wvs.properties" value="publishAllLatestVersionPrototypeDrawingsNoRep"/>
<Property name="publishAllLatestVersionPrototypeDrawingsNoRep.class" overridable="true" targetFile="codebase/wvs.properties" value="ext.publishing.CustomScheduleJobs"/>
<Property name="publishAllLatestVersionPrototypeDrawingsNoRep.description" overridable="true" targetFile="codebase/wvs.properties" value="publishAllLatestVersionPrototypeDrawingsNoRep"/>
<Property name="publishAllLatestVersionPrototypeDrawingsNoRep.method" overridable="true" targetFile="codebase/wvs.properties" value="allLatestVersionPrototypeDrawingsNoRep"/>
<Property name="publishAllLatestVersionPrototypeDrawingsNoRep.enableOnContainers" overridable="true" targetFile="codebase/wvs.properties" value="false"/>

compile
javac -d d:\ptc\windchill\codebase D:\ptc\Windchill\codebase\ext\publishing\CustomScheduleJobs.java

xconfmanager -p
*/

package ext.publishing;

import com.ptc.wvs.server.util.PublishUtils;
import wt.content.ApplicationData;
import wt.content.ContentHelper;
import wt.content.ContentHolder;
import wt.content.ContentItem;
import wt.doc.*;
import wt.epm.*;
import wt.epm.EPMAuthoringAppType;
import wt.epm.EPMDocConfigSpec;
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.inf.container.*;
import wt.lifecycle.*;
import wt.lifecycle.State;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.representation.Representation;
import wt.vc.VersionControlHelper;
import wt.vc.config.ConfigHelper;
import wt.vc.config.LatestConfigSpec;
import wt.vc.config.LifeCycleConfigSpec;
import wt.viewmarkup.DerivedImage;

public class CustomScheduleJobs {

	//Publish all RELEASED versions of an object

	public static QueryResult allLatestVersionReleasedDrawingsNoRep()
	{
		ObjectVector objectvector = new ObjectVector();

		try
		{
			QuerySpec queryspec = new QuerySpec(wt.epm.EPMDocumentMaster.class);
			queryspec.appendSearchCondition(new SearchCondition(wt.epm.EPMDocumentMaster.class, wt.epm.EPMDocumentMaster.NUMBER, SearchCondition.LIKE, "%.DRW"));

			//Following section for container support
			/* Does not work; probably because of EPMDocumentMaster instead of EPMDocument in searchcondition
			WTContainerRef wtcontainerref = com.ptc.wvs.server.schedule.ScheduleJobs.getCurrentContainer();
			if(wtcontainerref != null)
			{
				System.out.println("All Latest versions of Released EPMDocs in Container: " + wtcontainerref.getName());
				ContainerSpec containerspec = new ContainerSpec();
				containerspec.addSearchContainer(wtcontainerref);
				queryspec.setAdvancedQueryEnabled(true);
				queryspec.appendWhere(WTContainerHelper.getWhereContainerIn(containerspec, wt.epm.EPMDocumentMaster.class), new int[] {
					0
				});
			} //end container support
			*/

			QueryResult queryresult = PersistenceHelper.manager.find(queryspec);
			do {
				if(!queryresult.hasMoreElements())
					break;
				EPMDocumentMaster epmdocumentmaster = (EPMDocumentMaster)queryresult.nextElement();

				EPMDocConfigSpec epmConfigSpec = EPMDocConfigSpec.newEPMDocConfigSpec();
				epmConfigSpec.setLifeCycle(LifeCycleConfigSpec.newLifeCycleConfigSpec(State.RELEASED));
				epmConfigSpec.setLifeCycleActive(true);

				QueryResult qr1 = ConfigHelper.service.filteredIterationsOf( epmdocumentmaster, epmConfigSpec );

				if(qr1.hasMoreElements()){
					EPMDocument epmdocument =(EPMDocument)qr1.nextElement();
					LifeCycleManaged LMObject = (LifeCycleManaged)epmdocument;
					System.out.println("STATE:"+(String)LMObject.getLifeCycleState().toString() );
					Representation representation = PublishUtils.getRepresentation(epmdocument);
					if(representation == null)
						objectvector.addElement(epmdocument);
				}
			} while(true);
		}
		catch(Exception exception)
		{
			exception.printStackTrace();
			objectvector = new ObjectVector();
		}
		return new QueryResult(objectvector);
	}

	//Publish all PROTOTYPE versions of an object

	public static QueryResult allLatestVersionPrototypeDrawingsNoRep()
	{
		ObjectVector objectvector = new ObjectVector();

		try
		{
			QuerySpec queryspec = new QuerySpec(wt.epm.EPMDocumentMaster.class);
			queryspec.appendSearchCondition(new SearchCondition(wt.epm.EPMDocumentMaster.class, wt.epm.EPMDocumentMaster.NUMBER, SearchCondition.LIKE, "%.DRW"));

			QueryResult queryresult = PersistenceHelper.manager.find(queryspec);
			do {
				if(!queryresult.hasMoreElements())
					break;
				EPMDocumentMaster epmdocumentmaster = (EPMDocumentMaster)queryresult.nextElement();

				EPMDocConfigSpec epmConfigSpec = EPMDocConfigSpec.newEPMDocConfigSpec();
				epmConfigSpec.setLifeCycle(LifeCycleConfigSpec.newLifeCycleConfigSpec(State.PROTOTYPE));
				epmConfigSpec.setLifeCycleActive(true);

				QueryResult qr1 = ConfigHelper.service.filteredIterationsOf( epmdocumentmaster, epmConfigSpec );

				if(qr1.hasMoreElements()){
					EPMDocument epmdocument =(EPMDocument)qr1.nextElement();
					LifeCycleManaged LMObject = (LifeCycleManaged)epmdocument;
					System.out.println("STATE:"+(String)LMObject.getLifeCycleState().toString() );
					Representation representation = PublishUtils.getRepresentation(epmdocument);
					if(representation == null)
						objectvector.addElement(epmdocument);
				}
			} while(true);
		}
		catch(Exception exception)
		{
			exception.printStackTrace();
			objectvector = new ObjectVector();
		}
		return new QueryResult(objectvector);
	}
}
