Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
2141 ghuddy 1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Text;
7
using System.IO;
8
 
9
namespace EA_ReqPro
10
{
2151 ghuddy 11
   /// <summary>
12
   /// Summary description for ExportToReqProCSVForm.
13
   /// </summary>
14
   public class ExportToReqProCSVForm : System.Windows.Forms.Form
15
   {
2141 ghuddy 16
      private StringBuilder csv_text;
17
 
18
      private System.Windows.Forms.Button button_ok;
19
      private System.Windows.Forms.Button button_cancel;
20
      private System.Windows.Forms.TextBox textBox_tag;
21
      private System.Windows.Forms.Label label1;
22
      private System.Windows.Forms.CheckBox checkBox_IgnoreWronglyTaggedItems;
23
 
2151 ghuddy 24
      /// <summary>
25
      /// Required designer variable.
26
      /// </summary>
27
      private System.ComponentModel.Container components = null;
2141 ghuddy 28
 
2151 ghuddy 29
      public ExportToReqProCSVForm()
30
      {
31
         //
32
         // Required for Windows Form Designer support
33
         //
34
         InitializeComponent();
2141 ghuddy 35
 
2151 ghuddy 36
         //
37
         // TODO: Add any constructor code after InitializeComponent call
38
         //
39
      }
2141 ghuddy 40
 
2151 ghuddy 41
      /// <summary>
42
      /// Clean up any resources being used.
43
      /// </summary>
44
      protected override void Dispose( bool disposing )
45
      {
46
         if( disposing )
47
         {
48
            if(components != null)
49
            {
50
               components.Dispose();
51
            }
52
         }
53
         base.Dispose( disposing );
54
      }
55
 
56
      #region Windows Form Designer generated code
57
      /// <summary>
58
      /// Required method for Designer support - do not modify
59
      /// the contents of this method with the code editor.
60
      /// </summary>
61
      private void InitializeComponent()
62
      {
2141 ghuddy 63
         this.button_ok = new System.Windows.Forms.Button();
64
         this.button_cancel = new System.Windows.Forms.Button();
65
         this.textBox_tag = new System.Windows.Forms.TextBox();
66
         this.label1 = new System.Windows.Forms.Label();
67
         this.checkBox_IgnoreWronglyTaggedItems = new System.Windows.Forms.CheckBox();
68
         this.SuspendLayout();
69
         // 
70
         // button_ok
71
         // 
72
         this.button_ok.DialogResult = System.Windows.Forms.DialogResult.OK;
73
         this.button_ok.Location = new System.Drawing.Point(104, 144);
74
         this.button_ok.Name = "button_ok";
75
         this.button_ok.Size = new System.Drawing.Size(88, 32);
76
         this.button_ok.TabIndex = 0;
77
         this.button_ok.Text = "OK";
78
         // 
79
         // button_cancel
80
         // 
81
         this.button_cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
82
         this.button_cancel.Location = new System.Drawing.Point(216, 144);
83
         this.button_cancel.Name = "button_cancel";
84
         this.button_cancel.Size = new System.Drawing.Size(88, 32);
85
         this.button_cancel.TabIndex = 1;
86
         this.button_cancel.Text = "Cancel";
87
         // 
88
         // textBox_tag
89
         // 
90
         this.textBox_tag.Location = new System.Drawing.Point(192, 32);
91
         this.textBox_tag.Name = "textBox_tag";
92
         this.textBox_tag.TabIndex = 2;
93
         this.textBox_tag.Text = "SPR";
94
         // 
95
         // label1
96
         // 
97
         this.label1.Location = new System.Drawing.Point(16, 32);
98
         this.label1.Name = "label1";
99
         this.label1.Size = new System.Drawing.Size(152, 23);
100
         this.label1.TabIndex = 3;
101
         this.label1.Text = "Enter Requirement Tag";
102
         this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
103
         // 
104
         // checkBox_IgnoreWronglyTaggedItems
105
         // 
106
         this.checkBox_IgnoreWronglyTaggedItems.Checked = true;
107
         this.checkBox_IgnoreWronglyTaggedItems.CheckState = System.Windows.Forms.CheckState.Checked;
108
         this.checkBox_IgnoreWronglyTaggedItems.Location = new System.Drawing.Point(16, 80);
109
         this.checkBox_IgnoreWronglyTaggedItems.Name = "checkBox_IgnoreWronglyTaggedItems";
110
         this.checkBox_IgnoreWronglyTaggedItems.Size = new System.Drawing.Size(272, 24);
111
         this.checkBox_IgnoreWronglyTaggedItems.TabIndex = 4;
112
         this.checkBox_IgnoreWronglyTaggedItems.Text = "Ignore wrongly tagged requirements";
113
         // 
114
         // ExportToReqProCSVForm
115
         // 
116
         this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
117
         this.ClientSize = new System.Drawing.Size(312, 200);
118
         this.Controls.Add(this.checkBox_IgnoreWronglyTaggedItems);
119
         this.Controls.Add(this.textBox_tag);
120
         this.Controls.Add(this.label1);
121
         this.Controls.Add(this.button_cancel);
122
         this.Controls.Add(this.button_ok);
2151 ghuddy 123
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
2141 ghuddy 124
         this.Name = "ExportToReqProCSVForm";
2151 ghuddy 125
         this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
2141 ghuddy 126
         this.Text = "Export To ReqPro CSV";
2151 ghuddy 127
         this.TopMost = true;
2141 ghuddy 128
         this.ResumeLayout(false);
129
 
130
      }
2151 ghuddy 131
      #endregion
2141 ghuddy 132
 
133
 
134
      /// <summary>
135
      /// Export the requirements found in the selected package to a ReqPro CSV file.
136
      /// </summary>
137
      /// <param name="ea_repository"></param>
2151 ghuddy 138
      public void Export()
2141 ghuddy 139
      {
2153 ghuddy 140
         try
2141 ghuddy 141
         {
2153 ghuddy 142
            object o;
143
            EA.ObjectType type;
144
            type = Main.EA_Repository.GetTreeSelectedItem(out o);
145
            if (type == EA.ObjectType.otPackage)
2141 ghuddy 146
            {
2153 ghuddy 147
               EA.Package EA_ParentPackage = (EA.Package)o;
2141 ghuddy 148
 
2153 ghuddy 149
               // Choose CSV file name and location
150
               SaveFileDialog ofd = new SaveFileDialog();
151
               ofd.Title = "Select Name of Export file";
152
               ofd.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
153
               DialogResult dlgRes = ofd.ShowDialog();
154
               if (dlgRes == DialogResult.OK)
155
               {
156
                  Main.EA_Repository.EnsureOutputVisible(Main.GUI_OUTPUT_TAB_NAME);
157
                  Main.EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Exporting to ReqPro CSV File", -1);
2141 ghuddy 158
 
2153 ghuddy 159
                  // Begin to build the CSV file content, starting with the header line ReqPro requires.
160
                  csv_text = new StringBuilder();
161
                  csv_text.Append( String.Format("\"{0} Tag\",\"Requirement text\",\"Name\"\r\n", textBox_tag.Text));
2141 ghuddy 162
 
2153 ghuddy 163
                  // recursively descend the package
164
                  parse_package(EA_ParentPackage, 0);
165
 
166
                  // write the CSV file content
167
                  TextWriter tw = new StreamWriter( ofd.FileName );
168
                  tw.WriteLine(csv_text.ToString());
169
                  tw.Close();
170
                  MessageBoxEx.Show("Export Completed", "Progress");
171
                  Main.EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Export Completed", -1);
172
               }
2141 ghuddy 173
            }
174
         }
2153 ghuddy 175
         catch (Exception exc)
176
         {
177
            Main.MessageBoxException(exc, "Export Failed");
178
         }
2141 ghuddy 179
      }
180
 
181
      /// <summary>
182
      /// Recursive method to process the EA package and all its sub-packages.
183
      /// For each package found, the method grabs all of the elements in the package and for each
184
      /// one that is a requirement, exports it, dependant upon its tag and the options already set
185
      /// by the user for the export.
186
      /// </summary>
187
      /// <param name="ea_repository"></param>
188
      /// <param name="thePackage"></param>
189
      /// <param name="recurse_level"></param>
2151 ghuddy 190
      private void parse_package(EA.Package thePackage, int recurse_level)
2141 ghuddy 191
      {
192
         // consume elements - we have to use a special sorting class here because of peculiarties
2151 ghuddy 193
         // in the way EA holds the elements in the collections.
2141 ghuddy 194
         EA_ElementSorter elementSorter = new EA_ElementSorter(thePackage);
195
         EA.Element theElement = null;
196
         int theElementsRelativeLevel = 0;
197
         if (true == elementSorter.getFirst(ref theElement, ref theElementsRelativeLevel))
198
         {
199
            do
200
            {
201
               if ( theElement.Type.StartsWith("Requirement"))
202
               {
203
                  int theElementsRecurseLevel = 1 + recurse_level + theElementsRelativeLevel;
204
 
205
                  // If the element has the right tag...
206
                  if (theElement.Name.StartsWith(textBox_tag.Text))
207
                  {
208
                     // Pull out the ID from the name
209
                     int pos = theElement.Name.IndexOf( " ", 0, theElement.Name.Length );
210
                     string tag = theElement.Name.Substring( 0, pos );
211
 
212
                     // Pull out the short description from the rest of the name
213
                     string name = theElement.Name.Substring( pos, theElement.Name.Length-pos );
214
                     name = name.Trim();
215
 
216
                     csv_text.Append( String.Format( "\"{0}\",\"{1}\",\"{2}\"\r\n", tag, theElement.Notes, name) );
217
 
2151 ghuddy 218
                     Main.EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Exported : " + theElement.Name, theElement.ElementID );
2141 ghuddy 219
                  }
220
                  // else if user has asked us to ignore wrong tags...
221
                  else if (false == checkBox_IgnoreWronglyTaggedItems.Checked)
222
                  {
223
                     csv_text.Append( String.Format( "\"{0}\",\"{1}\",\"{2}\"\r\n", textBox_tag.Text, theElement.Notes, theElement.Name) );
224
 
2151 ghuddy 225
                     Main.EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Exported : " + theElement.Name, theElement.ElementID );
2141 ghuddy 226
                  }
227
                  // else ignore this requirement
228
                  else
229
                  {
2151 ghuddy 230
                     Main.EA_Repository.WriteOutput( Main.GUI_OUTPUT_TAB_NAME, "Ignored : " + theElement.Name, theElement.ElementID );
2141 ghuddy 231
                  }
232
               }
233
            } while (true == elementSorter.getNext(ref theElement, ref theElementsRelativeLevel));
234
         }
235
 
236
         // Scan through the packages within this package.
237
         foreach(EA.Package lowerLevelPackage in thePackage.Packages)
238
         {
239
            // recurse
2151 ghuddy 240
            parse_package(lowerLevelPackage, recurse_level+1);
2141 ghuddy 241
         }
242
      }
2151 ghuddy 243
   }
2141 ghuddy 244
}