Skip to main content
summaryrefslogtreecommitdiffstats
blob: 469099172e217d554b16829a612530d4c1847b10 (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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
/*******************************************************************************
 * 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
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.api.IAtsConfigObject;
import org.eclipse.osee.ats.api.IAtsWorkItem;
import org.eclipse.osee.ats.api.ai.IAtsActionableItem;
import org.eclipse.osee.ats.api.data.AtsArtifactTypes;
import org.eclipse.osee.ats.api.data.AtsAttributeTypes;
import org.eclipse.osee.ats.api.data.AtsRelationTypes;
import org.eclipse.osee.ats.api.team.IAtsTeamDefinition;
import org.eclipse.osee.ats.api.version.IAtsVersion;
import org.eclipse.osee.ats.api.workdef.IAtsStateDefinition;
import org.eclipse.osee.ats.api.workdef.IAtsWorkDefinition;
import org.eclipse.osee.ats.api.workdef.StateType;
import org.eclipse.osee.ats.api.workflow.log.IAtsLogItem;
import org.eclipse.osee.ats.api.workflow.log.LogType;
import org.eclipse.osee.ats.core.client.review.AbstractReviewArtifact;
import org.eclipse.osee.ats.core.client.review.AtsReviewCache;
import org.eclipse.osee.ats.core.client.task.TaskArtifact;
import org.eclipse.osee.ats.core.client.team.TeamWorkFlowArtifact;
import org.eclipse.osee.ats.core.client.util.AtsChangeSet;
import org.eclipse.osee.ats.core.client.util.AtsTaskCache;
import org.eclipse.osee.ats.core.client.workflow.AbstractWorkflowArtifact;
import org.eclipse.osee.ats.core.config.TeamDefinitions;
import org.eclipse.osee.ats.core.config.Versions;
import org.eclipse.osee.ats.core.util.AtsUtilCore;
import org.eclipse.osee.ats.core.workflow.transition.TransitionManager;
import org.eclipse.osee.ats.internal.Activator;
import org.eclipse.osee.ats.internal.AtsClientService;
import org.eclipse.osee.ats.world.WorldXNavigateItemAction;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.exception.BranchDoesNotExist;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.core.util.XResultData;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
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.ElapsedTime;
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.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactCache;
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.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.eclipse.osee.framework.skynet.core.utility.ConnectionHandler;
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.notify.OseeEmail;
import org.eclipse.osee.framework.ui.skynet.results.XResultDataUI;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.jdbc.JdbcStatement;

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

   private static String SELECT_COMMON_ART_IDS = "SELECT /*+ ordered */ art1.art_id, txs1.branch_id " + //
      "FROM osee_artifact art1, osee_txs txs1 " + //
      "WHERE art1.gamma_id = txs1.gamma_id AND txs1.tx_current = 1 AND txs1.branch_id = ? " + //
      "ORDER BY art1.art_id, txs1.branch_id ";
   private boolean fixAttributeValues = false;
   private final Set<String> atsIds = new HashSet<>();
   private final Map<String, String> legacyPcrIdToParentId = new HashMap<>(50000);
   private String emailOnComplete = null;
   private final ValidateResults results = new ValidateResults();

   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 class Report extends Job {

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

      @Override
      protected IStatus run(IProgressMonitor monitor) {
         try {
            ElapsedTime et = new ElapsedTime(getName());
            XResultData rd = new XResultData();

            runIt(monitor, rd);

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

   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 = new ArrayList<>();
      //      List<Integer> ids = new ArrayList<>();
      //      ids.add(new Integer(1070598));
      //      artIdLists.add(ids);

      // Un-comment to load from guid list
      //      artIdLists = getFromGuids();

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

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

         atsIds.clear();
         legacyPcrIdToParentId.clear();

         //         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()));
            Date date = new Date();
            Collection<Artifact> allArtifacts =
               ArtifactQuery.getArtifactListFromIds(artIdList, AtsUtilCore.getAtsBranch());
            results.logTestTimeSpent(date, "ArtifactQuery.getArtifactListFromIds");
            //            elapsedTime.end();

            // NOTE: Use DoesNotWorkItemAts to process list of IDs

            // remove all deleted/purged artifacts first
            List<Artifact> artifacts = new ArrayList<>(allArtifacts.size());
            for (Artifact artifact : allArtifacts) {
               if (!artifact.isDeleted()) {
                  artifacts.add(artifact);
               }
            }
            count += artifacts.size();

            testAtsAttributevaluesWithPersist(artifacts);
            testCompletedCancelledStateAttributesSetWithPersist(artifacts);
            testCompletedCancelledPercentComplete(artifacts);
            testStateAttributeDuplications(artifacts);
            testArtifactIds(artifacts);
            testStateInWorkDefinition(artifacts);
            testAttributeSetWorkDefinitionsExist(artifacts);
            testAtsActionsHaveTeamWorkflow(artifacts);
            testAtsWorkflowsHaveAction(artifacts);
            testAtsWorkflowsValidVersion(artifacts);
            testTasksHaveParentWorkflow(artifacts);
            testReviewsHaveParentWorkflowOrActionableItems(artifacts);
            testTeamWorkflows(artifacts);
            testAtsBranchManager(artifacts);
            testTeamDefinitions(artifacts, results);
            testVersionArtifacts(artifacts, results);
            testParallelConfig(artifacts, results);
            testActionableItemToTeamDefinition(artifacts, results);

            for (IAtsHealthCheck atsHealthCheck : AtsHealthCheck.getAtsHealthCheckItems()) {
               atsHealthCheck.validateAtsDatabase(artifacts, results);
            }

            // Clear ATS caches
            for (Artifact artifact : artifacts) {
               if (artifact instanceof TeamWorkFlowArtifact) {
                  AtsTaskCache.decache((TeamWorkFlowArtifact) artifact);
                  AtsReviewCache.decache((TeamWorkFlowArtifact) artifact);
               }
               if (!(artifact instanceof User)) {
                  ArtifactCache.deCache(artifact);
               }
            }

            if (monitor != null) {
               monitor.worked(1);
            }
         }
         // Log resultMap data into xResultData
         results.addResultsMapToResultData(xResultData);
         results.addTestTimeMapToResultData(xResultData);

      } finally {
         OseeEventManager.setDisableEvents(false);
      }
      xResultData.reportSevereLoggingMonitor(monitorLog);
      if (monitor != null) {
         xResultData.log(monitor, "Completed processing " + count + " artifacts.");
      }
   }

   @SuppressWarnings({"unused"})
   private List<Collection<Integer>> getFromGuids() {
      List<String> guids = Arrays.asList("AD3zXUb9kkF08ltQPwQA", "AD3zWI5UrUmhDxwBekAA", "BPLQGf99g2qG_LoknEQA");
      List<Artifact> artifacts = ArtifactQuery.getArtifactListFromIds(guids, AtsUtilCore.getAtsBranch());
      List<Integer> artIds = new ArrayList<>();
      for (Artifact art : artifacts) {
         artIds.add(art.getArtId());
      }
      return Arrays.asList((Collection<Integer>) artIds);
   }

   public void testCompletedCancelledStateAttributesSetWithPersist(Collection<Artifact> artifacts) {
      try {
         SkynetTransaction transaction =
            TransactionManager.createTransaction(AtsUtilCore.getAtsBranch(), "Validate ATS Database");
         testCompletedCancelledStateAttributesSet(artifacts, transaction, results);
         transaction.execute();
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
         results.log("testCompletedCancelledStateAttributesSet", "Error: Exception: " + ex.getLocalizedMessage());
      }
   }

   public static void testCompletedCancelledStateAttributesSet(Collection<Artifact> artifacts, SkynetTransaction transaction, ValidateResults results) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         try {
            if (artifact instanceof AbstractWorkflowArtifact) {
               AbstractWorkflowArtifact awa = (AbstractWorkflowArtifact) artifact;
               if (awa.isCompleted()) {
                  IAtsStateDefinition stateDef = awa.getStateDefinition();
                  if (stateDef == null) {
                     results.log(artifact, "testCompletedCancelledStateAttributesSet",
                        String.format("Error: State Definition null for state [%s] for [%s]", awa.getCurrentStateName(),
                           XResultDataUI.getHyperlink(artifact)));
                  } else if (stateDef.getStateType() != StateType.Completed) {
                     results.log(artifact, "testCompletedCancelledStateAttributesSet",
                        String.format("Error: awa.isCompleted()==true but State [%s] not Completed state for [%s]",
                           stateDef.getName(), XResultDataUI.getHyperlink(artifact)));
                     if (stateDef.getStateType() == StateType.Working) {
                        awa.setSoleAttributeFromString(AtsAttributeTypes.CurrentStateType, StateType.Working.name());
                        AtsChangeSet changes = new AtsChangeSet(ValidateAtsDatabase.class.getSimpleName());
                        TransitionManager.logWorkflowUnCompletedEvent(awa, stateDef, changes,
                           AtsClientService.get().getAttributeResolver());
                        TransitionManager.logWorkflowUnCancelledEvent(awa, stateDef, changes,
                           AtsClientService.get().getAttributeResolver());
                        awa.persist(transaction);
                        results.log(artifact, "testCompletedCancelledStateAttributesSet", "FIXED");
                     } else {
                        results.log(artifact, "testCompletedCancelledStateAttributesSet", "MANUAL FIX REQUIRED");
                     }
                  } else if (awa.getCompletedBy() == null || awa.getCompletedDate() == null || !Strings.isValid(
                     awa.getCompletedFromState())) {
                     results.log(artifact, "testCompletedCancelledStateAttributesSet",
                        String.format("Error: Completed [%s] missing one or more Completed attributes for [%s]",
                           awa.getArtifactTypeName(), XResultDataUI.getHyperlink(artifact)));
                     fixCompletedByAttributes(transaction, awa, results);
                  }
               }
               if (awa.isCancelled()) {
                  IAtsStateDefinition stateDef = awa.getStateDefinition();
                  if (stateDef.getStateType() != StateType.Cancelled) {
                     results.log(artifact, "testCompletedCancelledStateAttributesSet",
                        String.format("Error: awa.isCancelled()==true but State [%s] not Cancelled state for [%s]",
                           stateDef.getName(), XResultDataUI.getHyperlink(artifact)));
                     results.log(artifact, "testCompletedCancelledStateAttributesSet", "MANUAL FIX REQUIRED");
                  } else if (awa.getCancelledBy() == null || awa.getCancelledDate() == null || !Strings.isValid(
                     awa.getCancelledFromState())) {
                     results.log(artifact, "testCompletedCancelledStateAttributesSet",
                        String.format("Error: Cancelled missing Cancelled By attribute for [%s]",
                           XResultDataUI.getHyperlink(artifact)));
                     fixCancelledByAttributes(transaction, awa, results);
                  }
               }
            }
         } catch (Exception ex) {
            results.log(artifact, "testCompletedCancelledStateAttributesSet",
               "Error: on [" + artifact.toStringWithId() + "] exception: " + ex.getLocalizedMessage());
         }
      }
      results.logTestTimeSpent(date, "testCompletedCancelledStateAttributesSet");
   }

   private void testCompletedCancelledPercentComplete(Collection<Artifact> artifacts) {
      Date date = new Date();
      try {
         SkynetTransaction transaction =
            TransactionManager.createTransaction(AtsUtilCore.getAtsBranch(), "Validate ATS Database");
         for (Artifact artifact : artifacts) {
            try {
               if (artifact instanceof AbstractWorkflowArtifact) {
                  AbstractWorkflowArtifact awa = (AbstractWorkflowArtifact) artifact;
                  Integer percentComplete = awa.getSoleAttributeValue(AtsAttributeTypes.PercentComplete, 0);
                  if (awa.isCompletedOrCancelled() && percentComplete != 100) {
                     results.log(artifact, "testCompletedCancelledPercentComplete",
                        String.format("Error: Completed/Cancelled Percent Complete != 100; is [%d] for [%s]",
                           percentComplete, XResultDataUI.getHyperlink(artifact)));
                     awa.setSoleAttributeValue(AtsAttributeTypes.PercentComplete, 100);
                     awa.persist(transaction);
                     results.log(artifact, "testCompletedCancelledPercentComplete", "FIXED");
                  }
               }
            } catch (Exception ex) {
               results.log(artifact, "testCompletedCancelledPercentComplete",
                  "Error: on [" + artifact.toStringWithId() + "] exception: " + ex.getLocalizedMessage());
            }
         }
         transaction.execute();
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
         results.log("testCompletedCancelledPercentComplete", "Error: Exception: " + ex.getLocalizedMessage());
      }

      results.logTestTimeSpent(date, "testCompletedCancelledStateAttributesSet");
   }

   private static void fixCancelledByAttributes(SkynetTransaction transaction, AbstractWorkflowArtifact awa, ValidateResults results) throws OseeCoreException {
      IAtsLogItem cancelledItem = getCancelledLogItem(awa);
      if (cancelledItem != null) {
         results.log(awa, "testCompletedCancelledStateAttributesSet",
            String.format("   FIXED to By [%s] From State [%s] Date [%s] Reason [%s]", cancelledItem.getUserId(),
               cancelledItem.getDate(), cancelledItem.getState(), cancelledItem.getMsg()));
         awa.setSoleAttributeValue(AtsAttributeTypes.CancelledBy, cancelledItem.getUserId());
         awa.setSoleAttributeValue(AtsAttributeTypes.CancelledDate, cancelledItem.getDate());
         awa.setSoleAttributeValue(AtsAttributeTypes.CancelledFromState, cancelledItem.getState());
         if (Strings.isValid(cancelledItem.getMsg())) {
            awa.setSoleAttributeValue(AtsAttributeTypes.CancelledReason, cancelledItem.getMsg());
         }
         awa.persist(transaction);
      }
   }

   private static void fixCompletedByAttributes(SkynetTransaction transaction, AbstractWorkflowArtifact awa, ValidateResults results) throws OseeCoreException {
      IAtsLogItem completedItem = getPreviousStateLogItem(awa);
      if (completedItem != null) {
         results.log(awa, "testCompletedCancelledStateAttributesSet",
            String.format("   FIXED to By [%s] From State [%s] Date [%s]", completedItem.getUserId(),
               completedItem.getDate(), completedItem.getState()));
         awa.setSoleAttributeValue(AtsAttributeTypes.CompletedBy, completedItem.getUserId());
         awa.setSoleAttributeValue(AtsAttributeTypes.CompletedDate, completedItem.getDate());
         awa.setSoleAttributeValue(AtsAttributeTypes.CompletedFromState, completedItem.getState());
         awa.persist(transaction);
      }
   }

   private static IAtsLogItem getCancelledLogItem(AbstractWorkflowArtifact awa) throws OseeCoreException {
      String currentStateName = awa.getCurrentStateName();
      IAtsLogItem fromItem = null;
      for (IAtsLogItem item : awa.getLog().getLogItemsReversed()) {
         if (item.getType() == LogType.StateCancelled && Strings.isValid(item.getState()) && !currentStateName.equals(
            item.getState())) {
            fromItem = item;
            break;
         }
      }
      if (fromItem == null) {
         fromItem = getPreviousStateLogItem(awa);
      }
      return fromItem;
   }

   private static IAtsLogItem getPreviousStateLogItem(AbstractWorkflowArtifact awa) throws OseeCoreException {
      String currentStateName = awa.getCurrentStateName();
      IAtsLogItem fromItem = null;
      for (IAtsLogItem item : awa.getLog().getLogItemsReversed()) {
         if (item.getType() == LogType.StateComplete && Strings.isValid(item.getState()) && !currentStateName.equals(
            item.getState())) {
            fromItem = item;
            break;
         }
      }
      return fromItem;
   }

   private void testStateAttributeDuplications(Collection<Artifact> artifacts) throws OseeCoreException {
      SkynetTransaction transaction =
         TransactionManager.createTransaction(AtsUtilCore.getAtsBranch(), "Validate ATS Database");
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         try {
            if (artifact instanceof AbstractWorkflowArtifact) {
               AbstractWorkflowArtifact awa = (AbstractWorkflowArtifact) artifact;
               Map<String, Attribute<String>> stateNamesToStateStr = new HashMap<>();
               Attribute<String> currentStateAttr = awa.getSoleAttribute(AtsAttributeTypes.CurrentState);
               String currentStateStr = currentStateAttr.getValue();
               String currentStateName = currentStateStr.replaceAll(";.*$", "");
               stateNamesToStateStr.put(currentStateName, currentStateAttr);

               @SuppressWarnings("deprecation")
               List<Attribute<String>> attributes = awa.getAttributes(AtsAttributeTypes.State);
               for (Attribute<String> stateAttr : attributes) {
                  String stateStr = stateAttr.getValue();
                  String stateName = stateStr.replaceAll(";.*$", "");
                  Attribute<String> storedStateAttr = stateNamesToStateStr.get(stateName);
                  String storedStateStr = "";
                  if (storedStateAttr != null) {
                     storedStateStr = stateNamesToStateStr.get(stateName).getValue();
                  }
                  // If != null, this stateName has already been found
                  if (Strings.isValid(storedStateStr)) {
                     String errorStr =
                        "Error: " + artifact.getArtifactTypeName() + " - " + awa.getAtsId() + " duplicate state: " + stateName;
                     // delete if state attr is same as current state
                     if (currentStateName.equals(stateName)) {
                        errorStr += String.format(" - state [%s] matches currentState [%s] lastModified [%s] - FIXED",
                           stateStr, currentStateStr, awa.getLastModified());
                        stateAttr.delete();
                     }
                     // delete if strings are same (name; assignee; hours; percent)
                     else if (stateStr.equals(storedStateStr)) {
                        errorStr +=
                           String.format(" - stateStr [%s] matches storedStateStr [%s] lastModified [%s] - FIXED",
                              stateStr, storedStateStr, awa.getLastModified());
                        stateAttr.delete();
                     }
                     // else attempt to delete the oldest
                     else if (storedStateAttr != null && stateAttr.getGammaId() < storedStateAttr.getGammaId()) {
                        errorStr += String.format(
                           " - stateStr [%s] earlier than storedStateStr [%s] - deleted stateAttr - FIXED", stateStr,
                           storedStateStr, awa.getLastModified());
                        stateAttr.delete();
                     } else if (storedStateAttr != null && storedStateAttr.getGammaId() < stateAttr.getGammaId()) {
                        errorStr += String.format(
                           " - stateStr [%s] later than storedStateStr [%s] - deleted storeStateAttr - FIXED", stateStr,
                           storedStateStr, awa.getLastModified());
                        storedStateAttr.delete();
                     } else {
                        errorStr += " - NO FIX AVAIL";
                     }
                     results.log(artifact, "testStateAttributeDuplications", errorStr);
                  } else {
                     stateNamesToStateStr.put(stateName, stateAttr);
                  }
               }
               if (awa.isDirty()) {
                  awa.persist(transaction);
               }
            }
         } catch (Exception ex) {
            results.log(artifact, "testStateAttributeDuplications",
               "Error: " + artifact.getArtifactTypeName() + " exception: " + ex.getLocalizedMessage());
         }
      }
      transaction.execute();
      results.logTestTimeSpent(date, "testStateAttributeDuplications");
   }

   public void testAtsAttributevaluesWithPersist(Collection<Artifact> artifacts) {
      try {
         SkynetTransaction transaction =
            TransactionManager.createTransaction(AtsUtilCore.getAtsBranch(), "Validate ATS Database");
         testAtsAttributeValues(transaction, results, fixAttributeValues, artifacts);
         transaction.execute();
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
         results.log("testAtsAttributeValues", "Error: Exception: " + ex.getLocalizedMessage());
      }
   }

   private void testAttributeSetWorkDefinitionsExist(Collection<Artifact> artifacts) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         try {
            String workDefName = artifact.getSoleAttributeValue(AtsAttributeTypes.WorkflowDefinition, "");
            if (Strings.isValid(
               workDefName) && AtsClientService.get().getWorkDefinitionAdmin().getWorkDefinition(workDefName) == null) {
               results.log(artifact, "testAttributeSetWorkDefinitionsExist",
                  String.format(
                     "Error: ats.Work Definition attribute value [%s] not valid work definition for " + XResultDataUI.getHyperlink(
                        artifact),
                     workDefName));
            }
         } catch (Exception ex) {
            results.log(artifact, "testAttributeSetWorkDefinitionsExist",
               "Error: " + artifact.getArtifactTypeName() + " exception: " + ex.getLocalizedMessage());
         }
      }
      results.logTestTimeSpent(date, "testAttributeSetWorkDefinitionsExist");
   }

   private void testStateInWorkDefinition(Collection<Artifact> artifacts) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         try {
            if (artifact instanceof AbstractWorkflowArtifact) {
               AbstractWorkflowArtifact awa = (AbstractWorkflowArtifact) artifact;
               if (awa.isInWork()) {
                  String currentStatename = awa.getCurrentStateName();
                  IAtsWorkDefinition workDef = awa.getWorkDefinition();
                  if (workDef.getStateByName(currentStatename) == null) {
                     results.log(artifact, "testStateInWorkDefinition",
                        String.format(
                           "Error: Current State [%s] not valid for Work Definition [%s] for " + XResultDataUI.getHyperlink(
                              artifact),
                           currentStatename, workDef.getName()));
                  }
               }
            }
         } catch (Exception ex) {
            results.log(artifact, "testStateInWorkDefinition",
               "Error: " + artifact.getArtifactTypeName() + " exception: " + ex.getLocalizedMessage());
         }
      }
      results.logTestTimeSpent(date, "testStateInWorkDefinition");
   }

   private void testArtifactIds(Collection<Artifact> artifacts) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         try {
            if (artifact.isDeleted()) {
               continue;
            }
            // Check that duplicate Legacy PCR IDs team arts do not exist with different parent actions
            if (artifact.isOfType(AtsArtifactTypes.TeamWorkflow)) {
               TeamWorkFlowArtifact teamWf = (TeamWorkFlowArtifact) artifact;
               String legacyPcrId = artifact.getSoleAttributeValueAsString(AtsAttributeTypes.LegacyPcrId, null);
               if (legacyPcrId != null) {
                  String parentActionId = teamWf.getParentActionArtifact().getAtsId();
                  String storedParentActionId = legacyPcrIdToParentId.get(legacyPcrId);
                  if (storedParentActionId != null) {
                     if (!storedParentActionId.equals(parentActionId)) {
                        String errorStr = String.format(
                           "Error: Duplicate Legacy PCR Ids [%s] in Different Actions: teamWf %s parentActionId[%s] != storedActionId [%s] ",
                           legacyPcrId, teamWf.toStringWithId(), parentActionId, storedParentActionId);
                        results.log(artifact, "testArtifactIds", errorStr);
                     }
                  } else {
                     legacyPcrIdToParentId.put(legacyPcrId, parentActionId);
                  }
               }
            }
            // Test that ATS Id is set
            if (artifact instanceof IAtsWorkItem) {
               if (artifact.getSoleAttributeValue(AtsAttributeTypes.AtsId, null) == null) {
                  String errorStr =
                     String.format("Error: ATS Id not set for work item [%s] ", artifact.toStringWithId());
                  results.log(artifact, "testArtifactIds", errorStr);
               }
            }
         } catch (Exception ex) {
            results.log(artifact, "testArtifactIds",
               "Error: " + artifact.getArtifactTypeName() + " exception: " + ex.getLocalizedMessage());
         }
      }
      results.logTestTimeSpent(date, "testArtifactIds");
   }

   public static void testVersionArtifacts(Collection<Artifact> artifacts, ValidateResults results) throws OseeCoreException {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         if (artifact.isDeleted()) {
            continue;
         }
         if (artifact.isOfType(AtsArtifactTypes.Version)) {
            IAtsVersion version = AtsClientService.get().getCache().getByUuid(artifact.getUuid(), IAtsVersion.class);
            if (version != null) {
               try {
                  long parentBranchUuid = version.getBaselineBranchUuid();
                  if (parentBranchUuid > 0) {
                     validateBranchUuid(version, parentBranchUuid, results);
                  }
                  if (AtsClientService.get().getVersionService().getTeamDefinition(version) == null) {
                     results.log(artifact, "testVersionArtifacts",
                        "Error: " + version.toStringWithId() + " not related to Team Definition");
                  }

               } catch (Exception ex) {
                  results.log(artifact, "testVersionArtifacts",
                     "Error: " + version.getName() + " exception testing testVersionArtifacts: " + ex.getLocalizedMessage());
               }
            }
         }
      }
      results.logTestTimeSpent(date, "testVersionArtifacts");
   }

   public static void testParallelConfig(List<Artifact> artifacts, ValidateResults results) throws OseeCoreException {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         if (artifact.isDeleted()) {
            continue;
         }
         if (artifact.isOfType(AtsArtifactTypes.Version)) {
            IAtsVersion version = AtsClientService.get().getCache().getByUuid(artifact.getUuid(), IAtsVersion.class);
            if (version != null) {
               for (IAtsVersion parallelVersion : Versions.getParallelVersions(version, AtsClientService.get())) {
                  if (parallelVersion != null) {
                     try {
                        if (!AtsClientService.get().getBranchService().isBranchValid(parallelVersion)) {
                           results.log(artifact, "testParallelConfig",
                              "Error: [" + parallelVersion.toStringWithId() + "] in parallel config without parent branch uuid");
                        }
                     } catch (Exception ex) {
                        results.log(artifact, "testParallelConfig",
                           "Error: " + version.getName() + " exception testing testVersionArtifacts: " + ex.getLocalizedMessage());
                     }
                  }
               }
            }
         }
      }
      results.logTestTimeSpent(date, "testParallelConfig");
   }

   public static void testTeamDefinitions(Collection<Artifact> artifacts, ValidateResults results) throws OseeCoreException {
      Date date = new Date();
      for (Artifact art : artifacts) {
         if (art.isDeleted()) {
            continue;
         }
         if (art.isOfType(AtsArtifactTypes.TeamDefinition)) {
            IAtsTeamDefinition teamDef =
               AtsClientService.get().getCache().getByUuid(art.getUuid(), IAtsTeamDefinition.class);
            try {
               long parentBranchUuid = teamDef.getBaselineBranchUuid();
               if (parentBranchUuid > 0) {
                  validateBranchUuid(teamDef, parentBranchUuid, results);
               }
            } catch (Exception ex) {
               results.log("testTeamDefinitionss",
                  "Error: " + teamDef.getName() + " exception testing testTeamDefinitions: " + ex.getLocalizedMessage());
            }
         }
      }
      results.logTestTimeSpent(date, "testTeamDefinitions");
   }

   private void testTeamWorkflows(Collection<Artifact> artifacts) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         if (artifact.isDeleted()) {
            continue;
         }
         if (artifact.isOfType(AtsArtifactTypes.TeamWorkflow)) {
            TeamWorkFlowArtifact teamArt = (TeamWorkFlowArtifact) artifact;
            try {
               if (teamArt.getActionableItemsDam().getActionableItems().isEmpty()) {
                  results.log(artifact, "testTeamWorkflows",
                     "Error: TeamWorkflow " + XResultDataUI.getHyperlink(teamArt) + " has 0 ActionableItems");
               }
               if (teamArt.getTeamDefinition() == null) {
                  results.log(artifact, "testTeamWorkflows",
                     "Error: TeamWorkflow " + XResultDataUI.getHyperlink(teamArt) + " has no TeamDefinition");
               }
               List<Long> badUuids = getInvalidUuids(teamArt.getActionableItemsDam().getActionableItemUuids());
               if (!badUuids.isEmpty()) {
                  results.log(artifact, "testTeamWorkflows", "Error: TeamWorkflow " + XResultDataUI.getHyperlink(
                     teamArt) + " has AI uuids that don't exisit " + badUuids);
               }
            } catch (Exception ex) {
               results.log(artifact, "testTeamWorkflows",
                  teamArt.getArtifactTypeName() + " exception: " + ex.getLocalizedMessage());
            }
         }
      }
      results.logTestTimeSpent(date, "testTeamWorkflows");
   }

   private List<Long> getInvalidUuids(List<Long> uuids) throws OseeCoreException {
      List<Long> badUuids = new ArrayList<>();
      for (Long uuid : uuids) {
         if (AtsClientService.get().getArtifact(uuid.longValue()) == null) {
            badUuids.add(uuid);
         }
      }
      return badUuids;
   }

   private void testAtsBranchManager(Collection<Artifact> artifacts) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         if (artifact.isDeleted()) {
            continue;
         }
         if (artifact.isOfType(AtsArtifactTypes.TeamWorkflow)) {
            TeamWorkFlowArtifact teamArt = (TeamWorkFlowArtifact) artifact;
            try {
               Branch workingBranch = (Branch) AtsClientService.get().getBranchService().getWorkingBranch(teamArt);
               if (workingBranch != null && !BranchManager.getType(workingBranch).isBaselineBranch()) {
                  if (!BranchManager.getState(workingBranch).isCommitted()) {
                     Collection<BranchId> branchesCommittedTo =
                        AtsClientService.get().getBranchService().getBranchesCommittedTo(teamArt);
                     if (!branchesCommittedTo.isEmpty()) {
                        results.log(artifact, "testAtsBranchManagerA",
                           "Error: TeamWorkflow " + XResultDataUI.getHyperlink(
                              teamArt) + " has committed branches but working branch [" + workingBranch.getUuid() + "] != COMMITTED");
                     }
                  } else if (!BranchManager.isArchived(workingBranch)) {
                     Collection<BranchId> branchesLeftToCommit =
                        AtsClientService.get().getBranchService().getBranchesLeftToCommit(teamArt);
                     if (branchesLeftToCommit.isEmpty()) {
                        results.log(artifact, "testAtsBranchManagerA",
                           "Error: TeamWorkflow " + XResultDataUI.getHyperlink(
                              teamArt) + " has committed all branches but working branch [" + workingBranch.getUuid() + "] != ARCHIVED");
                     }
                  }
               }
            } catch (Exception ex) {
               results.log("testAtsBranchManager",
                  teamArt.getArtifactTypeName() + " exception: " + ex.getLocalizedMessage());
            }
         }
      }
      results.logTestTimeSpent(date, "testAtsBranchManager");
   }

   public static void validateBranchUuid(IAtsConfigObject name, long parentBranchUuid, ValidateResults results) {
      Date date = new Date();
      try {
         BranchId branch = TokenFactory.createBranch(parentBranchUuid);
         if (BranchManager.isArchived(branch)) {
            results.log("validateBranchUuid",
               String.format(
                  "Error: [%s][%d][%s] has Parent Branch Uuid attribute set to Archived Branch [%s] named [%s]",
                  name.getName(), name.getUuid(), name, parentBranchUuid, branch));
         } else if (!BranchManager.getType(branch).isBaselineBranch()) {
            results.log("validateBranchUuid", String.format(
               "Error: [%s][%d][%s] has Parent Branch Uuid attribute [%s][%s] that is a [%s] branch; should be a BASELINE branch",
               name.getName(), name.getUuid(), name, BranchManager.getType(branch).name(), parentBranchUuid, branch));
         }
      } catch (BranchDoesNotExist ex) {
         results.log("validateBranchUuid",
            String.format("Error: [%s][%d][%s] has Parent Branch Uuid attribute [%s] that references a non-existant",
               name.getName(), name.getUuid(), name, parentBranchUuid));
      } catch (Exception ex) {
         results.log("validateBranchUuid",
            "Error: " + name.getName() + " [" + name.toStringWithId() + "] exception: " + ex.getLocalizedMessage());
      }
      results.logTestTimeSpent(date, "validateBranchUuid");
   }

   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 = getCommonArtifactIds(xResultData);
      if (artIds.isEmpty()) {
         xResultData.error("Error: Artifact load returned 0 artifacts to check");
      }
      xResultData.log(monitor, "testLoadAllCommonArtifactIds - Completed " + DateUtil.getMMDDYYHHMM());
      return Collections.subDivide(artIds, 4000);
   }

   private static List<Integer> getCommonArtifactIds(XResultData xResultData) throws OseeCoreException {
      List<Integer> artIds = new ArrayList<>();
      xResultData.log(null, "getCommonArtifactIds - Started " + DateUtil.getMMDDYYHHMM());
      JdbcStatement chStmt = ConnectionHandler.getStatement();
      try {
         chStmt.runPreparedQuery(SELECT_COMMON_ART_IDS, new Object[] {AtsUtilCore.getAtsBranch().getUuid()});
         while (chStmt.next()) {
            artIds.add(chStmt.getInt(1));
         }
      } finally {
         chStmt.close();
         xResultData.log(null, "getCommonArtifactIds - Completed " + DateUtil.getMMDDYYHHMM());
      }
      return artIds;
   }

   public static void testAtsAttributeValues(SkynetTransaction transaction, ValidateResults results, boolean fixAttributeValues, Collection<Artifact> artifacts) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         if (artifact.isDeleted()) {
            continue;
         }
         try {
            // Test for null attribute values
            for (Attribute<?> attr : artifact.getAttributes()) {
               if (attr.getValue() == null) {
                  results.log(artifact, "testAtsAttributeValues", "Error: Artifact: " + XResultDataUI.getHyperlink(
                     artifact) + " Types: " + artifact.getArtifactTypeName() + " - Null Attribute");
                  if (fixAttributeValues) {
                     attr.delete();
                  }
               }
            }

            if (artifact instanceof AbstractWorkflowArtifact) {
               checkAndResolveDuplicateAttributes(artifact, fixAttributeValues, results, transaction);
            }

            if (artifact.hasDirtyAttributes()) {
               artifact.persist(transaction);
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
            results.log(artifact, "testAtsAttributeValues",
               "Error: " + artifact.getArtifactTypeName() + " exception: " + ex.getLocalizedMessage());
         }
      }
      results.logTestTimeSpent(date, "testAtsAttributeValues");
   }

   @SuppressWarnings("deprecation")
   private static void checkAndResolveDuplicateAttributes(Artifact artifact, boolean fixAttributeValues, ValidateResults results, SkynetTransaction transaction) throws OseeCoreException {
      for (AttributeType attrType : artifact.getAttributeTypesUsed()) {
         if (artifact.isDeleted()) {
            continue;
         }
         int count = artifact.getAttributeCount(attrType);
         if (count > attrType.getMaxOccurrences()) {
            String result = String.format(
               "Error: Artifact: " + XResultDataUI.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<>();
            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);
                  results.log("checkAndResolveDuplicateAttributesForAttributeNameContains", "Fixed");
               }
            }
            results.log("checkAndResolveDuplicateAttributesForAttributeNameContains", result);
         }
      }
   }

   private void testAtsActionsHaveTeamWorkflow(Collection<Artifact> artifacts) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         if (artifact.isDeleted()) {
            continue;
         }
         try {
            if (artifact.isOfType(AtsArtifactTypes.Action) && artifact.getRelatedArtifactsCount(
               AtsRelationTypes.ActionToWorkflow_WorkFlow) == 0) {
               results.log(artifact, "testAtsActionsHaveTeamWorkflow",
                  "Error: Action " + XResultDataUI.getHyperlink(artifact) + " has no Team Workflows\n");
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
            results.log(artifact, "testAtsActionsHaveTeamWorkflow", "Error: Exception: " + ex.getLocalizedMessage());
         }
      }
      results.logTestTimeSpent(date, "testAtsActionsHaveTeamWorkflow");
   }

   private void testAtsWorkflowsHaveAction(Collection<Artifact> artifacts) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         if (artifact.isDeleted()) {
            continue;
         }
         if (artifact.isOfType(AtsArtifactTypes.TeamWorkflow)) {
            try {
               int actionCount =
                  ((TeamWorkFlowArtifact) artifact).getRelatedArtifactsCount(AtsRelationTypes.ActionToWorkflow_Action);
               if (actionCount != 1) {
                  results.log(artifact, "testAtsWorkflowsHaveAction", "Error: Team " + XResultDataUI.getHyperlink(
                     artifact) + " has " + actionCount + " parent Action, should be 1\n");
               }
            } catch (Exception ex) {
               results.log(artifact, "testAtsWorkflowsHaveAction",
                  "Error: Team " + artifact.getName() + " has no parent Action: exception " + ex);
            }
         }
      }
      results.logTestTimeSpent(date, "testAtsWorkflowsHaveAction");
   }

   private void testAtsWorkflowsValidVersion(Collection<Artifact> artifacts) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         if (artifact.isDeleted()) {
            continue;
         }
         try {
            if (artifact.isOfType(AtsArtifactTypes.TeamWorkflow)) {
               TeamWorkFlowArtifact teamArt = (TeamWorkFlowArtifact) artifact;
               IAtsVersion verArt = AtsClientService.get().getVersionService().getTargetedVersion(teamArt);
               if (verArt != null && teamArt.getTeamDefinition().getTeamDefinitionHoldingVersions() != null) {
                  if (!teamArt.getTeamDefinition().getTeamDefinitionHoldingVersions().getVersions().contains(verArt)) {
                     results.log(artifact, "testAtsWorkflowsValidVersion",
                        "Error: Team workflow " + XResultDataUI.getHyperlink(
                           teamArt) + " has version" + XResultDataUI.getHyperlink(
                              artifact) + " that does not belong to teamDefHoldingVersions" + XResultDataUI.getHyperlink(
                                 AtsClientService.get().getConfigArtifact(
                                    teamArt.getTeamDefinition().getTeamDefinitionHoldingVersions())));
                  }
               }
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
            results.log(artifact, "testAtsWorkflowsValidVersion", "Error: Exception: " + ex.getLocalizedMessage());
         }
      }
      results.logTestTimeSpent(date, "testAtsWorkflowsValidVersion");
   }

   private void testTasksHaveParentWorkflow(Collection<Artifact> artifacts) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         if (artifact.isDeleted()) {
            continue;
         }
         try {
            if (artifact.isOfType(AtsArtifactTypes.Task)) {
               TaskArtifact taskArtifact = (TaskArtifact) artifact;
               if (taskArtifact.getRelatedArtifactsCount(AtsRelationTypes.TeamWfToTask_TeamWf) != 1) {
                  results.log(artifact, "testTasksHaveParentWorkflow",
                     "Error: Task " + XResultDataUI.getHyperlink(
                        taskArtifact) + " has " + taskArtifact.getRelatedArtifacts(
                           AtsRelationTypes.TeamWfToTask_TeamWf).size() + " parents.");
               }
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
            results.log(artifact, "testTasksHaveParentWorkflow", "Error: Exception: " + ex.getLocalizedMessage());
         }
      }
      results.logTestTimeSpent(date, "testTasksHaveParentWorkflow");
   }

   public static void testActionableItemToTeamDefinition(Collection<Artifact> artifacts, ValidateResults results) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         if (artifact.isDeleted()) {
            continue;
         }
         try {
            if (artifact.isOfType(AtsArtifactTypes.ActionableItem)) {
               IAtsActionableItem aia =
                  AtsClientService.get().getCache().getByUuid(artifact.getUuid(), IAtsActionableItem.class);
               if (aia.isActionable() && TeamDefinitions.getImpactedTeamDefs(Arrays.asList(aia)).isEmpty()) {
                  results.log(artifact, "testActionableItemToTeamDefinition",
                     "Error: ActionableItem " + XResultDataUI.getHyperlink(artifact.getName(),
                        artifact) + " has no related IAtsTeamDefinition and is set to Actionable");
               }
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
            results.log(artifact, "testActionableItemToTeamDefinition",
               "Error: Exception: " + ex.getLocalizedMessage());
         }
      }
      results.logTestTimeSpent(date, "testActionableItemToTeamDefinition");

   }

   private void testReviewsHaveParentWorkflowOrActionableItems(Collection<Artifact> artifacts) {
      Date date = new Date();
      for (Artifact artifact : artifacts) {
         if (artifact.isDeleted()) {
            continue;
         }
         try {
            if (artifact instanceof AbstractReviewArtifact) {
               AbstractReviewArtifact reviewArtifact = (AbstractReviewArtifact) artifact;
               if (reviewArtifact.getRelatedArtifactsCount(
                  AtsRelationTypes.TeamWorkflowToReview_Team) == 0 && reviewArtifact.getActionableItemsDam().getActionableItemUuids().isEmpty()) {
                  results.log(artifact, "testReviewsHaveParentWorkflowOrActionableItems",
                     "Error: Review " + XResultDataUI.getHyperlink(
                        reviewArtifact) + " has 0 related parents and 0 actionable items.");
               }
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
            results.log(artifact, "testTeamDefinitionHasWorkflow", "Error: Exception: " + ex.getLocalizedMessage());
         }
      }
      results.logTestTimeSpent(date, "testReviewsHaveParentWorkflowOrActionableItems");
   }

   public void setFixAttributeValues(boolean fixAttributeValues) {
      this.fixAttributeValues = fixAttributeValues;
   }

   public void setEmailOnComplete(String emailOnComplete) {
      this.emailOnComplete = emailOnComplete;
   }

}

Back to the top