Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7b4c50f6c9257dc7f2e723555daf7c5a7aeacd8f (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
/*******************************************************************************
 * 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.framework.ui.skynet.commandHandlers.branch.commit;

import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.mutable.MutableBoolean;
import org.eclipse.core.commands.ExecutionEvent;
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.jface.viewers.IStructuredSelection;
import org.eclipse.osee.framework.access.AccessControlManager;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.plugin.core.util.Jobs;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.conflict.ConflictManagerExternal;
import org.eclipse.osee.framework.ui.plugin.util.CommandHandler;
import org.eclipse.osee.framework.ui.skynet.commandHandlers.Handlers;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.util.MergeInProgressHandler;
import org.eclipse.osee.framework.ui.skynet.widgets.xBranch.BranchOptionsEnum;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.ui.PlatformUI;

/**
 * @author Jeff C. Phillips
 * @author Ryan D. Brooks
 */
public abstract class CommitHandler extends CommandHandler {
   protected final boolean useParentBranch;

   public CommitHandler(boolean useParentBranch) {
      this.useParentBranch = useParentBranch;
   }

   public static boolean commitBranch(final ConflictManagerExternal conflictManager, final boolean archiveSourceBranch) throws OseeCoreException {
      return commitBranch(conflictManager, archiveSourceBranch, false);
   }

   public static boolean commitBranch(final ConflictManagerExternal conflictManager, final boolean archiveSourceBranch, boolean skipPrompts) throws OseeCoreException {
      boolean toReturn = false;
      BranchState state = BranchManager.getState(conflictManager.getSourceBranch());
      if (!state.isRebaselineInProgress() && !state.isRebaselined()) {
         if (conflictManager.getOriginalConflicts().size() > 0) {
            toReturn = MergeInProgressHandler.handleMergeInProgress(conflictManager, archiveSourceBranch, skipPrompts);
         } else {
            final MutableBoolean dialogResult = new MutableBoolean(false);
            if (!skipPrompts) {
               Displays.pendInDisplayThread(new Runnable() {
                  @Override
                  public void run() {
                     dialogResult.setValue(
                        MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                           "Commit Branch", String.format("Commit branch\n\n\"[%s]\" onto destination\n\n\"[%s]\"",
                              conflictManager.getSourceBranch(), conflictManager.getDestinationBranch())));
                  }
               });
            } else {
               dialogResult.setValue(true);
            }

            if (dialogResult.booleanValue()) {
               BranchManager.commitBranch(null, conflictManager, archiveSourceBranch, false);
               toReturn = true;
            }
         }
      }

      return toReturn;
   }

   @Override
   public Object executeWithException(ExecutionEvent event, IStructuredSelection selection) throws OseeCoreException {
      try {
         List<IOseeBranch> branches = Handlers.getBranchesFromStructuredSelection(selection);
         Iterator<IOseeBranch> iterator = branches.iterator();
         if (iterator.hasNext()) {
            BranchId sourceBranch = iterator.next();

            BranchId destinationBranch = null;
            if (useParentBranch) {
               destinationBranch = BranchManager.getParentBranch(sourceBranch);
            } else {
               destinationBranch = TokenFactory.createBranch(
                  Long.parseLong(event.getParameter(BranchOptionsEnum.BRANCH_ID.origKeyName)));
            }
            Jobs.startJob(new CommitJob(sourceBranch, destinationBranch,
               Boolean.parseBoolean(event.getParameter(CommitBranchParameter.ARCHIVE_PARENT_BRANCH))));
         }
      } catch (Exception ex) {
         OseeCoreException.wrapAndThrow(ex);
      }
      return null;
   }

   @Override
   public boolean isEnabledWithException(IStructuredSelection structuredSelection) throws OseeCoreException {
      List<IOseeBranch> branches = Handlers.getBranchesFromStructuredSelection(structuredSelection);

      if (branches.size() == 1) {
         IOseeBranch branch = branches.iterator().next();
         return useParentBranchValid(branch) || !useParentBranch && AccessControlManager.isOseeAdmin();
      }
      return false;
   }

   protected boolean useParentBranchValid(IOseeBranch branch) throws OseeCoreException {
      return !branch.equals(CoreBranches.SYSTEM_ROOT) && useParentBranch && !BranchManager.isChangeManaged(
         branch) && !BranchManager.isArchived(branch);
   }
   protected class CommitJob extends Job {
      private final BranchId sourceBranch;
      private final BranchId destinationBranch;
      private final boolean archiveSourceBranch;

      public CommitJob(BranchId sourceBranch, BranchId destinationBranch, boolean archiveSourceBranch) {
         super("Commit Branch");
         this.destinationBranch = destinationBranch;
         this.sourceBranch = sourceBranch;
         this.archiveSourceBranch = archiveSourceBranch;
      }

      @Override
      protected IStatus run(IProgressMonitor monitor) {
         try {
            commitBranch(new ConflictManagerExternal(destinationBranch, sourceBranch), archiveSourceBranch);
         } catch (OseeCoreException ex) {
            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getLocalizedMessage(), ex);
         }
         return Status.OK_STATUS;
      }
   }
}

Back to the top