bug[TW18632]: Archive branch when workflow transitions to complete

Change-Id: Ic994cef847e829c54fc4df1b2841ec5dc5011751
Signed-off-by: David W. Miller <david.w.miller6@boeing.com>
diff --git a/plugins/org.eclipse.osee.ats.api/src/org/eclipse/osee/ats/api/workflow/IAtsBranchService.java b/plugins/org.eclipse.osee.ats.api/src/org/eclipse/osee/ats/api/workflow/IAtsBranchService.java
index fbcd6a4..909c4d5 100644
--- a/plugins/org.eclipse.osee.ats.api/src/org/eclipse/osee/ats/api/workflow/IAtsBranchService.java
+++ b/plugins/org.eclipse.osee.ats.api/src/org/eclipse/osee/ats/api/workflow/IAtsBranchService.java
@@ -124,6 +124,8 @@
 
    boolean isArchived(BranchId branch);
 
+   void archiveBranch(BranchId branch);
+
    TransactionRecord getCommitTransactionRecord(IAtsTeamWorkflow teamWf, BranchId branch);
 
    Collection<BranchId> getBranchesToCommitTo(IAtsTeamWorkflow teamWf);
diff --git a/plugins/org.eclipse.osee.ats.core/src/org/eclipse/osee/ats/core/workflow/AtsWorkItemServiceImpl.java b/plugins/org.eclipse.osee.ats.core/src/org/eclipse/osee/ats/core/workflow/AtsWorkItemServiceImpl.java
index 2826921..9cce9c5 100644
--- a/plugins/org.eclipse.osee.ats.core/src/org/eclipse/osee/ats/core/workflow/AtsWorkItemServiceImpl.java
+++ b/plugins/org.eclipse.osee.ats.core/src/org/eclipse/osee/ats/core/workflow/AtsWorkItemServiceImpl.java
@@ -58,6 +58,7 @@
 import org.eclipse.osee.ats.core.util.AtsObjects;
 import org.eclipse.osee.ats.core.util.hooks.AtsNotificationTransitionHook;
 import org.eclipse.osee.ats.core.validator.AtsXWidgetValidateManager;
+import org.eclipse.osee.ats.core.workflow.hooks.AtsCommitBranchWhenCompleteHook;
 import org.eclipse.osee.ats.core.workflow.hooks.AtsForceAssigneesToTeamLeadsWorkItemHook;
 import org.eclipse.osee.ats.core.workflow.hooks.AtsPeerToPeerReviewReviewWorkItemHook;
 import org.eclipse.osee.ats.core.workflow.note.ArtifactNote;
@@ -110,6 +111,7 @@
       transitionHooks.add(new AtsDecisionReviewPrepareWorkItemHook());
       transitionHooks.add(new AtsForceAssigneesToTeamLeadsWorkItemHook());
       transitionHooks.add(new AtsPeerToPeerReviewReviewWorkItemHook());
+      transitionHooks.add(new AtsCommitBranchWhenCompleteHook());
    }
 
    @Override
diff --git a/plugins/org.eclipse.osee.ats.core/src/org/eclipse/osee/ats/core/workflow/hooks/AtsCommitBranchWhenCompleteHook.java b/plugins/org.eclipse.osee.ats.core/src/org/eclipse/osee/ats/core/workflow/hooks/AtsCommitBranchWhenCompleteHook.java
new file mode 100644
index 0000000..a7b3149
--- /dev/null
+++ b/plugins/org.eclipse.osee.ats.core/src/org/eclipse/osee/ats/core/workflow/hooks/AtsCommitBranchWhenCompleteHook.java
@@ -0,0 +1,59 @@
+/*********************************************************************
+ * Copyright (c) 2021 Boeing
+ *
+ * This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License 2.0
+ * which is available at https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ *     Boeing - initial API and implementation
+ **********************************************************************/
+
+package org.eclipse.osee.ats.core.workflow.hooks;
+
+import java.util.Collection;
+import java.util.logging.Level;
+import org.eclipse.osee.ats.api.IAtsWorkItem;
+import org.eclipse.osee.ats.api.user.AtsUser;
+import org.eclipse.osee.ats.api.util.IAtsChangeSet;
+import org.eclipse.osee.ats.api.workdef.IStateToken;
+import org.eclipse.osee.ats.api.workflow.IAtsTeamWorkflow;
+import org.eclipse.osee.ats.api.workflow.hooks.IAtsTransitionHook;
+import org.eclipse.osee.ats.core.internal.AtsApiService;
+import org.eclipse.osee.framework.core.data.IOseeBranch;
+import org.eclipse.osee.framework.logging.OseeLog;
+
+/**
+ * Contributed via AtsWorkItemServiceImpl
+ *
+ * @author David W. Miller
+ */
+public class AtsCommitBranchWhenCompleteHook implements IAtsTransitionHook {
+
+   public String getName() {
+      return AtsCommitBranchWhenCompleteHook.class.getSimpleName();
+   }
+
+   @Override
+   public String getDescription() {
+      return "When the Workflow is transitioned to Completed, archive the branch, if it is not already archived.";
+   }
+
+   @Override
+   public void transitioned(IAtsWorkItem workItem, IStateToken fromState, IStateToken toState, Collection<? extends AtsUser> toAssignees, IAtsChangeSet changes) {
+      try {
+         if (workItem instanceof IAtsTeamWorkflow && toState.getStateType().isCompleted()) {
+            IOseeBranch branch =
+               AtsApiService.get().getBranchService().getWorkingBranch((IAtsTeamWorkflow) workItem, true);
+            if (branch != null && branch.isValid() && !AtsApiService.get().getBranchService().isArchived(branch)) {
+               AtsApiService.get().getBranchService().archiveBranch(branch);
+            }
+         }
+      } catch (Exception ex) {
+         OseeLog.log(AtsCommitBranchWhenCompleteHook.class, Level.SEVERE, "Error archiving branch in complete hook",
+            ex);
+      }
+   }
+}
diff --git a/plugins/org.eclipse.osee.ats.ide/src/org/eclipse/osee/ats/ide/branch/internal/AtsBranchServiceImpl.java b/plugins/org.eclipse.osee.ats.ide/src/org/eclipse/osee/ats/ide/branch/internal/AtsBranchServiceImpl.java
index 276b2e0..5caf33f 100644
--- a/plugins/org.eclipse.osee.ats.ide/src/org/eclipse/osee/ats/ide/branch/internal/AtsBranchServiceImpl.java
+++ b/plugins/org.eclipse.osee.ats.ide/src/org/eclipse/osee/ats/ide/branch/internal/AtsBranchServiceImpl.java
@@ -102,6 +102,11 @@
    }
 
    @Override
