package examples;

import wt.doc.*;
import wt.query.*;
import wt.fc.*;
import wt.folder.*;
import wt.method.*;
import wt.vc.wip.*;
import wt.org.*;
import wt.admin.*;
import wt.team.*;
import java.util.*;
import wt.vc.struct.*;
import wt.enterprise.*;
import wt.vc.*;
import wt.part.*;
import wt.vc.*;

public class Rollback implements RemoteAccess{

	public static void main(String [] args) throws Exception
	{
		if(args.length < 5)
		{
			System.out.println("Usage :--> windchill examples.Rollback <admin_name> <admin_pswd> <From> <To> <number>");
		}
		else
		{
			try{
				RemoteMethodServer rms = RemoteMethodServer.getDefault();

				//This is so that it does not prompt for the user login
				rms.setUserName(args[0]);
				rms.setPassword(args[1]);
				String from =args[2];
				String to = args[3];
				String number =args[4];
				/*
				Class[] rmiArgTypes = new Class [1];
				rmiArgTypes[0]=java.lang.String.class;

				String[] rmiArgs = new String[3];
				rmiArgs[0]=from;
				rmiArgs[1]=to;
				rmiArgs[3]=number;
				rms.invoke("doRoll", "examples.Rollback", null,rmiArgTypes,rmiArgs);
				*/
				doRoll(from,to,number);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	public static void doRoll(String from,String to,String number) throws Exception
	{
		try
		{
			//Find the appropriate doc here
			WTProduct MyProduct = new WTProduct();
			WTProduct MyProduct_from = new WTProduct();
			WTProduct MyProduct_to = new WTProduct();

			QuerySpec qs = new QuerySpec(WTProduct.class);

			qs.appendWhere(new SearchCondition(WTProduct.class, "master>number", SearchCondition.EQUAL, number));
			QueryResult qr = PersistenceHelper.manager.find(qs);

			while (qr.hasMoreElements())
			{
				MyProduct = (WTProduct)qr.nextElement();

				if(MyProduct.getIterationInfo().getIdentifier().getValue().compareToIgnoreCase(from)==0)
				{
					MyProduct_from = MyProduct;
				}
				if(MyProduct.getIterationInfo().getIdentifier().getValue().compareToIgnoreCase(to)==0)
				{
					MyProduct_to = MyProduct;
				}
			}

			System.out.println("Found Product_from with Value: " + MyProduct_from.getIterationInfo().getIdentifier().getValue());
			System.out.println("Found Product_to with Value: " + MyProduct_to.getIterationInfo().getIdentifier().getValue());

			Iterated myIterated = VersionControlHelper.service.rollback(MyProduct_from,MyProduct_to);

		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
