Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1ed76799323c1ed9835d7a0a1cd525ca9cc821f4 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*********************************************************************
 * Copyright (c) 2020 Boeing
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Boeing - initial API and implementation
 **********************************************************************/

package org.eclipse.osee.ats.api.task.create;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.osee.ats.api.AtsApi;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.data.ArtifactToken;
import org.eclipse.osee.framework.core.data.ArtifactTypeToken;
import org.eclipse.osee.framework.core.data.AttributeTypeToken;
import org.eclipse.osee.framework.core.data.BranchToken;
import org.eclipse.osee.framework.core.data.RelationTypeToken;
import org.eclipse.osee.framework.core.enums.DeletionFlag;
import org.eclipse.osee.framework.core.enums.ModificationType;
import org.eclipse.osee.framework.core.model.change.ChangeItem;
import org.eclipse.osee.framework.core.model.change.ChangeItemData;
import org.eclipse.osee.framework.core.model.change.ChangeItemUtil;
import org.eclipse.osee.framework.core.model.change.ChangeReportRollup;
import org.eclipse.osee.framework.core.model.change.ChangeType;
import org.eclipse.osee.framework.jdk.core.type.Id;
import org.eclipse.osee.framework.jdk.core.util.Strings;

/**
 * @author Donald G. Dunne
 */
public interface IAtsChangeReportTaskNameProvider {

   public ChangeReportTaskNameProviderToken getId();

   default Map<ArtifactId, ArtifactToken> getTasksComputedAsNeeded(ChangeReportTaskData crtd,
      ChangeReportTaskTeamWfData crttwd, AtsApi atsApi) {
      return getTasksComputedAsNeeded(crtd, crttwd, atsApi, isAddTaskMatch());
   }

   /**
    * @return true if want default task names; false otherwise and this method will only fill up mod,del,rel lists
    */
   default boolean isAddTaskMatch() {
      return true;
   }

   default public Map<ArtifactId, ArtifactToken> getTasksComputedAsNeeded(ChangeReportTaskData crtd,
      ChangeReportTaskTeamWfData crttwd, AtsApi atsApi, boolean addTaskMatch) {
      Map<ArtifactId, ArtifactToken> idToArtifact = new HashMap<>();
      getModifiedArifactNames(crtd, crttwd, idToArtifact, atsApi, addTaskMatch);
      getRelArtifactNames(crtd, crttwd, idToArtifact, atsApi, addTaskMatch);
      getExtensionNames(crtd, crttwd, idToArtifact, atsApi, addTaskMatch);
      getApiAndTaskNames(crtd, crttwd, idToArtifact, atsApi, addTaskMatch);
      return idToArtifact;
   }

   /**
    * Add tasks defined in StaticTaskDefinition through java api. These will be added regardless of change report
    * contents.
    */
   default void getApiAndTaskNames(ChangeReportTaskData crtd, ChangeReportTaskTeamWfData crttwd,
      Map<ArtifactId, ArtifactToken> idToArtifact, AtsApi atsApi, boolean addTaskMatch) {
      for (StaticTaskDefinition taskDef : crtd.getSetDef().getStaticTaskDefs()) {
         ChangeReportTaskMatch match = new ChangeReportTaskMatch();
         match.setTaskName(taskDef.getName());
         match.setCreateTaskDef(taskDef);
         match.setType(ChangeReportTaskMatchType.StaticTskCompAsNeeded);
         match.setAutoTaskGenType(AutoTaskGenType.Static.name());
         crttwd.getTaskMatches().add(match);
      }
   }

   /**
    * Allow extensions to add other names
    */
   default void getExtensionNames(ChangeReportTaskData crtd, ChangeReportTaskTeamWfData crttwd,
      Map<ArtifactId, ArtifactToken> idToArtifact, AtsApi atsApi, boolean addTaskMatch) {
      // do nothing
   }

   /**
    * Compute all artifacts on an included rel that doesn't have 2 artifacts both excluded.
    */
   default void getRelArtifactNames(ChangeReportTaskData crtd, ChangeReportTaskTeamWfData crttwd,
      Map<ArtifactId, ArtifactToken> idToArtifact, AtsApi atsApi, boolean addTaskMatch) {
      for (ArtifactId chgRptArt : getRelArts(crtd, crttwd, atsApi)) {
         logAndAddTaskName(crtd, crttwd, atsApi, ArtifactToken.valueOf(chgRptArt, ""), idToArtifact,
            TaskChangeType.Relation.name(), addTaskMatch);
      }
   }

