summaryrefslogtreecommitdiffstats
blob: 337bcda9743ec7271e3f54f7954e56d8668d056b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.define.traceability.blam;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.nebula.widgets.xviewer.XViewerColumn;
import org.eclipse.nebula.widgets.xviewer.XViewerColumn.SortDataType;
import org.eclipse.osee.define.internal.Activator;
import org.eclipse.osee.define.traceability.data.BaseTraceDataCache;
import org.eclipse.osee.define.traceability.data.CodeUnitData;
import org.eclipse.osee.define.traceability.data.RequirementData;
import org.eclipse.osee.define.traceability.data.TestUnitData;
import org.eclipse.osee.define.traceability.report.AbstractArtifactRelationReport;
import org.eclipse.osee.define.traceability.report.ArtifactToRelatedArtifact;
import org.eclipse.osee.define.traceability.report.ArtifactTraceCount;
import org.eclipse.osee.define.traceability.report.ArtifactsWithoutRelations;
import org.eclipse.osee.define.traceability.report.IReportDataCollector;
import org.eclipse.osee.framework.core.data.IRelationTypeSide;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.io.CharBackedInputStream;
import org.eclipse.osee.framework.jdk.core.util.io.xml.ExcelXmlWriter;
import org.eclipse.osee.framework.jdk.core.util.io.xml.ISheetWriter;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.util.AIFile;
import org.eclipse.osee.framework.plugin.core.util.IExceptionableRunnable;
import org.eclipse.osee.framework.plugin.core.util.Jobs;
import org.eclipse.osee.framework.plugin.core.util.OseeData;
import org.eclipse.osee.framework.ui.skynet.blam.AbstractBlam;
import org.eclipse.osee.framework.ui.skynet.blam.VariableMap;
import org.eclipse.osee.framework.ui.skynet.results.IResultsEditorProvider;
import org.eclipse.osee.framework.ui.skynet.results.IResultsEditorTab;
import org.eclipse.osee.framework.ui.skynet.results.ResultsEditor;
import org.eclipse.osee.framework.ui.skynet.results.html.ResultsEditorHtmlTab;
import org.eclipse.osee.framework.ui.skynet.results.table.IResultsXViewerRow;
import org.eclipse.osee.framework.ui.skynet.results.table.ResultsEditorTableTab;
import org.eclipse.osee.framework.ui.skynet.results.table.ResultsXViewerRow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.program.Program;

/**
 * @author Roberto E. Escobar
 */
public class TraceReportBlam extends AbstractBlam {
   private final List<IResultsEditorTab> resultsTabs;

   public TraceReportBlam() {
      this.resultsTabs = new ArrayList<IResultsEditorTab>();
   }

   @Override
   public String getName() {
      return "Trace Report";
   }

   @Override
   public Collection<String> getCategories() {
      return Arrays.asList("Define.Trace");
   }

   @Override
   public String getDescriptionUsage() {
      return "Generates a trace report by scanning the selected branch for the selected trace unit types.";
   }

   private String getOperationsCheckBoxes(String value) {
      StringBuilder builder = new StringBuilder();
      builder.append("<XWidget xwidgetType=\"XCheckBox\" displayName=\"");
      builder.append(value);
      builder.append("\" labelAfter=\"true\" horizontalLabel=\"true\"/>");
      return builder.toString();
   }

   @Override
   public String getXWidgetsXml() {
      StringBuilder builder = new StringBuilder();
      builder.append("<xWidgets>");
      //      builder.append("<XWidget xwidgetType=\"XFileSelectionDialog\" displayName=\"Select UI List File\" />");
      builder.append("<XWidget xwidgetType=\"XBranchSelectWidget\" displayName=\"Requirements Branch\" />");
      builder.append("<XWidget xwidgetType=\"XLabel\" displayName=\"Select Trace Types:\"/>");
      for (TraceTypeEnum traceType : TraceTypeEnum.values()) {
         builder.append(getOperationsCheckBoxes(traceType.asLabel()));
      }
      builder.append("<XWidget xwidgetType=\"XLabel\" displayName=\"Select Report Output:\"/>");
      builder.append("<XWidget xwidgetType=\"XCheckBox\" displayName=\"Result Editor\" labelAfter=\"true\" horizontalLabel=\"true\" />");
      builder.append("<XWidget xwidgetType=\"XCheckBox\" displayName=\"Excel\" labelAfter=\"true\" horizontalLabel=\"true\" />");
      builder.append("</xWidgets>");
      return builder.toString();
   }

