Subversion Repositories DevTools

Rev

Rev 2145 | 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>
        /// Summary description for ImportReqProDatabase.
        /// </summary>
        public class ImportReqProDatabase : CopyReqProDatabaseToMemory
        {
      /// <summary>
      /// Constructor logic
      /// </summary>
      /// <param name="ea_repository"></param>
      public ImportReqProDatabase(EA.Repository ea_repository): base(ea_repository)
                {
                }


      /// <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 parse(EA.Repository ea_repository)
      {
         try
         {
            if (true == base.parse(ea_repository))
            {
               ea_repository.EnsureOutputVisible(Main.GUI_OUTPUT_TAB_NAME);
               ImportFromReqPro(ea_repository);
               return true;
            }
         }
         catch (Exception ex)
         {
            MessageBox.Show(ex.Message, "Error (ImportReqProDatabase::parse)", MessageBoxButtons.OK);
         }      
         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.Repository ea_repository, 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 entry
         delimStr = ":";
         delim = delimStr.ToCharArray();

         ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME,"Displaying Import Change Log", -1);

         foreach(string s in log_strings)
         {
            // 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 element
                  EA.Element ea_req = ea_repository.GetElementByGuid(parameters[0]);

                  // Now discriminate actions based on the second parameter

                  if (parameters[1].StartsWith("Created"))
                  {
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME,"  Created : " + ea_req.Name, ea_req.ElementID );
                  }
                  /////////////////////////////////////////////////////////////////////////////////////////////////////////////
                  else if (parameters[1].StartsWith("Orphaned"))
                  {
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME,"  Orphaned : " + ea_req.Name, ea_req.ElementID );

                     foreach (EA.Connector theConnector in ea_req.Connectors)
                     {
                        EA.Element tgt_ele = (EA.Element)ea_repository.GetElementByID( theConnector.SupplierID );
                        if (tgt_ele != null)
                        {
                           if (!tgt_ele.Type.StartsWith("Requirement"))
                           {
                              ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     --> " + tgt_ele.Name, tgt_ele.ElementID);
                           }
                        }
                     }
                  }
                  /////////////////////////////////////////////////////////////////////////////////////////////////////////////
                  else if (parameters[1].StartsWith("Name"))
                  {
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "  Modified Name: " + ea_req.Name, ea_req.ElementID );
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     <<< " + parameters[2], ea_req.ElementID);
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     >>> " + parameters[3], ea_req.ElementID);
                  }
                  /////////////////////////////////////////////////////////////////////////////////////////////////////////////
                  else if (parameters[1].StartsWith("Notes"))
                  {
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "  Modified Description: " + ea_req.Name, ea_req.ElementID );
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     <<< " + parameters[2], ea_req.ElementID);
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     >>> " + parameters[3], ea_req.ElementID);
                  }
                  /////////////////////////////////////////////////////////////////////////////////////////////////////////////
                  else if (parameters[1].StartsWith("Difficulty"))
                  {
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "  Modified Difficulty: " + ea_req.Name, ea_req.ElementID );
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     <<< " + parameters[2], ea_req.ElementID);
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     >>> " + parameters[3], ea_req.ElementID);
                  }
                  /////////////////////////////////////////////////////////////////////////////////////////////////////////////
                  else if (parameters[1].StartsWith("Priority"))
                  {
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "  Modified Priority: " + ea_req.Name, ea_req.ElementID );
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     <<< " + parameters[2], ea_req.ElementID);
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     >>> " + parameters[3], ea_req.ElementID);
                  }
                  /////////////////////////////////////////////////////////////////////////////////////////////////////////////
                  else if (parameters[1].StartsWith("Version"))
                  {
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "  Modified Version: " + ea_req.Name, ea_req.ElementID );
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     <<< " + parameters[2], ea_req.ElementID);
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     >>> " + parameters[3], ea_req.ElementID);
                  }
                  /////////////////////////////////////////////////////////////////////////////////////////////////////////////
                  else if (parameters[1].StartsWith("Status"))
                  {
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "  Modified Status: " + ea_req.Name, ea_req.ElementID );
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     <<< " + parameters[2], ea_req.ElementID);
                     ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "     >>> " + parameters[3], ea_req.ElementID);
                  }

                  // accumulate good item into the newList
                  newList += 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 log
         changeLog.Notes = newList;
         changeLog.Update();
      }



      /// <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(EA.Repository ea_repository)
      {
         try
         {
            // Create a ReqProDB_Artifact object and obtain the EA ReqProDB element from it.
            ReqProDB_Artifact RQ_Artifact = new ReqProDB_Artifact();
            EA.Element ea_rq_artifact = RQ_Artifact.get_rq_artifact(ea_repository);

            //IFormatProvider cultureInfo = new CultureInfo("en-US", true);

            string importDateTime = System.TimeZone.CurrentTimeZone.ToUniversalTime( System.DateTime.Now ).ToString();

            ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "EA Requirement import at " + importDateTime, -1 );

            // Update the ReqProDB stereotypes element with all the EA requirements in the package
            RQ_Artifact.UpdatePackageToReqProDatabaseAssociation(ea_repository, ea_rq_artifact);

            // Get a list of EA requirements and their associated ReqPro GUIDs
            ArrayList ea_reqs = new ArrayList();
            ArrayList ea_GUIDs = new ArrayList();
            ArrayList ea_req_matched = new ArrayList();
            foreach (EA.Connector connector in ea_rq_artifact.Connectors)
            {
               EA.Element ea_req = ea_repository.GetElementByID( connector.SupplierID );
               if (ea_req != null)
               {
                  ea_reqs.Add(ea_req);
                  string GUID = EA_Utils.ReadTag(ea_req, "GUID");
                  ea_GUIDs.Add(GUID);
                  ea_req_matched.Add(false);
               }
            }

            // Obtain the EA parent package so that we can create under it an import package
            // if need be.
            EA.Package parentPackage = ea_repository.GetPackageByID( ea_rq_artifact.PackageID );

            EA.Package importPackage = (EA.Package )parentPackage.Packages.AddNew( "import " + importDateTime, "Package");
            importPackage.Update();

            EA.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 filtered
            ArrayList rq_req_collection = new ArrayList();
            get_rq_req_collection(ea_repository, ref rq_req_collection);

            int newRequirementCount = 0;
            int modifiedRequirementCount = 0;
            int statusUpdatedRequirementCount = 0;
            int orphanedCount = 0;


            // loop through all of the ReqPro requirements
            foreach(ReqPro_object rq_obj in rq_req_collection)
            {
               try
               {
                  // Find which EA requirement element refers to the current ReqPro GUID
                  int i_ea_req;
                  i_ea_req = ea_GUIDs.IndexOf( rq_obj.guid );

                  // If EA element was found
                  if (0 <= i_ea_req)
                  {
                     ea_req_matched[i_ea_req] = true;

                     EA.Element ea_req = ((EA.Element)ea_reqs[i_ea_req]);

                     // 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
                     // if the requirement text has changed, which could ofcoarse change the meaning 
                     // of the requirement.

                     bool meaningMayHaveChanged = false;
                     bool statusMayHaveChanged = false;
                     
                     string ea_req_name = rq_obj.tag + " " + rq_obj.name;

                     if ( ea_req.Name.CompareTo(ea_req_name) != 0 )
                     {
                        changeLog.Notes += ea_req.ElementGUID + ":Name:" + ea_req.Name + ":" + ea_req_name + "\r\n";
                        changeLog.Update();
                        meaningMayHaveChanged = true;
                        ea_req.Name  = ea_req_name;
                     }

                     if ( ea_req.Notes.CompareTo(rq_obj.text) != 0 )
                     {
                        changeLog.Notes += ea_req.ElementGUID + ":Notes:" + ea_req.Notes + ":" + rq_obj.text + "\r\n";
                        changeLog.Update();
                        meaningMayHaveChanged = true;
                        ea_req.Notes = rq_obj.text;
                     }

                     if (ea_req.Difficulty != rq_obj.difficulty)
                     {
                        changeLog.Notes += ea_req.ElementGUID + ":Difficulty:" + ea_req.Difficulty + ":" + rq_obj.difficulty + "\r\n";
                        changeLog.Update();
                        statusMayHaveChanged = true;
                        ea_req.Difficulty    = rq_obj.difficulty;
                     }
                     if (ea_req.Priority != rq_obj.priority)
                     {
                        changeLog.Notes += ea_req.ElementGUID + ":Priority:" + ea_req.Priority + ":" + rq_obj.priority + "\r\n";
                        changeLog.Update();
                        statusMayHaveChanged = true;
                        ea_req.Priority      = rq_obj.priority;
                     }
                     if (ea_req.Version != rq_obj.version)
                     {
                        changeLog.Notes += ea_req.ElementGUID + ":Version:" + ea_req.Version + ":" + rq_obj.version + "\r\n";
                        changeLog.Update();
                        statusMayHaveChanged = true;
                        ea_req.Version       = rq_obj.version;
                     }
                     if (ea_req.Status != rq_obj.status)
                     {
                        changeLog.Notes += ea_req.ElementGUID + ":Status:" + ea_req.Status + ":" + rq_obj.status + "\r\n";
                        changeLog.Update();
                        statusMayHaveChanged = true;
                        ea_req.Status        = rq_obj.status;
                     }

                     ea_req.Update();

                     if (meaningMayHaveChanged)
                     {
                        modifiedRequirementCount++;
                     }
                     
                     if (statusMayHaveChanged)
                     {
                        statusUpdatedRequirementCount++;
                     }
                  }
                  else
                  {
                     // 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 package
                     EA.Element ea_req = (EA.Element)importPackage.Elements.AddNew( rq_obj.tag + " " + rq_obj.name, "Requirement");
                     ea_req.Notes = rq_obj.text;

                     ea_req.Difficulty = rq_obj.difficulty;
                     ea_req.Priority   = rq_obj.priority;
                     ea_req.Version    = rq_obj.version;
                     ea_req.Status     = rq_obj.status;

                     EA_Utils.WriteTag(ea_req, "GUID", rq_obj.guid);
                     EA_Utils.WriteTag(ea_req, "TAG", rq_obj.tag);
                     ea_req.Update();

                     // Connect the new requirement to the ea_rq_artifact
                     RQ_Artifact.add_connection(ea_repository, ea_rq_artifact, ea_req);

                     changeLog.Notes += ea_req.ElementGUID + ":Created:" + ea_req.Name + "\r\n";
                     changeLog.Update();
                  }
                  
               }
               catch (Exception ex)
               {
                  ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Exception (ImportFromReqPro), " + ex.Message + ", " + rq_obj.name, -1);
               }
            }


            // Now check for orphaned EA requirements
            for (int i = 0; i < ea_GUIDs.Count; i++)
            {
               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();
                  }
               }
            }

            displayChangeLog(ea_repository, changeLog);

            ea_repository.RefreshModelView(parentPackage.PackageID);

            ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, newRequirementCount.ToString() + " new requirements", -1);
            ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, modifiedRequirementCount.ToString() + " requirements modified", -1);
            ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, statusUpdatedRequirementCount.ToString() + " requirements had status changes", -1);
            ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, orphanedCount.ToString() + " requirements were orphaned", -1);

            ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Completed import", -1);

            MessageBox.Show("Import Completed");
         }
         catch (Exception ex)
         {
            MessageBox.Show(ex.Message, "Error (ImportFromReqPro)", MessageBoxButtons.OK);
         }
      }




      /// <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(EA.Repository ea_repository, ref ArrayList rq_req_collection)
      {
         foreach( ReqPro_object sub_obj in rq_root_package.ReqPro_objects )
         {
            get_rq_req_collection(ea_repository, ref rq_req_collection, sub_obj);
         }
      }

      private void get_rq_req_collection(EA.Repository ea_repository,
                                         ref ArrayList rq_req_collection,
                                         ReqPro_object rq_obj )
      {
         if (rq_obj.isPackage)
         {
            if (rq_obj.filtered == false)
            {
               // Using recursion, scan this objects sub-objects
               foreach( ReqPro_object sub_obj in rq_obj.ReqPro_objects )
               {
                  get_rq_req_collection(ea_repository, 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 accumulating
               rq_req_collection.Add(rq_obj);

               // Using recursion, scan this objects sub-objects
               foreach( ReqPro_object sub_obj in rq_obj.ReqPro_objects )
               {
                  get_rq_req_collection(ea_repository, ref rq_req_collection, sub_obj);
               }
            }
         }
      }


        }
}