Subversion Repositories DevTools

Rev

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