Skip to main content
summaryrefslogtreecommitdiffstats
blob: 85957c39e992cacadc95e34910edd552a9cae760 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*******************************************************************************
 * 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.ats.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
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.osee.ats.AtsOpenOption;
import org.eclipse.osee.ats.api.IAtsServices;
import org.eclipse.osee.ats.api.ai.IAtsActionableItem;
import org.eclipse.osee.ats.api.data.AtsArtifactToken;
import org.eclipse.osee.ats.api.data.AtsArtifactTypes;
import org.eclipse.osee.ats.api.data.AtsAttributeTypes;
import org.eclipse.osee.ats.api.data.AtsRelationTypes;
import org.eclipse.osee.ats.api.team.IAtsTeamDefinition;
import org.eclipse.osee.ats.api.util.IAtsChangeSet;
import org.eclipse.osee.ats.api.version.IAtsVersion;
import org.eclipse.osee.ats.api.workdef.IAtsWorkDefinition;
import org.eclipse.osee.ats.api.workdef.IWorkDefinitionMatch;
import org.eclipse.osee.ats.core.client.util.AtsChangeSet;
import org.eclipse.osee.ats.core.client.util.AtsUtilClient;
import org.eclipse.osee.ats.core.config.TeamDefinitions;
import org.eclipse.osee.ats.core.util.AtsUtilCore;
import org.eclipse.osee.ats.internal.Activator;
import org.eclipse.osee.ats.internal.AtsClientService;
import org.eclipse.osee.ats.util.AtsUtil;
import org.eclipse.osee.ats.workdef.AtsWorkDefinitionSheetProviders;
import org.eclipse.osee.ats.workdef.provider.AtsWorkDefinitionImporter;
import org.eclipse.osee.framework.core.data.IArtifactToken;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.exception.OseeWrappedException;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.core.util.XResultData;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
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.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.ui.skynet.render.PresentationType;
import org.eclipse.osee.framework.ui.skynet.render.RendererManager;
import org.eclipse.ui.progress.UIJob;

/**
 * This class creates a simple configuration of ATS given team definition name, version names (if desired), actionable
 * items and workflow id.
 *
 * @author Donald G. Dunne
 */
public class AtsConfigOperation extends AbstractOperation {

   public static interface Display {
      public void openAtsConfigurationEditors(IAtsTeamDefinition teamDef, Collection<IAtsActionableItem> aias, IAtsWorkDefinition workDefinition);
   }

   private final String name;
   private final String teamDefName;
   private final Collection<String> versionNames;
   private final Collection<String> actionableItemsNames;
   private IAtsTeamDefinition teamDefinition;
   private Collection<IAtsActionableItem> actionableItems;
   private IAtsWorkDefinition workDefinition = null;
   private IArtifactToken teamDefToken = null;
   private IArtifactToken actionableItemToken = null;

   /**
    * @param teamDefName - name of team definition to use
    * @param versionNames - list of version names (if team is using versions)
    * @param actionableItems - list of actionable items
    */
   public AtsConfigOperation(String name, String teamDefName, Collection<String> versionNames, Collection<String> actionableItems) {
      super("Configure Ats", Activator.PLUGIN_ID);
      this.name = name;
      this.teamDefName = teamDefName;
      this.versionNames = versionNames;
      this.actionableItemsNames = actionableItems;
   }

   public AtsConfigOperation(String name, IArtifactToken teamDefToken, Collection<String> versionNames, IArtifactToken actionableItemToken) {
      this(name, teamDefToken.getName(), versionNames, Arrays.asList(actionableItemToken.getName()));
      this.teamDefToken = teamDefToken;
      this.actionableItemToken = actionableItemToken;
   }