   /**
    * Compute all artifacts modified and deleted
    */
   default void getModifiedArifactNames(ChangeReportTaskData crtd, ChangeReportTaskTeamWfData crttwd,
      Map<ArtifactId, ArtifactToken> idToArtifact, AtsApi atsApi, boolean addTaskMatch) {
      processModifiedDeletedArts(crtd, crttwd, idToArtifact, atsApi, addTaskMatch);
   }

   /**
    * Override to provide additional attributes to be added to created task
    */
   default ChangeReportTaskMatch logAndAddTaskName(ChangeReportTaskData crtd, ChangeReportTaskTeamWfData crttwd,
      AtsApi atsApi, ArtifactToken chgRptArt, Map<ArtifactId, ArtifactToken> idToArtifact, String chgType,
      boolean addTaskMatch) {
      ArtifactToken art =
         atsApi.getQueryService().getArtifact(chgRptArt, crtd.getWorkOrParentBranchId(), DeletionFlag.INCLUDE_DELETED);
      idToArtifact.put(art, art);
      if (addTaskMatch) {
         String safeName = getTaskName(atsApi, chgType, art);
         if (Strings.isValid(safeName)) {
            ChangeReportTaskMatch match = new ChangeReportTaskMatch();
            match.setChgRptArt(chgRptArt);
            match.setChgRptArtName(art.getName());
            boolean deleted = isChangeItemDeleted(art, crtd.getChangeItems());
            match.setChgRptArtDeleted(deleted);
            match.setTaskName(safeName);
            match.setType(ChangeReportTaskMatchType.ChgRptTskCompAsNeeded);
            if (chgType.equals(TaskChangeType.Deleted.name())) {
               match.setAutoTaskGenType(AutoTaskGenType.ChgRptDelete.name());
            } else if (chgType.equals(TaskChangeType.Add.name())) {
               match.setAutoTaskGenType(AutoTaskGenType.ChgRptAdd.name());
            } else if (chgType.equals(TaskChangeType.Mod.name())) {
               match.setAutoTaskGenType(AutoTaskGenType.ChgRptMod.name());
            }
            crttwd.getTaskMatches().add(match);
            return match;
         }
      }
      return null;
   }

   public static boolean isChangeItemAdded(ArtifactToken reqArt, Collection<ChangeItem> changeItems) {
      boolean added = false;
      if (changeItems != null && !changeItems.isEmpty()) {
         for (ChangeItem change : changeItems) {
            if (change.getArtId().equals(reqArt) && change.getChangeType().isAttributeChange()) {
               if (change.getNetChange().getModType() == ModificationType.NEW) {
                  added = true;
               }
            }
         }
      }
      return added;
   }

   public static boolean isChangeItemModified(ArtifactToken reqArt, Collection<ChangeItem> changeItems) {
      boolean modified = false;
      if (changeItems != null && !changeItems.isEmpty()) {
         for (ChangeItem change : changeItems) {
            if (change.getArtId().equals(reqArt) && change.getChangeType().isAttributeChange()) {
               if (change.getNetChange().getModType() == ModificationType.MODIFIED) {
                  modified = true;
               }
            }
         }
      }
      return modified;
   }

   public static boolean isChangeItemDeleted(ArtifactToken reqArt, Collection<ChangeItem> changeItems) {
      boolean deleted = false;
      if (changeItems != null && !changeItems.isEmpty()) {
         for (ChangeItem change : changeItems) {
            if (change.getArtId().equals(reqArt) && change.getChangeType().isAttributeChange()) {
               if (change.getNetChange().getModType() == ModificationType.ARTIFACT_DELETED) {
                  deleted = true;
               }
            }
         }
      }
      return deleted;
   }

   /**
    * @return Name or null for no task
    */
   default String getTaskName(AtsApi atsApi, String chgType, ArtifactToken art) {
      String safeName = String.format("Handle %s change to [%s]", chgType, atsApi.getStoreService().getSafeName(art));
      if (TaskChangeType.Deleted.name().equals(chgType)) {
         safeName += " (Deleted)";
      }
      return safeName;
   }

