Skip to main content
summaryrefslogtreecommitdiffstats
blob: b31b7fb5d17e899f952fd474aa197a8052362b78 (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
/*******************************************************************************
 * Copyright (c) 2010 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;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.jface.dialogs.IDialogConstants;
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.enums.PermissionEnum;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.ui.plugin.util.CommandHandler;
import org.eclipse.osee.framework.ui.skynet.commandHandlers.Handlers;
import org.eclipse.osee.framework.ui.swt.Displays;

/**
 * @author Karol M. Wilk
 */
public abstract class GeneralBranchHandler extends CommandHandler {
   public enum OpTypeEnum {
      DELETE("delete", "Delete Branch"),
      PURGE("purge", "Purge Branch");

      private final String dialogType;
      private final String dialogTitle;

      private OpTypeEnum(String type, String title) {
         dialogType = type;
         dialogTitle = Strings.capitalize(type);
      }
   };
   private final OpTypeEnum type;

   public GeneralBranchHandler(OpTypeEnum type) {
      this.type = type;
   }

   public abstract void performOperation(final List<IOseeBranch> branches);

   @Override
   public Object executeWithException(ExecutionEvent arg0, IStructuredSelection selection) throws OseeCoreException {
      List<IOseeBranch> selectedBranches = Handlers.getBranchesFromStructuredSelection(selection);

      Iterator<IOseeBranch> iterator = selectedBranches.iterator();
      List<BranchId> hasChildren = new LinkedList<>();
      List<BranchId> hasNoPermission = new LinkedList<>();
      while (iterator.hasNext()) {
         BranchId branch = iterator.next();
         boolean removed = false;
         if (!AccessControlManager.hasPermission(branch, PermissionEnum.WRITE)) {
            iterator.remove();
            hasNoPermission.add(branch);
            removed = true;
         }
         if (BranchManager.hasChildren(branch)) {
            if (!removed) {
               iterator.remove();
            }
            hasChildren.add(branch);
         }
      }

      if (!hasNoPermission.isEmpty()) {
         StringBuilder noPermission = new StringBuilder();
         noPermission.append(String.format(
            "User does not have permission on the following branches and cannot be %sd:\n", type.dialogType));
         noPermission.append(String.format("%s", hasNoPermission.toString()));

         MessageDialog.openError(Displays.getActiveShell(), type.dialogTitle, noPermission.toString());
      }

      if (!hasChildren.isEmpty()) {
         StringBuilder children = new StringBuilder();
         children.append(String.format("The following branches have children and cannot be %sd:\n", type.dialogType));
         for (BranchId branch : hasChildren) {
            List<BranchId> branches = new LinkedList<>(BranchManager.getChildBranches(branch, true));
            children.append(
               String.format("Branch Id %s has children: %s\n", branch.getId(), Strings.buildStatment(branches)));
         }
         MessageDialog.openError(Displays.getActiveShell(), type.dialogTitle, children.toString());
      }

      if (!selectedBranches.isEmpty()) {
         StringBuilder branchesStatement = new StringBuilder();
         branchesStatement.append(String.format("Are you sure you want to %s branch(es): ", type.dialogType));
         branchesStatement.append(Strings.buildStatment(selectedBranches));
         branchesStatement.append(" \u003F");

         MessageDialog dialog =
            new MessageDialog(Displays.getActiveShell(), type.dialogTitle, null, branchesStatement.toString(),
               MessageDialog.QUESTION, new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL}, 1);

         if (dialog.open() == 0) {
            performOperation(selectedBranches);
         }
      }

      return null;
   }

   @Override
   public boolean isEnabledWithException(IStructuredSelection structuredSelection) throws OseeCoreException {
      List<? extends BranchId> branches = Handlers.getBranchesFromStructuredSelection(structuredSelection);
      return !branches.isEmpty() && (AccessControlManager.isOseeAdmin() || canEnableBranches(branches));
   }

   private boolean canEnableBranches(List<? extends BranchId> branches) {
      boolean canBeDeleted = true;
      for (BranchId branch : branches) {
         if (!isEnableAllowed(branch)) {
            canBeDeleted = false;
            break;
         }
      }
      return canBeDeleted;
   }

   private boolean isEnableAllowed(BranchId branch) {
      return !BranchManager.isChangeManaged(branch) && BranchManager.getType(branch).isWorkingBranch();
   }
}

Back to the top