Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3493f722dde14bd67b6aa4a36f9d5d6dc9b53113 (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
/*******************************************************************************
 * 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.actions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osee.ats.artifact.AtsAttributeTypes;
import org.eclipse.osee.ats.artifact.TeamWorkFlowArtifact;
import org.eclipse.osee.ats.internal.AtsPlugin;
import org.eclipse.osee.ats.util.AtsBranchManager;
import org.eclipse.osee.ats.world.WorldEditor;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.change.ArtifactChange;
import org.eclipse.osee.framework.skynet.core.change.Change;
import org.eclipse.osee.framework.skynet.core.revision.ChangeManager;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.eclipse.osee.framework.skynet.core.types.IArtifact;
import org.eclipse.osee.framework.ui.skynet.FrameworkImage;
import org.eclipse.osee.framework.ui.skynet.render.RenderingUtil;
import org.eclipse.osee.framework.ui.skynet.render.word.WordChangeReportOperation;
import org.eclipse.osee.framework.ui.swt.ImageManager;

/**
 * @author Donald G. Dunne
 */
public class ExportChangeReportsAction extends Action {
   private final WorldEditor worldEditor;

   //   private final boolean reverse;

   public ExportChangeReportsAction(WorldEditor worldEditor, boolean reverse) {
      super();
      //      this.reverse = reverse;
      setText("Export Change Report(s)");
      setImageDescriptor(ImageManager.getImageDescriptor(FrameworkImage.EXPORT_DATA));
      this.worldEditor = worldEditor;
   }

   @SuppressWarnings("unused")
   public Collection<TeamWorkFlowArtifact> getWorkflows() throws OseeCoreException {
      //      if (true) {
      //         Collection<String> dontCreate = Arrays.asList(new String[] {});
      //         Collection<String> legacyIds = Arrays.asList(new String[] {"12442"});
      //
      //         List<TeamWorkFlowArtifact> workflows = new ArrayList<TeamWorkFlowArtifact>();
      //         if (workflows.isEmpty()) {
      //            List<Artifact> artifacts =
      //               ArtifactQuery.getArtifactListFromAttributeValues(AtsAttributeTypes.LegacyPcrId, legacyIds,
      //                  CoreBranches.COMMON, legacyIds.size());
      //            for (Artifact artifact : artifacts) {
      //               if (artifact.getArtifactType().getGuid().equals("AAMFDjZ1UVAQTXHk2GgA")) {
      //                  TeamWorkFlowArtifact teamWorkflow = (TeamWorkFlowArtifact) artifact;
      //                  String legacyId = teamWorkflow.getWorldViewLegacyPCR();
      //                  if (!dontCreate.contains(legacyId)) {
      //                     workflows.add(teamWorkflow);
      //                  }
      //               }
      //            }
      //            Collections.sort(workflows, new Comparator<TeamWorkFlowArtifact>() {
      //               @Override
      //               public int compare(TeamWorkFlowArtifact workflow1, TeamWorkFlowArtifact workflow2) {
      //                  try {
      //                     int compare = workflow1.getWorldViewLegacyPCR().compareTo(workflow2.getWorldViewLegacyPCR());
      //                     return reverse ? -1 * compare : compare;
      //                  } catch (OseeCoreException ex) {
      //                     return -1;
      //                  }
      //               }
      //            });
      //         }
      //         return workflows;
      //      }

      return worldEditor.getWorldComposite().getXViewer().getSelectedTeamWorkflowArtifacts();
   }

   @Override
   public void run() {
      try {
         IOperation operation = new ExportChangesOperation(getWorkflows());
         Operations.executeAsJob(operation, true);
      } catch (OseeCoreException ex) {
         OseeLog.log(AtsPlugin.class, Level.SEVERE, ex.toString(), ex);
      }
   }

   @Override
   public ImageDescriptor getImageDescriptor() {
      return ImageManager.getImageDescriptor(FrameworkImage.EXPORT_DATA);
   }

   public void updateEnablement() throws OseeCoreException {
      setEnabled(!getWorkflows().isEmpty());
   }

   private static final class ExportChangesOperation extends AbstractOperation {
      private final Collection<TeamWorkFlowArtifact> workflows;

      public ExportChangesOperation(Collection<TeamWorkFlowArtifact> workflows) {
         super("Exporting Change Report(s)", AtsPlugin.PLUGIN_ID);
         this.workflows = workflows;
      }

      private TransactionRecord pickTransaction(IArtifact workflow) throws OseeCoreException {
         int minTransactionId = -1;
         for (TransactionRecord transaction : TransactionManager.getCommittedArtifactTransactionIds(workflow)) {
            if (minTransactionId < transaction.getId()) {
               minTransactionId = transaction.getId();
            }
         }
         if (minTransactionId == -1) {
            throw new OseeStateException("no transaction records found for [%s]", workflow);
         }
         return TransactionManager.getTransactionId(minTransactionId);
      }

      @Override
      protected void doWork(IProgressMonitor monitor) throws OseeCoreException {
         RenderingUtil.setPopupsAllowed(false);

         for (Artifact workflow : workflows) {

            Collection<Change> changes = computeChanges(workflow, monitor);
            if (!changes.isEmpty() && changes.size() < 4000) {
               String folderName = workflow.getSoleAttributeValueAsString(AtsAttributeTypes.LegacyPcrId, null);
               doSubWork(new WordChangeReportOperation(changes, folderName), monitor, 0.50);
            } else {
               monitor.worked(calculateWork(0.50));
            }
         }
         RenderingUtil.setPopupsAllowed(true);
      }

      private Collection<Change> computeChanges(Artifact workflow, IProgressMonitor monitor) throws OseeCoreException {
         AtsBranchManager atsBranchMgr = ((TeamWorkFlowArtifact) workflow).getBranchMgr();

         List<Change> changes = new ArrayList<Change>();
         IOperation operation = null;
         if (atsBranchMgr.isCommittedBranchExists()) {
            operation = ChangeManager.comparedToPreviousTx(pickTransaction(workflow), changes);
         } else {
            Branch workingBranch = atsBranchMgr.getWorkingBranch();
            if (workingBranch != null && !workingBranch.getBranchType().isBaselineBranch()) {
               operation = ChangeManager.comparedToParent(workingBranch, changes);
            }
         }
         if (operation != null) {
            doSubWork(operation, monitor, 0.50);

            Iterator<Change> iterator = changes.iterator();
            while (iterator.hasNext()) {
               if (!(iterator.next() instanceof ArtifactChange)) {
                  iterator.remove();
               }
            }

            Collections.sort(changes);
         }
         return changes;
      }
   }
}

Back to the top