ResourceSample

using System;  using TestFramework.ApplicationAPI; using TestFramework.AuthenticateAPI;
using TestFramework;  namespace Tests.resource  { 	/// <summary>
	/// Summary description for ResourceSample.
	/// 
	/// ResourceSample tries to Update a resource with a certain
	///  external id in the database, 
	///  if the resource doesn't exist, it creates it.
	/// </summary>
	public class ResourceSample
	{

		//All these variables are used in this sample
		public static String resourceExternalID = "testABCDEF";
		public static String poolName = "POOL1_testA";
		public static String resourceName = "IBM_testABCDEF";
		public static String userid = "IBM_USER";
		public static String passid = "IBM_PASS";

		//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 ResourceSample()
		{

		}

		public void sampleTest()
		{


			// start the communication with the api
			sessionid = APISetup.SetUp();


			// Declare the resource wich we are going to use
			Resource resourceToUpdate = null;

			// create a resourceScope to keep track of the relation 
			// between the resource and its attribute
			ResourceScope rScope = new ResourceScope();

			// Find the resource in RPM using XPath. 
			// If resource doesn't exist, create it using a new 
			// Resource. Fill the External ID.
			LoadResult load = APISetup.application.
				loadFromXpath(sessionid, 
				"/Resource[externalid='" + resourceExternalID + "']"
				, null );

			// if logic verifies the result of the xpath query
			// rpmObjectList contains resources returned
			if( load.rpmObjectList == null || 
				load.rpmObjectList.Length == 0 )
			{
	
				
				// query the pool you want to add the resource to
				// with the name of the pool in it.
				// loadFromXpath method an array
				Pool pool = (Pool)APISetup.application.
					loadFromXpath(sessionid, 
					"/Pool[name='" + poolName + "']",
					null).rpmObjectList[0];

				// create a new resource
				resourceToUpdate = new Resource();

				// add an external id to the resource
				resourceToUpdate.externalID = resourceExternalID;

				// add a name to the resource
				resourceToUpdate.fullName = resourceName;

				// add a pool to the resource
				// this pool represents the parent of 
				// the resource and is necessary
				resourceToUpdate.parent = pool;
				
				// initialise the pool with default data
				resourceToUpdate.userName = userid;
				resourceToUpdate.password = passid;
				
				// save the resource in the RPM repository
				// use the sessionid acquired in the setUp
				// a scope is not necessary because only the 
				// resource is being saved
				// use a reloadType that alows you to continue 
				// working with the resource
				SaveResult rSave = new SaveResult();
				rSave = APISetup.application.save(sessionid, 
					resourceToUpdate, null, ReloadType.SavedResult );

				// method showing the detail of any error in the save
				APISetup.checkForErrors( rSave );

				// reload the resource
				resourceToUpdate = (Resource)APISetup.application.
					loadFromXpath(sessionid, 
					"/Resource[externalid='" + resourceExternalID + 
					"']", null ).rpmObjectList[0];

				

			}
			else
			{
				// update existing resource
				// in this case the resource already exists
				resourceToUpdate = (Resource)load.rpmObjectList[0];
			}
 
			// Fill the Resource information
			resourceToUpdate.active = true;
			resourceToUpdate.nickName = resourceName;
			resourceToUpdate.weeklyMaxHours = 40;
			resourceToUpdate.weeklyMinHours = 0;

			// create a WorkLocation to initialise the 
			// resource employee information
			WorkLocation work = (WorkLocation)APISetup.application.
				loadFromXpath(sessionid, 
				"/WorkLocation[value='None']", 
				null).rpmObjectList[0];
			resourceToUpdate.workLocation = work;

			// adjust resource scope to keep work location
			rScope.workLocation = true;

			// create an Experience to initialise 
			// the resource employee information
			Experience exp = (Experience)APISetup.application.
				loadFromXpath(sessionid, 
				"/Experience[value='1  year +']", 
				null).rpmObjectList[0];
			resourceToUpdate.experience = exp;

			// adjust resource scope to keep experience
			rScope.experience = true;

			// create dateTime for the effectiveStartDate 
			// and the discontinueDate
			resourceToUpdate.effectiveStartDate  = 
				new DateTime(2005, 10, 10, 10, 10, 10 );
			resourceToUpdate.discontinueDate  = 
				new DateTime(2005, 11, 10, 10, 10, 10 );

			// save the resource in the RPM repository
			// use the sessionid acquired in the setUp
			// a scope is not necessary because only the 
			// resource is being saved
			SaveResult rSave2 = APISetup.application.save(sessionid, 
				resourceToUpdate, rScope, ReloadType.SavedResult );
			APISetup.checkForErrors( rSave2 );
		

			// end the communication with the api.
			APISetup.CleanUp(sessionid);
		}

	}
}