Subversion Repositories DevTools

Rev

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

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;
using System.IO;

namespace EA_ReqPro
{
   /// <summary>
   /// Summary description for ExportToReqProCSVForm.
   /// </summary>
   public class ExportToReqProCSVForm : System.Windows.Forms.Form
   {
      private StringBuilder csv_text;

      private System.Windows.Forms.Button button_ok;
      private System.Windows.Forms.Button button_cancel;
      private System.Windows.Forms.TextBox textBox_tag;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.CheckBox checkBox_IgnoreWronglyTaggedItems;

      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.Container components = null;

      public ExportToReqProCSVForm()
      {
         //
         // Required for Windows Form Designer support
         //
         InitializeComponent();

         //
         // TODO: Add any constructor code after InitializeComponent call
         //
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
         if( disposing )
         {
            if(components != null)
            {
               components.Dispose();
            }
         }
         base.Dispose( disposing );
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
         this.button_ok = new System.Windows.Forms.Button();
         this.button_cancel = new System.Windows.Forms.Button();
         this.textBox_tag = new System.Windows.Forms.TextBox();
         this.label1 = new System.Windows.Forms.Label();
         this.checkBox_IgnoreWronglyTaggedItems = new System.Windows.Forms.CheckBox();
         this.SuspendLayout();
         // 
         // button_ok
         // 
         this.button_ok.DialogResult = System.Windows.Forms.DialogResult.OK;
         this.button_ok.Location = new System.Drawing.Point(104, 144);
         this.button_ok.Name = "button_ok";
         this.button_ok.Size = new System.Drawing.Size(88, 32);
         this.button_ok.TabIndex = 0;
         this.button_ok.Text = "OK";
         // 
         // button_cancel
         // 
         this.button_cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
         this.button_cancel.Location = new System.Drawing.Point(216, 144);
         this.button_cancel.Name = "button_cancel";
         this.button_cancel.Size = new System.Drawing.Size(88, 32);
         this.button_cancel.TabIndex = 1;
         this.button_cancel.Text = "Cancel";
         // 
         // textBox_tag
         // 
         this.textBox_tag.Location = new System.Drawing.Point(192, 32);
         this.textBox_tag.Name = "textBox_tag";
         this.textBox_tag.TabIndex = 2;
         this.textBox_tag.Text = "SPR";
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(16, 32);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(152, 23);
         this.label1.TabIndex = 3;
         this.label1.Text = "Enter Requirement Tag";
         this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
         // 
         // checkBox_IgnoreWronglyTaggedItems
         // 
         this.checkBox_IgnoreWronglyTaggedItems.Checked = true;
         this.checkBox_IgnoreWronglyTaggedItems.CheckState = System.Windows.Forms.CheckState.Checked;
         this.checkBox_IgnoreWronglyTaggedItems.Location = new System.Drawing.Point(16, 80);
         this.checkBox_IgnoreWronglyTaggedItems.Name = "checkBox_IgnoreWronglyTaggedItems";
         this.checkBox_IgnoreWronglyTaggedItems.Size = new System.Drawing.Size(272, 24);
         this.checkBox_IgnoreWronglyTaggedItems.TabIndex = 4;
         this.checkBox_IgnoreWronglyTaggedItems.Text = "Ignore wrongly tagged requirements";
         // 
         // ExportToReqProCSVForm
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
         this.ClientSize = new System.Drawing.Size(312, 200);
         this.Controls.Add(this.checkBox_IgnoreWronglyTaggedItems);
         this.Controls.Add(this.textBox_tag);
         this.Controls.Add(this.label1);
         this.Controls.Add(this.button_cancel);
         this.Controls.Add(this.button_ok);
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
         this.Name = "ExportToReqProCSVForm";
         this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
         this.Text = "Export To ReqPro CSV";
         this.TopMost = true;
         this.ResumeLayout(false);

      }
      #endregion