   default ChangeReportModDelArts processModifiedDeletedArts(ChangeReportTaskData crtd,
      ChangeReportTaskTeamWfData crttwd, Map<ArtifactId, ArtifactToken> idToArtifact, //
      AtsApi atsApi, boolean addTaskMatch) {

      List<ChangeItem> changeItems = crtd.getChangeItems();

      Set<ArtifactId> addArts = new HashSet<>();
      Set<ArtifactId> modArts = new HashSet<>();
      Set<ArtifactId> delArts = new HashSet<>();

      if (crtd.getWorkOrParentBranchId().isValid()) {
         BranchToken branch = atsApi.getBranchService().getBranch(crtd.getWorkOrParentBranchId());

         ChangeItemData data = getChangeItemData(changeItems, branch, atsApi);
         Collection<ChangeReportRollup> rollups = data.getRollups().values();
         for (ChangeReportRollup rollup : rollups) {
            ArtifactIncluded result = isIncluded(crtd, crttwd, rollup, rollup.getArtType(), atsApi);

            if (result.isIncluded()) {
               if (result.isDeleted()) {
                  delArts.add(rollup.getArtId());
                  logAndAddTaskName(crtd, crttwd, atsApi, rollup.getArtToken(), idToArtifact,
                     TaskChangeType.Deleted.name(), addTaskMatch);
               } else if (result.isAdded()) {
                  logAndAddTaskName(crtd, crttwd, atsApi, rollup.getArtToken(), idToArtifact, TaskChangeType.Add.name(),
                     addTaskMatch);
                  addArts.add(rollup.getArtId());
               } else if (result.isModified()) {
                  logAndAddTaskName(crtd, crttwd, atsApi, rollup.getArtToken(), idToArtifact, TaskChangeType.Mod.name(),
                     addTaskMatch);
                  modArts.add(rollup.getArtId());
               }
            }
         }
      }
      ChangeReportModDelArts modDel = new ChangeReportModDelArts();
      modDel.setAdded(addArts);
      modDel.setModified(modArts);
      modDel.setDeleted(delArts);
      return modDel;
   }

   /**
    * @return A, B where A is if Artifact/Attributes are included and B if deleted
    */
   default ArtifactIncluded isIncluded(ChangeReportTaskData crtd, ChangeReportTaskTeamWfData crttwd,
      ChangeReportRollup rollup, ArtifactTypeToken artType, AtsApi atsApi) {

      Collection<ArtifactTypeToken> incArtTypes = crtd.getSetDef().getChgRptOptions().getArtifactTypes();
      Collection<ArtifactTypeToken> exclArtTypes = crtd.getSetDef().getChgRptOptions().getNotArtifactTypes();
      Collection<AttributeTypeToken> incAttrTypes = crtd.getSetDef().getChgRptOptions().getAttributeTypes();
      Collection<AttributeTypeToken> exclAttrTypes = crtd.getSetDef().getChgRptOptions().getNotAttributeTypes();

      boolean added = false;
      boolean modified = false;
      boolean included = false;
      boolean deleted = false;

      // Check for included/excluded artifact type
      if (artType.inheritsFromAny(exclArtTypes)) {
         // included == false
         return new ArtifactIncluded(added, modified, included, deleted);
      }
      if (!incArtTypes.isEmpty() && !artType.inheritsFromAny(incArtTypes)) {
         // included == false
         return new ArtifactIncluded(added, modified, included, deleted);
      }

      // Check for included/excluded attribute type
      for (ChangeItem item : rollup.getChangeItems()) {
         if (ChangeItemUtil.createdAndDeleted(item)) {
            continue;
         }
         if (item.getChangeType() != ChangeType.Relation && ChangeItemUtil.isArtifactDeleted(
            item.getCurrentVersion())) {
            deleted = true;
         }

         // Change Type or Applicability always true
         if (item.getChangeType().isArtifactChange() && !item.isSynthetic()) {
            deleted = calculatedIfDeleted(rollup);
            if (ChangeItemUtil.isNew(item.getCurrentVersion())) {
               return new ArtifactIncluded(true, modified, true, deleted);
            } else {
               return new ArtifactIncluded(added, true, true, deleted);
            }
         }

         // Look through attrs for anything that should be included
         if (item.getChangeType().isAttributeChange()) {

            Id typeId = item.getItemTypeId();
            AttributeTypeToken attrType = atsApi.tokenService().getAttributeType(typeId.getId());

            // Set to include if no include types specified
            boolean incAttrType = incAttrTypes.isEmpty();

            // If not included, add if included
            if (!incAttrType) {
               incAttrType = incAttrTypes.contains(attrType);
            }

            // If included, remove if excluded
            if (incAttrType) {
               incAttrType = !exclAttrTypes.contains(attrType);
            }

            if (incAttrType) {
               included = true;
               if (item.getNetChange().getModType().matches(ModificationType.MODIFIED, ModificationType.MERGED)) {
                  modified = true;
                  break;
               } else if (item.getNetChange().getModType().matches(ModificationType.NEW, ModificationType.INTRODUCED)) {
                  added = true;
               } else {
                  included = false;
               }
            }
         }
      }
      return new ArtifactIncluded(added, modified, included, deleted);
   }