+   public void archiveBranch(BranchId branch) {
+      BranchManager.setArchiveState(branch, BranchArchivedState.ARCHIVED);
+   }
+
+   @Override
    public BranchId getParentBranch(BranchId branch) {
       return BranchManager.getParentBranch(branch);
    }
diff --git a/plugins/org.eclipse.osee.ats.ide/src/org/eclipse/osee/ats/ide/util/widgets/commit/CommitXManager.java b/plugins/org.eclipse.osee.ats.ide/src/org/eclipse/osee/ats/ide/util/widgets/commit/CommitXManager.java
index 488a89d..7d6e1dc 100644
--- a/plugins/org.eclipse.osee.ats.ide/src/org/eclipse/osee/ats/ide/util/widgets/commit/CommitXManager.java
+++ b/plugins/org.eclipse.osee.ats.ide/src/org/eclipse/osee/ats/ide/util/widgets/commit/CommitXManager.java
@@ -151,9 +151,7 @@
          } else if (commitStatus == CommitStatus.Commit_Needed || commitStatus == CommitStatus.Merge_In_Progress) {
             XResultData rd = new XResultData();
             AtsApiService.get().getBranchServiceIde().commitWorkingBranch(xCommitManager.getTeamArt(), true, false,
-               branch,
-               AtsApiService.get().getBranchService().isBranchesAllCommittedExcept(xCommitManager.getTeamArt(), branch),
-               rd);
+               branch, false, rd);
             if (rd.isErrors()) {
                ResultsEditor.open("Commit Failure", rd);
             }
diff --git a/plugins/org.eclipse.osee.ats.rest/src/org/eclipse/osee/ats/rest/internal/util/AtsBranchServiceImpl.java b/plugins/org.eclipse.osee.ats.rest/src/org/eclipse/osee/ats/rest/internal/util/AtsBranchServiceImpl.java
index 52ce4fb..fd37ac1 100644
--- a/plugins/org.eclipse.osee.ats.rest/src/org/eclipse/osee/ats/rest/internal/util/AtsBranchServiceImpl.java
+++ b/plugins/org.eclipse.osee.ats.rest/src/org/eclipse/osee/ats/rest/internal/util/AtsBranchServiceImpl.java
@@ -40,6 +40,7 @@
 import org.eclipse.osee.framework.jdk.core.type.HashCollectionSet;
 import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
 import org.eclipse.osee.orcs.OrcsApi;
+import org.eclipse.osee.orcs.data.ArchiveOperation;
 import org.eclipse.osee.orcs.data.TransactionReadable;
 import org.eclipse.osee.orcs.search.BranchQuery;
 import org.eclipse.osee.orcs.search.TransactionQuery;
@@ -133,6 +134,15 @@
    }
 
    @Override
+   public void archiveBranch(BranchId branch) {
+      try {
+         orcsApi.getBranchOps().archiveUnarchiveBranch(branch, ArchiveOperation.ARCHIVE).call();
+      } catch (Exception ex) {
+         throw OseeCoreException.wrap(ex);
+      }
+   }
+
+   @Override
    public Collection<TransactionRecord> getCommittedArtifactTransactionIds(IAtsTeamWorkflow teamWf) {
       ArtifactId artId = ArtifactId.valueOf(teamWf.getId());
       if (!commitArtifactIdMap.containsKey(artId)) {