      /// <summary>
      /// Export the requirements found in the selected package to a ReqPro CSV file.
      /// </summary>
      /// <param name="ea_repository"></param>
      public void Export()
      {
         try
         {
            object o;
            EA.ObjectType type;
            type = Main.EA_Repository.GetTreeSelectedItem(out o);
            if (type == EA.ObjectType.otPackage)
            {
               EA.Package EA_ParentPackage = (EA.Package)o;

               // Choose CSV file name and location
               SaveFileDialog ofd = new SaveFileDialog();
               ofd.Title = "Select Name of Export file";
               ofd.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
               DialogResult dlgRes = ofd.ShowDialog();
               if (dlgRes == DialogResult.OK)
               {
                  Main.EA_Repository.EnsureOutputVisible(Main.GUI_OUTPUT_TAB_NAME);
                  Main.WriteOutput("Exporting to ReqPro CSV File", -1);

                  // Begin to build the CSV file content, starting with the header line ReqPro requires.
                  csv_text = new StringBuilder();
                  csv_text.Append( String.Format("\"{0} Tag\",\"Requirement text\",\"Name\"\r\n", textBox_tag.Text));

                  // recursively descend the package
                  parse_package(EA_ParentPackage, 0);

                  // write the CSV file content
                  TextWriter tw = new StreamWriter( ofd.FileName );
                  tw.WriteLine(csv_text.ToString());
                  tw.Close();
                  MessageBoxEx.Show("Export Completed", "Progress");
                  Main.WriteOutput("Export Completed", -1);
               }
            }
         }
         catch (Exception exc)
         {
            Main.MessageBoxException(exc, "Export Failed");
         }
      }

      /// <summary>
      /// Recursive method to process the EA package and all its sub-packages.
      /// For each package found, the method grabs all of the elements in the package and for each
      /// one that is a requirement, exports it, dependant upon its tag and the options already set
      /// by the user for the export.
      /// </summary>
      /// <param name="ea_repository"></param>
      /// <param name="thePackage"></param>
      /// <param name="recurse_level"></param>
      private void parse_package(EA.Package thePackage, int recurse_level)
      {
         // consume elements - we have to use a special sorting class here because of peculiarties
         // in the way EA holds the elements in the collections.
         EA_ElementSorter elementSorter = new EA_ElementSorter(thePackage);
         EA.Element theElement = null;
         int theElementsRelativeLevel = 0;
         if (true == elementSorter.getFirst(ref theElement, ref theElementsRelativeLevel))
         {
            do
            {
               if ( theElement.Type.StartsWith("Requirement"))
               {
                  int theElementsRecurseLevel = 1 + recurse_level + theElementsRelativeLevel;

                  // If the element has the right tag...
                  if (theElement.Name.StartsWith(textBox_tag.Text))
                  {
                     // Pull out the ID from the name
                     int pos = theElement.Name.IndexOf( " ", 0, theElement.Name.Length );
                     string tag = theElement.Name.Substring( 0, pos );

                     // Pull out the short description from the rest of the name
                     string name = theElement.Name.Substring( pos, theElement.Name.Length-pos );
                     name = name.Trim();

                     csv_text.Append( String.Format( "\"{0}\",\"{1}\",\"{2}\"\r\n", tag, theElement.Notes, name) );

                     Main.WriteOutput("Exported : " + theElement.Name, theElement.ElementID );
                  }
                  // else if user has asked us to ignore wrong tags...
                  else if (false == checkBox_IgnoreWronglyTaggedItems.Checked)
                  {
                     csv_text.Append( String.Format( "\"{0}\",\"{1}\",\"{2}\"\r\n", textBox_tag.Text, theElement.Notes, theElement.Name) );

                     Main.WriteOutput("Exported : " + theElement.Name, theElement.ElementID );
                  }
                  // else ignore this requirement
                  else
                  {
                     Main.WriteOutput("Ignored : " + theElement.Name, theElement.ElementID );
                  }
               }
            } while (true == elementSorter.getNext(ref theElement, ref theElementsRelativeLevel));
         }

         // Scan through the packages within this package.
         foreach(EA.Package lowerLevelPackage in thePackage.Packages)
         {
            // recurse
            parse_package(lowerLevelPackage, recurse_level+1);
         }
      }
   }
}