   default public boolean calculatedIfDeleted(ChangeReportRollup rollup) {
      for (ChangeItem item : rollup.getChangeItems()) {
         if (item.getChangeType().isArtifactChange() && item.isDeleted()) {
            return true;
         }
      }
      return false;
   }

   /**
    * Create CID and set all art types in rollups
    */
   default ChangeItemData getChangeItemData(Collection<ChangeItem> changes, BranchToken branchId, AtsApi atsApi) {
      ChangeItemData data = new ChangeItemData(changes);
      for (ChangeReportRollup rollup : data.getRollups().values()) {
         ArtifactId artId = rollup.getArtId();
         ArtifactTypeToken artType = atsApi.getStoreService().getArtifactType(artId, branchId);
         rollup.setArtType(artType);
         ArtifactToken tok = atsApi.getQueryService().getArtifact(artId, branchId, DeletionFlag.INCLUDE_DELETED);
         rollup.setArtToken(ArtifactToken.valueOf(tok.getId(), tok.getName(), branchId, tok.getArtifactType()));
      }
      return data;
   }

   default Collection<ArtifactId> getRelArts(ChangeReportTaskData crtd, ChangeReportTaskTeamWfData crttwd,
      AtsApi atsApi) {

      Collection<RelationTypeToken> incRelTypes = crtd.getSetDef().getChgRptOptions().getRelationTypes();
      Collection<RelationTypeToken> exclRelTypes = crtd.getSetDef().getChgRptOptions().getNotRelationTypes();
      BranchToken branch = atsApi.getBranchService().getBranch(crtd.getWorkOrParentBranchId());
      Set<ArtifactId> arts = new HashSet<>();
      ChangeItemData data = getChangeItemData(crtd.getChangeItems(), branch, atsApi);

      for (ChangeReportRollup rollup : data.getRollups().values()) {

         List<ChangeItem> reqArtChangeItems = rollup.getChangeItems();
         for (ChangeItem item : reqArtChangeItems) {
            if (item.getChangeType().isNotRelationChange()) {
               continue;
            }
            if (ChangeItemUtil.createdAndDeleted(item)) {
               continue;
            }
            if (exclRelTypes.contains(item.getItemTypeId())) {
               continue;
            }
            if (!incRelTypes.isEmpty() && !incRelTypes.contains(item.getItemTypeId())) {
               continue;
            }

            ArtifactTypeToken artAType = atsApi.getStoreService().getArtifactType(item.getArtId(), branch);
            ArtifactTypeToken artBType = atsApi.getStoreService().getArtifactType(item.getArtIdB(), branch);

            ArtifactIncluded artAInc = isIncluded(crtd, crttwd, rollup, artAType, atsApi);
            ArtifactIncluded artBInc = isIncluded(crtd, crttwd, rollup, artBType, atsApi);

            if ((artAInc.isNotDeleted() && artAInc.isIncluded()) || //
               (artBInc.isNotDeleted() && artBInc.isIncluded())) {
               arts.add(item.getArtId());
               arts.add(item.getArtIdB());
            }
         }
      }
      return arts;
   }

}

Back to the top