Skip to main content
summaryrefslogtreecommitdiffstats
blob: 64e7b6942bd18c38651f63b975f5e7a49db61081 (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
/*******************************************************************************
 * Copyright (c) 2013 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.core.ai;

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.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.osee.ats.api.ai.IAtsActionableItem;
import org.eclipse.osee.ats.api.team.CreateTeamData;
import org.eclipse.osee.ats.api.team.CreateTeamOption;
import org.eclipse.osee.ats.api.team.IAtsTeamDefinition;
import org.eclipse.osee.ats.api.user.IAtsUser;
import org.eclipse.osee.ats.api.workflow.IAtsTeamWorkflow;
import org.eclipse.osee.ats.core.config.ITeamDefinitionUtility;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.core.util.XResultData;

/**
 * @author Donald G Dunne
 */
public class ModifyActionableItems {

   private final XResultData results;
   private final IAtsTeamWorkflow teamWf;
   private final Collection<IAtsActionableItem> currAIsForAllWfs;
   private final Collection<IAtsActionableItem> currWorkflowDesiredAIs;
   private final Collection<IAtsActionableItem> newAIs;
   private final IAtsUser modifiedBy;
   private final List<CreateTeamData> teamDatas = new ArrayList<CreateTeamData>();
   private final List<IAtsActionableItem> addAis = new ArrayList<IAtsActionableItem>();
   private final List<IAtsActionableItem> removeAis = new ArrayList<IAtsActionableItem>();
   private final Map<IAtsTeamDefinition, CreateTeamData> teamDefToTeamDataMap =
      new HashMap<IAtsTeamDefinition, CreateTeamData>();
   private final ITeamDefinitionUtility teamDefUtil;

   public ModifyActionableItems(XResultData results, IAtsTeamWorkflow teamWf, Collection<IAtsActionableItem> currAIsForAllWfs, Collection<IAtsActionableItem> currWorkflowDesiredAIs, Collection<IAtsActionableItem> newAIs, IAtsUser modifiedBy, ITeamDefinitionUtility teamDefUtil) {
      this.results = results;
      this.teamWf = teamWf;
      this.currAIsForAllWfs = currAIsForAllWfs;
      this.currWorkflowDesiredAIs = currWorkflowDesiredAIs;
      this.newAIs = newAIs;
      this.modifiedBy = modifiedBy;
      this.teamDefUtil = teamDefUtil;
   }

   public void performModification() throws OseeCoreException {
      Conditions.checkNotNull(results, "results");
      Conditions.checkNotNull(teamWf, "teamWf");
      Conditions.checkNotNull(modifiedBy, "modifiedBy");
      // Determine if changes to this workflow's actionable items
      processAisAddedRemovedFromSelectedTeamWf();
      // Determine what workflows to add
      processAisAddedForNewWorkflows();
   }

   private void processAisAddedForNewWorkflows() throws OseeCoreException {
      Set<IAtsActionableItem> allAIsForNewWorkflow = new HashSet<IAtsActionableItem>();
      Set<IAtsActionableItem> duplicatedAIs = new HashSet<IAtsActionableItem>();
      // determine AIs that already have a team workflow associated
      for (IAtsActionableItem checkAi : newAIs) {
         if (!checkAi.isActionable()) {
            results.logErrorWithFormat("Actionable Item [%s] is not actionable; select item lower in hierarchy",
               checkAi);
         } else {
            if (currAIsForAllWfs.contains(checkAi)) {
               duplicatedAIs.add(checkAi);
            }
            allAIsForNewWorkflow.add(checkAi);
         }
      }
      Date createdDate = new Date();
      // process new and duplicated workflows
      for (IAtsActionableItem ai : allAIsForNewWorkflow) {
         IAtsTeamDefinition teamDef = getImpactedTeamDef(ai);
         if (teamDefToTeamDataMap.containsKey(teamDef)) {
            CreateTeamData createTeamData = teamDefToTeamDataMap.get(teamDef);
            createTeamData.getActionableItems().add(ai);
         } else {
            CreateTeamData createTeamData =
               new CreateTeamData(teamDef, Arrays.asList(ai), new LinkedList<IAtsUser>(teamDef.getLeads()),
                  createdDate, modifiedBy, CreateTeamOption.Duplicate_If_Exists);
            teamDatas.add(createTeamData);
            teamDefToTeamDataMap.put(teamDef, createTeamData);
         }
      }
      // add messages to results
      for (CreateTeamData data : teamDatas) {
         results.log(String.format("Create New Team Workflow for Actionable Item(s) %s", data.getActionableItems()));
         for (IAtsActionableItem ai : data.getActionableItems()) {
            if (duplicatedAIs.contains(ai)) {
               results.logWithFormat("   - Note: Actionable Item [%s] is impacted by an existing Team Workflow\n", ai);
            }
         }
      }
   }

   /**
    * Return single Team Definition associated with this Actionable Item
    */
   private IAtsTeamDefinition getImpactedTeamDef(IAtsActionableItem ai) throws OseeCoreException {
      IAtsTeamDefinition teamDef = null;
      Collection<IAtsTeamDefinition> impactedTeamDefs = teamDefUtil.getImpactedTeamDefs(Arrays.asList(ai));
      if (impactedTeamDefs.size() > 0) {
         teamDef = impactedTeamDefs.iterator().next();
      }
      return teamDef;
   }

   private void processAisAddedRemovedFromSelectedTeamWf() throws OseeCoreException {
      Set<IAtsActionableItem> currAIs = teamWf.getActionableItems();
      if (currWorkflowDesiredAIs.isEmpty()) {
         results.logError("All AIs can not be removed from a Team Workflow; Cancel workflow instead");
      } else {
         for (IAtsActionableItem checkedAi : currWorkflowDesiredAIs) {
            if (!currAIs.contains(checkedAi)) {
               results.logWithFormat("Add Actionable Item [%s] to the selected Team Workflow\n", checkedAi);
               addAis.add(checkedAi);
            }
         }
         for (IAtsActionableItem currAi : currAIs) {
            if (!currWorkflowDesiredAIs.contains(currAi)) {
               results.logWithFormat("Remove Actionable Item [%s] to the selected Team Workflow\n", currAi);
               removeAis.add(currAi);
            }
         }
      }
   }

   public List<CreateTeamData> getTeamDatas() {
      return teamDatas;
   }

   public List<IAtsActionableItem> getAddAis() {
      return addAis;
   }

   public List<IAtsActionableItem> getRemoveAis() {
      return removeAis;
   }

}

Back to the top