using System; using TestFramework.ApplicationAPI; using TestFramework.AuthenticateAPI; using TestFramework; namespace Tests.resource { /// <summary> /// Summary description for ResourceCField. /// /// this sample creates a custom field and assigns it /// to a resource from the RPM repository. Then it /// queries the custom fields and assigns them /// a default value. /// </summary> public class ResourceCField { public ResourceCField() { } //All these variables are used in this sample public static String resourceName = "IBM_testABCDEF"; public static String cFieldName = "IBMFIELD"; public static String cfcName = "IBM_CATEGORY"; public static FieldInputType[] fieldType = new FieldInputType[6] {FieldInputType.Checkbox ,FieldInputType.Currency ,FieldInputType.Datafield ,FieldInputType.Date ,FieldInputType.Numeric ,FieldInputType.Text }; //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() { // start the communication with the api sessionid = APISetup.SetUp(); // Declare the resource wich we are going to use Resource res = null; // create a resourceScope to keep track of the relation // between the resource and the custom field ResourceScope rScope = new ResourceScope(); rScope.customFieldAssignments = true; // create a save result object SaveResult save = null; // create custom field assignment CustomFieldAssignment assign = null; // create a custom field category CustomFieldCategory cfCategory = new CustomFieldCategory(); // adjust the resource scope to keep the relation // with the custom field rScope.customFieldAssignments = true; // Find the resource in RPM using XPath. // take resource in RPM repository by its // fullname or externalid res = (Resource)APISetup.application. loadFromXpath(sessionid, "/Resource[fullname='" + resourceName + "']", rScope ).rpmObjectList[0]; // load a custom field category from RPM repository cfCategory = (CustomFieldCategory)APISetup.application. loadFromXpath(sessionid, "/ResourceModule/customfieldcategory" + "[name='" + cfcName + "']", null ).rpmObjectList[0]; CustomFieldAssignment [] fieldAssignements = new CustomFieldAssignment[6]; for (int i = 0 ; i < 6 ; i ++) { // create a custom field CustomField cField = new CustomField(); // assign the custom field category // to the custom field as a parent cField.parent = cfCategory; // initialise the custom field with default values cField.name = cFieldName + i; // save the custom field with a // desired FieldInputType cField = saveCustomField(cField, fieldType[i]); // create a new custom field assignment assign = new CustomFieldAssignment(); // initialize custom field assignement // assign the field to the assignment assign.customField = cField; assign.parent = res; fieldAssignements[i] = assign; } // assign custom field to the resource res.customFieldAssignments = fieldAssignements; // save the resource in the RPM repository // use the sessionid acquired in the setUp // use the resource scope try { save = new SaveResult(); save = APISetup.application.save(sessionid, res, rScope, ReloadType.SavedResult ); } catch (Exception e) { System.Console.Out.WriteLine(e.Message); } APISetup.checkForErrors( save ); // create a scope that enables us to load // the category with the fields just created CustomFieldScope cfScope = new CustomFieldScope(); cfScope.children = new CustomFieldScope(); cfCategory = (CustomFieldCategory)APISetup.application. loadFromXpath(sessionid, "/ResourceModule/customfieldcategory" + "[name='" + cfcName + "']", cfScope ).rpmObjectList[0]; for (int i = 0 ; i < 6 ; i ++) { int k = 0; while (cfCategory.children[k].name != cFieldName + i) {k++;} switch (cfCategory.children[k].inputType) { case FieldInputType.Text : cfCategory.children[k].defaultValue = "DEFAULT_CUSTOM_VALUE"; break; case FieldInputType.Numeric : cfCategory.children[k].defaultValue = 1; break; case FieldInputType.Date : cfCategory.children[k].defaultValue = new DateTime(2005, 10, 10, 10, 10, 10); break; case FieldInputType.Datafield : Experience exp = (Experience)APISetup. application.loadFromXpath(sessionid, "/Experience[value='1 year +']", null). rpmObjectList[0]; cfCategory.children[k].defaultValue = exp; break; case FieldInputType.Currency : Currency currency = (Currency)APISetup.application. loadFromXpath(sessionid, "/Currency[name='Canadian dollars']", null ).rpmObjectList[0]; cfCategory.children[k].defaultValue = currency; break; case FieldInputType.Checkbox : cfCategory.children[k].defaultValue = false; break; } } // save the category containing the custom // field's new values // use a scope to save its childrens // reloadtype can be none because we // don't reload object afterward save = new SaveResult(); save = APISetup.application.save(sessionid, cfCategory, cfScope, ReloadType.None); APISetup.checkForErrors( save ); // end the communication with the api. APISetup.CleanUp(sessionid); } /** * saveCustomField * * this fonctions saves a custom field into the * RPM repository with the desired FieldInputType. **/ public CustomField saveCustomField(CustomField field, FieldInputType type) { // create a scope to save your custom field CustomFieldScope cfScope = new CustomFieldScope(); field.inputType = type; // save the custom field in the RPM repository SaveResult save = APISetup.application.save(sessionid, field, cfScope, ReloadType.ReloadResult); APISetup.checkForErrors( save ); // load custom field to assign it field = (CustomField)save .rpmObject; return field; } } }