Skip to main content
summaryrefslogtreecommitdiffstats
blob: eebea4ae12073b6382e1a67d856a3227e39980e8 (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
/*******************************************************************************
 * 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.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.ai.IAtsActionableItem;
import org.eclipse.osee.ats.api.data.AtsArtifactTypes;
import org.eclipse.osee.ats.api.team.IAtsTeamDefinition;
import org.eclipse.osee.ats.api.version.IAtsVersion;
import org.eclipse.osee.ats.api.workdef.IAtsWorkDefinition;
import org.eclipse.osee.ats.core.client.config.AtsArtifactToken;
import org.eclipse.osee.ats.core.client.config.store.ActionableItemArtifactStore;
import org.eclipse.osee.ats.core.client.config.store.TeamDefinitionArtifactStore;
import org.eclipse.osee.ats.core.client.config.store.VersionArtifactStore;
import org.eclipse.osee.ats.core.client.util.AtsUsersClient;
import org.eclipse.osee.ats.core.client.util.AtsUtilCore;
import org.eclipse.osee.ats.core.client.workdef.AtsWorkDefinitionStore;
import org.eclipse.osee.ats.core.client.workdef.WorkDefinitionFactory;
import org.eclipse.osee.ats.core.config.ActionableItemFactory;
import org.eclipse.osee.ats.core.config.AtsConfigCache;
import org.eclipse.osee.ats.core.config.AtsVersionService;
import org.eclipse.osee.ats.core.config.TeamDefinitionFactory;
import org.eclipse.osee.ats.core.config.TeamDefinitions;
import org.eclipse.osee.ats.core.config.VersionFactory;
import org.eclipse.osee.ats.core.workdef.AtsWorkDefinitionService;
import org.eclipse.osee.ats.core.workdef.WorkDefinitionMatch;
import org.eclipse.osee.ats.internal.Activator;
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.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
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.util.GUID;
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.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
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 AtsConfigManager 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> actionableItems;
   private final Display display;

   /**
    * @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 AtsConfigManager(Display display, 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.actionableItems = actionableItems;
      this.display = display;
   }

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

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

      SkynetTransaction transaction =
         TransactionManager.createTransaction(AtsUtil.getAtsBranch(), "Configure ATS for Default Team");

      IAtsTeamDefinition teamDefinition = createTeamDefinition(transaction);

      Collection<IAtsActionableItem> actionableItems = createActionableItems(transaction, teamDefinition);

      createVersions(transaction, teamDefinition);

      XResultData resultData = new XResultData();
      IAtsWorkDefinition workDefinition = createWorkflow(transaction, resultData, teamDefinition);

      transaction.execute();
      monitor.worked(calculateWork(0.30));

      display.openAtsConfigurationEditors(teamDefinition, actionableItems, workDefinition);
      monitor.worked(calculateWork(0.10));
   }

   private IAtsTeamDefinition createTeamDefinition(SkynetTransaction transaction) throws OseeCoreException {
      IAtsTeamDefinition teamDef = TeamDefinitionFactory.createTeamDefinition(GUID.create(), teamDefName);
      if (versionNames == null || versionNames.size() > 0) {
         teamDef.setTeamUsesVersions(true);
      }
      teamDef.getLeads().add(AtsUsersClient.getUser());
      teamDef.getMembers().add(AtsUsersClient.getUser());
      TeamDefinitions.getTopTeamDefinition().getChildrenTeamDefinitions().add(teamDef);
      TeamDefinitionArtifactStore teamDefStore = new TeamDefinitionArtifactStore(teamDef);
      teamDefStore.getArtifactOrCreate(transaction);
      teamDefStore.saveToArtifact(transaction);
      AtsConfigCache.cache(teamDef);
      return teamDef;
   }

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

      // Create top actionable item
      IAtsActionableItem topAia = ActionableItemFactory.createActionableItem(GUID.create(), teamDefName);
      topAia.setActionable(false);
      topAia.setTeamDefinition(teamDef);
      ActionableItemArtifactStore topAiaStore = new ActionableItemArtifactStore(topAia);
      topAiaStore.getArtifactOrCreate(transaction);
      topAiaStore.saveToArtifact(transaction);

      teamDef.getActionableItems().add(topAia);
      new TeamDefinitionArtifactStore(teamDef).saveToArtifact(transaction);

      aias.add(topAia);

      // Create children actionable item
      for (String name : actionableItems) {
         IAtsActionableItem childAi = ActionableItemFactory.createActionableItem(GUID.create(), name);
         childAi.setActionable(true);
         topAia.getChildrenActionableItems().add(childAi);
         childAi.setParentActionableItem(topAia);
         ActionableItemArtifactStore childAiStore = new ActionableItemArtifactStore(childAi);
         childAiStore.getArtifactOrCreate(transaction);
         childAiStore.saveToArtifact(transaction);
         aias.add(childAi);
      }
      topAiaStore.saveToArtifact(transaction);
      return aias;
   }

   private void createVersions(SkynetTransaction transaction, IAtsTeamDefinition teamDef) throws OseeCoreException {
      if (versionNames != null) {
         for (String name : versionNames) {
            IAtsVersion version = VersionFactory.createVersion(name);
            teamDef.getVersions().add(version);
            VersionArtifactStore verStore = new VersionArtifactStore(version);
            verStore.getArtifactOrCreate(transaction);
            verStore.saveToArtifact(transaction);
            AtsVersionService.get().setTeamDefinition(version, teamDef);
         }
      }
   }

   private IAtsWorkDefinition createWorkflow(SkynetTransaction transaction, XResultData resultData, IAtsTeamDefinition teamDef) throws OseeCoreException {
      WorkDefinitionMatch workDefMatch = WorkDefinitionFactory.getWorkDefinition(name);
      IAtsWorkDefinition workDef = null;
      // If can't be found, create a new one
      if (!workDefMatch.isMatched()) {
         workDef = generateDefaultWorkflow(name, resultData, transaction, teamDef);
         try {
            String workDefXml = AtsWorkDefinitionService.getService().getStorageString(workDef, resultData);
            Artifact workDefArt =
               AtsWorkDefinitionImporter.get().importWorkDefinitionToDb(workDefXml, workDef.getName(), name,
                  resultData, transaction);
            Artifact folder = AtsUtilCore.getFromToken(AtsArtifactToken.WorkDefinitionsFolder);
            folder.addChild(workDefArt);
            folder.persist(transaction);
         } catch (Exception ex) {
            throw new OseeWrappedException(ex);
         }
      } else {
         workDef = workDefMatch.getWorkDefinition();
      }
      // Relate new team def to workflow artifact
      teamDef.setWorkflowDefinition(workDef.getId());
      new TeamDefinitionArtifactStore(teamDef).saveToArtifact(transaction);
      return workDef;
   }

   private IAtsWorkDefinition generateDefaultWorkflow(String name, XResultData resultData, SkynetTransaction transaction, IAtsTeamDefinition teamDef) {
      IAtsWorkDefinition defaultWorkDef =
         WorkDefinitionFactory.getWorkDefinition(AtsWorkDefinitionSheetProviders.WORK_DEF_TEAM_DEFAULT).getWorkDefinition();

      // Duplicate default team workflow definition w/ namespace changes

      IAtsWorkDefinition newWorkDef =
         AtsWorkDefinitionService.getService().copyWorkDefinition(name, defaultWorkDef, resultData,
            AtsWorkDefinitionStore.getInstance().getAttributeResolver(),
            AtsWorkDefinitionStore.getInstance().getUserResolver());
      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 = new TeamDefinitionArtifactStore(teamDef).getArtifact();
                  AtsUtil.openATSAction(teamDefArt, AtsOpenOption.OpenAll);
                  for (IAtsActionableItem aia : aias) {
                     AtsUtil.openATSAction(new ActionableItemArtifactStore(aia).getArtifact(), AtsOpenOption.OpenAll);
                  }
                  RendererManager.open(ArtifactQuery.getArtifactFromTypeAndName(AtsArtifactTypes.WorkDefinition,
                     workDefinition.getName(), AtsUtil.getAtsBranch()), PresentationType.SPECIALIZED_EDIT, monitor);
               } catch (OseeCoreException ex) {
                  OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
               }
               return Status.OK_STATUS;
            }
         };
         Jobs.startJob(job, true);
      }
   }

}

Back to the top