Subversion Repositories DevTools

Rev

Rev 2094 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using Word;

namespace EA_DocGen
{
   /// <summary>
   /// A Base Class designed to work with the findAndProcessPackageElements method.
   /// Users will normally derive their own classes from this and add real functionality
   /// to the over-ridable methods the base class provides.
   /// </summary>
   public class EA_UtilitiesRecursionWorker
   {
      public EA_UtilitiesRecursionWorker()
      {
      }

      public virtual void processElement( EA.Element theElement )
      {
      }
      public virtual void processPackage( EA.Package thePackage )
      {
      }
   }

        /// <summary>
        /// Class containing functions that implement the EA_DocGen menu items.
        /// </summary>
        public class EA_Utilities
        {
      // Data
      public EA_DocGenOptions options = null;

      public GUID_Clipboard guid_clipboard = null;

      private EA.Repository EA_Repository = null;

      public EA_Finders EA_Finder = null;

      private EA_RelationshipMatrix EA_RelMatrix = null;

      // Operations

                public EA_Utilities()
                {
         guid_clipboard = new GUID_Clipboard(this);

         options = new EA_DocGenOptions(this);

         EA_Finder = new EA_Finders();
   
         EA_RelMatrix = new EA_RelationshipMatrix(this);
                }
     
      public void accept_EA_RepositoryRef(EA.Repository EA_RepositoryRef)
      {
         EA_Repository = EA_RepositoryRef;
         EA_Finder.accept_EA_RepositoryRef(EA_RepositoryRef);
      }




