Subversion Repositories DevTools

Rev

Rev 2155 | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using ReqPro40;

namespace EA_ReqPro
{
        /// <summary>
        /// This class is a rather unsuccessful attempt to encapsulate ReqPro automation interface
        /// dependencies but unfortunately, outside of this class, many other classes still know about
        /// ReqPro types. A gradual movement of functionality into this class should be attempted over 
        /// time. Return of types objects should be altered to returning simple system.object values, etc.
        /// A series of collection parsing functions that use delegate callbacks could be written to pull
        /// in the functionality that exists outside of this class that depends on ReqPro types.
        /// </summary>
        public class ReqProDatabase
        {
      private static ReqPro40.Application RQ_app = null;
      private static ReqPro40.Project RQ_project = null;

      private static bool gotApplication()
      {
         if (RQ_app == null)
         {
            RQ_app = new ReqPro40.ApplicationClass();
         }
         if (RQ_app == null)
            return false;
         else
            return true;
      }

      private static bool gotProject()
      {
         if (RQ_project == null)
            return false;
         else
            return true;
      }


      public static bool opened()
      {
         return gotProject();
      }

      public static string name()
      {
         if (RQ_project != null)
            return RQ_project.Name;
         else
            return "";
      }

      public static string description()
      {
         if (RQ_project != null)
            return RQ_project.Description;
         else
            return "";
      }

      public static string guid()
      {
         if (RQ_project != null)
            return RQ_project.GUID;
         else
            return "";
      }

      public static ReqPro40.RootPackage get_root_package()
      {
         if (gotProject())
         {
            return RQ_project.GetRootPackage(true);
         }
         return null;
      }

      public static ReqPro40.Package get_package(int iKey)
      {
         if (gotProject())
         {
            return RQ_project.GetPackage(iKey, ReqPro40.enumPackageWeights.ePackageWeight_Empty);
         }
         return null;
      }

      public static ReqPro40.Requirement get_requirement_by_guid(string guid)
      {
         if (gotProject())
         {
            return RQ_project.GetRequirement(guid, ReqPro40.enumRequirementLookups.eReqLookup_GUID,
               ReqPro40.enumRequirementsWeights.eReqWeight_Heavy, 
               ReqPro40.enumRequirementFlags.eReqFlag_Empty);
         }
         return null;
      }

      public static ReqPro40.Requirement get_requirement_by_key(object key)
      {
         if (gotProject())
         {
            return 
               RQ_project.GetRequirement(key,
               ReqPro40.enumRequirementLookups.eReqLookup_Key,
               ReqPro40.enumRequirementsWeights.eReqWeight_Heavy,
               ReqPro40.enumRequirementFlags.eReqFlag_RetainHierarchy);
         }
         return null;
      }

      public static ReqPro40.ReqTypes get_requirement_types()
      {
         if (gotProject())
         {
            return RQ_project.ReqTypes;
         }
         return null;
      }

      public static ReqPro40.Requirements get_requirements()
      {
         if (gotProject())
         {
            return RQ_project.GetRequirements(
               null,
               ReqPro40.enumRequirementsLookups.eReqsLookup_All,
               ReqPro40.enumRequirementsWeights.eReqWeight_Heavy,
               ReqPro40.enumRequirementFlags.eReqFlag_Empty, 1000, 4);
         }
         return null;
      }


      
      public static bool open(string location, string user_name, string password)
      {
         if (gotApplication())
         {
            RQ_project = RQ_app.OpenProject(
               location,
               ReqPro40.enumOpenProjectOptions.eOpenProjOpt_RQSFile,
               user_name,
               password,
               enumProjectFlags.eProjFlag_Normal,
               enumRelatedProjectOptions.eRelatedProjOption_ConnectAsSpecified);
            if (RQ_project != null)
               return true;
         }
         return false;
      }

      public static void close()
      {
         if (RQ_project != null)
         {
            RQ_project.CloseProject();
            RQ_project = null;
            RQ_app = null;
         }
         else
         {
            RQ_app = null;
         }
      }

       
      public static ReqPro40.Package create_package(string name, string description)
      {
         ReqPro40.RootPackage rp_root_pkg = ReqProDatabase.get_root_package();
         if (rp_root_pkg != null)
         {
            return rp_root_pkg.CreatePackage(name, description, ReqPro40.enumPackageTypes.ePackageType_Empty);
         }
         return null;
      }

