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>
        /// The ReqProDB_Artifact class contains methods supporting the use of the ReqProDB 
        /// element used to control ReqPro imports for requirement-to-design tracability 
        /// purposes. 
        /// </summary>
        public class ReqProDB_Artifact
        {
      private ReqPro40.Application reqPro;

      /// <summary>
      /// Constructor logic
      /// </summary>
                public ReqProDB_Artifact()
                {
         reqPro = new ReqPro40.ApplicationClass();
                }

  
      /// <summary>
      /// This method is used to open a ReqPro database file indicated by a given ReqProDB artifact
      /// element.
      /// </summary>
      /// <param name="ea_repository"></param>
      /// <param name="rq_artifact"></param>
      /// <returns></returns>
      public ReqPro40.Project OpenReqProProject(EA.Repository ea_repository, EA.Element rq_artifact)
      {
         EA_Utilities EA_Utils = new EA_Utilities(ea_repository);

         Logon logon = new Logon(EA_Utils.ReadTag(rq_artifact, "Username"));
         
         DialogResult dlgRes = logon.ShowDialog();
         if (dlgRes == DialogResult.OK)
         {
            ReqPro40.Project project = reqPro.OpenProject(
               EA_Utils.ReadTag(rq_artifact, "Location"),
               ReqPro40.enumOpenProjectOptions.eOpenProjOpt_RQSFile,
               EA_Utils.ReadTag(rq_artifact, "Username"),
               logon.ebPassword.Text, 
               enumProjectFlags.eProjFlag_Normal,
               enumRelatedProjectOptions.eRelatedProjOption_ConnectAsSpecified);

            if (project == null)
            {
               MessageBox.Show("ERROR: Cannot establish connection to ReqPro database.\n\n" +
                  "Check the tagged values in the artifact are correct.\n" +
                  "Check the database still exists or is not locked by\n" +
                  "another user, or has not been password protected." );
            }

            return project;
         }
         return null;
      }


      /// <summary>
      /// This method is used to return the ReqProDB artifact, on the assumption that it is the
      /// item the user has highlighted in EA's project browser prior to initiating the process
      /// leading up to the call to this method.
      /// </summary>
      /// <param name="ea_repository"></param>
      /// <returns></returns>
      public EA.Element get_rq_artifact(EA.Repository ea_repository)
      {
         // assumes this method will only be used when user has selected a ReqProDB element
         // in the project browser.
         return (EA.Element)ea_repository.GetTreeSelectedObject();
      }


      /// <summary>
      ///This method is used to associate a package in EA with a specific ReqPro database
      ///file. 
      /// </summary>
      /// <param name="repository"></param>
      public void AssociatePackageToReqProDatabase(EA.Repository ea_repository)
      {
         object o;
         EA.ObjectType type = ea_repository.GetTreeSelectedItem(out o);
         if (type == EA.ObjectType.otPackage)
         {
            EA.Package package = (EA.Package)o;

            try
            {
               // Get user to identify the ReqPro database file
               OpenFileDialog ofd = new OpenFileDialog();
               ofd.Title = "Select Requisite Pro project file";
               ofd.Filter = "ReqPro files (*.rqs)|*.rqs|All files (*.*)|*.*";
               DialogResult dlgRes = ofd.ShowDialog();

               if (dlgRes == DialogResult.OK)
               {
                  // Get user to login to the database
                  Logon logon = new Logon("");
                  dlgRes = logon.ShowDialog();

                  if (dlgRes == DialogResult.OK)
                  {
                     string username = logon.ebUserName.Text;
                     string password = logon.ebPassword.Text;
                  
                     // attempt to test if the database can be opened
                     ReqPro40.Project rq_project = reqPro.OpenProject(
                        ofd.FileName,
                        ReqPro40.enumOpenProjectOptions.eOpenProjOpt_RQSFile,
                        logon.ebUserName.Text,
                        logon.ebPassword.Text, 
                        enumProjectFlags.eProjFlag_Normal,
                        enumRelatedProjectOptions.eRelatedProjOption_ConnectAsSpecified);

                     if (rq_project != null)
                     {
                        // Success - add the RQ project to the EA repository
                        EA.Element rq_artifact = create_rq_artifact( ea_repository, 
                           package, 
                           rq_project.Name, 
                           rq_project.Description, 
                           logon.ebUserName.Text, 
                           ofd.FileName, 
                           rq_project.GUID );

                        if (rq_artifact != null)
                        {
                           UpdatePackageToReqProDatabaseAssociation(ea_repository, rq_artifact);
                        }

                        rq_project.CloseProject();
                     }
                  }
               }
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
            }
         }
      }


      /// <summary>
      /// This method updates a ReqProDB artifact elements internal data. Such an update may be
      /// needed if the user has deleted requirements from EA that are linked to the specified
      /// ReqProDB artifact.
      /// </summary>
      /// <param name="repository"></param>
      /// <param name="rq_artifact"></param>
      public void UpdatePackageToReqProDatabaseAssociation(EA.Repository repository, EA.Element rq_artifact)
      {
         repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Please Wait - Updating ReqProDB element", rq_artifact.ElementID);

         // Scan EA package to find all requirement elements, and add links to them
         // from the ReqPro artifact element we have written into the selected package.
         EA_Utilities EA_Utils = new EA_Utilities(repository);

         ArrayList allowedElementTypes = new ArrayList();
         allowedElementTypes.Add("Requirement");
         allowedElementTypes.Add("UseCase");

         ElementAccumulator reqLister = new ElementAccumulator(allowedElementTypes);

         EA.Package thePackage = repository.GetPackageByID( rq_artifact.PackageID );
         EA_Utils.findAndProcessPackageElements( thePackage, reqLister, true );

         // Get all existing connectors into a list
         ArrayList connectors = new ArrayList();
         foreach(EA.Connector theConnector in rq_artifact.Connectors)
         {
            connectors.Add(theConnector.SupplierID);
         }

         // For each element, if it is not already referred to by a connector, add a connector for it
         foreach (EA.Element theElement in reqLister.Elements)
         {
            if (!connectors.Contains(theElement.ElementID))
            {
               add_connection(repository, rq_artifact, theElement);
            }
         }
         rq_artifact.Connectors.Refresh();


         // Now remove any connectors that point to element IDs that are no longer in the package
         short i = 0;
         foreach (EA.Connector theConnector in rq_artifact.Connectors)
         {
            if (!reqLister.ElementIDs.Contains(theConnector.SupplierID))
            {
               rq_artifact.Connectors.Delete(i);
               rq_artifact.Connectors.Refresh();
            }
            else
            {
               i++;
            }
         }
         rq_artifact.Update();
         rq_artifact.Refresh();
      }


      /// <summary>
      /// This method is used to create a ReqProDB element in an EA package.
      /// Do not modify the tag names that are in current use. Only extend with
      /// additional new tags.
      /// </summary>
      /// <param name="ea_repository"></param>
      /// <param name="package"></param>
      /// <param name="name"></param>
      /// <param name="description"></param>
      /// <param name="reqpro_db_username"></param>
      /// <param name="reqpro_db_filename"></param>
      /// <param name="reqpro_db_guid"></param>
      /// <returns></returns>
      private EA.Element create_rq_artifact( EA.Repository ea_repository, 
         EA.Package package,
         string name,
         string description,
         string reqpro_db_username,
         string reqpro_db_filename,
         string reqpro_db_guid )
      {
         EA_Utilities EA_Utils = new EA_Utilities(ea_repository);

         package.Notes = description;
         package.Update();
            
         EA.Element element;
         element = (EA.Element)package.Elements.AddNew(name + " - " + reqpro_db_username, "Artifact");
         if (element != null)
         {
            element.Stereotype = "ReqProDB";
            element.Notes = description;

            EA_Utils.WriteTag( element, "Location", reqpro_db_filename);
            EA_Utils.WriteTag( element, "Username", reqpro_db_username);
            EA_Utils.WriteTag( element, "GUID", reqpro_db_guid);
            EA_Utils.WriteTag( element, "Enable_Update", "false");

            element.Update();
            element.Refresh();
            element.TaggedValues.Refresh();
            package.Packages.Refresh();
            package.Elements.Refresh(); 
            
            return element;
         }
         return null;
      }


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



        }
}