ScorecardSample

using System;  using TestFramework.ApplicationAPI; using TestFramework.AuthenticateAPI;
using TestFramework;  namespace Tests.ScoreCard { 	/// <summary>
	/// Summary description for ScorecardSample.
	/// 
	/// this sample creates a scorecard with full structure 
	/// for Wbs, Scope and Asset
	/// </summary>
	public class ScorecardSample
	{
		public ScorecardSample()
		{

		}

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

			// initialize every object so they contain at least the
			// minimal information required to be saved

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

			// 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};
			
			saveWbsScorecardFolder(scFolder);
			saveAssetScorecardFolder(scFolder);
			saveScopeManagementScorecardFolder(scFolder);


		}
			
		private void saveWbsScorecardFolder(ScorecardFolder scFolder)
		{

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

			// create a module to save the score card folder
			WbsModule module = new WbsModule();

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

			// initialize the ModuleScope so that 
			// it includes full structure
			// of the scorecard we wish to create
			WbsModuleScope mScope = new WbsModuleScope();
			mScope.scorecardFolders = new ScorecardFolderScope();
			mScope.scorecardFolders.scorecards = 
				new ScorecardScope();
			mScope.scorecardFolders.scorecards.scorecardCategories =
				new ScorecardCategoryScope();
			mScope.scorecardFolders.scorecards.scorecardCategories.
				scorecardQuestions = new ScorecardQuestionScope();
			mScope.scorecardFolders.scorecards.scorecardCategories.
				scorecardQuestions.scorecardResponses = 
				new ScorecardResponseScope();

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

		}

		private void saveAssetScorecardFolder(
			ScorecardFolder scFolder)
		{

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

			// create a module to save the score card folder
			AssetModule module = new AssetModule();
			
			// assign the scorecard folder to the Module
			scFolder.parent = module;
			module.scorecardFolders = 
				new ScorecardFolder[] {scFolder};

			// initialize the ModuleScope so that 
			// it includes full structure
			// of the scorecard we wish to create
			AssetModuleScope mScope = new AssetModuleScope();
			mScope.scorecardFolders = new ScorecardFolderScope();
			mScope.scorecardFolders.scorecards = 
				new ScorecardScope();
			mScope.scorecardFolders.scorecards.scorecardCategories = 
				new ScorecardCategoryScope();
			mScope.scorecardFolders.scorecards.scorecardCategories.
				scorecardQuestions = new ScorecardQuestionScope();
			mScope.scorecardFolders.scorecards.scorecardCategories.
				scorecardQuestions.scorecardResponses = 
				new ScorecardResponseScope();

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

		}

		private void saveScopeManagementScorecardFolder(
			ScorecardFolder scFolder)
		{

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

			// create a module to save the score card folder
			ScopeManagementModule module = new ScopeManagementModule();
			
			// assign the scorecard folder to the Module
			scFolder.parent = module;
			module.scorecardFolders = new ScorecardFolder[] {scFolder};

			// initialize the ModuleScope so that 
			// it includes full structure
			// of the scorecard we wish to create
			ScopeManagementModuleScope mScope = 
				new ScopeManagementModuleScope();
			mScope.scorecardFolders = new ScorecardFolderScope();
			mScope.scorecardFolders.scorecards = 
				new ScorecardScope();
			mScope.scorecardFolders.scorecards.scorecardCategories = 
				new ScorecardCategoryScope();
			mScope.scorecardFolders.scorecards.scorecardCategories.
				scorecardQuestions = new ScorecardQuestionScope();
			mScope.scorecardFolders.scorecards.scorecardCategories.
				scorecardQuestions.scorecardResponses = 
				new ScorecardResponseScope();

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

		}

	}
}