Subversion Repositories DevTools

Rev

Rev 2141 | 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>
        /// CopyReqProDatabase is a specialisation of ReqProParser, designed to copy the 
        /// ReqPro database content into an EA database, maintaining the structure of 
        /// and hierarchy of packages and requirements found in the ReqPro database.
        /// </summary>
   public class CopyReqProDatabase : CopyReqProDatabaseToMemory
   {
      /// <summary>
      /// Construct the object
      /// </summary>
      /// <param name="ea_repository"></param>
      public CopyReqProDatabase(EA.Repository ea_repository): base(ea_repository)
      {
         try
         {
            if (ea_rootPackage.Elements.Count > 0
               || ea_rootPackage.Packages.Count > 0)
            {
               DialogResult dlgRes = MessageBox.Show("Package is not empty, delete existing content first?", "Confirm", MessageBoxButtons.YesNo);
               if (dlgRes == DialogResult.Yes)
               {
                  // Delete packages and requirement elements
                  short i;
                  for(i=0; i < ea_rootPackage.Packages.Count; i++)
                  {
                     ea_rootPackage.Packages.Delete(i);
                  }
                  for(i=0; i < ea_rootPackage.Elements.Count; i++)
                  {
                     if ( ((EA.Element)ea_rootPackage.Elements.GetAt(i)).Type.StartsWith("Requirement") )
                     {
                        ea_rootPackage.Elements.Delete(i);
                     }
                  }
                  ea_rootPackage.Packages.Refresh();
                  // refresh project browser view
                  ea_repository.RefreshModelView(ea_rootPackage.PackageID);               }
            }
         }
         catch (Exception ex)
         {
            MessageBox.Show(ex.Message, "Error (CopyReqProDatabase::CopyReqProDatabase)", MessageBoxButtons.OK);
         }           
      }


      /// <summary>
      /// Method to parse a ReqPro database and copy it into an EA database.
      /// </summary>
      /// <param name="ea_repository"></param>
      /// <returns></returns>
      public override bool prompt_and_parse(EA.Repository ea_repository)
      {
         try
         {
            // use the base classes parser to read the ReqPro database content and allow the user to
            // filter it.
            if (true == base.prompt_and_parse(ea_repository))
            {
               ea_repository.EnsureOutputVisible(Main.GUI_OUTPUT_TAB_NAME);

               // write the captured info from reqpro, into the ea database, obeying the filter
               // settings the user has specified.
               write_ea_database(ea_repository);
               MessageBox.Show("Import Completed");
               return true;
            }
         }
         catch (Exception ex)
         {
            MessageBox.Show(ex.Message, "Error (CopyReqProDatabase::parse)", MessageBoxButtons.OK);
         }      
         return false;
      }
 

   
      /// <summary>
      /// Finds and returns the ReqPro_object instance that contains the specified GUID. The
      /// GUID is that of the originating ReqPro requirement, which is copied into a ReqPro_object
      /// instance when it is created. This find operation supports the ability to resolve object to
      /// object trace relationships mirrored from the ReqPro database during the construction
      /// of the ReqPro_object hierarchy.
      /// </summary>
      /// <param name="rq_obj"></param>
      /// <param name="ReqProGUID"></param>
      /// <returns></returns>
      private ReqPro_object findReqPro_object_byReqProGUID(ReqPro_object rq_obj, string ReqProGUID)
      {
         if (rq_obj.guid.CompareTo(ReqProGUID) == 0)
         {
            return rq_obj;
         }

         foreach (ReqPro_object sub_rq_obj in rq_obj.ReqPro_objects)
         {
            ReqPro_object tgt_obj = findReqPro_object_byReqProGUID(sub_rq_obj, ReqProGUID);
            if (tgt_obj != null)
               return tgt_obj;
         }

         return null;
      }


      /// <summary>
      /// This method examines all of the ReqPro_object trace relationships and mirrors them in 
      /// the EA requirement elements that have been formed from each un-filtered ReqPro_objects.
      /// </summary>
      /// <param name="ea_repository"></param>
      /// <param name="rq_obj"></param>
      private void write_traces(EA.Repository ea_repository, ReqPro_object rq_obj)
      {
         if (rq_obj.isRequirement)
         {
            // if this object had an EA element made for it during the write_ea_database() process...
            if (rq_obj.ea_element_ID != -1)
            {
               EA.Element ea_rq_obj = ea_repository.GetElementByID(rq_obj.ea_element_ID);
               if (ea_rq_obj != null)
               {
                  foreach(string s in rq_obj.ReqPro_traces)
                  {
                     ReqPro_object tgt_obj = findReqPro_object_byReqProGUID(rq_root_package,s);
                     if (tgt_obj != null)
                     {
                        if (tgt_obj.ea_element_ID != -1)
                        {
                           EA.Element ea_tgt_obj = ea_repository.GetElementByID(tgt_obj.ea_element_ID);
                           if (ea_tgt_obj != null)
                           {
                              add_connection(ea_repository, ea_rq_obj, ea_tgt_obj);
                           }
                        }
                     }
                  }
               }
            }
         }

         // recurse to ensure we examine the entire hiearchy
         foreach(ReqPro_object sub_obj in rq_obj.ReqPro_objects)
         {
            write_traces(ea_repository, sub_obj);
         }
      }

      /// <summary>
      /// Adds a connection between one EA element and another
      /// </summary>
      /// <param name="repository"></param>
      /// <param name="rq_artifact"></param>
      /// <param name="ea_req"></param>
      private void add_connection(EA.Repository ea_repository, EA.Element src_element, EA.Element dest_element)
      {
         // Add the new requirement to the src_element
         EA.Connector c = (EA.Connector)src_element.Connectors.AddNew("", "Relationship");
         c.SupplierID = dest_element.ElementID;
         //c.Stereotype = "trace";
         c.Direction = "Source -> Destination";
         if (false == c.Update())
         {
            ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "New Connector Error : " + c.GetLastError(), dest_element.ElementID );
         }
         src_element.Connectors.Refresh();
      }

      /// <summary>
      /// This method (along with its sibling overloads) perform the copy operation once
      /// the ReqPro database content has been acquired, and the user has submitted their
      /// filtering requirements. This method begins the copy operation, but the real nitty
      /// gritty of it occurs in the other overloaded method.
      /// </summary>
      /// <param name="ea_repository"></param>
      private void write_ea_database(EA.Repository ea_repository)
      {
         ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Copying ReqPro Database Content to EA", -1);

         foreach( ReqPro_object sub_obj in rq_root_package.ReqPro_objects )
         {
            write_ea_database(ea_repository, ea_rootPackage, null, sub_obj);
         }

         ea_rootPackage.Packages.Refresh();
         // refresh project browser view
         ea_repository.RefreshModelView(ea_rootPackage.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.
         ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Writing Trace Information", -1);
         foreach( ReqPro_object sub_obj in rq_root_package.ReqPro_objects )
         {
            write_traces(ea_repository, sub_obj);
         }
      }

      private void write_ea_database(EA.Repository ea_repository,
                                     EA.Package ea_parent_package,
                                     EA.Element ea_parent_element,
                                     ReqPro_object rq_obj )
      {
         if (rq_obj.isPackage)
         {
            if (rq_obj.filtered == false)
            {
               if (ea_parent_package != null)
               {
                  // create a representative package in EA
                  EA.Package new_ea_package = EA_Utils.createPackage(ea_parent_package, rq_obj.name, rq_obj.treePos);
                  rq_obj.ea_element_ID = new_ea_package.PackageID;
                  ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Created Package : " + rq_obj.name, new_ea_package.PackageID );

                  // Using recursion, scan this objects sub-objects
                  foreach( ReqPro_object sub_obj in rq_obj.ReqPro_objects )
                  {
                     write_ea_database(ea_repository, new_ea_package, null, sub_obj);
                  }
               }
               else
               {
                  // should never get here - I have never seen it happen so far.
                  ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "ERROR,write_ea_database, parent package was null", -1);
               }
            }
            else if (base.allowPackageStructureFragments)
            {
               // Using recursion, scan this objects sub-objects
               foreach( ReqPro_object sub_obj in rq_obj.ReqPro_objects )
               {
                  if (sub_obj.isPackage)
                     write_ea_database(ea_repository, ea_parent_package, null, sub_obj);
               }
            }
         }
         else if (rq_obj.isRequirement)
         {
            if ( ! (reqTypeIsFiltered(rq_obj) || reqStatusTypeIsFiltered(rq_obj)))
            {
               string rq_obj_name = rq_obj.tag + " " + rq_obj.name;

               // If needed, create the requirement element as a child of a parent element
               if (ea_parent_element != null)
               {
                  // create a representative element in EA
                  EA.Element new_ea_element = (EA.Element)ea_parent_element.Elements.AddNew(rq_obj_name, "Requirement");
                  rq_obj.ea_element_ID = new_ea_element.ElementID;
                  ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Created Element : " + rq_obj_name, new_ea_element.ElementID );

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

                  // If the ReqPro requirements detailed text is more than what the name already contains (allowing for it
                  // to have a stop character that the name may not have) then copy it over, otherwise ignore it. This
                  // prevents the same text appearing twice in any generated document made using EA_DocGen.
                  if (!rq_obj.text.StartsWith(rq_obj.name) || (rq_obj.text.Length > (rq_obj.name.Length + 1)))
                     new_ea_element.Notes = rq_obj.text;

                  new_ea_element.TreePos = rq_obj.treePos;
                  new_ea_element.Status  = rq_obj.status;
                  new_ea_element.Update();

                  // Write EA tag information exactly as is done for the import for traceability use. This 
                  // opens up the possibility that a document model import could be converted into a traceability
                  // use model in some future update to EA_ReqPro addin.
                  EA_Utils.WriteTag(new_ea_element, "GUID", rq_obj.guid);
                  EA_Utils.WriteTag(new_ea_element, "TAG", rq_obj.tag);

                  // Using recursion, scan this objects sub-objects
                  foreach( ReqPro_object sub_obj in rq_obj.ReqPro_objects )
                  {
                     write_ea_database(ea_repository, null, new_ea_element, sub_obj);
                  }
               }
               // else create the requirement element as a child of a parent package
               else if (ea_parent_package != null)
               {
                  // create a representative element in EA
                  EA.Element new_ea_element = (EA.Element)ea_parent_package.Elements.AddNew(rq_obj_name, "Requirement");
                  rq_obj.ea_element_ID = new_ea_element.ElementID;
                  ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Created Element : " + rq_obj_name, new_ea_element.ElementID );

                  // If the ReqPro requirements detailed text is more than what the name already contains (allowing for it
                  // to have a stop character that the name may not have) then copy it over, otherwise ignore it. This
                  // prevents the same text appearing twice in any generated document made using EA_DocGen.
                  if (!rq_obj.text.StartsWith(rq_obj.name) || (rq_obj.text.Length > (rq_obj.name.Length + 1)))
                     new_ea_element.Notes = rq_obj.text;

                  new_ea_element.TreePos = rq_obj.treePos;
                  new_ea_element.Status  = rq_obj.status;
                  new_ea_element.Update();

                  // Write EA tag information exactly as is done for the import for traceability use. This 
                  // opens up the possibility that a document model import could be converted into a traceability
                  // use model in some future update to EA_ReqPro addin.
                  EA_Utils.WriteTag(new_ea_element, "GUID", rq_obj.guid);
                  EA_Utils.WriteTag(new_ea_element, "TAG", rq_obj.tag);

                  // Using recursion, scan this objects sub-objects
                  foreach( ReqPro_object sub_obj in rq_obj.ReqPro_objects )
                  {
                     write_ea_database(ea_repository, null, new_ea_element, sub_obj);
                  }
               }
               else
               {
                  // should never get here - I have never seen it happen so far.
                  ea_repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "ERROR,write_ea_database, parent package was null", -1);
               }
            }
         }
      }




        }


}