WBSProjectChilds

using System;  using TestFramework.ApplicationAPI; using TestFramework.AuthenticateAPI;
using TestFramework;  namespace Tests.wbs { 	/// <summary>
	/// Summary description for WBSProjectChilds.
	/// 
	/// this sample creates child tasks and milestones and assigns 
	/// them to a new summary task. This summary task is assigned 
	/// to a new deliverable. This deliverable is assigned to a 
	/// project loaded from RPM repository.
	/// </summary>
	public class WBSProjectChilds
	{
		public WBSProjectChilds()
		{

		}

		// these are all variables that are used in this sample
		public static String projectName = "IBM_GENERIC_PROJECT_ABC";
		public static String deliverableName = "IBM_DELIVERABLE";
		public static String sumTaskName = "IBM_SUMMARY_TASK";
		public static String taskName = "IBM_CHILD_TASK";
		public static String milestoneName = "IBM_MILESTONE";
		public static String wProductName = "IBM_WORK_PRODUCT";
		
		//Session ID is use for all transaction and is get from the 
		//Authenticate.login function. The session ID will replace 
		// the User password while transacting
		public static String sessionid;

		public void sampleTest() 
		{

			// initialize a session with the API
			sessionid = APISetup.SetUp();

			// create a save result object 
			SaveResult save = null;

			// create a project scope
			// this scope enables us to work with a project and 
			// any child object we wish to use
			WorkElementScope weScope = new WorkElementScope();

			// initialize the project scope to have access to 
			// the parent if needed
			weScope.parent = new WorkElementScope();

			// create a project object
			Project project = null;

			// query the project
			project = (Project)APISetup.application.loadFromXpath(
				sessionid, "/Project[name='" + projectName + "']", 
				weScope ).rpmObjectList[0];

			// create child objects needed
			Deliverable deliverable = new Deliverable();

			SummaryTask sumTask = new SummaryTask();

			Task task = new Task();

			Milestone milestone = new Milestone();

			WorkProduct wProduct = new WorkProduct();
			
			// initialize the deliverable
			// assign the deliverable to the project
			deliverable.parent = project;
			deliverable.name = deliverableName;

			// save the deliverable
			deliverable = (Deliverable)saveRPMObject(deliverable);

			// initialize the work product
			// assign a work product to the project
			wProduct.parent = project;
			wProduct.name = wProductName;

			// save the work product
			wProduct = (WorkProduct)saveRPMObject(wProduct);

			// initialize the summary task
			// assign a summary task to a deliverable
			sumTask.parent = deliverable;
			sumTask.name = sumTaskName;
			
			// save the summary task
			sumTask = (SummaryTask)saveRPMObject(sumTask);

			// initialize the task
			// assign a task to a summary task
			task.parent = sumTask;
			task.name = taskName;
			// for a task to be saved
			// it has to have a planned start date
			task.expectedDate = new WbsScheduleDate();
			task.expectedDate.startDate = 
				new DateTime(2005,10,10,10,10,10,10);

			//TODO : cannot save task
			// save the task object
			task = (Task)saveRPMObject(task);
			
			// initialize the milestone
			// assign a milestone to a summary task
			milestone.parent = sumTask;
			milestone.name = milestoneName;

			// save a milestone object
			milestone = (Milestone)saveRPMObject(milestone);

			// save the project into the RPM repository
			// the scope is necessary to save changes with link 
			// to the parent
			save = new SaveResult();
			save = APISetup.application.save(sessionid, project,
				weScope, ReloadType.None);
			APISetup.checkForErrors( save );

			// close connection with API
			APISetup.CleanUp(sessionid);
		}

		/** RPMObject saveWBSObject
		 * 
		 * simple fonction used to save any RPMObject with no Scope 
		 * returns the object after reloading it
		 */
		public RPMObject saveRPMObject(RPMObject obj)
		{

			SaveResult save = new SaveResult();
			save = APISetup.application.save(sessionid, obj, 
				null,ReloadType.ReloadResult);
			APISetup.checkForErrors( save );
			obj = (RPMObject)save.rpmObject;

			return obj;
		}

	}
}