package ext.publishing;

import com.ptc.wvs.server.util.PublishUtils;
import wt.viewmarkup.DerivedImage;

import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.fc.PersistenceServerHelper;
import wt.fc.QueryResult;
import wt.fc.ObjectVector;
import wt.fc.ObjectVectorIfc;
import wt.vc.VersionControlHelper;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.epm.*;
import wt.doc.*;
import wt.epm.EPMAuthoringAppType;
import wt.representation.Representation;
import wt.inf.container.*;
import wt.vc.config.ConfigHelper;
import wt.vc.config.LifeCycleConfigSpec;
import wt.lifecycle.State;
import wt.epm.EPMDocConfigSpec;
import wt.lifecycle.*;
import wt.vc.config.LatestConfigSpec;
import wt.content.ContentHolder;
import wt.content.ContentHelper;
import wt.content.ContentItem;
import wt.content.ApplicationData;


  

 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);
    }
    
    
  }