      public static ReqPro40.Package create_sub_package(ReqPro40.Package parent_pkg, string sub_pkg_name, string sub_pkg_description)
      {
         ReqPro40.Package sub_pkg = create_package(sub_pkg_name, sub_pkg_description);
         if (sub_pkg != null)
         {
            parent_pkg.AddElement(sub_pkg, 
               ReqPro40.enumPackageLookups.ePackageLookup_Object, 
               ReqPro40.enumElementTypes.eElemType_Package);

            return sub_pkg;
         }
         
         return null;
      }


      public static ReqPro40.Requirement add_requirement(object req_collection, string name, string description, string type)
      {
         return ((ReqPro40.Requirements)req_collection).Add(name, description, type,
            ReqPro40.enumReqTypesLookups.eReqTypesLookups_Prefix, 
            "1.0", "Version 1.0", 
            null, ReqPro40.enumRequirementLookups.eReqLookup_Empty);
      }

      public static ReqPro40.Requirement add_requirement(object req_collection, string name, string description, string type, ReqPro40.Requirement parent_rp_req)
      {
         return ((ReqPro40.Requirements)req_collection).Add(name, description, type,
            ReqPro40.enumReqTypesLookups.eReqTypesLookups_Prefix, 
            "1.0", "Version 1.0", 
            parent_rp_req, ReqPro40.enumRequirementLookups.eReqLookup_Object);
      }

      public static bool any_relationship_exists(ReqPro40.Requirement rp_req, string guid)
      {
         return (  traces_to_relationship_exists(rp_req, guid)
                || traces_from_relationship_exists(rp_req, guid)
                || child_relationship_exists(rp_req, guid));
      }

      public static bool child_relationship_exists(ReqPro40.Requirement rp_req, string guid)
      {
         int number_of_rels = 0;
         if (true == rp_req.get_HasChildren(ref number_of_rels))
         {
            ReqPro40.Relationships rp_req_c = (ReqPro40.Relationships)rp_req.Children;
            if (rp_req_c != null)
            {
               return relationship_exists(rp_req_c, number_of_rels, guid);
            }
         }
         return false;
      }

      public static bool traces_to_relationship_exists(ReqPro40.Requirement rp_req, string guid)
      {
         int number_of_rels = 0;
         if (true == rp_req.get_HasTracesTo(ref number_of_rels))
         {
            ReqPro40.Relationships rp_req_c = (ReqPro40.Relationships)rp_req.TracesTo;
            if (rp_req_c != null)
            {
               return relationship_exists(rp_req_c, number_of_rels, guid);
            }
         }
         return false;
      }

      public static bool traces_from_relationship_exists(ReqPro40.Requirement rp_req, string guid)
      {
         int number_of_rels = 0;
         if (true == rp_req.get_HasTracesFrom(ref number_of_rels))
         {
            ReqPro40.Relationships rp_req_c = (ReqPro40.Relationships)rp_req.TracesFrom;
            if (rp_req_c != null)
            {
               return relationship_exists(rp_req_c, number_of_rels, guid);
            }
         }
         return false;
      }

      private static bool relationship_exists(ReqPro40.Relationships rq_rels, int number_of_rels, string guid)
      {
         int i;
         rq_rels.MoveFirst();
         for (i = 0; i < number_of_rels; i++)
         {
            if (Main.mustAbort)
               break;

            ReqPro40.Relationship thisRelationship = rq_rels.GetCurrentRelationship();

            ReqPro40.Requirement subRequirement = 
               thisRelationship.get_DestinationRequirement(ReqPro40.enumRequirementsWeights.eReqWeight_Heavy);

            if (subRequirement != null)
            {
               if (subRequirement.GUID.Equals(guid))
               {
                  return true;
               }
            }

            rq_rels.MoveNext();
         }
         return false;
      }
 
        }
}