TestProject

using System;  using TestFramework.ApplicationAPI; using TestFramework.AuthenticateAPI;
using TestFramework;  namespace Tests.wbs   { 	/// <summary>
	/// Shows use of [ExpectedException].
	/// </summary>

	public class TestWBS
	{
		//User, password, DataSource Name are use to 
		authenticate with the service
		public static String username = "administrator";
		public static String password = "prodserv96";
		public static String dsn = "jdbc/RPMDATASOURCE";

		public static String projectExternalID = "external";
		public static String projectName = "name";
		public static String parentProjectExternalID = "parentExternalID";
		public static String assignNewExisting = "new";
		public static String assignCustomerCode = "code";
		public static String projectManagerID = "MANAGER_ID";
		public static String chargeCodeName = "charge code name";

		//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;

		//The url will describe the location of all the services
		//It will need to have the service attach to the url like
		//http://localhost:8080/rpm/services/Application
		public static String url = "http://localhost:8080/crpm/services/";

		//Object AuthenticateService which provide de 
		//login/logout/change password interface
		//It is a wsdl imported from project->addWebReference
		public static AuthenticateService authenticate;

		//Object ApplicationService which provide de
		//Save/load/checkin/checkout interface
		//It is a wsdl imported from project->addWebReference
		public static ApplicationService application;


		public static void SetUp() 
		{
			//Get an instance of authenticateService
			authenticate = new AuthenticateService();
			
			//Specify the url to connect to which is represented
			//by the location of all the services and the specified
			//service Authenticate
			authenticate.Url = url + "Authenticate";

			//Login from the authenticateService api with your
			//user/password/dsn. This will return your session ID
			//To use for the application acces.
			sessionid = authenticate.login(username, password, dsn);

			//Get an instance of ApplicationService
			application = new ApplicationService();

			//Specify the url to connect to which is represented
			//by the location of all the services and the specified
			//service Application
			application.Url = url+ "Application";
		}

		public static void CleanUp() 
		{
			authenticate.logout(sessionid);
		}

		public static void assignCustomField
		( Project project, String fieldName, String fieldValue )
		{
			// NOTE : API not ready for custom fields
			//foreach(CustomFieldAssignment 
			assignment in project.customFieldAssignments )
			//{
			//	CustomField field = assignemnt.customField;
			//	if( field.name.Equals(fieldName) )
			//	{
			//		assignment.fieldValue = fieldValue;
			//		break;
			//	}
			//}
		}

		public static void blah()
		{
			TestFramework.ApplicationAPI.Pool pool = 
				(TestFramework.ApplicationAPI.Pool)application.loadFromXpath(
				sessionid, "/Pool[name='xyz']", null ).rpmObjectList[0];

			TestFramework.ApplicationAPI.Resource resourceToMove = 
				(TestFramework.ApplicationAPI.Resource)application.loadFromXpath(
				sessionid, "/Resource[externalid='ABC']", null ).rpmObjectList[0];

			pool.resources = new TestFramework.ApplicationAPI.Resource[]{resourceToMove};

			PoolScope poolScope = new PoolScope();
			poolScope.resources = new ResourceScope();

			application.save(sessionid, pool, poolScope, ReloadType.SavedResult );
		}

		// Correct and reactivate
		public void _testWbs() 
		{
			// 1. Process start (either done by a trigger from eWorks 
			or as a scheduled batch process)
			// 2. Get the eWorks project information specified in sheet 17.

			SetUp();

			LoadResult parentLoad = application.loadFromXpath(
				sessionid, "/Project[externalid='" + 
			parentProjectExternalID + "']", null );

			Project parentProject = (Project)parentLoad.rpmObjectList[0];

			// 3. Find the project in RPM using the XPath:  
			/Project[externalid='EWORKS_PROPOSAL_ID']. 
			// Include the custom fields, financial and 
			resource assignment in scope. 
			// If project doesn't exist, create it using a new Project. 
			// Fill the External ID, Save the project with 
			custom fields in scope. (field #1)
			WorkElementScope scope = new WorkElementScope();
			scope.resourceTaskAssignments = new ResourceTaskAssignmentScope();
		 //attribution changed according to new model 17/1/06
			scope.financialAssignments = true;
			// NOTE: API not ready yet for custom fields
			// scope.customFieldAssignemnt = true;

			LoadResult load = application.loadFromXpath(
				sessionid, "/Project[externalid='" + projectExternalID +
			 "']", scope );

			Project projectToUpdate = null;
			if( load.rpmObjectList == null || load.rpmObjectList.Length == 0 )
			{
				// create a new project
				projectToUpdate = new Project();
				projectToUpdate.externalID = projectExternalID;
				projectToUpdate.name = projectName;
				projectToUpdate.parent = parentProject;

				TestFramework.ApplicationAPI.RPMCalendar projectCalendar = 
					(TestFramework.ApplicationAPI.RPMCalendar)
				application.loadFromXpath(
					sessionid, "/RPMCalendar[name='Standard (Default)']",
				 null ).rpmObjectList[0];

				projectToUpdate.calendar = projectCalendar;

				Currency projectCurrency = 
				(Currency)application.loadFromXpath(
					sessionid, "/Currency[name='Canadian dollars']",
				 null ).rpmObjectList[0];

				projectToUpdate.currency = projectCurrency;
				projectToUpdate.estimatedStartDate = new DateTime
				(2005, 10, 10, 10, 10, 10 );
				projectToUpdate.estimatedFinishDate = new DateTime
				(2005, 11, 10, 10, 10, 10 );
				projectToUpdate.assignmentType = AssignmentType.Work;

				SaveResult save = application.save
				(sessionid, projectToUpdate, scope, ReloadType.ReloadResult);
				TestHelper.traceErrorsAndFail( save );
				projectToUpdate = (Project)save.rpmObject;
			}
			else
			{
				// update existing project
				projectToUpdate = (Project)load.rpmObjectList[0];
			}

			/*   4. Find the parent project in RPM using the XPath  
			 * /Project[externalid='EWORKS_PARENT_PROJECT_ID']. 
			 * Assigh this as the parent of the current project.  (field #2)
			 */
			projectToUpdate.parent = parentProject;


			/* 
			 * 5. Fill the project information: 
			(note, for custom fields, you need to browse the list of 
			 * assigned custom fields, find the one you want and 
				fill the value contained in the 
			 * customfield assignment)
			 */

			projectToUpdate.name = projectName;
			projectToUpdate.externalID = projectExternalID;
			assignCustomField( projectToUpdate, "new-existing customer",
			 assignNewExisting );
			assignCustomField( projectToUpdate, "customer code", 
			assignCustomerCode );
			//  INSERT OTHER CUSTOM FIELDS HERE

			//6. Find the resource related to eworks that will be the 
			project manager using a XPath: 
			///resource[externalid='MANAGER_ID'] 
			//Check if the resource assignment already exists, it if doesn't, 
			//create the resource assignment. set the resource on it, 
			//and hook it to the project. (field #23)

			TestFramework.ApplicationAPI.Resource projectManager = 
			(TestFramework.ApplicationAPI.Resource)
				application.loadFromXpath( sessionid, "/Resource
			[externalid='" + projectManagerID +	"']", 
				null ).rpmObjectList[0];

			bool found = false;
			ResourceTaskAssignment[] assignmentArray = 
			projectToUpdate.resourceTaskAssignments;

			if( assignmentArray != null  )
			{
				foreach( ResourceTaskAssignment	resourceAssignment 
			in assignmentArray )
				{
					TestFramework.ApplicationAPI.Resource assignedResource = 
			resourceAssignment.resource;

					if ( assignedResource.ID.Equals(projectManager.ID ))
					{
						found =	true;
						break;
					}
				}
			}

			if ( !found )
			{
				ResourceTaskAssignment newAssignment = 
new ResourceTaskAssignment();
				newAssignment.resource = projectManager;
				projectToUpdate.resourceTaskAssignments = 
new ResourceTaskAssignment[]{newAssignment};
			}

//			7. Find the contract amount charge code using a XPath : 
//			/ChargeCode[name='CHARGE_CODE_NAME'] 
//			In the project wbsFinancial array, find if the the charge 
			code is already created. 
//			If it isn't, create a WbsFinancial element, assign the chargecode
 			to it and add 
//			it to the project wbsFinancial array. (field #39)


			ChargeCode contractAmountChargeCode = (ChargeCode)
				application.loadFromXpath( sessionid, "/ChargeCode[name='" + 
			chargeCodeName + "']", 
				null ).rpmObjectList[0];

			found = false;
			WbsFinancial[] financialArray = projectToUpdate.wbsFinancials;
			if (financialArray != null )
			{
				foreach( WbsFinancial financial in financialArray )
				{
					ChargeCode currentChargeCode = financial.chargeCode;

					if ( currentChargeCode.ID.Equals(contractAmountChargeCode.ID ))
					{
						found =	true;
						break;
					}
				}
			}

			if ( ! found )
			{
				addFinancialToProject(projectToUpdate, contractAmountChargeCode);
			}

			/*
			8. Repeat Step 7 for Contingency charge code  (field #40)
			9. Repeat Step 7 for Assets charge code  (field #41)
			10. Repeat Step 7 for Airfare/stay/expenses charge code  (field #42)
			*/

			// 11. Save the project in RPM. Include the custom fields, 
			financial and resource assignment in scope.
			SaveResult save2 = application.save(sessionid, projectToUpdate, 
			scope, ReloadType.ReloadResult);
			TestHelper.traceErrorsAndFail( save2 );
			projectToUpdate = (Project)save2.rpmObject;

			CleanUp();
		}

		public void addFinancialToProject( Project project, ChargeCode code )
		{
			TestFramework.ApplicationAPI.WbsFinancial[] sourceArray = 
			project.wbsFinancials;
			if (sourceArray == null )
			{
				sourceArray = new WbsFinancial[0];
			}
			long newSize = sourceArray.Length + 1;
			WbsFinancial[] newArray = new WbsFinancial[newSize];

			Array.Copy(sourceArray, 0, newArray, 0, sourceArray.Length );

			WbsFinancial addFinancial = new WbsFinancial();
			addFinancial.chargeCode = code;

			newArray[newSize-1] = addFinancial;

			project.wbsFinancials = newArray;
		}

		static void Main()
		{
			//resource.ResourceContactGroup x = 
			new Tests.resource.ResourceContactGroup();
			//resource.ResourceCalendar x = 
			new Tests.resource.ResourceCalendar();
			//resource.ResourceCurrency x = 
			new Tests.resource.ResourceCurrency();
			//resource.SkillCompRessSample x = 
			new Tests.resource.SkillCompRessSample();
			//resource.ResourceCField x = new Tests.resource.ResourceCField();
			//resource.ResourceGeo x = new Tests.resource.ResourceGeo();
			//resource.ResourceIndusExp x = new Tests.resource.ResourceIndusExp();
			//resource.ResourceSample x = new Tests.resource.ResourceSample();
			//resource.ResourceOrganization x = 
			new Tests.resource.ResourceOrganization();
			//resource.ResourceRate x = new Tests.resource.ResourceRate();
			//resource.ResourceLang x = new Tests.resource.ResourceLang();
			//resource.ResourcePassport x = 
			new Tests.resource.ResourcePassport();
			//resource.ResourceCCC x = new Tests.resource.ResourceCCC();
			//resource.ResourceDoc x = new Tests.resource.ResourceDoc();
			//pool.PoolAttribute x = new Tests.pool.PoolAttribute();
			//wbs.WBSTaskConstraints x = new WBSTaskConstraints();
			//wbs.WBSFillOpportunity x = new WBSFillOpportunity();
			//wbs.WBSTaskDependency x = new WBSTaskDependency();
			//pool.PoolSample x = new Tests.pool.PoolSample();
			//wbs.WBSProjectSample x = new WBSProjectSample();
			//wbs.WBSProjectChilds x = new WBSProjectChilds();
			//wbs.WBSTaskResource x = new WBSTaskResource();
			//wbs.WBSProjectRTF x = new WBSProjectRTF();
			//pool.PoolRef x = new Tests.pool.PoolRef();
			//pool.PoolCCC x = new Tests.pool.PoolCCC();
			//pool.PoolDoc x = new Tests.pool.PoolDoc();
			//pool.PoolRes x = new Tests.pool.PoolRes();
			//wbs.WBSCommitRes x = new WBSCommitRes();
			//wbs.WBSProjectSC x = new WBSProjectSC();
			//wbs.WBSTaskCPTP x = new WBSTaskCPTP();
			//wbs.WBSTask x = new WBSTask();
			//wbs.WBSDoc x = new WBSDoc();
			//ScoreCard.ScorecardSample x = new Tests.ScoreCard.
			ScorecardSample();
			clientCostCenter.ClientCostCenterRtf x = 
			new Tests.clientCostCenter.ClientCostCenterRtf();
			
			try
			{
				x.sampleTest();
			}
			catch( Exception e )
			{
				System.Console.Out.WriteLine(e.Message);
				System.Console.Out.WriteLine();
				System.Console.Out.WriteLine(e.ToString());
				int junk = 0;
				junk++;
			}
		 }
	}
}