      /// <summary>
      /// Begins the document generation process, opening the dialog to capture input/output
      /// files. the createWordDoc dialog class does most of the work ofcoarse.
      /// </summary>
      public void createWordDoc()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            // Make sure this package has an EA_DocGen element
            if (true == options.lookForAndProcess_EA_DocGen_Element( ((EA.Package)obj) ))
            {
               // bring up the dialog for doc generation              
               createWordDoc dialog = new createWordDoc(EA_Repository, ((EA.Package)obj), this);
               dialog.Text = "Generate Document From Model Layout";
               dialog.ShowDialog();                  }
            else
            {
               MessageBox.Show("EA_DocGen document model packages must contain an EA_DocGen element");
            }
         }
         else
         {
            MessageBox.Show("You must select a package whose content is to be formed into a document");
         }      
      }


   
 
 


      /// <summary>
      /// Creates a package under the specified parent package. the package is given a tree position
      /// as specified in order for it to be ordered in the model as the caller requires.
      /// </summary>
      /// <param name="parentPackage"></param>
      /// <param name="name"></param>
      /// <param name="treePos"></param>
      /// <returns></returns>
      public EA.Package createPackage(EA.Package parentPackage, string name, int treePos)
      {
         EA.Package newobj = (EA.Package)parentPackage.Packages.AddNew(name, "Package");
         newobj.TreePos = treePos;
         newobj.Update();
         return newobj;
      }
  

      /// <summary>
      /// Generate a package hierarchy that reflects the document layout of BMS-00289, the
      /// ERG Product Software Design Document.
      /// </summary>
      public void create_BMS00289_Layout()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            if (  ((EA.Package)obj).Packages.Count == 0 
               && ((EA.Package)obj).Elements.Count == 0
               && ((EA.Package)obj).Diagrams.Count == 0 )
            {
               EA.Package parentPackage = ((EA.Package)obj);

               EA.Package newobj = null;
               EA.Package newobj2 = null;
               EA.Package newobj3 = null;
               EA.Package newobj4 = null;
               EA.Package newobj5 = null;

               newobj  = createPackage(parentPackage, "Introduction", 1);
               newobj2 = createPackage(newobj, "Purpose", 1);
               newobj2 = createPackage(newobj, "Scope", 2);
               newobj2 = createPackage(newobj, "Terminology", 3);
               newobj2 = createPackage(newobj, "References", 4);

               newobj  = createPackage(parentPackage, "Design Assumptions And Constraints", 2);

               newobj  = createPackage(parentPackage, "System Overview", 3);

               newobj  = createPackage(parentPackage, "High Level Design", 4);
               newobj2 = createPackage(newobj, "Software Architecture", 1);
               newobj2 = createPackage(newobj, "External Interfaces", 2);
               newobj2 = createPackage(newobj, "Internal Interfaces", 3);
               newobj2 = createPackage(newobj, "Memory And Processing Time Allocation", 4);
               newobj2 = createPackage(newobj, "Operational Modes", 5);
               newobj2 = createPackage(newobj, "Component Descriptions", 6);

               newobj = createPackage(parentPackage, "Detailed Design", 5);

               newobj = createPackage(parentPackage, "Unit Test Design",6);

               newobj2 = createPackage(newobj, "Test Data", 1);
               newobj2 = createPackage(newobj, "Test Stubs", 2);
               newobj2 = createPackage(newobj, "Other Elements", 3);
               newobj2 = createPackage(newobj, "Unit Test Traceability To Design", 4);
               newobj2 = createPackage(newobj, "Test Suites and Test Cases", 5);
               newobj3 = createPackage(newobj2, "Test Suite Name", 1);
               newobj4 = createPackage(newobj3, "Test Name", 1);
               newobj5 = createPackage(newobj4, "Description", 1);
               newobj5 = createPackage(newobj4, "Inputs", 2);
               newobj5 = createPackage(newobj4, "Expected Outputs", 3);

               newobj = createPackage(parentPackage, "Requirements Traceability", 7);

               // create an EA_DocGen element
               EA.Element newElement = (EA.Element)parentPackage.Elements.AddNew( "EA_DocGen", "InformationItem" );
               newElement.Update();

               parentPackage.Packages.Refresh();

               // refresh project browser view
               EA_Repository.RefreshModelView(parentPackage.PackageID);
            }
            else
            {
               MessageBox.Show("Can only insert layout into an empty package");
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the layout will be inserted");
         }
      }


  
      /// <summary>
      /// This function works its way up the parental hierarchy to the root model, collecting the
      /// strings names of each package and prepending them to a string accumulator with a view to 
      /// obtaing the full path of an element,diagram, or package.
      /// </summary>
      /// <param name="parentId"></param>
      /// <param name="instr"></param>
      /// <returns></returns>
      public string GetPackagePath( int parentId, string instr )
      {
         if (parentId != 0)
         {
            EA.Package pkg;
            pkg = EA_Repository.GetPackageByID( parentId );
            if (pkg != null)
            {
               instr = pkg.Name + "/" + instr;
               instr = GetPackagePath(pkg.ParentID, instr);
            }        
         }
         return instr;
      }



      private void copy_GUID_to_clipboard(object obj, EA.ObjectType objType, bool confirm)
      {
         guid_clipboard.copy(obj, objType, confirm);
      }

      /// <summary>
      /// For the selected item, copy its GUID to the internal add-in GUID clipboard and the 
      /// global external clipboard. Also copy to the internal add-in GUID clipboard, the name of
      /// the element and its type. This will support the AddLinkElement() method.
      /// </summary>
      public void copy_GUID_to_clipboard()
      {
         EA.ObjectType objType;
         object obj;
         objType = EA_Repository.GetTreeSelectedItem( out obj );
         guid_clipboard.copy(obj, objType, true);
      }



      private void AddLinkElement(object obj, EA.ObjectType objType)
      {
         if (objType == EA.ObjectType.otPackage)
         {
            object newobj = null;

            if (guid_clipboard.objType == EA.ObjectType.otPackage)
            {
               newobj = ((EA.Package)obj).Elements.AddNew("EA_DocGenPackageLink - " + guid_clipboard.name, "InformationItem");
            }
            else if (guid_clipboard.objType == EA.ObjectType.otDiagram)
            {
               newobj = ((EA.Package)obj).Elements.AddNew("EA_DocGenDiagramLink - " + guid_clipboard.name, "InformationItem");
            }
            else if (guid_clipboard.objType == EA.ObjectType.otElement)
            {
               newobj = ((EA.Package)obj).Elements.AddNew("EA_DocGenElementLink - " + guid_clipboard.name, "InformationItem");
            }
            else if (guid_clipboard.objType == EA.ObjectType.otAttribute)
            {
               MessageBox.Show("Attribute links are not currently supported");
            }
            else if (guid_clipboard.objType == EA.ObjectType.otMethod)
            {
               MessageBox.Show("Method links are not currently supported");
            }

            if (newobj != null)
            {
               ((EA.Element)newobj).Notes = guid_clipboard.name + "\r\n" + guid_clipboard.guid;
               ((EA.Element)newobj).Update();
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the link will be inserted");
         }      
      }

      /// <summary>
      /// Using the internal add-in GUID clipboard, paste a link element into the selected
      /// package.
      /// </summary>
      public void AddLinkElement()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );

         AddLinkElement(obj, objType);
      }


      public void GeneratePackagesFromElementList()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            if (guid_clipboard.objType == EA.ObjectType.otPackage)
            {
               EA.Package theFoundPackage = EA_Finder.findPackageInRepositoryByGUID(guid_clipboard.guid);
               if (theFoundPackage != null)
               {
                  EA.Package srcPackage = theFoundPackage;
                  EA.Package destPackage = ((EA.Package)obj);
                     
                  foreach( EA.Element srcElement in srcPackage.Elements)
                  {
                     if (false == EA_Finder.elementNameExistsInPackage(destPackage, srcElement.Name.ToString()))
                     {
                        createPackage(destPackage, srcElement.Name.ToString(), 1);
                     }
                  }
               }               
            }
            else
            {
               MessageBox.Show("You must first copy a Source Package GUID to the clipboard");
            }
         }
         else
         {
            MessageBox.Show("You must select a Destination Package into which the Generated Packages will be inserted");
         }
      }


      public void GenerateSubPackageLinks()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            if (guid_clipboard.objType == EA.ObjectType.otPackage)
            {
               EA.Package theFoundPackage = EA_Finder.findPackageInRepositoryByGUID(guid_clipboard.guid);
               if (theFoundPackage != null)
               {
                  EA.Package srcPackage = theFoundPackage;
                  EA.Package destPackage = ((EA.Package)obj);
                       
                  foreach( EA.Package subPackage in srcPackage.Packages)
                  {
                     copy_GUID_to_clipboard( (object)subPackage, EA.ObjectType.otPackage, false);
                     AddLinkElement( (object)destPackage, EA.ObjectType.otPackage);
                  }
               }               
            }
            else
            {
               MessageBox.Show("You must first copy a Source Package GUID to the clipboard");
            }
         }
         else
         {
            MessageBox.Show("You must select a Destination Package into which the Sub-Package Links will be inserted");
         }
      }


      public void GenerateElementLinks()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            if (guid_clipboard.objType == EA.ObjectType.otPackage)
            {
               EA.Package theFoundPackage = EA_Finder.findPackageInRepositoryByGUID(guid_clipboard.guid);
               if (theFoundPackage != null)
               {
                  EA.Package srcPackage = theFoundPackage;
                  EA.Package destPackage = ((EA.Package)obj);
                       
                  foreach( EA.Element subElement in srcPackage.Elements)
                  {
                     copy_GUID_to_clipboard( (object)subElement, EA.ObjectType.otElement, false);
                     AddLinkElement( (object)destPackage, EA.ObjectType.otPackage);
                  }
               }               
            }
            else
            {
               MessageBox.Show("You must first copy a Source Package GUID to the clipboard");
            }
         }
         else
         {
            MessageBox.Show("You must select a Destination Package into which the Element Links will be inserted");
         }      
      }


      public void AddTableElement()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            object newobj = null;

            newobj = ((EA.Package)obj).Elements.AddNew("EA_DocGenTable", "InformationItem");
            if (newobj != null)
            {
               ((EA.Element)newobj).Notes = "title=insertYourTitleHere\r\n"
                                          + "columns=2\r\n"
                                          + "seperator=,\r\n"
                                          + "column1Title,column2Title\r\n"
                                          + "cellcontent,cellcontent\r\n"
                                          + "etc,etc";
               ((EA.Element)newobj).Update();
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the Table Element will be inserted");
         }      
      }

      
      public void AddTextElement()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            object newobj = null;

            newobj = ((EA.Package)obj).Elements.AddNew("EA_DocGenText", "InformationItem");
            if (newobj != null)
            {
               ((EA.Element)newobj).Notes = "Add your text here.";
               ((EA.Element)newobj).Update();
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the Text Element will be inserted");
         }            
      }

      public void AddRelationshipMatrixElement()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            object newobj = null;

            newobj = ((EA.Package)obj).Elements.AddNew("EA_DocGenRelationshipMatrix", "InformationItem");
            if (newobj != null)
            {
               ((EA.Element)newobj).Notes  = EA_RelMatrix.optionTemplateForRelationshipMatrix();
               ((EA.Element)newobj).Update();
            }
         }
         else
         {
            MessageBox.Show("You must select a package into which the RelationshipMatrix Element will be inserted");
         }         
      }

      /// <summary>
      /// This function is designed to parse EA models/packages in a predefined way, whilst allowing
      /// a user to specify what processing is to be done upon or with each element found.
      /// </summary>
      /// <param name="thePackage"></param>
      /// <param name="worker"></param>
      /// <param name="recurse"></param>
      public void findAndProcessPackageElements( EA.Package thePackage, EA_UtilitiesRecursionWorker worker, bool recurse)
      {
         worker.processPackage( thePackage );

         foreach (EA.Element theElement in thePackage.Elements)
         {
            worker.processElement( theElement );
         }

         if (recurse == true)
         {
            foreach (EA.Package subPackage in thePackage.Packages)
            {
               // RECURSION
               findAndProcessPackageElements( subPackage, worker, true);
            }         
         }

      }




      #region Temporary or experimental code



      /// <summary>
      /// Parse a package structure in a similar way to that taken when users are generating a word
      /// document, only here we simply write diagnostic text to the output tab.
      /// </summary>
      /// <param name="thePackage"></param>
      /// <param name="recurse_level"></param>
      private void parse_package(EA.Package thePackage, int recurse_level)
      {
         recurse_level++;

         EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, 
                                      "Level:"
                                    + recurse_level.ToString() 
                                    + ", ParentID:"
                                    + thePackage.ParentID.ToString()
                                    + ", PackageID:"
                                    + thePackage.PackageID.ToString()
                                    + ",  PACKAGE: " 
                                    + thePackage.Name, -1);

         // default handling of diagrams
         foreach(EA.Diagram theDiagram in thePackage.Diagrams)
         {
            EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, 
                                         "Level:"
                                       + recurse_level.ToString() 
                                       + ", ParentID:"
                                       + theDiagram.ParentID.ToString()
                                       + ", PackageID:"
                                       + theDiagram.PackageID.ToString()
                                       + ", DiagramID:"
                                       + theDiagram.DiagramID.ToString()
                                       + "  DIAGRAM: " 
                                       + theDiagram.Name, -1);
         }

         EA_ElementSorter elementSorter = new EA_ElementSorter(thePackage);
         EA.Element theElement = null;
         int theElementsRelativeLevel = 0;
         if (true == elementSorter.getFirst(ref theElement, ref theElementsRelativeLevel))
         {
            do
            {
               int theElementsRecurseLevel = recurse_level + theElementsRelativeLevel;

               EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, 
                    "Level:"
                  + theElementsRecurseLevel.ToString()
                  + ", "
                  + theElement.TreePos.ToString() 
                  + ", ParentID:"
                  + theElement.ParentID.ToString()
                  + ", PackageID:"
                  + theElement.PackageID.ToString()
                  + ", ElementID:"
                  + theElement.ElementID.ToString()
                  + ",  ELEMENT: " 
                  + theElement.Name, -1);

            } while (true == elementSorter.getNext(ref theElement, ref theElementsRelativeLevel));
         }

         foreach(EA.Package lowerLevelPackage in thePackage.Packages)
         {
            parse_package(lowerLevelPackage, recurse_level);
         }

         recurse_level--;
      }


      /// <summary>
      /// Parses a specified package in a similar way to what would be done if a user were generating
      /// a word document, but here do nothing except write diagnostic text to the output tab.
      /// </summary>
      public void showDiscoveryOrder()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            EA.Package EA_ParentPackage = ((EA.Package)obj);

            EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Showing Object Discovery order for " + EA_ParentPackage.Name, -1);

            foreach(EA.Package thePackage in EA_ParentPackage.Packages)
            {
               parse_package(thePackage, 0);
            }
         }
         else
         {
            MessageBox.Show("You must select a package to parse");
         }
      }


      public void SaveVersionControlledPackage()
      {
         EA.ObjectType objType;
         object obj;

         objType = EA_Repository.GetTreeSelectedItem( out obj );
         if (objType == EA.ObjectType.otPackage)
         {
            EA.Package EA_Package = ((EA.Package)obj);

            if (EA_Package.IsControlled == true)
            {
               EA.Project EA_Project = EA_Repository.GetProjectInterface();

               EA_Project.SaveControlledPackage( EA_Package.PackageGUID );

               //EA_Project.SaveControlledPackage( EA_Project.GUIDtoXML( EA_Package.PackageGUID ) );
            }
            else
            {
               MessageBox.Show("The package selected is not version controlled");
            }
          }
         else
         {
            MessageBox.Show("You must select a package");
         }            
      }


      #endregion
        }

  
}