WBSProjectSC

using System;  using TestFramework.ApplicationAPI; using TestFramework.AuthenticateAPI;
using TestFramework;  namespace Tests.wbs { 	/// <summary>
	/// Summary description for WBSProjectSC.
	/// 
	/// this sample creates a scorecard and saves it into
	/// RPM repository, it assigns it to a project and then publishes it.
	/// </summary>
	public class WBSProjectSC
	{
		public WBSProjectSC()
		{

		}

		// all these variables are used in this sample
		public static String projectName = "IBM_GENERIC_PROJECT_ABC";
		public static String scfName = "IBM_SCORECARD_FOLDER";
		public static String scName = "IBM_SCORECARD";
		public static String sccName = "IBM_SCORECARD_CATEGORY";
		public static String scqName = "IBM_SCORECARD_QUESTION";
		public static String scrName = "IBM_SCORECARD_RESPONSE";
		// number of questions
		public static int nbrQ = 5;  
		// number of responses per question
		public static int nbrR = 3; 
		public static int iWeight = 1;
		
		//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 saver = null;

			// create a project scope
			// this scope enables us to save 
			// information around the project
			WorkElementScope pScope = new WorkElementScope();

			// adjust the project scope so it links the 
			// assigned scorecard
			pScope.assignedScorecards = new ScorecardScope();
			pScope.assignedScorecards.parent = new RPMObjectScope();
			pScope.assignedScorecards.scorecardCategories = 
				new ScorecardCategoryScope();
			pScope.assignedScorecards.scorecardCategories.
				scorecardQuestions = new ScorecardQuestionScope();
			pScope.assignedScorecards.scorecardCategories.
				scorecardQuestions.scorecardResponses = 
				new ScorecardResponseScope();

			// create a project object
			Project project = new Project();

			// load the project from the ROM repository
			// use a scope to load assigned 
			// scorecard links if there is any
			project = (Project)APISetup.application.loadFromXpath(
				sessionid, "/Project[name='" + projectName + "']", 
				pScope ).rpmObjectList[0];

			// create every object needed to 
			// create a complete scorecard
			ScorecardFolder scFolder = new ScorecardFolder();
			Scorecard sCard = new Scorecard();
			ScorecardCategory scCat = new ScorecardCategory();
			ScorecardQuestion scQuestion = null;
			ScorecardResponse scResponse = null;

			//necessary for any new scorecard folder
			WbsModule module = new WbsModule(); 

			// initialize every object so they contain at least the
			// minimal information required to be saved
			// WBSModule does not need to be initialized

			// initialize scorecard folder
			// the parent is a WBSModule
			scFolder.name = scfName;
			scFolder.parent = module;

			// initialize the scorecard
			// there can be any number of scorecard 
			// in a scorecard folder
			sCard.name = scName;
			sCard.weight = iWeight;
			sCard.probabilityType = ProbabilityType.None;
			sCard.parent = scFolder;

			// initialize the scorecard
			scCat.name = sccName;
			scCat.parent = sCard;
			scCat.weight = iWeight;
			
			// create an array of scorecard questions
			ScorecardQuestion[] questions = 
				new ScorecardQuestion[nbrQ];

			for (int i = 0 ; i < nbrQ ; i ++)
			{
				// initialize a scorecard question
				scQuestion = new ScorecardQuestion();
				scQuestion.name = scqName + i;
				scQuestion.parent = scCat;
				scQuestion.weight = iWeight;

				// create an array of scorecard 
				// responses for each question
				ScorecardResponse[] responses = 
					new ScorecardResponse[nbrR];
				for (int j = 0 ; j < nbrR ; j ++)
				{
					// initialize the scorecard response
					scResponse = new ScorecardResponse();
					scResponse.name = scrName + j;
					scResponse.parent = scQuestion;
					scResponse.weight = iWeight;

					// scorecard question must have no more
					// than one scorecard response 
					// set to default response
					if (j == 0)
						{scResponse.defaultResponse = true;}
					else
						{scResponse.defaultResponse = false;}

					responses[j] = scResponse;
				}

				// assign responses to question
				scQuestion.scorecardResponses = responses;

				questions[i] = scQuestion;
			}

			// assign scorecard questions to scorecard category
			scCat.scorecardQuestions = questions;

			// assign scorecard category to scorecard
			sCard.scorecardCategories = 
				new ScorecardCategory[] {scCat};

			// assign scorecard to scorecard folder
			scFolder.scorecards = new Scorecard[] {sCard};

			// assign scorecard folder to WBSModule
			module.scorecardFolders = 
				new ScorecardFolder[] {scFolder};

			// create WbsModuleScope that includes all tree
			// of the scorecard you wish to create
			WbsModuleScope wbsmScope = new WbsModuleScope();
			wbsmScope.scorecardFolders = new ScorecardFolderScope();
			wbsmScope.scorecardFolders.scorecards = 
				new ScorecardScope();
			wbsmScope.scorecardFolders.scorecards.
				scorecardCategories = new ScorecardCategoryScope();
			wbsmScope.scorecardFolders.scorecards.
				scorecardCategories.scorecardQuestions = 
				new ScorecardQuestionScope();
			wbsmScope.scorecardFolders.scorecards.
				scorecardCategories.scorecardQuestions.
				scorecardResponses = new ScorecardResponseScope();

			// save the scorecard folder by saving the WbsModule
			// a new scorecard folder cannot be saved on is own
			// use the WbsModuleScope to save every 
			// element of the scorecard desired
			saver = new SaveResult();
			saver = APISetup.application.save(sessionid, 
				module, wbsmScope, ReloadType.None);
			APISetup.checkForErrors( saver );

			// load the scorecard created, this is 
			// needed to assign it afterward
			sCard = (Scorecard)APISetup.application.loadFromXpath(
				sessionid, "/Scorecard[name='" + scName + "']", 
				null).rpmObjectList[0];

			// a scorecard must be active to be 
			// assigned to any object
			sCard.active = true;

			// assign the active scorecard to the 
			// project loaded in the first place
			APISetup.application.assignScoreCard(sessionid, 
				project, pScope, sCard, ReloadType.None);

			// reload project that contains 
			// newly saved assignedScorecard
			project = (Project)APISetup.application.loadFromXpath(
				sessionid, "/Project[name='" + projectName + "']",
				pScope ).rpmObjectList[0];

			// create a scorecardScope to 
			// publish the assignedScorecard
			ScorecardScope publishScope = new ScorecardScope();
			publishScope.scorecardCategories = 
				new ScorecardCategoryScope();
			publishScope.scorecardCategories.scorecardQuestions = 
				new ScorecardQuestionScope();
			publishScope.scorecardCategories.scorecardQuestions.
				scorecardResponses = new ScorecardResponseScope();
			
			// create an assignedScorecard object
			// initialize with our project assignedScorecard
			AssignedScorecard assign = project.assignedScorecards[0];
			assign.parent = project;
			
			// publish the scorecard so it can be viewed as a report
			// publish needs an assignedScorecard and a Scorecard 
			// scope assignedScorecard must have a valid parent
			assign = (AssignedScorecard)APISetup.application.
				publishScorecard(sessionid, assign, publishScope).
				rpmObject;

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


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

	}
}