Skip to main content
summaryrefslogtreecommitdiffstats
blob: 09d1558ca643c0221cd1711fc7ce3f9468a8b36e (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2015 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.disposition.rest.internal.importer;

import static org.eclipse.osee.disposition.model.DispoSummarySeverity.IGNORE;
import static org.eclipse.osee.disposition.model.DispoSummarySeverity.UPDATE;
import static org.eclipse.osee.disposition.model.DispoSummarySeverity.WARNING;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.osee.disposition.model.CopySetParamOption;
import org.eclipse.osee.disposition.model.Discrepancy;
import org.eclipse.osee.disposition.model.DispoAnnotationData;
import org.eclipse.osee.disposition.model.DispoItem;
import org.eclipse.osee.disposition.model.DispoItemData;
import org.eclipse.osee.disposition.model.DispoStrings;
import org.eclipse.osee.disposition.model.OperationReport;
import org.eclipse.osee.disposition.rest.internal.DispoConnector;
import org.eclipse.osee.disposition.rest.util.DispoUtil;
import org.eclipse.osee.framework.jdk.core.util.Strings;

/**
 * @author Angel Avila
 */
public class DispoSetCopier {

   private final DispoConnector connector;

   public DispoSetCopier(DispoConnector connector) {
      this.connector = connector;
   }

   public List<DispoItem> copyAllDispositions(Map<String, Set<DispoItemData>> nameToDestItems, Collection<DispoItem> sourceItems, boolean isCoverageCopy, OperationReport report) {
      List<DispoItem> modifiedItems = new ArrayList<>();

      // Iterate through every source item since we want to try to find a match for every item in the source
      for (DispoItem sourceItem : sourceItems) {
         DispoItemData destItem = getCorrespondingDestItem(nameToDestItems, sourceItem);

         if (destItem != null) {
            // Only try to copy over annotations if matching dest item is NOT PASS
            if (!destItem.getStatus().equals(DispoStrings.Item_Pass)) {
               DispoItemData newItem = createNewItemWithCopiedAnnotations(destItem, sourceItem, isCoverageCopy, report);
               if (newItem != null) {
                  modifiedItems.add(newItem);

                  if (!newItem.getGuid().equals(sourceItem.getGuid())) {
                     List<DispoAnnotationData> destAnnotationsList = destItem.getAnnotationsList();
                     String message = String.format("Had %s Dispositions now has %s", destAnnotationsList.size(),
                        newItem.getAnnotationsList().size());
                     report.addEntry(destItem.getName(), message, UPDATE);
                  }
               }
            } else if (!Strings.isValid(destItem.getGuid()) && !sourceItem.getStatus().equals(DispoStrings.Item_Pass)) {
               /**
                * In the case of Coverage, the destination Item is the item created by a new import so we assign it the
                * id of the source so that it will overwrite the source date with the new import data
                */
               destItem.setGuid(sourceItem.getGuid());
               modifiedItems.add(destItem);
            }

         } else {
            report.addEntry(sourceItem.getName(), "No matching item found in the Destination Set", WARNING);
         }
      }
      return modifiedItems;
   }

   private DispoItemData getCorrespondingDestItem(Map<String, Set<DispoItemData>> nameToDestItems, DispoItem sourceItem) {
      DispoItemData destItem = null;
      String name = sourceItem.getName();
      Set<DispoItemData> itemsWithSameName = nameToDestItems.get(name);

      if (itemsWithSameName != null) {
         if (itemsWithSameName.size() == 1) {
            destItem = itemsWithSameName.iterator().next();
         } else {
            for (DispoItemData itemWithSameName : itemsWithSameName) {
               if (itemWithSameName.getMethodNumber().equals(
                  sourceItem.getMethodNumber()) && itemWithSameName.getFileNumber().equals(
                     sourceItem.getFileNumber())) {
                  destItem = itemWithSameName;
               }
            }
         }
      }
      return destItem;
   }

   private DispoItemData createNewItemWithCopiedAnnotations(DispoItemData destItem, DispoItem sourceItem, boolean isCoverageCopy, OperationReport report) {
      DispoItemData toReturn = null;
      boolean isSameDiscrepancies = matchAllDiscrepancies(destItem, sourceItem);
      if (!isSameDiscrepancies) {
         report.addEntry(destItem.getName(),
            String.format("Tried to copy from item id: [%s] but discrepancies were not the same", sourceItem.getGuid()),
            WARNING);

      }
      toReturn = buildNewItem(destItem, sourceItem, isCoverageCopy, report, isSameDiscrepancies);
      return toReturn;
   }

   private DispoItemData buildNewItem(DispoItemData destItem, DispoItem sourceItem, boolean isCoverageCopy, OperationReport report, boolean isSameDiscrepancies) {
      boolean isChangesMade = false;
      DispoItemData newItem = initNewItem(destItem, sourceItem);
      List<DispoAnnotationData> newAnnotations = newItem.getAnnotationsList();
      List<DispoAnnotationData> sourceAnnotations = sourceItem.getAnnotationsList();
      Set<String> destDefaultAnntationLocations = getDefaultAnnotations(newItem);
      Map<String, Integer> placeHolderAnnotationLocations = getPlaceHolderAnnotations(newItem);
      List<String> destDiscrepanciesTextOnly = discrepanciesTextOnly(destItem.getDiscrepanciesList());

      for (DispoAnnotationData sourceAnnotation : sourceAnnotations) {
         String sourceLocation = sourceAnnotation.getLocationRefs();

         // Check for ignore cases
         if (DispoUtil.isDefaultAnotation(sourceAnnotation) || !Strings.isValid(sourceAnnotation.getResolutionType())) {
            /**
             * This means this annotation is TEST_UNIT, Exception_Handling, or just place holder, so don't copy it over,
             * only log if the destination item doesn't have this annotation as a Default i.e means something changed
             * user should be aware.Currently only for Coverage
             */
            if (!destDefaultAnntationLocations.contains(sourceLocation)) {
               report.addEntry(destItem.getName(),
                  String.format("Did not copy annotations for location(s) [%s] because they are default annotations",
                     sourceAnnotation.getLocationRefs()),
                  IGNORE);

            }
         } else if (destDefaultAnntationLocations.contains(sourceLocation)) {
            /**
             * isCoverageCopy is true when annotation copier is called by a coverage import, this means we need to also
             * check that the matching dest annotation isn't a DEFAULT resolution before copying over.
             */
            report.addEntry(destItem.getName(),
               String.format(
                  "Did not copy annotations for location(s) [%s] because the destination item already has a default annotations at these locations",
                  sourceAnnotation.getLocationRefs()),
               IGNORE);

         } else if (newAnnotations.toString().contains(sourceAnnotation.getGuid())) {
            report.addEntry(destItem.getName(),
               String.format(
                  "Did not copy annotations for location(s) [%s] because the destination item already has this Annotation [%s]",
                  sourceAnnotation.getLocationRefs(), sourceAnnotation.getGuid()),
               IGNORE);

         } else {
            // Try to copy but check if Discrepancy is the same and present in the destination set
            if (isSameDiscrepancies && isCoveredDiscrepanciesExistInDest(destDiscrepanciesTextOnly, sourceItem,
               sourceAnnotation, report)) {
               DispoAnnotationData newAnnotation = sourceAnnotation;
               if (destDefaultAnntationLocations.contains(sourceLocation)) {
                  /**
                   * The discrepancy of this manual disposition is now covered by a Default Annotation so this Manual
                   * Annotation is invalid, mark as such by making the location Ref negative, don't bother connecting
                   * the annotation
                   */
                  // Make location ref negative to indicate this
                  String locationRefs = sourceAnnotation.getLocationRefs();
                  Integer locationRefAsInt = Integer.valueOf(locationRefs);
                  if (locationRefAsInt > 0) {
                     newAnnotation.setLocationRefs(String.valueOf(locationRefAsInt * -1));
                  }
                  report.addEntry(destItem.getName(),
                     String.format("The annotation was copied over but is no longer needed: [%s]", locationRefs),
                     WARNING);
               }
               connector.connectAnnotation(newAnnotation, newItem.getDiscrepanciesList());
               isChangesMade = true;
               // Both the source and destination are dispositionable so copy the annotation
               int nextIndex;
               if (placeHolderAnnotationLocations.containsKey(sourceLocation)) {
                  nextIndex = placeHolderAnnotationLocations.get(sourceLocation);
                  newAnnotation.setIndex(nextIndex);
                  newAnnotations.set(nextIndex, newAnnotation);
               } else {
                  nextIndex = newAnnotations.size();
                  newAnnotation.setIndex(nextIndex);
                  newAnnotations.add(nextIndex, newAnnotation);
               }
            }
         }
      }

      if (isChangesMade) {
         newItem.setAnnotationsList(newAnnotations);
         String newStatus = connector.getItemStatus(newItem);
         newItem.setStatus(newStatus);
      } else if (!isCoverageCopy) {
         // We want to take the new import version of this item even though no changes were made, this will only occur if
         // 1. All the Annotations from the source Item were default, in which case we can ignore those and take the ones from the new import
         // 2. None of the non-default Annotations cover a Discrepancy in the new import, in which case don't copy them over: MIGHT CHANGE THIS
         newItem = destItem;
         newItem.setGuid(sourceItem.getGuid());
      } else {
         report.addEntry(destItem.getName(), "Nothing to copy", IGNORE);
         newItem = null;
      }
      return newItem;
   }

   private boolean isCoveredDiscrepanciesExistInDest(List<String> destDescrepanciesTextOnly, DispoItem sourceItem, DispoAnnotationData annotation, OperationReport report) {
      List<String> idsOfCoveredDiscrepancies = annotation.getIdsOfCoveredDiscrepancies();
      Map<String, Discrepancy> sourceDiscrepancies = sourceItem.getDiscrepanciesList();
      for (String id : idsOfCoveredDiscrepancies) {
         Discrepancy coveredDiscrepancy = sourceDiscrepancies.get(id);
         if (coveredDiscrepancy == null || !destDescrepanciesTextOnly.contains(coveredDiscrepancy.getText())) {
            return false;
         }
      }

      return true;
   }

   private List<String> discrepanciesTextOnly(Map<String, Discrepancy> discrepancies) {
      List<String> toReturn = new ArrayList<>();
      for (String key : discrepancies.keySet()) {
         Discrepancy discrepancy = discrepancies.get(key);
         toReturn.add(discrepancy.getText());
      }
      return toReturn;
   }

   private DispoItemData initNewItem(DispoItemData destItem, DispoItem sourceItem) {
      DispoItemData newItem = new DispoItemData();
      newItem.setDiscrepanciesList(destItem.getDiscrepanciesList());
      List<DispoAnnotationData> newList = destItem.getAnnotationsList();
      newItem.setAnnotationsList(newList);
      if (Strings.isValid(destItem.getGuid())) {
         newItem.setGuid(destItem.getGuid());
      } else {
         newItem.setGuid(sourceItem.getGuid());
      }
      newItem.setName(destItem.getName());
      return newItem;
   }

   private Map<String, Integer> getPlaceHolderAnnotations(DispoItemData item) {
      Map<String, Integer> placeHolderAnnotationLocations = new HashMap<>();
      List<DispoAnnotationData> annotations = item.getAnnotationsList();
      if (annotations == null) {
         annotations = new ArrayList<>();
      }
      for (DispoAnnotationData annotation : annotations) {
         if (!Strings.isValid(annotation.getResolutionType())) {
            placeHolderAnnotationLocations.put(annotation.getLocationRefs(), annotation.getIndex());
         }
      }

      return placeHolderAnnotationLocations;
   }

   private Set<String> getDefaultAnnotations(DispoItemData item) {
      Set<String> defaultAnnotationLocations = new HashSet<>();
      List<DispoAnnotationData> annotations = item.getAnnotationsList();
      if (annotations == null) {
         annotations = new ArrayList<>();
      }
      for (DispoAnnotationData annotation : annotations) {
         if (DispoUtil.isDefaultAnotation(annotation)) {
            defaultAnnotationLocations.add(annotation.getLocationRefs());
         }
      }

      return defaultAnnotationLocations;
   }

   private boolean matchAllDiscrepancies(DispoItemData destItem, DispoItem sourceItem) {
      Map<String, String> destLocationToText = generateLocationToTextMap(destItem);
      boolean toReturn = true;

      Map<String, Discrepancy> sourceDiscrepancies = sourceItem.getDiscrepanciesList();
      for (String key : sourceDiscrepancies.keySet()) {
         Discrepancy sourceDiscrepancy = sourceDiscrepancies.get(key);

         String sourceLocation = sourceDiscrepancy.getLocation();
         String destDicrepancyText = destLocationToText.get(sourceLocation);
         if (destDicrepancyText == null) {
            // No Discrepancy with that location in the destination item, return false
            toReturn = false;
            break;
         } else if (sourceDiscrepancy.getText().equals(destDicrepancyText)) {
            continue;
         } else {
            toReturn = false;
            break;
         }

      }
      return toReturn;
   }

   private Map<String, String> generateLocationToTextMap(DispoItem item) {
      Map<String, String> locationToText = new HashMap<>();
      Map<String, Discrepancy> discrepancies = item.getDiscrepanciesList();
      for (String key : discrepancies.keySet()) {
         Discrepancy discrepancy = discrepancies.get(key);
         locationToText.put(discrepancy.getLocation(), discrepancy.getText());
      }
      return locationToText;
   }

   public void copyCategories(Map<String, Set<DispoItemData>> destinationItems, Collection<DispoItem> sourceItems, Map<String, DispoItem> toEdit, CopySetParamOption option) {
      for (DispoItem sourceItem : sourceItems) {
         DispoItem destItem = getCorrespondingDestItem(destinationItems, sourceItem);

         if (destItem != null) {
            String currentCategory = destItem.getCategory();
            String sourceCategory = sourceItem.getCategory();
            String newCategory;

            if (Strings.isValid(sourceCategory)) {
               switch (option) {
                  case OVERRIDE:
                     newCategory = sourceCategory;
                     break;
                  case OVERRIDE_EMPTY:
                     if (!Strings.isValid(currentCategory)) {
                        newCategory = sourceCategory;
                     } else {
                        newCategory = currentCategory;
                     }
                     break;
                  case MERGE:
                     if (!Strings.isValid(currentCategory)) {
                        newCategory = sourceCategory;
                     } else {
                        newCategory = currentCategory + "::" + sourceCategory;
                     }
                     break;
                  case NONE:
                  default:
                     newCategory = currentCategory;
                     break;
               }

               // Check to see if this item is already set to be edited
               DispoItem matchingToEdit = toEdit.get(sourceItem.getName());
               if (matchingToEdit != null) {
                  ((DispoItemData) matchingToEdit).setCategory(newCategory);
               } else {
                  DispoItemData newToEdit = new DispoItemData();
                  newToEdit.setGuid(destItem.getGuid());
                  newToEdit.setName(destItem.getName());
                  newToEdit.setCategory(newCategory);
                  toEdit.put(newToEdit.getName(), newToEdit);
               }
            }
         }
      }
   }

   public void copyAssignee(Map<String, Set<DispoItemData>> destinationItems, Collection<DispoItem> sourceItems, Map<String, DispoItem> toEdit, CopySetParamOption option) {
      for (DispoItem sourceItem : sourceItems) {
         DispoItem destItem = getCorrespondingDestItem(destinationItems, sourceItem);

         if (destItem != null) {
            String currentAssignee = destItem.getAssignee();
            String sourceAssignee = sourceItem.getAssignee();
            String newAssignee;

            if (!sourceAssignee.equalsIgnoreCase("UNASSIGNED")) {
               switch (option) {
                  case OVERRIDE:
                     newAssignee = sourceAssignee;
                     break;
                  case OVERRIDE_EMPTY:
                     if (currentAssignee.equalsIgnoreCase("UNASSIGNED")) {
                        newAssignee = sourceAssignee;
                     } else {
                        newAssignee = currentAssignee;
                     }
                     break;
                  case MERGE:
                     // Should not get here
                  case NONE:
                  default:
                     newAssignee = currentAssignee;
                     break;
               }

               // Check to see if this item is already set to be edited
               DispoItem matchingToEdit = toEdit.get(sourceItem.getName());
               if (matchingToEdit != null) {
                  ((DispoItemData) matchingToEdit).setAssignee(newAssignee);
               } else {
                  DispoItemData newToEdit = new DispoItemData();
                  newToEdit.setGuid(destItem.getGuid());
                  newToEdit.setName(destItem.getName());
                  newToEdit.setAssignee(newAssignee);
                  toEdit.put(newToEdit.getName(), newToEdit);
               }
            }
         }
      }
   }

   public void copyNotes(Map<String, Set<DispoItemData>> destinationItems, Collection<DispoItem> sourceItems, Map<String, DispoItem> toEdit, CopySetParamOption option) {
      for (DispoItem sourceItem : sourceItems) {
         DispoItem destItem = getCorrespondingDestItem(destinationItems, sourceItem);

         if (destItem != null) {
            String currentItemNotes = destItem.getItemNotes();
            String sourceItemNotes = sourceItem.getItemNotes();
            String newItemNotes;

            if (Strings.isValid(sourceItemNotes)) {
               switch (option) {
                  case OVERRIDE:
                     newItemNotes = sourceItemNotes;
                     break;
                  case OVERRIDE_EMPTY:
                     if (!Strings.isValid(currentItemNotes)) {
                        newItemNotes = sourceItemNotes;
                     } else {
                        newItemNotes = currentItemNotes;
                     }
                     break;
                  case MERGE:
                     if (!Strings.isValid(currentItemNotes)) {
                        newItemNotes = sourceItemNotes;
                     } else {
                        newItemNotes = currentItemNotes + "::" + sourceItemNotes;
                     }
                     break;
                  case NONE:
                  default:
                     newItemNotes = currentItemNotes;
                     break;
               }

               // Check to see if this item is already set to be edited
               DispoItem matchingToEdit = toEdit.get(sourceItem.getName());
               if (matchingToEdit != null) {
                  ((DispoItemData) matchingToEdit).setItemNotes(newItemNotes);
               } else {
                  DispoItemData newToEdit = new DispoItemData();
                  newToEdit.setGuid(destItem.getGuid());
                  newToEdit.setName(destItem.getName());
                  newToEdit.setItemNotes(newItemNotes);
                  toEdit.put(newToEdit.getName(), newToEdit);
               }
            }
         }
      }
   }
}

Back to the top