Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8f6d92c3648fedadc8561f70d1f13c378ab05de4 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*******************************************************************************
 * 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.rest.internal.workitem;

import org.eclipse.osee.ats.api.AtsApi;
import org.eclipse.osee.ats.api.IAtsConfigObject;
import org.eclipse.osee.ats.api.agile.IAgileFeatureGroup;
import org.eclipse.osee.ats.api.agile.IAgileTeam;
import org.eclipse.osee.ats.api.ai.IAtsActionableItem;
import org.eclipse.osee.ats.api.country.IAtsCountry;
import org.eclipse.osee.ats.api.data.AtsArtifactTypes;
import org.eclipse.osee.ats.api.data.AtsRelationTypes;
import org.eclipse.osee.ats.api.ev.IAtsWorkPackage;
import org.eclipse.osee.ats.api.insertion.IAtsInsertion;
import org.eclipse.osee.ats.api.insertion.IAtsInsertionActivity;
import org.eclipse.osee.ats.api.insertion.JaxInsertion;
import org.eclipse.osee.ats.api.insertion.JaxInsertionActivity;
import org.eclipse.osee.ats.api.program.IAtsProgram;
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.core.config.AbstractConfigItemFactory;
import org.eclipse.osee.ats.core.config.ActionableItem;
import org.eclipse.osee.ats.core.config.Country;
import org.eclipse.osee.ats.core.config.Program;
import org.eclipse.osee.ats.core.config.TeamDefinition;
import org.eclipse.osee.ats.core.config.Version;
import org.eclipse.osee.ats.core.insertion.Insertion;
import org.eclipse.osee.ats.core.insertion.InsertionActivity;
import org.eclipse.osee.ats.core.model.WorkPackage;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.search.QueryBuilder;
import org.eclipse.osee.orcs.transaction.TransactionBuilder;

/**
 * @author Donald G. Dunne
 * @author David W. Miller
 */
public class ConfigItemFactory extends AbstractConfigItemFactory {

   private final Log logger;
   private final AtsApi atsApi;
   private final OrcsApi orcsApi;

   public ConfigItemFactory(Log logger, AtsApi atsApi, OrcsApi orcsApi) {
      this.logger = logger;
      this.atsApi = atsApi;
      this.orcsApi = orcsApi;
   }

   @Override
   public IAtsConfigObject getConfigObject(ArtifactId artifact) {
      if (artifact instanceof IAtsConfigObject) {
         return (IAtsConfigObject) artifact;
      } else if (artifact instanceof ArtifactReadable) {
         return getConfigObject((ArtifactReadable) artifact);
      }
      QueryBuilder query = orcsApi.getQueryFactory().fromBranch(atsApi.getAtsBranch());
      return getConfigObject(query.andId(artifact).getResults().getExactlyOne());
   }

   private IAtsConfigObject getConfigObject(ArtifactReadable artifact) {
      IAtsConfigObject configObject;
      if (artifact.isOfType(AtsArtifactTypes.Version)) {
         configObject = getVersion(artifact);
      } else if (artifact.isOfType(AtsArtifactTypes.TeamDefinition)) {
         configObject = getTeamDef(artifact);
      } else if (artifact.isOfType(AtsArtifactTypes.ActionableItem)) {
         configObject = getActionableItem(artifact);
      } else if (artifact.isOfType(AtsArtifactTypes.Program)) {
         configObject = getProgram(artifact);
      } else if (artifact.isOfType(AtsArtifactTypes.AgileTeam)) {
         configObject = getAgileTeam(artifact);
      } else if (artifact.isOfType(AtsArtifactTypes.AgileFeatureGroup)) {
         configObject = getAgileFeatureGroup(artifact);
      } else if (artifact.isOfType(AtsArtifactTypes.Insertion)) {
         configObject = getInsertion(artifact);
      } else if (artifact.isOfType(AtsArtifactTypes.InsertionActivity)) {
         configObject = getInsertionActivity(artifact);
      } else if (artifact.isOfType(AtsArtifactTypes.Country)) {
         configObject = getCountry(artifact);
      } else if (artifact.isOfType(AtsArtifactTypes.WorkPackage)) {
         configObject = getWorkPackage(artifact);
      } else {
         throw new OseeArgumentException("Unexpected artifact type [%s]", artifact.getArtifactTypeId());
      }
      return configObject;
   }