   private void checkWorkItemNamespaceUnique() throws OseeCoreException {
      IWorkDefinitionMatch match = null;
      try {
         match = AtsClientService.get().getWorkDefinitionAdmin().getWorkDefinition(name);
      } catch (Exception ex) {
         return;
      }
      if (match.isMatched()) {
         throw new OseeArgumentException(
            String.format("Configuration Namespace [%s] already used, choose a unique namespace.", name));
      }
   }

   public IAtsWorkDefinition getWorkDefinition() {
      return workDefinition;
   }

   public Collection<IAtsActionableItem> getActionableItems() {
      return actionableItems;
   }

   @Override
   protected void doWork(IProgressMonitor monitor) throws Exception {
      checkWorkItemNamespaceUnique();
      monitor.worked(calculateWork(0.10));

      AtsChangeSet changes = new AtsChangeSet("Configure ATS for Default Team");

      teamDefinition = createTeamDefinition(changes, AtsClientService.get());

      actionableItems = createActionableItems(changes, teamDefinition);

      createVersions(changes, teamDefinition);

      XResultData resultData = new XResultData();
      this.workDefinition = createWorkflow(changes, resultData, teamDefinition);

      changes.execute();
      monitor.worked(calculateWork(0.30));
   }

   private IAtsTeamDefinition createTeamDefinition(IAtsChangeSet changes, IAtsServices services) throws OseeCoreException {
      IAtsTeamDefinition teamDef = null;
      if (teamDefToken == null) {
         teamDef = AtsClientService.get().createTeamDefinition(teamDefName, changes, services);
      } else {
         teamDef = AtsClientService.get().createTeamDefinition(teamDefToken.getGuid(), teamDefToken.getName(),
            teamDefToken.getUuid(), changes, services);
      }
      changes.relate(TeamDefinitions.getTopTeamDefinition(AtsClientService.get().getQueryService()),
         AtsRelationTypes.TeamMember_Member, AtsClientService.get().getUserService().getCurrentUser());
      changes.relate(TeamDefinitions.getTopTeamDefinition(AtsClientService.get().getQueryService()),
         AtsRelationTypes.TeamLead_Lead, AtsClientService.get().getUserService().getCurrentUser());
      changes.relate(TeamDefinitions.getTopTeamDefinition(AtsClientService.get().getQueryService()),
         CoreRelationTypes.Default_Hierarchical__Child, teamDef);
      return teamDef;
   }

   private Collection<IAtsActionableItem> createActionableItems(IAtsChangeSet changes, IAtsTeamDefinition teamDef) throws OseeCoreException {
      Collection<IAtsActionableItem> aias = new ArrayList<>();

      // Create top actionable item
      IAtsActionableItem topAia = AtsClientService.get().createActionableItem(teamDefName);
      topAia.setActionable(false);
      topAia.setTeamDefinition(teamDef);
      AtsClientService.get().storeConfigObject(topAia, changes);
      teamDef.getActionableItems().add(topAia);
      AtsClientService.get().storeConfigObject(teamDef, changes);

      aias.add(topAia);

      // Create children actionable item
      if (actionableItemToken == null) {
         for (String name : actionableItemsNames) {
            IAtsActionableItem childAi = AtsClientService.get().createActionableItem(name);
            addChildAi(topAia, childAi, changes, aias);
         }
      } else {
         IAtsActionableItem childAi = AtsClientService.get().createActionableItem(actionableItemToken.getGuid(),
            actionableItemToken.getName(), actionableItemToken.getUuid());
         addChildAi(topAia, childAi, changes, aias);
      }
      AtsClientService.get().storeConfigObject(topAia, changes);
      return aias;
   }

   private void addChildAi(IAtsActionableItem topAia, IAtsActionableItem childAi, IAtsChangeSet changes, Collection<IAtsActionableItem> aias) {
      childAi.setActionable(true);
      topAia.getChildrenActionableItems().add(childAi);
      childAi.setParentActionableItem(topAia);
      AtsClientService.get().storeConfigObject(childAi, changes);
      aias.add(childAi);
   }

