Skip to main content
summaryrefslogtreecommitdiffstats
blob: ae26534458acc3beaea4b49c8ea14732ba8c6257 (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*******************************************************************************
 * 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.util;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osee.ats.AtsPlugin;
import org.eclipse.osee.ats.artifact.ATSAttributes;
import org.eclipse.osee.ats.artifact.ATSBranchMetrics;
import org.eclipse.osee.ats.artifact.TeamWorkFlowArtifact;
import org.eclipse.osee.ats.artifact.VersionArtifact;
import org.eclipse.osee.ats.editor.IAtsStateItem;
import org.eclipse.osee.ats.editor.SMAManager;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.util.OseeProperties;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.User;
import org.eclipse.osee.framework.skynet.core.access.AccessControlData;
import org.eclipse.osee.framework.skynet.core.access.AccessControlManager;
import org.eclipse.osee.framework.skynet.core.access.PermissionEnum;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.Branch;
import org.eclipse.osee.framework.skynet.core.artifact.BranchPersistenceManager;
import org.eclipse.osee.framework.skynet.core.change.ModificationType;
import org.eclipse.osee.framework.skynet.core.exception.BranchDoesNotExist;
import org.eclipse.osee.framework.skynet.core.exception.ConflictDetectionException;
import org.eclipse.osee.framework.skynet.core.exception.MultipleAttributesExist;
import org.eclipse.osee.framework.skynet.core.exception.OseeCoreException;
import org.eclipse.osee.framework.skynet.core.exception.TransactionDoesNotExist;
import org.eclipse.osee.framework.skynet.core.revision.ArtifactChange;
import org.eclipse.osee.framework.skynet.core.revision.ChangeReportInput;
import org.eclipse.osee.framework.skynet.core.revision.RevisionManager;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionId;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionIdManager;
import org.eclipse.osee.framework.ui.plugin.util.AWorkbench;
import org.eclipse.osee.framework.ui.plugin.util.IExceptionableRunnable;
import org.eclipse.osee.framework.ui.plugin.util.Jobs;
import org.eclipse.osee.framework.ui.plugin.util.Result;
import org.eclipse.osee.framework.ui.skynet.branch.BranchContentProvider;
import org.eclipse.osee.framework.ui.skynet.branch.BranchView;
import org.eclipse.osee.framework.ui.skynet.changeReport.ChangeReportView;
import org.eclipse.osee.framework.ui.skynet.util.OSEELog;
import org.eclipse.osee.framework.ui.skynet.widgets.IBranchArtifact;
import org.eclipse.osee.framework.ui.skynet.widgets.xchange.ChangeView;
import org.eclipse.osee.framework.ui.skynet.widgets.xcommit.CommitManagerView;
import org.eclipse.osee.framework.ui.skynet.widgets.xmerge.MergeView;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

/**
 * BranchManager contains methods necessary for ATS objects to interact with creation, view and commit of branches.
 * 
 * @author Donald G. Dunne
 */
public class BranchManager {

   private boolean commitPopup;
   private final SMAManager smaMgr;
   private final ATSBranchMetrics atsBranchMetrics;
   public static String BRANCH_CATEGORY = "Branch Changes";

   public BranchManager(SMAManager smaMgr) {
      this.smaMgr = smaMgr;
      atsBranchMetrics = new ATSBranchMetrics(this);
   }

   /**
    * Opens the branch currently associated with this state machine artifact.
    */
   public void showWorkingBranch() {
      try {
         if (!isWorkingBranch()) {
            AWorkbench.popup("ERROR", "No Current Working Branch");
            return;
         }
         BranchView.revealBranch(getWorkingBranch());
      } catch (Exception ex) {
         OSEELog.logException(AtsPlugin.class, ex, true);
      }
   }

   public Integer getBranchId() throws SQLException {
      if (getWorkingBranch() == null) return null;
      return getWorkingBranch().getBranchId();
   }

   /**
    * If working branch has no changes, allow for deletion.
    */
   public void deleteEmptyWorkingBranch() {
      try {
         Branch branch = getWorkingBranch();
         if (branch.hasChanges()) {
            if (!MessageDialog.openQuestion(
                  PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                  "Delete Branch with Changes",
                  "Warning: Changes have been made on this branch.\n\nAre you sure you want to delete the branch: " + branch)) return;
         } else if (!MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
               "Delete Branch", "Are you sure you want to delete the branch: " + branch)) {
         }
         Job job = BranchPersistenceManager.getInstance().deleteBranch(branch);
         job.join();

         AWorkbench.popup("Delete Complete", "Deleted Branch Successfully");

      } catch (Exception ex) {
         OSEELog.logException(AtsPlugin.class, "Can't delete change report.", ex, true);
      }
   }

   /**
    * @return TransactionId associated with this state machine artifact
    * @throws SQLException
    */
   public TransactionId getTransactionId() throws SQLException {
      Set<Integer> tranSet = RevisionManager.getInstance().getTransactionDataPerCommitArtifact(smaMgr.getSma());
      // Cache null transactionId so don't re-search for every call
      if (tranSet.size() == 0) {
         return null;
      } else if (tranSet.size() > 1) {
         OSEELog.logWarning(AtsPlugin.class,
               "Unexpected multiple transactions per committed artifact id " + smaMgr.getSma().getArtId(), false);
      }
      try {
         return TransactionIdManager.getInstance().getPossiblyEditableTransactionId(tranSet.iterator().next());
      } catch (Exception ex) {
         // there may be times where the transaction id cache is not up-to-date yet; don't throw error
      }
      return null;
   }

   /**
    * Display change report associated with the branch, if exists, or transaction, if branch has been committed.
    */
   public void showChangeReportOld() {
      try {
         if (isWorkingBranch()) {
            ChangeReportView.openViewUpon(getWorkingBranch());
         } else if (isCommittedBranch()) {
            ChangeReportView.openViewUpon(new ChangeReportInput(smaMgr.getSma().getDescriptiveName(),
                  getTransactionId()));
         } else {
            AWorkbench.popup("ERROR", "No Branch or Committed Transaction Found.");
         }
      } catch (Exception ex) {
         OSEELog.logException(AtsPlugin.class, "Can't show change report.", ex, true);
      }
   }

   /**
    * Display change report associated with the branch, if exists, or transaction, if branch has been committed.
    */
   public void showChangeReport() {
      try {
         if (isWorkingBranch()) {
            ChangeView.open(getWorkingBranch());
         } else if (isCommittedBranch()) {
            ChangeView.open(getTransactionId().getTransactionNumber());
         } else {
            AWorkbench.popup("ERROR", "No Branch or Committed Transaction Found.");
         }
      } catch (Exception ex) {
         OSEELog.logException(AtsPlugin.class, "Can't show change report.", ex, true);
      }
   }

   /**
    * Display change report associated with the branch, if exists, or transaction, if branch has been committed.
    */
   public void showCommitManager() {
      try {
         if (smaMgr.getBranchMgr().getWorkingBranch() == null) {
            AWorkbench.popup("ERROR", "No working branch");
         } else if (!(smaMgr.getSma() instanceof IBranchArtifact)) {
            AWorkbench.popup("ERROR", "Not IBranchArtifact");
         } else
            CommitManagerView.openViewUpon((IBranchArtifact) smaMgr.getSma());
      } catch (SQLException ex) {
         OSEELog.logException(AtsPlugin.class, ex, true);
      }
   }

   /**
    * Return working branch associated with SMA; This data is cached across all workflows with the cache being updated
    * by local and remote events.
    * 
    * @return
    * @throws SQLException
    */
   public Branch getWorkingBranch() throws SQLException {
      Set<Branch> branches = BranchPersistenceManager.getInstance().getAssociatedArtifactBranches(smaMgr.getSma());
      if (branches.size() == 0) {
         return null;
      } else if (branches.size() > 1) {
         OSEELog.logWarning(AtsPlugin.class,
               "Unexpected multiple working branches per workflow" + smaMgr.getSma().getHumanReadableId(), false);
      } else {
         return branches.iterator().next();
      }
      return null;
   }

   /**
    * @return true if there is a current working branch
    * @throws SQLException
    */
   public boolean isWorkingBranch() throws SQLException {
      return getWorkingBranch() != null;
   }

   /**
    * @return true if there are committed changes associated with this state machine artifact
    */
   public boolean isCommittedBranch() throws SQLException {
      return (getTransactionId() != null);
   }

   /**
    * Set parent branch id associated with this state machine artifact
    * 
    * @param branchId
    * @throws SQLException
    * @throws MultipleAttributesExist
    */
   public void setParentBranchId(int branchId) throws SQLException, MultipleAttributesExist {
      smaMgr.getSma().setSoleAttributeValue(ATSAttributes.PARENT_BRANCH_ID_ATTRIBUTE.getStoreName(),
            String.valueOf(branchId));
   }

   /**
    * Perform error checks and popup confirmation dialogs associated with creating a working branch.
    * 
    * @param pageId if specified, WorkPage gets callback to provide confirmation that branch can be created
    * @param popup if true, errors are popped up to user; otherwise sent silently in Results
    * @return Result return of status
    */
   public Result createWorkingBranch(String pageId, boolean popup) {
      try {
         if (isCommittedBranch()) {
            if (popup) AWorkbench.popup("ERROR",
                  "Can not create another working branch once changes have been committed.");
            return new Result("Committed branch already exists.");
         }
         Branch parentBranch = getParentBranchForWorkingBranchCreation();
         if (parentBranch == null) {
            String errorStr =
                  "Parent Branch can not be determined.\n\nPlease specify " + "parent branch through Version Artifact or Team Definition Artifact.\n\n" + "Contact your team lead to configure this.";
            if (popup) AWorkbench.popup("ERROR", errorStr);
            return new Result(errorStr);
         }
         // Retrieve parent branch to create working branch from
         if (popup && !MessageDialog.openConfirm(
               Display.getCurrent().getActiveShell(),
               "Create Working Branch",
               "Create a working branch from parent branch\n\n\"" + parentBranch.getBranchName() + "\"?\n\n" + "NOTE: Working branches are necessary when OSEE Artifact changes " + "are made during implementation.")) return Result.FalseResult;
         createWorkingBranch(pageId, parentBranch);
      } catch (Exception ex) {
         OSEELog.logException(AtsPlugin.class, ex, false);
         return new Result("Exception occurred: " + ex.getLocalizedMessage());
      }
      return Result.TrueResult;
   }

   /**
    * @return Branch that is the configured branch to create working branch from.
    * @throws SQLException
    */
   private Branch getParentBranchForWorkingBranchCreation() throws SQLException, MultipleAttributesExist {
      Branch parentBranch = null;

      // Check for parent branch id in Version artifact
      if (smaMgr.isTeamUsesVersions()) {
         VersionArtifact verArt = smaMgr.getTargetedForVersion();
         if (verArt != null) {
            try {
               Integer branchId =
                     verArt.getSoleAttributeValue(ATSAttributes.PARENT_BRANCH_ID_ATTRIBUTE.getStoreName(), 0);
               if (branchId != null && branchId > 0) {
                  parentBranch = BranchPersistenceManager.getInstance().getBranch(branchId);
               }
            } catch (BranchDoesNotExist ex) {
               OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
            }
         }
      }

      // If not defined in version, check for parent branch from team definition
      if (parentBranch == null && (smaMgr.getSma() instanceof TeamWorkFlowArtifact)) {
         try {
            Integer branchId =
                  ((TeamWorkFlowArtifact) smaMgr.getSma()).getTeamDefinition().getSoleAttributeValue(
                        ATSAttributes.PARENT_BRANCH_ID_ATTRIBUTE.getStoreName());
            if (branchId != null && branchId > 0) {
               parentBranch = BranchPersistenceManager.getInstance().getBranch(branchId);
            }
         } catch (OseeCoreException ex) {
            // do nothing
         }
      }

      // If not defined, return null
      return parentBranch;
   }

   /**
    * Create a working branch associated with this state machine artifact. This should NOT be called by applications
    * except in test cases or automated tools. Use createWorkingBranchWithPopups
    * 
    * @param pageId
    * @param parentBranch
    * @throws Exception
    */
   public void createWorkingBranch(String pageId, Branch parentBranch) throws Exception {
      final Artifact stateMachineArtifact = smaMgr.getSma();
      String title = stateMachineArtifact.getDescriptiveName();
      if (title.length() > 40) title = title.substring(0, 39) + "...";
      final String branchName =
            String.format("%s - %s - %s", stateMachineArtifact.getHumanReadableId(),
                  stateMachineArtifact.getDescriptiveName(), title);
      String branchShortName = "";
      if (pageId != null && !pageId.equals("")) {
         List<IAtsStateItem> stateItems = smaMgr.getStateItems().getStateItems(pageId);
         if (stateItems.size() > 0) {
            branchShortName = (stateItems.iterator().next().getBranchShortName(smaMgr));
         }
      }
      final String finalBranchShortName = branchShortName;
      final TransactionId parentTransactionId =
            TransactionIdManager.getInstance().getEditableTransactionId(parentBranch);

      IExceptionableRunnable runnable = new IExceptionableRunnable() {
         public void run(IProgressMonitor monitor) throws Exception {
            BranchPersistenceManager.getInstance().createWorkingBranch(parentTransactionId, finalBranchShortName,
                  branchName, stateMachineArtifact);
         }
      };

      Jobs.run("Create Branch", runnable, AtsPlugin.getLogger(), AtsPlugin.PLUGIN_ID);
   }

   public void updateBranchAccessControl() throws Exception {
      // Only set/update branch access control if state item is configured to accept
      for (IAtsStateItem stateItem : smaMgr.getStateItems().getCurrentPageStateItems(smaMgr)) {
         if (stateItem.isAccessControlViaAssigneesEnabledForBranching()) {
            Branch branch = getWorkingBranch();
            if (branch != null) {
               for (AccessControlData acd : AccessControlManager.getInstance().getAccessControlList(branch)) {
                  // If subject is NOT an assignee, remove access control
                  if (!smaMgr.getStateMgr().getAssignees().contains(acd.getSubject())) {
                     AccessControlManager.getInstance().removeAccessControlData(acd);
                  }
               }
               // If subject doesn't have access, add it
               for (User user : smaMgr.getStateMgr().getAssignees())
                  AccessControlManager.getInstance().setPermission(user, branch, PermissionEnum.FULLACCESS);
            }
         }
      }
   }

   /**
    * @param popup if true, popup errors associated with results
    * @return Result
    */
   public Result commitWorkingBranch(boolean popup) {
      return commitWorkingBranch(popup, false);
   }

   /**
    * @param popup if true, popup errors associated with results
    * @param overrideStateValidation if true, don't do checks to see if commit can be performed. This should only be
    *           used for developmental testing or automation
    * @return Result
    */
   public Result commitWorkingBranch(boolean popup, boolean overrideStateValidation) {
      commitPopup = popup;

      try {
         Branch branch = getWorkingBranch();
         if (branch == null) {
            OSEELog.logSevere(AtsPlugin.class,
                  "Commit Branch Failed: Can not locate branch for workflow " + smaMgr.getSma().getHumanReadableId(),
                  popup);
            return new Result("Commit Branch Failed: Can not locate branch.");
         }

         // If team uses versions, then validate that the parent branch id is specified by either
         // the team definition's attribute or the related version's attribute
         if (smaMgr.getSma() instanceof TeamWorkFlowArtifact) {
            TeamWorkFlowArtifact team = (TeamWorkFlowArtifact) smaMgr.getSma();
            // Only perform checks if team definition uses ATS versions
            if (team.getTeamDefinition().isTeamUsesVersions()) {
               // Validate that a parent branch is specified in ATS configuration
               Branch parentBranch = getParentBranchForWorkingBranchCreation();
               if (parentBranch == null) {
                  return new Result(
                        String.format(
                              "Commit Branch Failed: Workflow \"%s\" can't access parent branch to commit to.\n\nSince the configured Team Definition uses versions, the parent branch must be specified in either the targeted Version Artifact or the Team Definition Artifact.",
                              smaMgr.getSma().getHumanReadableId()));
               }

               // Validate that the configured parentBranch is the same as the working branch's
               // parent branch.
               Integer targetedVersionBranchId = parentBranch.getBranchId();
               Integer workflowWorkingBranchParentBranchId =
                     smaMgr.getBranchMgr().getWorkingBranch().getParentBranchId();
               if (!targetedVersionBranchId.equals(workflowWorkingBranchParentBranchId)) {
                  return new Result(
                        String.format(
                              "Commit Branch Failed: Workflow \"%s\" targeted version \"%s\" branch id \"%s\" does not match branch's " + "parent branch id \"%s\"",
                              smaMgr.getSma().getHumanReadableId(), team.getTargetedForVersion().getDescriptiveName(),
                              String.valueOf(targetedVersionBranchId),
                              String.valueOf(workflowWorkingBranchParentBranchId)));
               }
            }
         }

         if (!overrideStateValidation) {
            // Check extenstion points for valid commit
            for (IAtsStateItem item : smaMgr.getStateItems().getStateItems(smaMgr.getWorkPageDefinition().getId())) {
               Result result = item.committing(smaMgr);
               if (result.isFalse()) return result;
            }
         }

         try {
            BranchPersistenceManager.getInstance().commitBranch(branch, true, false);
         } catch (ConflictDetectionException ex) {
            MessageDialog dialog;
            if (OseeProperties.isDeveloper()) {
               dialog =
                     new MessageDialog(
                           Display.getCurrent().getActiveShell(),
                           "Commit Failed",
                           null,
                           "Commit Failed Due To Unresolved Conflicts\n\nPossible Resolutions:\n  Cancel commit and resolve at a later time\n  Launch the Merge Manger to resolve conflicts\n  Force the commit",
                           MessageDialog.QUESTION, new String[] {"Cancel", "Launch Merge Manager", "Force Commit"}, 0);
            } else {
               dialog =
                     new MessageDialog(
                           Display.getCurrent().getActiveShell(),
                           "Commit Failed",
                           null,
                           "Commit Failed Due To Unresolved Conflicts\n\nPossible Resolutions:\n  Cancel commit and resolve at a later time\n  Launch the Merge Manger to resolve conflicts",
                           MessageDialog.QUESTION, new String[] {"Cancel", "Launch Merge Manager"}, 0);

            }

            try {
               int result = dialog.open();
               if (commitPopup && result == 1) {
                  MergeView.openViewUpon(branch, branch.getParentBranch(),
                        TransactionIdManager.getInstance().getStartEndPoint(branch).getKey());
               } else if (result == 2) {
                  BranchPersistenceManager.getInstance().commitBranch(branch, true, true);
               }
            } catch (Exception exc) {
               OSEELog.logException(AtsPlugin.class, "Commit Branch Failed", ex, popup);
               return new Result("Commit Branch Failed: " + ex.getLocalizedMessage());
            }
         }

      } catch (Exception ex) {
         OSEELog.logException(AtsPlugin.class, "Commit Branch Failed", ex, popup);
         return new Result("Commit Branch Failed" + ex.getLocalizedMessage());
      }
      return Result.TrueResult;
   }

   public ArtifactChange getArtifactChange(String artifactName) throws Exception {
      for (ArtifactChange artChange : getArtifactChanges()) {
         if (artChange.getName().equals(artifactName)) return artChange;
      }
      return null;
   }

   public Collection<ArtifactChange> getArtifactChanges() throws Exception {
      ArrayList<ArtifactChange> artChanges = new ArrayList<ArtifactChange>();
      if (smaMgr.getBranchMgr().isWorkingBranch()) {
         Branch workingBranch = smaMgr.getBranchMgr().getWorkingBranch();
         if (workingBranch != null) {
            try {
               for (Object obj : BranchContentProvider.getArtifactChanges(new ChangeReportInput(workingBranch))) {
                  if (obj instanceof ArtifactChange) artChanges.add((ArtifactChange) obj);
               }
            } catch (SQLException ex) {
               OSEELog.logSevere(AtsPlugin.class, "Error getting branch artifact changes", true);
            }
         }
      } else if (smaMgr.getBranchMgr().isCommittedBranch()) {
         TransactionId transactionId = getTransactionId();
         if (transactionId != null) {
            try {
               for (Object obj : BranchContentProvider.getArtifactChanges(new ChangeReportInput("", transactionId))) {
                  if (obj instanceof ArtifactChange) artChanges.add((ArtifactChange) obj);
               }
            } catch (SQLException ex) {
               OSEELog.logSevere(AtsPlugin.class, "Error getting transaction artifact changes", true);
            }
         }
      }
      return artChanges;
   }

   /**
    * Return the artifacts modified via transaction of branch commit during implementation state. This includes
    * artifacts that only had relation changes. NOTE: The returned artifacts are the old versions at the time of the
    * commit. They can't be used for editing or relating. NOTE: This is a VERY expensive operation as each artifact must
    * be loaded. Retrieving data through change report snapshot is cheaper.
    * 
    * @return artifacts modified
    */
   public Collection<Artifact> getArtifactsModified(boolean includeRelationOnlyChanges) {
      ArrayList<Artifact> arts = new ArrayList<Artifact>();
      try {
         if (isWorkingBranch() && !isChangesOnWorkingBranch()) return arts;
         if (smaMgr.getBranchMgr().isWorkingBranch()) {
            try {
               Branch workingBranch = smaMgr.getBranchMgr().getWorkingBranch();
               return RevisionManager.getInstance().getNewAndModifiedArtifacts(workingBranch,
                     includeRelationOnlyChanges);
            } catch (SQLException ex) {
               OSEELog.logException(AtsPlugin.class, ex, true);
            }
         } else if (smaMgr.getBranchMgr().isCommittedBranch()) {
            TransactionId transactionId = getTransactionId();
            if (transactionId != null) {

               return RevisionManager.getInstance().getNewAndModifiedArtifacts(transactionId, transactionId,
                     includeRelationOnlyChanges);

            }
         }
      } catch (Exception ex) {
         OSEELog.logSevere(AtsPlugin.class, "Error getting modified artifacts", true);
      }
      return arts;
   }

   /**
    * Return All artifacts who had a relation change. This includes relation only artifacts and those who had other
    * attribute changes.
    * 
    * @return artifacts
    */
   public Collection<Artifact> getArtifactsRelChanged() throws SQLException {
      ArrayList<Artifact> arts = new ArrayList<Artifact>();
      try {
         if (isWorkingBranch() && !isChangesOnWorkingBranch()) return arts;
         TransactionId transactionId = getTransactionId();
         if (transactionId == null) return arts;

         return RevisionManager.getInstance().getRelationChangedArtifacts(transactionId, transactionId);
      } catch (Exception ex) {
         OSEELog.logSevere(AtsPlugin.class, "Error getting relation changed artifacts", true);
      }
      return arts;
   }

   /**
    * @return true if isWorkingBranch() and changes exist else false
    * @throws SQLException
    * @throws TransactionDoesNotExist
    * @throws BranchDoesNotExist
    */
   public Boolean isChangesOnWorkingBranch() throws SQLException, BranchDoesNotExist, TransactionDoesNotExist {
      if (isWorkingBranch()) {
         Pair<TransactionId, TransactionId> transactionToFrom =
               TransactionIdManager.getInstance().getStartEndPoint(getWorkingBranch());
         if (transactionToFrom.getKey().equals(transactionToFrom.getValue())) {
            return false;
         }
         return true;
      }
      return false;
   }

   /**
    * Since deleted artifacts don't exist, this method will return the artifact object just prior to it's deletion.
    * NOTE: This is a VERY expensive operation as each artifact must be loaded. Retrieving data through change report
    * snapshot is cheaper.
    * 
    * @return artifacts that were deleted
    */
   public Collection<Artifact> getArtifactsDeleted() {
      ArrayList<Artifact> arts = new ArrayList<Artifact>();
      try {
         if (isWorkingBranch() && !isChangesOnWorkingBranch()) return arts;
         TransactionId transactionId = getTransactionId();
         if (transactionId == null) return arts;

         for (ArtifactChange artChange : RevisionManager.getInstance().getDeletedArtifactChanges(transactionId)) {
            if (artChange.getModType() == ModificationType.DELETED) {
               arts.add(artChange.getArtifact());
            }
         }
      } catch (Exception ex) {
         OSEELog.logException(AtsPlugin.class,
               "Error getting deleted artifacts " + smaMgr.getSma().getHumanReadableId(), ex, true);
      }
      return arts;
   }

   /**
    * @return the atsBranchMetrics
    */
   public ATSBranchMetrics getAtsBranchMetrics(boolean cache) throws Exception {
      if (cache) atsBranchMetrics.persist();
      return atsBranchMetrics;
   }

   /**
    * @return the smaMgr
    */
   public SMAManager getSmaMgr() {
      return smaMgr;
   }

}

Back to the top