Rev 2153 | Rev 2157 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
using System;using System.Text;using System.Globalization;using System.Collections;using System.Windows.Forms;using ReqPro40;namespace EA_ReqPro{/// <summary>/// ImportReqProDatabase is a specialisation of CopyReqProDatabaseToMemory, designed to copy the/// ReqPro database content into an EA database, discarding the structure of and hierarchy of/// packages, selecting just the requirements found in the ReqPro database for storage into EA,/// where they will be used for design traceability purposes./// </summary>public class ImportReqProDatabase : CopyReqProDatabaseToMemory{private int totalRequirements = 0;/// <summary>/// Constructor logic/// </summary>/// <param name="ea_repository"></param>public ImportReqProDatabase(): base(){totalRequirements = 0;}/// <summary>/// Method to parse a ReqPro database using the information in a ReqProDB artifact,/// assumed to be selected in EA's project browser prior to the method call./// </summary>/// <param name="ea_repository"></param>/// <returns></returns>public override bool prompt_and_parse(ReqProDB_Artifact.MODE mode, out bool cancelled){cancelled = false;try{base.structure_only = false;string importDateTime = System.DateTime.Now.ToString();Main.WriteOutput("EA Requirement import at " + importDateTime, -1 );Main.WriteOutput("",-1);if (true == base.prompt_and_parse(mode, out cancelled)){if (Main.mustAbort)return false;Main.EA_Repository.EnsureOutputVisible(Main.GUI_OUTPUT_TAB_NAME);// Configure the ReqProDB artifact as a traceability artifactRQ_Artifact.set_traceability_mode(RQ_Element);ImportFromReqPro(importDateTime);return true;}}catch (Exception ex){Main.MessageBoxException(ex, "Exception (parse)");}return false;}/// <summary>/// Displays the content of the change log element to the output trace display. This method must/// be kept in sync with the ImportFromReqPro method, obviously./// </summary>/// <param name="ea_repository"></param>/// <param name="changeLog"></param>public void displayChangeLog(EA.Element changeLog){// The change log element contains all the log in its notes section. Break it up into// lines, because each line is a single log entry, so that we can process them one at// a time.string delimStr = "\n";char [] delim = delimStr.ToCharArray();string[] log_strings = changeLog.Notes.Split(delim,2000);// Prepare a string to form an updated change log (needed to support the activity of orphaned// requirement deletion).string newList = "";// modify delimiters to : character so that we can extract the parameters for each log entrydelimStr = ":";delim = delimStr.ToCharArray();Main.WriteOutput("Displaying Import Change Log", -1);foreach(string s in log_strings){if (Main.mustAbort)break;// over time, users may delete the orphaned requirements from the EA database, but their// GUIDs will still exist in the change log. Use of such a GUID will cause// an exception in the GetElementByGuid() function. What should we do? We can attempt// to remove those GUIDs from the list. We can do this by accumulating a new list of GUIDs// that is formed from the old list, where only good items from the old list find their// way into the new list. Gradually, the list is wittled away as EA users delete orphaned// requirements having first dealt with the design change impacts that resulted from their// orphanage.try{if (s.StartsWith("{")){string trimmed_s = s.Trim();// Get log entry;s parameters.string[] parameters = trimmed_s.Split(delim, 10);// The first parameter is the GUID so use it to get the requirement elementEA.Element ea_req = Main.EA_Repository.GetElementByGuid(parameters[0]);// Now discriminate actions based on the second parameterif (parameters[1].StartsWith("Created")){Main.WriteOutput(" Created : " + ea_req.Name, ea_req.ElementID );}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("Orphaned")){Main.WriteOutput(" Orphaned : " + ea_req.Name, ea_req.ElementID );foreach (EA.Connector theConnector in ea_req.Connectors){EA.Element tgt_ele = (EA.Element)Main.EA_Repository.GetElementByID( theConnector.SupplierID );if (tgt_ele != null){if (!tgt_ele.Type.StartsWith("Requirement")){Main.WriteOutput(" --> " + tgt_ele.Name, tgt_ele.ElementID);}}}}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("Name")){Main.WriteOutput(" Modified Name: " + ea_req.Name, ea_req.ElementID );Main.WriteOutput(" <<< " + parameters[2], ea_req.ElementID);Main.WriteOutput(" >>> " + parameters[3], ea_req.ElementID);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("Notes")){Main.WriteOutput(" Modified Description: " + ea_req.Name, ea_req.ElementID );Main.WriteOutput(" <<< " + parameters[2], ea_req.ElementID);Main.WriteOutput(" >>> " + parameters[3], ea_req.ElementID);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("Difficulty")){Main.WriteOutput(" Modified Difficulty: " + ea_req.Name, ea_req.ElementID );Main.WriteOutput(" <<< " + parameters[2], ea_req.ElementID);Main.WriteOutput(" >>> " + parameters[3], ea_req.ElementID);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("Priority")){Main.WriteOutput(" Modified Priority: " + ea_req.Name, ea_req.ElementID );Main.WriteOutput(" <<< " + parameters[2], ea_req.ElementID);Main.WriteOutput(" >>> " + parameters[3], ea_req.ElementID);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("Version")){Main.WriteOutput(" Modified Version: " + ea_req.Name, ea_req.ElementID );Main.WriteOutput(" <<< " + parameters[2], ea_req.ElementID);Main.WriteOutput(" >>> " + parameters[3], ea_req.ElementID);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("Status")){Main.WriteOutput(" Modified Status: " + ea_req.Name, ea_req.ElementID );Main.WriteOutput(" <<< " + parameters[2], ea_req.ElementID);Main.WriteOutput(" >>> " + parameters[3], ea_req.ElementID);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("Subsystem")){Main.WriteOutput(" Modified Subsystem: " + ea_req.Name, ea_req.ElementID );Main.WriteOutput(" <<< " + parameters[2], ea_req.ElementID);Main.WriteOutput(" >>> " + parameters[3], ea_req.ElementID);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("Stability")){Main.WriteOutput(" Modified Stability: " + ea_req.Name, ea_req.ElementID );Main.WriteOutput(" <<< " + parameters[2], ea_req.ElementID);Main.WriteOutput(" >>> " + parameters[3], ea_req.ElementID);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("ReqType")){Main.WriteOutput(" Modified Type: " + ea_req.Name, ea_req.ElementID );Main.WriteOutput(" <<< " + parameters[2], ea_req.ElementID);Main.WriteOutput(" >>> " + parameters[3], ea_req.ElementID);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("SourceVersion")){Main.WriteOutput(" Modified Type: " + ea_req.Name, ea_req.ElementID );Main.WriteOutput(" <<< " + parameters[2], ea_req.ElementID);Main.WriteOutput(" >>> " + parameters[3], ea_req.ElementID);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("SourceSection")){Main.WriteOutput(" Modified Type: " + ea_req.Name, ea_req.ElementID );Main.WriteOutput(" <<< " + parameters[2], ea_req.ElementID);Main.WriteOutput(" >>> " + parameters[3], ea_req.ElementID);}/////////////////////////////////////////////////////////////////////////////////////////////////////////////else if (parameters[1].StartsWith("Source")){Main.WriteOutput(" Modified Type: " + ea_req.Name, ea_req.ElementID );Main.WriteOutput(" <<< " + parameters[2], ea_req.ElementID);Main.WriteOutput(" >>> " + parameters[3], ea_req.ElementID);}// accumulate good item into the newListnewList += trimmed_s + "\r\n";}}catch{// do nothing - the newList accumulator will not have the GUID that caused the exception// so the next time a display operation is attempted, the exception wont happen.}}// Update the change logchangeLog.Notes = newList;changeLog.Update();}/// <summary>/// Rid a string of any : char because we use that as a delimiter in the change log and so we/// do not want a users use of the character to confuse the change log display mechanism./// </summary>/// <param name="s"></param>/// <returns></returns>private string NO_COLONS(string s){string sc = s.Replace(':', ' ');sc = sc.Replace('\r' , ' ');sc = sc.Replace('\n' , ' ');return sc;}/// <summary>/// Get a new import package in which to store change log and new requirements from the import./// </summary>/// <param name="parentPackage"></param>/// <param name="importDateTime"></param>/// <returns></returns>private EA.Package createImportPackage(EA.Package parentPackage, string importDateTime){EA.Package importPackage = null;string importSubDir = EA_TaggedValues.Read(base.RQ_Element, Constants.TAG_IMPORT_SUB_DIR, "");if (importSubDir != null && importSubDir.Length > 0){importPackage = (EA.Package)parentPackage.Packages.GetByName(importSubDir);if (importPackage != null){parentPackage = importPackage;}else{// create a subdir and use it as the parentparentPackage = (EA.Package )parentPackage.Packages.AddNew( importSubDir, "Package");parentPackage.Update();}}// create the import dir in the parentimportPackage = (EA.Package )parentPackage.Packages.AddNew( "import " + importDateTime, "Package");importPackage.Update();return importPackage;}/// <summary>/// A method that imports requirements from a ReqPro database into EA for the purpose/// of supporting requirement-to-design traceability./// </summary>/// <param name="repository"></param>private void ImportFromReqPro(string importDateTime){try{// Get a list of EA requirements and their associated ReqPro GUIDsArrayList ea_reqs;ArrayList ea_GUIDs = new ArrayList();ArrayList ea_req_matched = new ArrayList();// Obtain the EA parent package so that we can create under it an import package// if need be, and scan for existing requirement elements.EA.Package parentPackage = Main.EA_Repository.GetPackageByID( base.RQ_Element.PackageID );ArrayList allowedElementTypes = new ArrayList();allowedElementTypes.Add("Requirement");allowedElementTypes.Add("UseCase");Main.WriteOutput("Acquiring list of elements already in EA", -1);ElementAccumulator reqLister = new ElementAccumulator(allowedElementTypes);EA_Utilities.findAndProcessPackageElements( parentPackage, reqLister, true );if (Main.mustAbort)return;ea_reqs = reqLister.Elements;foreach (EA.Element ea_req in ea_reqs){string GUID = EA_TaggedValues.Read(ea_req, Constants.TAG_GUID);ea_GUIDs.Add(GUID);ea_req_matched.Add(false);}if (Main.mustAbort)return;// Create an import package to store new requirements and the change logEA.Package importPackage = createImportPackage(parentPackage, importDateTime);// Create the change log element in the import packageEA.Element changeLog = (EA.Element)importPackage.Elements.AddNew("Change Log","InformationItem");changeLog.Update();// Get a flattened list of requirements from the hierarchical data the user has filteredArrayList rq_req_collection = new ArrayList();get_rq_req_collection(ref rq_req_collection);int newRequirementCount = 0;int modifiedRequirementCount = 0;int statusUpdatedRequirementCount = 0;int orphanedCount = 0;Main.WriteOutput("Merging ReqPro content to EA", -1);// loop through all of the ReqPro requirementsforeach(ReqPro_object rq_obj in rq_req_collection){if (Main.mustAbort)break;try{// Find which EA requirement element refers to the current ReqPro GUIDint i_ea_req;i_ea_req = ea_GUIDs.IndexOf( rq_obj.guid );// If EA element was foundif (0 <= i_ea_req){ea_req_matched[i_ea_req] = true;EA.Element ea_req = ((EA.Element)ea_reqs[i_ea_req]);rq_obj.ea_element_ID = ea_req.ElementID;// Only update the requirement if it was not mastered in EA, or// unrestricted imports have been enabledif ( (false == EA_TaggedValues.Read(ea_req, Constants.TAG_CREATED_IN_EA, Constants.DEFAULT_CREATED_IN_EA))|| (true == EA_TaggedValues.Read(ea_req, Constants.TAG_UNRESTRICTED_IMPORTS, Constants.DEFAULT_UNRESTRICTED_IMPORTS)) ){// This ReqPro requirement already has a counterpart in EA, so we must simply// update the counterpart. But, to aid the designer, we need to try to tell them// how the requirement has changed, which could ofcoarse change the meaning// of the requirement and require design changes to be done.bool meaningMayHaveChanged = false;bool statusMayHaveChanged = false;string ea_req_name = rq_obj.tag + " " + rq_obj.name;Main.WriteOutput("Updating: " + ea_req_name, rq_obj.ea_element_ID);ea_req.Name = updated_property_value_if_changed(changeLog, ea_req, ea_req.Name, "Name", ea_req_name, ref meaningMayHaveChanged);ea_req.Notes = updated_property_value_if_changed(changeLog, ea_req, ea_req.Notes, "Notes", rq_obj.text, ref meaningMayHaveChanged);ea_req.Difficulty = updated_property_value_if_changed(changeLog, ea_req, ea_req.Difficulty, "Difficulty", rq_obj.difficulty, ref statusMayHaveChanged);ea_req.Priority = updated_property_value_if_changed(changeLog, ea_req, ea_req.Priority, "Priority", rq_obj.priority, ref statusMayHaveChanged);ea_req.Status = updated_property_value_if_changed(changeLog, ea_req, ea_req.Status, "Status", rq_obj.status, ref statusMayHaveChanged);if_tagged_value_changed_update_it(changeLog, ea_req, Constants.TAG_SOURCE, "Source", rq_obj.source, ref statusMayHaveChanged);if_tagged_value_changed_update_it(changeLog, ea_req, Constants.TAG_SOURCE_VERSION, "SourceVersion", rq_obj.sourceVersion, ref statusMayHaveChanged);if_tagged_value_changed_update_it(changeLog, ea_req, Constants.TAG_SOURCE_SECTION, "SourceSection", rq_obj.sourceSection, ref statusMayHaveChanged);if_tagged_value_changed_update_it(changeLog, ea_req, Constants.TAG_SUBSYSTEM, "Subsystem", rq_obj.subsystem, ref statusMayHaveChanged);if_tagged_value_changed_update_it(changeLog, ea_req, Constants.TAG_STABILITY, "Stability", rq_obj.stability, ref statusMayHaveChanged);// Requirement Types are actually implemented using ordinary EA element stereotypes.// Use StereotypeEx field to see if the type already exists, since that field can be a comma// seperated list of more than one stereotype, just in case user has added additional stereotypes// to the item.if (0 > ea_req.StereotypeEx.IndexOf(rq_obj.type)){changeLog.Notes += ea_req.ElementGUID + ":ReqType:" + ea_req.Stereotype + ":" + rq_obj.type + "\r\n";changeLog.Update();statusMayHaveChanged = true;if (ea_req.StereotypeEx.Length > 0)ea_req.StereotypeEx = rq_obj.type + "," + ea_req.StereotypeEx;elseea_req.StereotypeEx = rq_obj.type;}if (ea_req.Stereotype.Length == 0){ea_req.Stereotype = ea_req.StereotypeEx.Split(",".ToCharArray())[0];}ea_req.Update();if (meaningMayHaveChanged){modifiedRequirementCount++;}if (statusMayHaveChanged){statusUpdatedRequirementCount++;}totalRequirements++;}}else{totalRequirements++;// This ReqPro requirement does not have a counterpart in EA, so we must create// a new one.newRequirementCount++;// create the new EA requirement in the import packageEA.Element ea_req = (EA.Element)importPackage.Elements.AddNew( rq_obj.tag + " " + rq_obj.name, "Requirement");rq_obj.ea_element_ID = ea_req.ElementID;CopyReqProDatabase.copyReq(ea_req, rq_obj);ea_req.Notes = rq_obj.text;ea_req.Update();changeLog.Notes += ea_req.ElementGUID + ":Created:" + ea_req.Name + "\r\n";changeLog.Update();Main.WriteOutput("Created: " + ea_req.Name, ea_req.ElementID);}}catch (Exception ex){Main.WriteOutput("Exception (ImportFromReqPro), " + ex.Message + ", " + rq_obj.name, -1);}}if (Main.mustAbort)return;Main.WriteOutput("Checking for orphaned requirements", -1);// Now check for orphaned EA requirementsfor (int i = 0; i < ea_GUIDs.Count; i++){if (Main.mustAbort)break;string rq_GUID = (string)ea_GUIDs[i];if (rq_GUID != ""){if ((bool)ea_req_matched[i] == false){EA.Element ea_req = (EA.Element)ea_reqs[i];orphanedCount++;changeLog.Notes += ea_req.ElementGUID + ":Orphaned:" + ea_req.Name + "\r\n";changeLog.Update();}}}bool changeLogDeleted = false;if (changeLog.Notes != null && changeLog.Notes.Length > 0){DialogResult dlgres = MessageBoxEx.Show("Display Change Log?", "Change Log", MessageBoxButtons.YesNo);if (dlgres == DialogResult.Yes)displayChangeLog(changeLog);}else{importPackage.Elements.Refresh();if (importPackage.Elements.Count == 1){EA_Utilities.deletePackage(importPackage);changeLogDeleted = true;}}Main.EA_Repository.RefreshModelView(parentPackage.PackageID);// Setup the internal requirement to requirement connectivity. This is seperate from the browser// organisation of elements in EA, but since in ReqPro, that organisation tells part of the story// of requirement to requirement connectivity, it is mirrored in the internal connectivity, along// with explicit trace relationships setup in ReqPro.// The purpose of this internal connectivity is to support relationship matrix tables in documentation// generated by EA_DocGen, or to support diagrammatic representations of the requirements in EA,// where the connectivity will become apparent through links ajoining the requirement elements in the// diagram.write_traces(totalRequirements);if (Main.mustAbort)return;writeDelayedMessages();// display summary statsMain.WriteOutput(newRequirementCount.ToString() + " new requirements", -1);Main.WriteOutput(modifiedRequirementCount.ToString() + " requirements modified", -1);Main.WriteOutput(statusUpdatedRequirementCount.ToString() + " requirements had status changes", -1);Main.WriteOutput(orphanedCount.ToString() + " requirements were orphaned", -1);if (changeLogDeleted){Main.WriteOutput("NOTE: import package and change log deleted, since nothing changed in this import", -1);}Main.WriteOutput("Import Completed", -1);MessageBoxEx.Show("Import Completed");}catch (Exception ex){Main.MessageBoxException(ex, "Exception (ImportFromReqPro)");}}private string updated_property_value_if_changed(EA.Element changeLog,EA.Element ea_req,string dest_value,string changeLogName,string source_value,ref bool mayHaveChanged){if (dest_value.CompareTo(source_value) != 0 ){changeLog.Notes += ea_req.ElementGUID + ":" + changeLogName+ ":" + NO_COLONS(dest_value) + ":" + NO_COLONS(source_value) + "\r\n";changeLog.Update();mayHaveChanged = true;return source_value;}return dest_value;}private void if_tagged_value_changed_update_it(EA.Element changeLog,EA.Element ea_req,string tagname,string changeLogName,string source_value,ref bool statusMayHaveChanged){string value = EA_TaggedValues.Read(ea_req, tagname, "");if ((source_value != null) && (false == source_value.Equals(value))){changeLog.Notes += ea_req.ElementGUID + ":" + changeLogName + ":" + value + ":" + source_value + "\r\n";changeLog.Update();EA_TaggedValues.Write(ea_req, tagname, source_value);statusMayHaveChanged = true;}}/// <summary>/// This method (along with its sibling overload) obtains a flattened view of the/// hierarchical requirements obtained from the ReqPro database. This accumulation/// takes account of the users filtering requests./// </summary>/// <param name="ea_repository"></param>private void get_rq_req_collection(ref ArrayList rq_req_collection){foreach( ReqPro_object sub_obj in rq_root_package.ReqPro_objects ){get_rq_req_collection(ref rq_req_collection, sub_obj);}}private void get_rq_req_collection(ref ArrayList rq_req_collection,ReqPro_object rq_obj ){if (rq_obj.isPackage){// if the package is not filtered, or if we are allowed to dig beneath filtered// packages...if (rq_obj.filtered == false || base.allowPackageStructureFragments){// Using recursion, scan this objects sub-objectsforeach( ReqPro_object sub_obj in rq_obj.ReqPro_objects ){// if the sub-object is a package, always recurse to process it, else// if the sub-object is a requirement, only recurse if the containing package// is not filtered, so that requirements of filtered packages are themselves// filtered out by virtue of their parent packages filtering state.if ( sub_obj.isPackage|| (sub_obj.isRequirement && rq_obj.filtered == false)){get_rq_req_collection(ref rq_req_collection, sub_obj);}}}}else if (rq_obj.isRequirement){if ( ! (reqTypeIsFiltered(rq_obj) || reqStatusTypeIsFiltered(rq_obj)) ){// Add this requirement to the flattened list we are accumulatingrq_req_collection.Add(rq_obj);// In ReqPro, a requirement can be related to another requirement in several ways:// 1. By virtue of its position relative to another in the browser display. That is,// if you place a requirement beneath a parent requirement, you are establishing a// parent-child relationship.// 2. By virtue of a specific TraceTo relationship being made via the Traceability menu// option.// Interestingly, ReqPro prevents you creating a relationship of one of the above types,// if a relationship of the other type already exists. This implies that the relationship// between a parent and child requirement must be singular and is important regardless// of the manner in which it was created or represented.// The CopyReqProDatabaseToMemory base class will already have taken care of recording// relationships detected from ReqPro made using method 2 above. What we need to do here is// take care of those apparent from the browser based hierarchical organisation (ie. made// in ReqPro using method 1).// All we need to do is grab the parent object (if any) and add the GUID of the child to// its list of trace-to's. Later on, the write_traces() class method will turn these into// EA based connection objects that belong to the parent requirement elements.if (rq_obj.parent != null){rq_obj.parent.ReqPro_traces.Add(rq_obj.guid);}// Using recursion, scan this objects sub-objects, which in practise will all// be requirement objects. At this time requirement objects cannot have packages// as children.foreach( ReqPro_object sub_obj in rq_obj.ReqPro_objects ){get_rq_req_collection(ref rq_req_collection, sub_obj);}}}}}}