   private void createVersions(IAtsChangeSet changes, IAtsTeamDefinition teamDef) throws OseeCoreException {
      if (versionNames != null) {
         for (String name : versionNames) {
            IAtsVersion version = AtsClientService.get().getVersionService().createVersion(name);
            teamDef.getVersions().add(version);
            AtsClientService.get().storeConfigObject(version, changes);
            AtsClientService.get().getVersionService().setTeamDefinition(version, teamDef);
         }
      }
   }

   private IAtsWorkDefinition createWorkflow(IAtsChangeSet changes, XResultData resultData, IAtsTeamDefinition teamDef) throws OseeCoreException {
      IWorkDefinitionMatch workDefMatch = AtsClientService.get().getWorkDefinitionAdmin().getWorkDefinition(name);
      IAtsWorkDefinition workDef = null;
      // If can't be found, create a new one
      if (!workDefMatch.isMatched()) {
         workDef = generateDefaultWorkflow(name, resultData, changes, teamDef);
         try {
            String workDefXml = AtsClientService.get().getWorkDefinitionAdmin().getStorageString(workDef, resultData);
            Artifact workDefArt = AtsWorkDefinitionImporter.get().importWorkDefinitionToDb(workDefXml,
               workDef.getName(), name, null, resultData, changes);
            Artifact folder = AtsUtilClient.getFromToken(AtsArtifactToken.WorkDefinitionsFolder);
            folder.addChild(workDefArt);
            changes.add(folder);
         } catch (Exception ex) {
            throw new OseeWrappedException(ex);
         }
      } else {
         workDef = workDefMatch.getWorkDefinition();
      }
      // Relate new team def to workflow artifact
      changes.setSoleAttributeValue(teamDef, AtsAttributeTypes.WorkflowDefinition, workDef.getId());
      return workDef;
   }

   private IAtsWorkDefinition generateDefaultWorkflow(String name, XResultData resultData, IAtsChangeSet changes, IAtsTeamDefinition teamDef) throws OseeCoreException {
      IAtsWorkDefinition defaultWorkDef = AtsClientService.get().getWorkDefinitionAdmin().getWorkDefinition(
         AtsWorkDefinitionSheetProviders.WORK_DEF_TEAM_DEFAULT).getWorkDefinition();

      // Duplicate default team workflow definition w/ namespace changes

      IAtsWorkDefinition newWorkDef =
         AtsClientService.get().getWorkDefinitionAdmin().copyWorkDefinition(name, defaultWorkDef, resultData);
      return newWorkDef;
   }

   public static final class OpenAtsConfigEditors implements Display {

      @Override
      public void openAtsConfigurationEditors(final IAtsTeamDefinition teamDef, final Collection<IAtsActionableItem> aias, final IAtsWorkDefinition workDefinition) {
         Job job = new UIJob("Open Ats Configuration Editors") {
            @Override
            public IStatus runInUIThread(IProgressMonitor monitor) {
               try {
                  Artifact teamDefArt = AtsClientService.get().getConfigArtifact(teamDef);
                  AtsUtil.openATSAction(teamDefArt, AtsOpenOption.OpenAll);
                  for (IAtsActionableItem aia : aias) {
                     AtsUtil.openATSAction(AtsClientService.get().getConfigArtifact(aia), AtsOpenOption.OpenAll);
                  }
                  RendererManager.open(ArtifactQuery.getArtifactFromTypeAndName(AtsArtifactTypes.WorkDefinition,
                     workDefinition.getName(), AtsUtilCore.getAtsBranch()), PresentationType.SPECIALIZED_EDIT, monitor);
               } catch (OseeCoreException ex) {
                  OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
               }
               return Status.OK_STATUS;
            }
         };
         Jobs.startJob(job, true);
      }
   }

   public IAtsTeamDefinition getTeamDefinition() {
      return teamDefinition;
   }

}

Back to the top