   @Override
   public IAtsWorkPackage getWorkPackage(ArtifactId artifact) {
      IAtsWorkPackage workPackage = null;
      if (artifact instanceof ArtifactReadable) {
         ArtifactReadable art = (ArtifactReadable) artifact;
         if (art.isOfType(AtsArtifactTypes.WorkPackage)) {
            workPackage = new WorkPackage(logger, art, atsApi);
         }
      }
      return workPackage;
   }

   @Override
   public IAtsVersion getVersion(ArtifactId artifact) {
      IAtsVersion version = null;
      if (artifact instanceof ArtifactReadable) {
         ArtifactReadable artRead = (ArtifactReadable) artifact;
         if (artRead.isOfType(AtsArtifactTypes.Version)) {
            version = new Version(logger, atsApi, artRead);
         }
      }
      return version;
   }

   @Override
   public IAtsTeamDefinition getTeamDef(ArtifactId artifact) {
      IAtsTeamDefinition teamDef = null;
      if (artifact instanceof ArtifactReadable) {
         ArtifactReadable artRead = (ArtifactReadable) artifact;
         if (artRead.isOfType(AtsArtifactTypes.TeamDefinition)) {
            teamDef = new TeamDefinition(logger, atsApi, artRead);
         }
      }
      return teamDef;
   }

   @Override
   public IAtsActionableItem getActionableItem(ArtifactId artifact) {
      IAtsActionableItem ai = null;
      if (artifact instanceof ArtifactReadable) {
         ArtifactReadable artRead = (ArtifactReadable) artifact;
         if (artRead.isOfType(AtsArtifactTypes.ActionableItem)) {
            ai = new ActionableItem(logger, atsApi, artRead);
         }
      }
      return ai;
   }

   @Override
   public IAtsProgram getProgram(ArtifactId artifact) {
      Program program = null;
      if (artifact instanceof ArtifactReadable) {
         ArtifactReadable artRead = (ArtifactReadable) artifact;
         if (artRead.isOfType(AtsArtifactTypes.Program)) {
            program = new Program(logger, atsApi, artRead);
         }
      }
      return program;
   }

   @Override
   public IAgileTeam getAgileTeam(ArtifactId artifact) {
      IAgileTeam agileTeam = null;
      if (artifact instanceof ArtifactReadable) {
         ArtifactReadable artRead = (ArtifactReadable) artifact;
         if (artRead.isOfType(AtsArtifactTypes.AgileTeam)) {
            agileTeam = atsApi.getAgileService().getAgileTeam(artRead);
         }
      }
      return agileTeam;
   }

   @Override
   public IAgileFeatureGroup getAgileFeatureGroup(ArtifactId artifact) {
      IAgileFeatureGroup agileTeam = null;
      if (artifact instanceof ArtifactReadable) {
         ArtifactReadable artRead = (ArtifactReadable) artifact;
         if (artRead.isOfType(AtsArtifactTypes.AgileFeatureGroup)) {
            agileTeam = atsApi.getAgileService().getAgileFeatureGroup(artRead);
         }
      }
      return agileTeam;
   }

   @Override
   public IAtsInsertion getInsertion(ArtifactId artifact) {
      Insertion insertion = null;
      if (artifact instanceof ArtifactReadable) {
         ArtifactReadable artRead = (ArtifactReadable) artifact;
         if (artRead.isOfType(AtsArtifactTypes.Insertion)) {
            insertion = new Insertion(logger, atsApi, artRead);
            ArtifactReadable programArt =
               ((ArtifactReadable) artifact).getRelated(AtsRelationTypes.ProgramToInsertion_Program).getOneOrNull();
            if (programArt != null) {
               insertion.setProgramId(programArt.getId());
            }
         } else {
            throw new OseeCoreException("Requested id not Insertion");
         }
      }
      return insertion;
   }

   @Override
   public IAtsInsertionActivity getInsertionActivity(ArtifactId artifact) {
      InsertionActivity insertionActivity = null;
      if (artifact instanceof ArtifactReadable) {
         ArtifactReadable artRead = (ArtifactReadable) artifact;
         if (artRead.isOfType(AtsArtifactTypes.InsertionActivity)) {
            insertionActivity = new InsertionActivity(logger, atsApi, artRead);
            ArtifactReadable insertionArt = ((ArtifactReadable) artifact).getRelated(
               AtsRelationTypes.InsertionToInsertionActivity_Insertion).getOneOrNull();
            if (insertionArt != null) {
               insertionActivity.setInsertionId(insertionArt.getId());
            }
         } else {
            throw new OseeCoreException("Requested id not Insertion Activity");
         }
      }
      return insertionActivity;
   }