   //   private String getUIsFilterFromFile(IProgressMonitor monitor, String filePath) throws Exception {
   //      String input;
   //      File file = new File(filePath);
   //      if (file == null || !file.exists()) {
   //         throw new OseeArgumentException("UI list file not accessible");
   //      }
   //      IFileStore fileStore = EFS.getStore(file.toURI());
   //      InputStream inputStream = null;
   //      try {
   //         inputStream = new BufferedInputStream(fileStore.openInputStream(EFS.NONE, monitor));
   //         input = Lib.inputStreamToString(inputStream);
   //      } finally {
   //         if (inputStream != null) {
   //            inputStream.close();
   //         }
   //      }
   //      return input;
   //   }

   private List<TraceTypeEnum> getCheckedTraceItems(VariableMap variableMap) throws OseeArgumentException {
      List<TraceTypeEnum> toReturn = new ArrayList<TraceTypeEnum>();
      for (TraceTypeEnum traceType : TraceTypeEnum.values()) {
         if (variableMap.getBoolean(traceType.asLabel())) {
            toReturn.add(traceType);
         }
      }
      return toReturn;
   }

   @Override
   public void runOperation(VariableMap variableMap, IProgressMonitor monitor) throws Exception {

      //      String fileName = variableMap.getString("Select UI List File");
      Branch branch = variableMap.getBranch("Requirements Branch");
      if (branch == null) {
         throw new OseeArgumentException("Branch cannot be null");
      }
      List<TraceTypeEnum> traceTypes = getCheckedTraceItems(variableMap);
      if (traceTypes.isEmpty()) {
         throw new OseeArgumentException("Please select a trace type");
      }
      boolean isExcelOutput = variableMap.getBoolean("Excel");
      boolean isResultEditorOutput = variableMap.getBoolean("Result Editor");
      OutputType output = OutputType.asType(isExcelOutput, isResultEditorOutput);
      if (output == null) {
         throw new OseeArgumentException("Please select a report output type");
      }
      ISheetWriter writer = null;
      CharBackedInputStream excelInputStream = null;
      Map<String, AbstractArtifactRelationReport> reports = new LinkedHashMap<String, AbstractArtifactRelationReport>();
      List<BaseTraceDataCache> traceCache = new ArrayList<BaseTraceDataCache>();
      RequirementData reqData = new RequirementData(branch);
      CodeUnitData codeUnit = null;
      TestUnitData testUnit = null;
      try {
         int TOTAL_WORK = Integer.MAX_VALUE;
         int TASK_WORK = TOTAL_WORK / 5;
         monitor.beginTask("Generate Trace Report", TOTAL_WORK);

         SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, TASK_WORK);
         reqData.initialize(subMonitor);
         traceCache.add(reqData);

         subMonitor = new SubProgressMonitor(monitor, TASK_WORK);
         if (TraceTypeEnum.Code_Unit_Trace.isIn(traceTypes)) {
            codeUnit = new CodeUnitData(branch);
            codeUnit.initialize(subMonitor);
            traceCache.add(codeUnit);
         } else {
            subMonitor.done();
         }

         subMonitor = new SubProgressMonitor(monitor, TASK_WORK);
         if (TraceTypeEnum.containsTestEnum(traceTypes)) {
            testUnit = new TestUnitData(branch);
            testUnit.initialize(subMonitor);
            traceCache.add(testUnit);
         } else {
            subMonitor.done();
         }

         if (!monitor.isCanceled()) {
            if (isExcelOutput(output)) {
               excelInputStream = new CharBackedInputStream();
               writer = new ExcelXmlWriter(excelInputStream.getWriter());
            }
            buildReport(reports, "Trace Unit <-> Requirement", output, writer,
               getTraceUnitToReqReport(codeUnit, testUnit, traceTypes));
            buildReport(reports, "Trace Unit with No Valid Traceability", output, writer,
               getDisconnectedTraceUnitReport(codeUnit, testUnit, traceTypes));
            buildReport(reports, "Requirement Trace Counts", output, writer, getTraceCountReport(reqData, traceTypes));

            subMonitor = new SubProgressMonitor(monitor, TASK_WORK);
            executeReports(subMonitor, reports);
         }
         subMonitor = new SubProgressMonitor(monitor, TASK_WORK);
         displayReports(subMonitor, writer, excelInputStream, output);
      } finally {
         try {
            for (AbstractArtifactRelationReport report : reports.values()) {
               report.clear();
            }
            for (BaseTraceDataCache cache : traceCache) {
               cache.reset();
            }
            reports.clear();
            traceCache.clear();

            resultsTabs.clear();
         } finally {
            monitor.done();
         }
      }
   }

   private void executeReports(IProgressMonitor monitor, Map<String, AbstractArtifactRelationReport> reports) throws OseeCoreException {
      try {
         monitor.beginTask("Create Reports", reports.size());
         for (String key : reports.keySet()) {
            monitor.subTask(String.format("Creating [%s]", key));
            if (monitor.isCanceled()) {
               break;
            }
            AbstractArtifactRelationReport report = reports.get(key);
            report.process(monitor);
            report.clear();
            monitor.worked(1);
         }
      } finally {
         monitor.done();
      }
   }

   private void displayReports(IProgressMonitor monitor, ISheetWriter excelWriter, CharBackedInputStream excelInputStream, OutputType outputType) throws IOException, OseeCoreException {
      try {
         boolean wasEmpty = resultsTabs == null || resultsTabs.isEmpty();
         monitor.beginTask("Open Reports", 2);
         boolean createReport = true;
         boolean excelValid = excelInputStream != null && excelInputStream.available() > 0;

         if (wasEmpty && !excelValid) {
            resultsTabs.add(new ResultsEditorHtmlTab(getName(), getName(), "Nothing Reported"));
            createReport = false;
         }

         openReport(resultsTabs);
         monitor.worked(1);

         if (createReport) {
            if (excelWriter != null && isExcelOutput(outputType)) {
               excelWriter.endWorkbook();
               openExcel(excelInputStream);
            }
         }
         monitor.worked(1);
      } finally {
         monitor.done();
      }
   }

   private boolean isExcelOutput(OutputType output) {
      return output == OutputType.Excel || output == OutputType.Both;
   }

   private boolean isEditorOutput(OutputType output) {
      return output == OutputType.ResultsEditor || output == OutputType.Both;
   }

   private void buildReport(Map<String, AbstractArtifactRelationReport> reports, String title, OutputType output, ISheetWriter writer, AbstractArtifactRelationReport report) {
      if (isExcelOutput(output)) {
         report.addReportDataCollector(new ExcelReport(title, writer));
      }
      if (isEditorOutput(output)) {
         report.addReportDataCollector(new ResultEditorReport(title));
      }
      reports.put(title, report);
   }

   private void openExcel(final InputStream inputStream) throws OseeCoreException {
      IFile iFile = OseeData.getIFile("Trace_Report_" + Lib.getDateTimeString() + ".xml");
      AIFile.writeToFile(iFile, inputStream);
      if (inputStream != null) {
         try {
            inputStream.close();
         } catch (IOException ex) {
            // Do Nothing
         }
      }
      Program.launch(iFile.getLocation().toOSString());
   }

   private void openReport(final List<IResultsEditorTab> resultsTabs) {
      final List<IResultsEditorTab> results = new ArrayList<IResultsEditorTab>(resultsTabs);
      if (!results.isEmpty()) {
         IExceptionableRunnable runnable = new IExceptionableRunnable() {

            @Override
            public IStatus run(IProgressMonitor monitor) throws Exception {
               ResultsEditor.open(new IResultsEditorProvider() {
                  @Override
                  public String getEditorName() {
                     return getName();
                  }

                  @Override
                  public List<IResultsEditorTab> getResultsEditorTabs() {
                     return results;
                  }
               });
               return Status.OK_STATUS;
            }
         };
         Jobs.runInJob(getName(), runnable, Activator.class, Activator.PLUGIN_ID);
      }
   }

   private void addRelationToCheck(List<TraceTypeEnum> traceTypes, AbstractArtifactRelationReport report, boolean fromTraceUnit) {
      for (TraceTypeEnum traceTypeEnum : traceTypes) {
         IRelationTypeSide relation =
            fromTraceUnit ? traceTypeEnum.getRelatedToRequirement() : traceTypeEnum.getRelatedToTraceUnit();
         report.addRelationToCheck(relation);
      }
   }

   private void addArtifacts(CodeUnitData codeUnit, TestUnitData testUnit, List<TraceTypeEnum> traceTypes, AbstractArtifactRelationReport report) {
      if (TraceTypeEnum.Code_Unit_Trace.isIn(traceTypes)) {
         report.setArtifacts(codeUnit.getAllCodeUnits());
      }
      if (TraceTypeEnum.Verified_By_Test_Unit_Trace.isIn(traceTypes)) {
         report.setArtifacts(testUnit.getTestCases());
      }
      if (TraceTypeEnum.Used_By_Test_Unit_Trace.isIn(traceTypes)) {
         report.setArtifacts(testUnit.getTestSupportItems());
      }
      if (TraceTypeEnum.Validation_By_TestProcedure.isIn(traceTypes)) {
         report.setArtifacts(testUnit.getTestProcedures());
      }
   }

   private AbstractArtifactRelationReport getTraceCountReport(RequirementData reqData, List<TraceTypeEnum> traceTypes) {
      ArtifactTraceCount report = new ArtifactTraceCount();
      report.setArtifacts(reqData.getDirectRequirements());
      addRelationToCheck(traceTypes, report, false);
      return report;
   }

   private AbstractArtifactRelationReport getDisconnectedTraceUnitReport(CodeUnitData codeUnit, TestUnitData testUnit, List<TraceTypeEnum> traceTypes) {
      ArtifactsWithoutRelations report = new ArtifactsWithoutRelations();
      addArtifacts(codeUnit, testUnit, traceTypes, report);
      addRelationToCheck(traceTypes, report, true);
      return report;
   }

   private AbstractArtifactRelationReport getTraceUnitToReqReport(CodeUnitData codeUnit, TestUnitData testUnit, List<TraceTypeEnum> traceTypes) {
      ArtifactToRelatedArtifact report = new ArtifactToRelatedArtifact();
      addArtifacts(codeUnit, testUnit, traceTypes, report);
      addRelationToCheck(traceTypes, report, true);
      return report;
   }

   private enum OutputType {
      ResultsEditor,
      Excel,
      Both;

      public static OutputType asType(boolean isExcel, boolean isEditor) {
         if (isExcel && isEditor) {
            return OutputType.Both;
         } else if (isExcel) {
            return OutputType.Excel;
         } else if (isEditor) {
            return OutputType.ResultsEditor;
         }
         return null;
      }
   }

   private enum TraceTypeEnum {
      Code_Unit_Trace(CoreRelationTypes.CodeRequirement_Requirement, CoreRelationTypes.CodeRequirement_CodeUnit, false),
      Verified_By_Test_Unit_Trace(CoreRelationTypes.Verification__Requirement, CoreRelationTypes.Verification__Verifier, true),
      Used_By_Test_Unit_Trace(CoreRelationTypes.Uses__Requirement, CoreRelationTypes.Uses__TestUnit, true),
      Validation_By_TestProcedure(CoreRelationTypes.Validation__Requirement, CoreRelationTypes.Validation__Validator, true);

      private IRelationTypeSide toReq;
      private IRelationTypeSide toTraceUnit;
      private boolean isTestType;

      TraceTypeEnum(IRelationTypeSide toReq, IRelationTypeSide toTraceUnit, boolean isTestType) {
         this.toReq = toReq;
         this.toTraceUnit = toTraceUnit;
         this.isTestType = isTestType;
      }

      public IRelationTypeSide getRelatedToRequirement() {
         return toReq;
      }

      public IRelationTypeSide getRelatedToTraceUnit() {
         return toTraceUnit;
      }

      public boolean isTestType() {
         return isTestType;
      }

      public String asLabel() {
         return name().replaceAll("_", " ");
      }

      public boolean isIn(Collection<TraceTypeEnum> traceTypes) {
         return traceTypes.contains(this);
      }

      public static boolean containsTestEnum(List<TraceTypeEnum> traceTypes) {
         for (TraceTypeEnum type : TraceTypeEnum.values()) {
            if (type.isTestType() && type.isIn(traceTypes)) {
               return true;
            }
         }
         return false;
      }
   }

   private static final class ExcelReport implements IReportDataCollector {
      private final String title;
      private final ISheetWriter sheetWriter;

      public ExcelReport(String title, ISheetWriter sheetWriter) {
         this.sheetWriter = sheetWriter;
         this.title = title;
      }

      @Override
      public void addRow(String... data) {
         try {
            sheetWriter.writeRow(data);
         } catch (IOException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }

      @Override
      public void addTableHeader(String... header) {
         try {
            sheetWriter.startSheet(title, header.length);
            sheetWriter.writeRow(header);
         } catch (IOException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }

      @Override
      public void endTable() {
         try {
            sheetWriter.endSheet();
         } catch (IOException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
   }

   private final class ResultEditorReport implements IReportDataCollector {
      private final String title;
      private List<XViewerColumn> columns;
      private List<IResultsXViewerRow> rows;

      public ResultEditorReport(String title) {
         this.title = title;
         this.columns = null;
         this.rows = null;
      }

      @Override
      public void addRow(String... data) {
         if (rows == null) {
            rows = new ArrayList<IResultsXViewerRow>();
         }
         rows.add(new ResultsXViewerRow(data));
      }

      @Override
      public void addTableHeader(String... header) {
         this.columns = new ArrayList<XViewerColumn>();
         for (String name : header) {
            columns.add(new XViewerColumn(name, name, 80, SWT.LEFT, true, SortDataType.String, false, ""));
         }
      }

      @Override
      public void endTable() {
         resultsTabs.add(new ResultsEditorTableTab(title, columns, rows));
      }
   }
}