Skip to main content
summaryrefslogtreecommitdiffstats
blob: ef8c73c8ec70811a3b0ebbea638cfb4ffb4abe7c (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
/*******************************************************************************
 * 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.List;
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.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.jdk.core.type.MutableInteger;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
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.AWorkbench;
import org.eclipse.osee.framework.ui.plugin.util.CommandHandler;
import org.eclipse.osee.framework.ui.skynet.SkynetGuiPlugin;
import org.eclipse.osee.framework.ui.skynet.commandHandlers.Handlers;
import org.eclipse.osee.framework.ui.skynet.widgets.xBranch.BranchViewPresentationPreferences;
import org.eclipse.osee.framework.ui.skynet.widgets.xmerge.MergeView;
import org.eclipse.osee.framework.ui.swt.Displays;

/**
 * @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, boolean archiveSourceBranch) throws OseeCoreException {
      final Branch sourceBranch = conflictManager.getSourceBranch();
      final Branch destinationBranch = conflictManager.getDestinationBranch();
      final TransactionRecord transactionId = sourceBranch.getBaseTransaction();
      boolean branchCommitted = false;

      if (!conflictManager.getRemainingConflicts().isEmpty()) {
         String message =
            "Commit stopped due to unresolved conflicts\n\nPossible Resolutions:\n  Cancel commit and resolve at a later time\n  Launch the Merge Manager to resolve conflicts";
         final String fMessage;
         final String[] choices;
         if (AccessControlManager.isOseeAdmin()) {
            fMessage = message + "\n  Force the commit";
            choices = new String[] {"Cancel", "Launch Merge Manager", "Force Commit (Admin Only)"};
         } else {
            fMessage = message;
            choices = new String[] {"Cancel", "Launch Merge Manager"};
         }

         final MutableInteger dialogResult = new MutableInteger(0);
         Displays.pendInDisplayThread(new Runnable() {
            @Override
            public void run() {
               MessageDialog dialog =
                  new MessageDialog(Displays.getActiveShell(), "Unresolved Conflicts", null, fMessage,
                     MessageDialog.QUESTION, choices, 0);
               dialogResult.setValue(dialog.open());
               if (dialogResult.getValue() == 1) {
                  MergeView.openView(sourceBranch, destinationBranch, transactionId);
               }
            }
         });

         if (dialogResult.getValue() == 2) {
            BranchManager.commitBranch(null, conflictManager, archiveSourceBranch, true);
            branchCommitted = true;
         }
      } else {
         final StringBuilder message =
            new StringBuilder(
               "Commit branch\n\n\"" + sourceBranch + "\"\n\n onto destination branch\n\n\"" + destinationBranch + "\"\n");
         int numOriginalConflicts = conflictManager.getOriginalConflicts().size();
         if (numOriginalConflicts > 0) {
            message.append("\nwith " + numOriginalConflicts + " conflicts resolved.\n");
         } else {
            message.append("\n(no conflicts found)\n");
         }
         message.append("\nCommit?");

         final MutableInteger dialogResult = new MutableInteger(0);
         Displays.pendInDisplayThread(new Runnable() {
            @Override
            public void run() {
               try {
                  if (conflictManager.getOriginalConflicts().isEmpty()) {
                     MessageDialog dialog =
                        new MessageDialog(Displays.getActiveShell(), "Commit Branch", null, message.toString(),
                           MessageDialog.QUESTION, new String[] {"Ok", "Cancel"}, 0);
                     dialogResult.setValue(dialog.open());
                  } else {
                     MessageDialog dialog =
                        new MessageDialog(Displays.getActiveShell(), "Commit Branch", null, message.toString(),
                           MessageDialog.QUESTION, new String[] {"Ok", "Launch Merge Manager", "Cancel"}, 0);
                     dialogResult.setValue(dialog.open());
                     if (dialogResult.getValue() == 1) {
                        MergeView.openView(sourceBranch, destinationBranch, transactionId);
                     }
                  }
               } catch (OseeCoreException ex) {
                  OseeLog.log(SkynetGuiPlugin.class, OseeLevel.SEVERE_POPUP, ex);
               }
            }
         });

         if (dialogResult.getValue() == 0) {
            BranchManager.commitBranch(null, conflictManager, archiveSourceBranch, false);
            branchCommitted = true;
         }
      }
      return branchCommitted;
   }

   @Override
   public Object executeWithException(ExecutionEvent event) throws OseeCoreException {
      IStructuredSelection selection =
         (IStructuredSelection) AWorkbench.getActivePage().getActivePart().getSite().getSelectionProvider().getSelection();

      List<Branch> branches = Handlers.getBranchesFromStructuredSelection(selection);
      Branch sourceBranch = branches.iterator().next();

      Branch destinationBranch = null;
      if (useParentBranch) {
         destinationBranch = sourceBranch.getParentBranch();
      } else {
         destinationBranch =
            BranchManager.getBranch(Integer.parseInt(event.getParameter(BranchViewPresentationPreferences.BRANCH_ID)));
      }
      Jobs.startJob(new CommitJob(sourceBranch, destinationBranch,
         Boolean.parseBoolean(event.getParameter(CommitBranchParameter.ARCHIVE_PARENT_BRANCH))));
      return null;
   }

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

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

   protected boolean useParentBranchValid(Branch branch) throws OseeCoreException {
      return branch.hasParentBranch() && useParentBranch && !BranchManager.isChangeManaged(branch) && !branch.getArchiveState().isArchived();
   }
   protected class CommitJob extends Job {
      private final Branch sourceBranch;
      private final Branch destinationBranch;
      private final boolean archiveSourceBranch;

      public CommitJob(Branch sourceBranch, Branch 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, SkynetGuiPlugin.PLUGIN_ID, ex.getLocalizedMessage(), ex);
         }
         return Status.OK_STATUS;
      }
   }
}

Back to the top