   @Override
   public IAtsInsertion createInsertion(ArtifactId programArtifact, JaxInsertion newInsertion) {

      long id = newInsertion.getId();
      if (id <= 0) {
         id = Lib.generateArtifactIdAsInt();
      }
      IAtsChangeSet changes =
         atsApi.getStoreService().createAtsChangeSet("Create new Insertion", atsApi.getUserService().getCurrentUser());
      ArtifactReadable insertionArt =
         (ArtifactReadable) changes.createArtifact(AtsArtifactTypes.Insertion, newInsertion.getName(), id);

      changes.relate(programArtifact, AtsRelationTypes.ProgramToInsertion_Insertion, insertionArt);
      changes.execute();
      return getInsertion(insertionArt);
   }

   @Override
   public IAtsInsertion updateInsertion(JaxInsertion updatedInsertion) {
      IAtsChangeSet changes =
         atsApi.getStoreService().createAtsChangeSet("Update Insertion", atsApi.getUserService().getCurrentUser());
      changes.setSoleAttributeValue(ArtifactId.valueOf(updatedInsertion.getId()), CoreAttributeTypes.Name,
         updatedInsertion.getName());
      changes.execute();
      return getInsertion(atsApi.getQueryService().getArtifact(updatedInsertion.getId()));
   }

   @Override
   public void deleteInsertion(ArtifactId artifact) {
      deleteConfigObject(artifact, "Delete Insertion", AtsArtifactTypes.Insertion);
   }

   @Override
   public IAtsInsertionActivity createInsertionActivity(ArtifactId insertion, JaxInsertionActivity newActivity) {
      long id = newActivity.getId();
      if (id <= 0) {
         id = Lib.generateArtifactIdAsInt();
      }
      IAtsChangeSet changes = atsApi.getStoreService().createAtsChangeSet("Create new Insertion Activity",
         atsApi.getUserService().getCurrentUser());
      ArtifactReadable insertionActivityArt =
         (ArtifactReadable) changes.createArtifact(AtsArtifactTypes.InsertionActivity, newActivity.getName(), id);

      changes.relate(insertion, AtsRelationTypes.InsertionToInsertionActivity_InsertionActivity, insertionActivityArt);
      changes.execute();
      return getInsertionActivity(insertionActivityArt);
   }

   @Override
   public IAtsInsertionActivity updateInsertionActivity(JaxInsertionActivity updatedActivity) {
      IAtsChangeSet changes =
         atsApi.getStoreService().createAtsChangeSet("Update Insertion", atsApi.getUserService().getCurrentUser());
      ArtifactReadable insertionActivityArt =
         (ArtifactReadable) atsApi.getQueryService().getArtifact(updatedActivity.getId());

      changes.setSoleAttributeValue(insertionActivityArt, CoreAttributeTypes.Name, updatedActivity.getName());
      changes.setSoleAttributeValue(ArtifactId.valueOf(updatedActivity.getId()), CoreAttributeTypes.Name,
         updatedActivity.getName());
      changes.execute();
      return getInsertionActivity(atsApi.getQueryService().getArtifact(updatedActivity.getId()));
   }

   @Override
   public void deleteInsertionActivity(ArtifactId artifact) {
      deleteConfigObject(artifact, "Delete Insertion Activity", AtsArtifactTypes.InsertionActivity);
   }

   private void deleteConfigObject(ArtifactId id, String comment, IArtifactType type) {
      ArtifactReadable toDelete = (ArtifactReadable) atsApi.getQueryService().getArtifact(id);
      if (toDelete == null) {
         throw new OseeCoreException("No object found for id %s", id);
      }

      if (!toDelete.isTypeEqual(type)) {
         throw new OseeCoreException("Artifact type does not match for %s", comment);
      }
      TransactionBuilder transaction =
         orcsApi.getTransactionFactory().createTransaction(atsApi.getAtsBranch(), toDelete, comment);
      transaction.deleteArtifact(toDelete);
      transaction.commit();
   }

   @Override
   public boolean isAtsConfigArtifact(ArtifactId artifact) {
      return getAtsConfigArtifactTypes().contains(((ArtifactReadable) artifact).getArtifactTypeId());
   }

   @Override
   public IAtsCountry getCountry(ArtifactId artifact) {
      IAtsCountry country = null;
      if (artifact instanceof ArtifactReadable) {
         ArtifactReadable artRead = (ArtifactReadable) artifact;
         if (artRead.isOfType(AtsArtifactTypes.Country)) {
            country = new Country(logger, atsApi, artRead);
         } else {
            throw new OseeCoreException("Requested id not Country");
         }
      }
      return country;
   }
}

Back to the top