Skip to main content
summaryrefslogtreecommitdiffstats
blob: f59c68673f4f4479a6e017e4949422980b46751e (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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/*******************************************************************************
 * 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.ats.health;

import static org.eclipse.osee.framework.core.enums.DeletionFlag.EXCLUDE_DELETED;
import java.util.Arrays;
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 java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osee.ats.AtsOpenOption;
import org.eclipse.osee.ats.artifact.ActionManager;
import org.eclipse.osee.ats.core.branch.AtsBranchManagerCore;
import org.eclipse.osee.ats.core.config.ActionableItemArtifact;
import org.eclipse.osee.ats.core.config.TeamDefinitionArtifact;
import org.eclipse.osee.ats.core.config.TeamDefinitionManagerCore;
import org.eclipse.osee.ats.core.review.AbstractReviewArtifact;
import org.eclipse.osee.ats.core.task.TaskArtifact;
import org.eclipse.osee.ats.core.team.TeamState;
import org.eclipse.osee.ats.core.team.TeamWorkFlowArtifact;
import org.eclipse.osee.ats.core.type.AtsArtifactTypes;
import org.eclipse.osee.ats.core.type.AtsAttributeTypes;
import org.eclipse.osee.ats.core.type.AtsRelationTypes;
import org.eclipse.osee.ats.core.workdef.WorkDefinitionFactoryLegacyMgr;
import org.eclipse.osee.ats.core.workflow.AbstractWorkflowArtifact;
import org.eclipse.osee.ats.core.workflow.SMAState;
import org.eclipse.osee.ats.core.workflow.XCurrentStateDam;
import org.eclipse.osee.ats.core.workflow.XStateDam;
import org.eclipse.osee.ats.core.workflow.log.AtsLog;
import org.eclipse.osee.ats.core.workflow.log.LogItem;
import org.eclipse.osee.ats.internal.AtsPlugin;
import org.eclipse.osee.ats.task.TaskEditor;
import org.eclipse.osee.ats.task.TaskEditorSimpleProvider;
import org.eclipse.osee.ats.util.AtsUtil;
import org.eclipse.osee.ats.world.WorldXNavigateItemAction;
import org.eclipse.osee.framework.core.data.SystemUser;
import org.eclipse.osee.framework.core.enums.BranchArchivedState;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.exception.BranchDoesNotExist;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.core.util.IWorkPage;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.jdk.core.util.DateUtil;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.logging.SevereLoggingMonitor;
import org.eclipse.osee.framework.plugin.core.util.Jobs;
import org.eclipse.osee.framework.skynet.core.User;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactTypeManager;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeTypeManager;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.utility.Artifacts;
import org.eclipse.osee.framework.skynet.core.utility.ElapsedTime;
import org.eclipse.osee.framework.ui.plugin.PluginUiImage;
import org.eclipse.osee.framework.ui.plugin.xnavigate.XNavigateComposite.TableLoadOption;
import org.eclipse.osee.framework.ui.plugin.xnavigate.XNavigateItem;
import org.eclipse.osee.framework.ui.skynet.results.XResultData;
import org.eclipse.osee.framework.ui.skynet.util.email.EmailUtil;
import org.eclipse.osee.framework.ui.swt.Displays;

/**
 * @author Donald G. Dunne
 */
public class ValidateAtsDatabase extends WorldXNavigateItemAction {

   private boolean fixAssignees = true;
   private boolean fixAttributeValues = true;
   private final Set<String> hrids = new HashSet<String>();
   private final Map<String, String> legacyPcrIdToParentHrid = new HashMap<String, String>();
   private String emailOnComplete = null;
   private static Artifact tempParentAction;

   public ValidateAtsDatabase(XNavigateItem parent) {
      this("Validate ATS Database", parent);
   }

   public ValidateAtsDatabase(String name, XNavigateItem parent) {
      super(parent, name, PluginUiImage.ADMIN);
   }

   @Override
   public void run(TableLoadOption... tableLoadOptions) {
      if (!MessageDialog.openConfirm(Displays.getActiveShell(), getName(), getName())) {
         return;
      }
      Jobs.startJob(new Report(getName()), true);
   }

   public void performTaskAndPend() throws InterruptedException {
      Report job = new Report(getName());
      job.setUser(true);
      job.setPriority(Job.LONG);
      job.schedule();
      job.join();
   }

   public class Report extends Job {

      public Report(String name) {
         super(name);
      }

      @Override
      protected IStatus run(IProgressMonitor monitor) {
         try {

            XResultData rd = new XResultData();

            runIt(monitor, rd);
            rd.report(getName());
            if (Strings.isValid(emailOnComplete)) {
               String html = rd.getReport(getName()).getManipulatedHtml();
               EmailUtil.emailHtml(java.util.Collections.singleton(emailOnComplete),
                  String.format("Sync - %s [%s]", DateUtil.getDateNow(), getName()), html);
            }
         } catch (Exception ex) {
            OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
            return new Status(IStatus.ERROR, AtsPlugin.PLUGIN_ID, -1, ex.getMessage(), ex);
         }
         monitor.done();
         return Status.OK_STATUS;
      }
   }

   private HashCollection<String, String> testNameToResultsMap = null;

   public void runIt(IProgressMonitor monitor, XResultData xResultData) throws OseeCoreException {
      SevereLoggingMonitor monitorLog = new SevereLoggingMonitor();
      OseeLog.registerLoggerListener(monitorLog);

      int count = 0;
      // Break artifacts into blocks so don't run out of memory
      List<Collection<Integer>> artIdLists = null;

      // Un-comment to process whole Common branch - Normal Mode
      ElapsedTime elapsedTime = new ElapsedTime("ValidateAtsDatabase - load ArtIds");
      artIdLists = loadAtsBranchArtifactIds(xResultData, monitor);
      elapsedTime.end();

      // Un-comment to process specific artifact from common - Test Mode
      // artIdLists = Arrays.asList((Collection<Integer>) Arrays.asList(new Integer(524575)));

      if (monitor != null) {
         monitor.beginTask(getName(), artIdLists.size());
      }

      // Remove this after 0.9.7 release and last sync
      OseeEventManager.setDisableEvents(true);
      try {

         testNameToResultsMap = new HashCollection<String, String>();
         int artSetNum = 1;
         for (Collection<Integer> artIdList : artIdLists) {
            // Don't process all lists if just trying to test this report
            elapsedTime =
               new ElapsedTime(String.format("ValidateAtsDatabase - load Artifact set %d/%d", artSetNum++,
                  artIdLists.size()));
            Collection<Artifact> artifacts = ArtifactQuery.getArtifactListFromIds(artIdList, AtsUtil.getAtsBranch());
            elapsedTime.end();
            count += artifacts.size();

            testArtifactIds(artifacts);
            testAtsAttributeValues(artifacts);
            testAtsActionsHaveTeamWorkflow(artifacts);
            testAtsWorkflowsHaveAction(artifacts);
            testAtsWorkflowsHaveZeroOrOneVersion(artifacts);
            testTasksHaveParentWorkflow(artifacts);
            testReviewsHaveParentWorkflowOrActionableItems(artifacts);
            testReviewsHaveValidDefectAndRoleXml(artifacts);
            testTeamWorkflows(artifacts);
            testAtsBranchManager(artifacts);
            testTeamDefinitions(artifacts);
            testVersionArtifacts(artifacts);
            testStateMachineAssignees(artifacts);
            testAtsLogs(artifacts);
            testActionableItemToTeamDefinition(testNameToResultsMap, artifacts);
            testTeamDefinitionHasWorkflow(testNameToResultsMap, artifacts);
            for (IAtsHealthCheck atsHealthCheck : AtsHealthCheck.getAtsHealthCheckItems()) {
               atsHealthCheck.validateAtsDatabase(artifacts, testNameToResultsMap);
            }
            if (monitor != null) {
               monitor.worked(1);
            }
         }
         // Log resultMap data into xResultData
         addResultsMapToResultData(xResultData, testNameToResultsMap);
      } finally {
         OseeEventManager.setDisableEvents(false);
      }
      xResultData.reportSevereLoggingMonitor(monitorLog);
      if (monitor != null) {
         xResultData.log(monitor, "Completed processing " + count + " artifacts.");
      }
   }

   public static void addResultsMapToResultData(XResultData xResultData, HashCollection<String, String> testNameToResultsMap) {
      String[] keys = testNameToResultsMap.keySet().toArray(new String[testNameToResultsMap.keySet().size()]);
      Arrays.sort(keys);
      for (String testName : keys) {
         xResultData.log(testName);
         for (String result : testNameToResultsMap.getValues(testName)) {
            xResultData.log(result);
         }
      }
   }

   private void testArtifactIds(Collection<Artifact> artifacts) {
      this.hrids.clear();
      this.legacyPcrIdToParentHrid.clear();
      for (Artifact artifact : artifacts) {
         try {
            // Check that HRIDs not duplicated on Common branch
            if (hrids.contains(artifact.getHumanReadableId())) {
               testNameToResultsMap.put("testArtifactIds",
                  "Error: Duplicate HRIDs: " + XResultData.getHyperlink(artifact));
            }
            // Check that duplicate Legacy PCR IDs team arts do not exist with different parent actions
            if (artifact.isOfType(AtsArtifactTypes.TeamWorkflow)) {
               TeamWorkFlowArtifact teamArt = (TeamWorkFlowArtifact) artifact;
               String legacyPcrId = artifact.getSoleAttributeValueAsString(AtsAttributeTypes.LegacyPcrId, null);
               if (legacyPcrId != null) {
                  if (legacyPcrIdToParentHrid.containsKey(legacyPcrId)) {
                     if (!legacyPcrIdToParentHrid.get(legacyPcrId).equals(
                        teamArt.getParentActionArtifact().getHumanReadableId())) {
                        testNameToResultsMap.put("testArtifactIds",
                           "Error: Duplicate Legacy PCR Ids in Different Actions: " + legacyPcrId);
                     }
                  } else {
                     legacyPcrIdToParentHrid.put(legacyPcrId, teamArt.getParentActionArtifact().getHumanReadableId());
                  }
               }
            }
         } catch (Exception ex) {
            testNameToResultsMap.put(
               "testArtifactIds",
               "Error: " + artifact.getArtifactTypeName() + " " + XResultData.getHyperlink(artifact) + " exception: " + ex.getLocalizedMessage());
         }
      }
   }

   private void testVersionArtifacts(Collection<Artifact> artifacts) {
      for (Artifact artifact : artifacts) {
         if (artifact.isOfType(AtsArtifactTypes.Version)) {
            Artifact verArt = artifact;
            try {
               String parentBranchGuid =
                  verArt.getSoleAttributeValueAsString(AtsAttributeTypes.BaselineBranchGuid, null);
               if (parentBranchGuid != null) {
                  validateBranchGuid(verArt, parentBranchGuid);
               }
            } catch (Exception ex) {
               testNameToResultsMap.put(
                  "testVersionArtifacts",
                  "Error: " + verArt.getArtifactTypeName() + " " + XResultData.getHyperlink(verArt) + " exception testing testVersionArtifacts: " + ex.getLocalizedMessage());
            }
         }
      }
   }

   private void testTeamDefinitions(Collection<Artifact> artifacts) {
      for (Artifact art : artifacts) {
         if (art instanceof TeamDefinitionArtifact) {
            TeamDefinitionArtifact teamDef = (TeamDefinitionArtifact) art;
            try {
               String parentBranchGuid =
                  teamDef.getSoleAttributeValueAsString(AtsAttributeTypes.BaselineBranchGuid, null);
               if (parentBranchGuid != null) {
                  validateBranchGuid(teamDef, parentBranchGuid);
               }
            } catch (Exception ex) {
               testNameToResultsMap.put(
                  "testTeamDefinitionss",
                  "Error: " + teamDef.getArtifactTypeName() + " " + XResultData.getHyperlink(teamDef) + " exception testing testTeamDefinitions: " + ex.getLocalizedMessage());
            }
         }
      }
   }

   private void testTeamWorkflows(Collection<Artifact> artifacts) {
      for (Artifact art : artifacts) {
         if (art.isOfType(AtsArtifactTypes.TeamWorkflow)) {
            TeamWorkFlowArtifact teamArt = (TeamWorkFlowArtifact) art;
            try {
               if (teamArt.getActionableItemsDam().getActionableItems().isEmpty()) {
                  testNameToResultsMap.put("testTeamWorkflows",
                     "Error: TeamWorkflow " + XResultData.getHyperlink(teamArt) + " has 0 ActionableItems");
               }
               if (teamArt.getTeamDefinition() == null) {
                  testNameToResultsMap.put("testTeamWorkflows",
                     "Error: TeamWorkflow " + XResultData.getHyperlink(teamArt) + " has no TeamDefinition");
               }
            } catch (Exception ex) {
               testNameToResultsMap.put(
                  "testTeamWorkflows",
                  teamArt.getArtifactTypeName() + " " + XResultData.getHyperlink(teamArt) + " exception: " + ex.getLocalizedMessage());
            }
         }
      }
   }

   private void testAtsBranchManager(Collection<Artifact> artifacts) {
      for (Artifact art : artifacts) {
         if (art.isOfType(AtsArtifactTypes.TeamWorkflow)) {
            TeamWorkFlowArtifact teamArt = (TeamWorkFlowArtifact) art;
            try {
               Collection<Branch> branchesCommittedTo = AtsBranchManagerCore.getBranchesCommittedTo(teamArt);
               Branch workingBranch = AtsBranchManagerCore.getWorkingBranch(teamArt);
               if (workingBranch != null && branchesCommittedTo.size() > 0 && workingBranch.getBranchState() != BranchState.COMMITTED && workingBranch.getBranchType() != BranchType.BASELINE) {
                  testNameToResultsMap.put(
                     "testAtsBranchManagerA",
                     "Error: TeamWorkflow " + XResultData.getHyperlink(teamArt) + " has committed branches but working branch [" + workingBranch.getGuid() + "] != COMMITTED");
               }
               if (workingBranch != null && workingBranch.getBranchState() == BranchState.COMMITTED && workingBranch.getArchiveState() == BranchArchivedState.UNARCHIVED) {
                  String fixStr = "";
                  if (teamArt.isCompleted()) {
                     fixStr = " - Fix: Workflow Completed, Branch can be Archived";
                  } else {
                     fixStr = " - Workflow not completed, verify manually";
                  }
                  testNameToResultsMap.put(
                     "testAtsBranchManagerB",
                     "Error: TeamWorkflow " + XResultData.getHyperlink(teamArt) + " has committed working branch [" + workingBranch.getGuid() + "] but not archived" + fixStr);
               }
            } catch (Exception ex) {
               testNameToResultsMap.put(
                  "testAtsBranchManager",
                  teamArt.getArtifactTypeName() + " " + XResultData.getHyperlink(teamArt) + " exception: " + ex.getLocalizedMessage());
            }
         }
      }
   }

   private void validateBranchGuid(Artifact artifact, String parentBranchGuid) {
      try {
         Branch branch = BranchManager.getBranchByGuid(parentBranchGuid);
         if (branch.getArchiveState().isArchived()) {
            testNameToResultsMap.put("validateBranchGuid", String.format(
               "Error: Parent Branch Id [%s][%s] can't be Archived branch for [%s][%s]", parentBranchGuid, branch,
               artifact.getHumanReadableId(), artifact));
         } else if (branch.getBranchType().isWorkingBranch()) {
            testNameToResultsMap.put(
               "validateBranchGuid",
               String.format(
                  "Error: Parent Branch [%s][%s] is WORKING branch and can't be parent branch for [%s][%s]; Switch to BASELINE?",
                  parentBranchGuid, branch, artifact.getHumanReadableId(), artifact));
         } else if (!branch.getBranchType().isBaselineBranch()) {
            testNameToResultsMap.put("validateBranchGuid", String.format(
               "Error: Parent Branch Id [%s][%s] must be Baseline branch for [%s][%s]", parentBranchGuid, branch,
               artifact.getHumanReadableId(), artifact));
         }
      } catch (BranchDoesNotExist ex) {
         testNameToResultsMap.put("validateBranchGuid", String.format(
            "Error: Parent Branch Id [%s] references non-existant branch for [%s][%s]", parentBranchGuid,
            artifact.getHumanReadableId(), artifact));
      } catch (Exception ex) {
         testNameToResultsMap.put(
            "validateBranchGuid",
            "Error: " + artifact.getArtifactTypeName() + " " + XResultData.getHyperlink(artifact) + " exception: " + ex.getLocalizedMessage());
      }
   }

   public static List<Collection<Integer>> loadAtsBranchArtifactIds(XResultData xResultData, IProgressMonitor monitor) throws OseeCoreException {
      if (xResultData == null) {
         xResultData = new XResultData();
      }
      xResultData.log(monitor, "testLoadAllCommonArtifactIds - Started " + DateUtil.getMMDDYYHHMM());
      List<Integer> artIds = ArtifactQuery.selectArtifactListFromBranch(AtsUtil.getAtsBranch(), EXCLUDE_DELETED);

      if (artIds.isEmpty()) {
         xResultData.logError("Error: Artifact load returned 0 artifacts to check");
      }
      xResultData.log(monitor, "testLoadAllCommonArtifactIds - Completed " + DateUtil.getMMDDYYHHMM());
      return Collections.subDivide(artIds, 5000);
   }

   private void testAtsAttributeValues(Collection<Artifact> artifacts) {
      try {
         SkynetTransaction transaction = new SkynetTransaction(AtsUtil.getAtsBranch(), "Validate ATS Database");
         for (Artifact artifact : artifacts) {

            try {
               // Test for null attribute values
               for (Attribute<?> attr : artifact.getAttributes()) {
                  if (attr.getValue() == null) {
                     testNameToResultsMap.put(
                        "testAtsAttributeValues",
                        "Error: Artifact: " + XResultData.getHyperlink(artifact) + " Types: " + artifact.getArtifactTypeName() + " - Null Attribute");
                     if (fixAttributeValues) {
                        attr.delete();
                     }
                  }
               }

               if (artifact instanceof AbstractWorkflowArtifact) {
                  checkAndResolveDuplicateAttributesForAttributeNameContains("ats", artifact, fixAttributeValues,
                     testNameToResultsMap, transaction);
               }

               // Test for ats.State Completed;;;<num> or Cancelled;;;<num> and cleanup
               if (artifact instanceof AbstractWorkflowArtifact) {
                  XStateDam stateDam = new XStateDam((AbstractWorkflowArtifact) artifact);
                  for (SMAState state : stateDam.getStates()) {
                     if ((state.getName().equals("Completed") || state.getName().equals("Cancelled")) && state.getPercentComplete() != 0) {
                        testNameToResultsMap.put(
                           "testAtsAttributeValues",
                           "Error: ats.State error for SMA: " + XResultData.getHyperlink(artifact) + " State: " + state.getName() + " Percent: " + state.getPercentComplete());
                        if (fixAttributeValues) {
                           state.setPercentComplete(0);
                           stateDam.setState(state);
                           testNameToResultsMap.put("testAtsAttributeValues", "Fixed");
                        }
                     }
                  }
               }

               // Test for ats.CurrentState Completed;;;<num> or Cancelled;;;<num> and cleanup
               if (artifact instanceof AbstractWorkflowArtifact) {
                  XCurrentStateDam currentStateDam = new XCurrentStateDam((AbstractWorkflowArtifact) artifact);
                  SMAState state = currentStateDam.getState();
                  if ((state.getName().equals("Completed") || state.getName().equals("Cancelled")) && state.getPercentComplete() != 0) {
                     testNameToResultsMap.put(
                        "testAtsAttributeValues",
                        "Error: ats.CurrentState error for SMA: " + XResultData.getHyperlink(artifact) + " State: " + state.getName() + " Percent: " + state.getPercentComplete());
                     if (fixAttributeValues) {
                        state.setPercentComplete(0);
                        currentStateDam.setState(state);
                        testNameToResultsMap.put("testAtsAttributeValues", "Fixed");
                     }
                  }
               }
               if (artifact.hasDirtyAttributes()) {
                  artifact.persist(transaction);
               }
            } catch (OseeCoreException ex) {
               OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
               testNameToResultsMap.put("testAtsAttributeValues",
                  "Error: Artifact: " + XResultData.getHyperlink(artifact) + " Exception: " + ex.getLocalizedMessage());
            }
         }
         transaction.execute();
      } catch (OseeCoreException ex) {
         OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
         testNameToResultsMap.put("testAtsAttributeValues", "Error: Exception: " + ex.getLocalizedMessage());
      }
   }

   public static void checkAndResolveDuplicateAttributesForAttributeNameContains(String nameContainsStr, Artifact artifact, boolean fixAttributeValues, HashCollection<String, String> resultsMap, SkynetTransaction transaction) throws OseeCoreException {
      for (AttributeType attrType : AttributeTypeManager.getAllTypes()) {
         if (attrType.getName().contains(nameContainsStr)) {
            int count = artifact.getAttributeCount(attrType);
            if (count > attrType.getMaxOccurrences()) {
               String result =
                  String.format(
                     "Error: Artifact: " + XResultData.getHyperlink(artifact) + " Type [%s] AttrType [%s] Max [%d] Actual [%d] Values [%s] ",
                     artifact.getArtifactTypeName(), attrType.getName(), attrType.getMaxOccurrences(), count,
                     artifact.getAttributesToString(attrType));
               Map<String, Attribute<?>> valuesAttrMap = new HashMap<String, Attribute<?>>();
               int latestGamma = 0;
               StringBuffer fixInfo = new StringBuffer(" - FIX AVAILABLE");
               for (Attribute<?> attr : artifact.getAttributes(attrType)) {
                  if (attr.getGammaId() > latestGamma) {
                     latestGamma = attr.getGammaId();
                  }
                  String info = String.format("[Gamma [%s] Value [%s]]", attr.getGammaId(), attr.getValue());
                  valuesAttrMap.put(info, attr);
                  fixInfo.append(info);
               }
               fixInfo.append(" - KEEP Gamma");
               fixInfo.append(latestGamma);
               if (latestGamma != 0) {
                  result += fixInfo;
                  if (fixAttributeValues) {
                     for (Attribute<?> attr : artifact.getAttributes(attrType)) {
                        if (attr.getGammaId() != latestGamma) {
                           attr.delete();
                        }
                     }
                     artifact.persist(transaction);
                     resultsMap.put("checkAndResolveDuplicateAttributesForAttributeNameContains", "Fixed");
                  }
               }
               resultsMap.put("checkAndResolveDuplicateAttributesForAttributeNameContains", result);
            }
         }
      }
   }

   private void testAtsActionsHaveTeamWorkflow(Collection<Artifact> artifacts) {
      for (Artifact artifact : artifacts) {
         try {
            if (artifact.isOfType(AtsArtifactTypes.Action) && ActionManager.getTeams(artifact).isEmpty()) {
               testNameToResultsMap.put("testAtsActionsHaveTeamWorkflow",
                  "Error: Action " + XResultData.getHyperlink(artifact) + " has no Team Workflows\n");
            }
         } catch (OseeCoreException ex) {
            OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
            testNameToResultsMap.put("testAtsActionsHaveTeamWorkflow", "Error: Exception: " + ex.getLocalizedMessage());
         }
      }
   }

   private void testAtsWorkflowsHaveAction(Collection<Artifact> artifacts) {
      for (Artifact artifact : artifacts) {
         try {
            if (artifact.isOfType(AtsArtifactTypes.TeamWorkflow)) {
               boolean noParent = false;
               try {
                  if (((TeamWorkFlowArtifact) artifact).getParentActionArtifact() == null) {
                     testNameToResultsMap.put("testAtsWorkflowsHaveAction",
                        "Error: Team " + XResultData.getHyperlink(artifact) + " has no parent Action\n");
                     noParent = true;
                  }
               } catch (Exception ex) {
                  testNameToResultsMap.put("testAtsWorkflowsHaveAction",
                     "Error: Team " + XResultData.getHyperlink(artifact) + " has no parent Action: exception " + ex);
                  noParent = true;
               }
               // Create temporary action so these can be either purged or re-assigned
               if (noParent) {
                  if (tempParentAction == null) {
                     tempParentAction =
                        ArtifactTypeManager.addArtifact(AtsArtifactTypes.Action, AtsUtil.getAtsBranch());
                     tempParentAction.setName("Temp Parent Action");
                     testNameToResultsMap.put(
                        "testAtsWorkflowsHaveAction",
                        "Error: Temp Parent Action " + XResultData.getHyperlink(tempParentAction) + " created for orphaned teams.");

                  }
                  tempParentAction.addRelation(AtsRelationTypes.ActionToWorkflow_WorkFlow, artifact);
                  tempParentAction.persist();
               }
            }
         } catch (OseeCoreException ex) {
            OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
            testNameToResultsMap.put("testAtsWorkflowsHaveAction", "Error: Exception: " + ex.getLocalizedMessage());
         }
      }
      if (tempParentAction != null) {
         AtsUtil.openATSAction(tempParentAction, AtsOpenOption.AtsWorld);
      }
   }

   private void testAtsWorkflowsHaveZeroOrOneVersion(Collection<Artifact> artifacts) {
      for (Artifact artifact : artifacts) {
         try {
            if (artifact.isOfType(AtsArtifactTypes.TeamWorkflow)) {
               TeamWorkFlowArtifact teamArt = (TeamWorkFlowArtifact) artifact;
               if (teamArt.getRelatedArtifacts(AtsRelationTypes.TeamWorkflowTargetedForVersion_Version).size() > 1) {
                  testNameToResultsMap.put(
                     "testAtsWorkflowsHaveZeroOrOneVersion",
                     "Error: Team workflow " + XResultData.getHyperlink(teamArt) + " has " + teamArt.getRelatedArtifacts(
                        AtsRelationTypes.TeamWorkflowTargetedForVersion_Version).size() + " versions");
               }
            }
         } catch (OseeCoreException ex) {
            OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
            testNameToResultsMap.put("testAtsWorkflowsHaveZeroOrOneVersion",
               "Error: Exception: " + ex.getLocalizedMessage());
         }
      }

   }

   private void testTasksHaveParentWorkflow(Collection<Artifact> artifacts) {
      Set<Artifact> badTasks = new HashSet<Artifact>(30);
      for (Artifact artifact : artifacts) {
         try {
            if (artifact.isOfType(AtsArtifactTypes.Task)) {
               TaskArtifact taskArtifact = (TaskArtifact) artifact;
               if (taskArtifact.getRelatedArtifacts(AtsRelationTypes.SmaToTask_Sma).size() != 1) {
                  testNameToResultsMap.put(
                     "testTasksHaveParentWorkflow",
                     "Error: Task " + XResultData.getHyperlink(taskArtifact) + " has " + taskArtifact.getRelatedArtifacts(
                        AtsRelationTypes.SmaToTask_Sma).size() + " parents.");
                  badTasks.add(taskArtifact);
               }
            }
         } catch (OseeCoreException ex) {
            OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
            testNameToResultsMap.put("testTasksHaveParentWorkflow", "Error: Exception: " + ex.getLocalizedMessage());
         }
      }
      try {
         if (badTasks.size() > 0) {
            TaskEditor.open(new TaskEditorSimpleProvider("ValidateATSDatabase: Tasks have !=1 parent workflows.",
               badTasks));
         }
      } catch (OseeCoreException ex) {
         OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
         testNameToResultsMap.put("testTasksHaveParentWorkflow", "Error: Exception: " + ex.getLocalizedMessage());
      }
   }

   public static void testActionableItemToTeamDefinition(HashCollection<String, String> testNameToResultsMap, Collection<Artifact> artifacts) {
      for (Artifact artifact : artifacts) {
         try {
            if (artifact instanceof ActionableItemArtifact) {
               ActionableItemArtifact aia = (ActionableItemArtifact) artifact;
               if (aia.isActionable() && TeamDefinitionManagerCore.getImpactedTeamDefs(Arrays.asList(aia)).isEmpty()) {
                  testNameToResultsMap.put(
                     "testActionableItemToTeamDefinition",
                     "Error: ActionableItem " + XResultData.getHyperlink(artifact.getName(), artifact) + " has to related TeamDefinition and is set to Actionable");
               }
            }
         } catch (OseeCoreException ex) {
            OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
            testNameToResultsMap.put("testActionableItemToTeamDefinition",
               "Error: Exception: " + ex.getLocalizedMessage());
         }
      }
   }

   public static void testTeamDefinitionHasWorkflow(HashCollection<String, String> testNameToResultsMap, Collection<Artifact> artifacts) {
      for (Artifact artifact : artifacts) {
         try {
            if (artifact instanceof TeamDefinitionArtifact) {
               TeamDefinitionArtifact teamDef = (TeamDefinitionArtifact) artifact;
               if (teamDef.isActionable() && WorkDefinitionFactoryLegacyMgr.getWorkFlowDefinitionFromTeamDefinition(teamDef) == null) {
                  testNameToResultsMap.put(
                     "testTeamDefinitionHasWorkflow",
                     "Error: TeamDefintion " + XResultData.getHyperlink(artifact.getName(), artifact) + " has no related workflow and is set to Actionable");
               }
            }
         } catch (OseeCoreException ex) {
            OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
            testNameToResultsMap.put("testTeamDefinitionHasWorkflow", "Error: Exception: " + ex.getLocalizedMessage());
         }
      }
   }

   private void testReviewsHaveValidDefectAndRoleXml(Collection<Artifact> artifacts) {
      for (Artifact artifact : artifacts) {
         if (artifact instanceof AbstractReviewArtifact) {
            AbstractReviewArtifact reviewArtifact = (AbstractReviewArtifact) artifact;
            try {
               if (reviewArtifact.getAttributes(AtsAttributeTypes.ReviewDefect).size() > 0 && reviewArtifact.getDefectManager().getDefectItems().isEmpty()) {
                  testNameToResultsMap.put(
                     "testReviewsHaveValidDefectAndRoleXml",
                     "Error: Review " + XResultData.getHyperlink(reviewArtifact) + " has defect attribute, but no defects (xml parsing error).");
               }
               if (reviewArtifact.getAttributes(AtsAttributeTypes.Role).size() > 0 && reviewArtifact.getUserRoleManager().getUserRoles().isEmpty()) {
                  testNameToResultsMap.put(
                     "testReviewsHaveValidDefectAndRoleXml",
                     "Error: Review " + XResultData.getHyperlink(reviewArtifact) + " has role attribute, but no roles (xml parsing error).");
               }
            } catch (OseeCoreException ex) {
               testNameToResultsMap.put(
                  "testReviewsHaveValidDefectAndRoleXml",
                  "Error: Exception processing Review " + XResultData.getHyperlink(reviewArtifact) + " defect test " + ex.getLocalizedMessage());
            }
         }
      }
   }

   private void testReviewsHaveParentWorkflowOrActionableItems(Collection<Artifact> artifacts) {
      for (Artifact artifact : artifacts) {
         try {
            if (artifact instanceof AbstractReviewArtifact) {
               AbstractReviewArtifact reviewArtifact = (AbstractReviewArtifact) artifact;
               if (reviewArtifact.getRelatedArtifacts(AtsRelationTypes.TeamWorkflowToReview_Team).isEmpty() && reviewArtifact.getActionableItemsDam().getActionableItemGuids().isEmpty()) {
                  testNameToResultsMap.put(
                     "testReviewsHaveParentWorkflowOrActionableItems",
                     "Error: Review " + XResultData.getHyperlink(reviewArtifact) + " has 0 related parents and 0 actionable items.");
               }
            }
         } catch (OseeCoreException ex) {
            OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
            testNameToResultsMap.put("testTeamDefinitionHasWorkflow", "Error: Exception: " + ex.getLocalizedMessage());
         }
      }
   }

   private void testAtsLogs(Collection<Artifact> artifacts) {
      for (Artifact art : artifacts) {
         if (art instanceof AbstractWorkflowArtifact) {
            AbstractWorkflowArtifact awa = (AbstractWorkflowArtifact) art;
            try {
               AtsLog log = awa.getLog();
               if (awa.getCreatedBy() == null) {
                  try {
                     testNameToResultsMap.put(
                        "testAtsLogs",
                        "Error: " + awa.getArtifactTypeName() + " " + XResultData.getHyperlink(awa) + " originator == null");
                  } catch (Exception ex) {
                     testNameToResultsMap.put(
                        "testAtsLogs",
                        "Error: " + awa.getArtifactTypeName() + " " + XResultData.getHyperlink(awa) + " exception accessing originator: " + ex.getLocalizedMessage());
                  }
               }
               for (IWorkPage state : Arrays.asList(TeamState.Completed, TeamState.Cancelled)) {
                  if (awa.isInState(state)) {
                     LogItem logItem = awa.getStateStartedData(state);
                     if (logItem == null) {
                        try {
                           testNameToResultsMap.put(
                              "testAtsLogs",
                              "Error: " + awa.getArtifactTypeName() + " " + XResultData.getHyperlink(awa) + " state \"" + state + "\" logItem == null");
                        } catch (Exception ex) {
                           testNameToResultsMap.put(
                              "testAtsLogs",
                              "Error: " + awa.getArtifactTypeName() + " " + XResultData.getHyperlink(awa) + " exception accessing logItem: " + ex.getLocalizedMessage());

                        }
                     } else if (logItem.getDate() == null) {
                        try {
                           testNameToResultsMap.put(
                              "testAtsLogs",
                              "Error: " + awa.getArtifactTypeName() + " " + XResultData.getHyperlink(awa) + " state \"" + state + "\" logItem.date == null");
                        } catch (Exception ex) {
                           testNameToResultsMap.put(
                              "testAtsLogs",
                              "Error: " + awa.getArtifactTypeName() + " " + XResultData.getHyperlink(awa) + " exception accessing logItem.date: " + ex.getLocalizedMessage());

                        }
                     }
                  }
               }
               // Generate html log which will exercise all the conversions
               log.getHtml();
               // Verify that all users are resolved
               for (LogItem logItem : awa.getLog().getLogItems()) {
                  if (logItem.getUser() == null) {
                     testNameToResultsMap.put(
                        "testAtsLogs",
                        "Error: " + awa.getArtifactTypeName() + " " + XResultData.getHyperlink(awa) + " user == null for userId \"" + logItem.getUserId() + "\"");
                  }
               }
            } catch (Exception ex) {
               testNameToResultsMap.put(
                  "testAtsLogs",
                  "Error: " + awa.getArtifactTypeName() + " " + XResultData.getHyperlink(awa) + " exception accessing AtsLog: " + ex.getLocalizedMessage());
            }
         }
      }
   }

   private static User unAssignedUser;
   private static User oseeSystemUser;

   private void testStateMachineAssignees(Collection<Artifact> artifacts) {
      if (unAssignedUser == null) {
         try {
            unAssignedUser = UserManager.getUser(SystemUser.UnAssigned);
            oseeSystemUser = UserManager.getUser(SystemUser.OseeSystem);
         } catch (OseeCoreException ex) {
            testNameToResultsMap.put("testStateMachineAssignees",
               "Error: Exception retrieving users: " + ex.getLocalizedMessage());
            OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
         }
      }
      for (Artifact art : artifacts) {
         if (art instanceof AbstractWorkflowArtifact) {
            try {
               AbstractWorkflowArtifact awa = (AbstractWorkflowArtifact) art;
               if ((awa.isCompleted() || awa.isCancelled()) && awa.getStateMgr().getAssignees().size() > 0) {
                  testNameToResultsMap.put(
                     "testStateMachineAssignees",
                     "Error: " + awa.getArtifactTypeName() + " " + XResultData.getHyperlink(awa) + " cancel/complete with attribute assignees");
                  if (fixAssignees) {
                     awa.getStateMgr().clearAssignees();
                     awa.persist();
                     testNameToResultsMap.put("testStateMachineAssignees", "Fixed");
                  }
               }
               if (awa.getStateMgr().getAssignees().size() > 1 && awa.getStateMgr().getAssignees().contains(
                  unAssignedUser)) {
                  testNameToResultsMap.put(
                     "testStateMachineAssignees",
                     "Error: " + awa.getArtifactTypeName() + " " + XResultData.getHyperlink(awa) + " is unassigned and assigned => " + Artifacts.toString(
                        "; ", awa.getStateMgr().getAssignees()));
                  if (fixAssignees) {
                     awa.getStateMgr().removeAssignee(unAssignedUser);
                     testNameToResultsMap.put("testStateMachineAssignees", "Fixed");
                  }
               }
               if (awa.getStateMgr().getAssignees().contains(oseeSystemUser)) {
                  testNameToResultsMap.put(
                     "testStateMachineAssignees",
                     "Error: " + art.getHumanReadableId() + " is assigned to OseeSystem; invalid assignment - MANUAL FIX REQUIRED");
               }
               if (!awa.isCompleted() && !awa.isCancelled() && awa.getStateMgr().getAssignees().isEmpty()) {
                  testNameToResultsMap.put(
                     "testStateMachineAssignees",
                     "Error: " + awa.getArtifactTypeName() + " " + XResultData.getHyperlink(awa) + " In Work without assignees");
               }
            } catch (OseeCoreException ex) {
               testNameToResultsMap.put("testStateMachineAssignees",
                  "Error: Exception testing assignees: " + ex.getLocalizedMessage());
               OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
            }
         }
      }
   }

   /**
    * @param fixAssignees the fixAssignees to set
    */
   public void setFixAssignees(boolean fixAssignees) {
      this.fixAssignees = fixAssignees;
   }

   /**
    * @param fixAttributeValues the fixAttributeValues to set
    */
   public void setFixAttributeValues(boolean fixAttributeValues) {
      this.fixAttributeValues = fixAttributeValues;
   }

   /**
    * set to email if desire email on completion
    */
   public void setEmailOnComplete(String emailOnComplete) {
      this.emailOnComplete = emailOnComplete;
   }

}

Back to the top