Skip to main content
summaryrefslogtreecommitdiffstats
blob: 75d7fdddbc9d1655edd033ab87fb8ad2420c5987 (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
/*******************************************************************************
 * 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.framework.skynet.core.revision;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.DeletionFlag;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.jdk.core.type.CompositeKeyHashMap;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
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.change.ArtifactChange;
import org.eclipse.osee.framework.skynet.core.change.ArtifactChangeBuilder;
import org.eclipse.osee.framework.skynet.core.change.ArtifactDelta;
import org.eclipse.osee.framework.skynet.core.change.AttributeChange;
import org.eclipse.osee.framework.skynet.core.change.AttributeChangeBuilder;
import org.eclipse.osee.framework.skynet.core.change.Change;
import org.eclipse.osee.framework.skynet.core.change.ChangeBuilder;
import org.eclipse.osee.framework.skynet.core.change.RelationChange;
import org.eclipse.osee.framework.skynet.core.change.RelationChangeBuilder;
import org.eclipse.osee.framework.skynet.core.internal.OseeSql;
import org.eclipse.osee.framework.skynet.core.internal.ServiceUtil;
import org.eclipse.osee.framework.skynet.core.revision.acquirer.ArtifactChangeAcquirer;
import org.eclipse.osee.framework.skynet.core.revision.acquirer.AttributeChangeAcquirer;
import org.eclipse.osee.framework.skynet.core.revision.acquirer.RelationChangeAcquirer;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.eclipse.osee.framework.skynet.core.utility.ConnectionHandler;

/**
 * Acquires changes for either branches or transactions.
 * 
 * @author Jeff C. Phillips
 */
public final class RevisionChangeLoader {

   protected RevisionChangeLoader() {
      super();
   }

   /**
    * @return Returns artifact, relation and attribute changes from a specific artifact
    */
   public Collection<Change> getChangesPerArtifact(Artifact artifact, IProgressMonitor monitor) throws OseeCoreException {
      return getChangesPerArtifact(artifact, monitor, LoadChangeType.artifact, LoadChangeType.attribute,
         LoadChangeType.relation);
   }

   /**
    * @return Returns artifact, relation and attribute changes from a specific artifact
    */
   private Collection<Change> getChangesPerArtifact(Artifact artifact, IProgressMonitor monitor, LoadChangeType... loadChangeTypes) throws OseeCoreException {
      Branch branch = artifact.getFullBranch();
      Set<TransactionRecord> transactionIds = new LinkedHashSet<TransactionRecord>();
      boolean recurseThroughBranchHierarchy = true;
      loadBranchTransactions(branch, artifact, transactionIds, TransactionManager.getHeadTransaction(branch),
         recurseThroughBranchHierarchy);

      Collection<Change> changes = new ArrayList<Change>();

      for (TransactionRecord transactionId : transactionIds) {
         loadChanges(null, transactionId, monitor, artifact, changes, loadChangeTypes);
      }
      return changes;
   }

   private void loadBranchTransactions(Branch branch, Artifact artifact, Set<TransactionRecord> transactionIds, TransactionRecord transactionId, boolean recurseThroughBranchHierarchy) throws OseeCoreException {
      loadTransactions(branch, artifact, transactionId, transactionIds);

      if (recurseThroughBranchHierarchy) {
         if (branch.hasParentBranch() && !branch.getParentBranch().getBranchType().isSystemRootBranch()) {
            loadBranchTransactions(branch.getParentBranch(), artifact, transactionIds, branch.getBaseTransaction(),
               recurseThroughBranchHierarchy);
         }
      }
   }

   private void loadTransactions(Branch branch, Artifact artifact, TransactionRecord transactionId, Set<TransactionRecord> transactionIds) throws OseeCoreException {
      IOseeStatement chStmt = ConnectionHandler.getStatement();
      try {
         chStmt.runPreparedQuery(ServiceUtil.getSql(OseeSql.LOAD_REVISION_HISTORY_TRANSACTION_ATTR),
            artifact.getArtId(), branch.getUuid(), transactionId.getId());

         while (chStmt.next()) {
            transactionIds.add(TransactionManager.getTransactionId(chStmt.getInt("transaction_id")));
         }

         chStmt.runPreparedQuery(ServiceUtil.getSql(OseeSql.LOAD_REVISION_HISTORY_TRANSACTION_REL),
            artifact.getArtId(), artifact.getArtId(), branch.getUuid(), transactionId.getId());

         while (chStmt.next()) {
            transactionIds.add(TransactionManager.getTransactionId(chStmt.getInt("transaction_id")));
         }
      } finally {
         chStmt.close();
      }
   }

   /**
    * Not Part of Change Report Acquires artifact, relation and attribute changes from a source branch since its
    * creation.
    */
   private void loadChanges(Branch sourceBranch, TransactionRecord transactionId, IProgressMonitor monitor, Artifact specificArtifact, Collection<Change> changes, LoadChangeType... loadChangeTypes) throws OseeCoreException {
      if (monitor == null) {
         monitor = new NullProgressMonitor();
      }
      monitor.beginTask("Find Changes", 100);

      Set<Integer> artIds = new HashSet<Integer>();
      Set<Integer> newAndDeletedArtifactIds = new HashSet<Integer>();
      boolean isHistorical = sourceBranch == null;

      ArrayList<ChangeBuilder> changeBuilders = new ArrayList<ChangeBuilder>();
      for (LoadChangeType changeType : loadChangeTypes) {
         switch (changeType) {
            case artifact:
               ArtifactChangeAcquirer artifactChangeAcquirer =
                  new ArtifactChangeAcquirer(sourceBranch, transactionId, monitor, specificArtifact, artIds,
                     changeBuilders, newAndDeletedArtifactIds);
               changeBuilders = artifactChangeAcquirer.acquireChanges();
               break;
            case attribute:
               AttributeChangeAcquirer attributeChangeAcquirer =
                  new AttributeChangeAcquirer(sourceBranch, transactionId, monitor, specificArtifact, artIds,
                     changeBuilders, newAndDeletedArtifactIds);
               changeBuilders = attributeChangeAcquirer.acquireChanges();
               break;
            case relation:
               RelationChangeAcquirer relationChangeAcquirer =
                  new RelationChangeAcquirer(sourceBranch, transactionId, monitor, specificArtifact, artIds,
                     changeBuilders, newAndDeletedArtifactIds);

               changeBuilders = relationChangeAcquirer.acquireChanges();
               break;
            default:
               break;
         }
      }
      monitor.subTask("Loading Artifacts from the Database");

      IOseeBranch branch = isHistorical ? transactionId.getBranch() : sourceBranch;

      Collection<Change> changesLoaded = getChanges(branch, isHistorical, changeBuilders);
      changes.addAll(changesLoaded);

      monitor.done();
   }

   private CompositeKeyHashMap<TransactionRecord, Integer, Artifact> getBulkLoadedArtifacts(IOseeBranch branch, boolean isHistorical, List<ChangeBuilder> changeBuilders) throws OseeCoreException {
      HashCollection<TransactionRecord, Integer> loadMap =
         new HashCollection<TransactionRecord, Integer>(false, HashSet.class);
      for (ChangeBuilder builder : changeBuilders) {
         TransactionRecord endTx = builder.getTxDelta().getEndTx();
         loadMap.put(endTx, builder.getArtId());
         if (builder instanceof RelationChangeBuilder) {
            RelationChangeBuilder relBuilder = (RelationChangeBuilder) builder;
            loadMap.put(endTx, relBuilder.getbArtId());
         }
      }

      CompositeKeyHashMap<TransactionRecord, Integer, Artifact> loadedMap =
         new CompositeKeyHashMap<TransactionRecord, Integer, Artifact>();

      for (Entry<TransactionRecord, Collection<Integer>> entry : loadMap.entrySet()) {
         Collection<Artifact> artifacts;
         if (isHistorical) {
            artifacts =
               ArtifactQuery.getHistoricalArtifactListFromIds(entry.getValue(), entry.getKey(),
                  DeletionFlag.INCLUDE_DELETED);
         } else {
            artifacts = ArtifactQuery.getArtifactListFromIds(entry.getValue(), branch, DeletionFlag.INCLUDE_DELETED);
         }
         for (Artifact artifact : artifacts) {
            loadedMap.put(entry.getKey(), artifact.getArtId(), artifact);
         }
      }
      return loadedMap;
   }

   private Collection<Change> getChanges(IOseeBranch branch, boolean isHistorical, List<ChangeBuilder> changeBuilders) throws OseeCoreException {
      CompositeKeyHashMap<TransactionRecord, Integer, Artifact> loadedMap =
         getBulkLoadedArtifacts(branch, isHistorical, changeBuilders);

      Collection<Change> changes = new ArrayList<Change>();
      for (ChangeBuilder builder : changeBuilders) {
         Change toReturn = null;
         Artifact changeArtifact = loadedMap.get(builder.getTxDelta().getEndTx(), builder.getArtId());

         if (changeArtifact != null) {
            ArtifactDelta delta = new ArtifactDelta(builder.getTxDelta(), changeArtifact, null);
            if (builder instanceof ArtifactChangeBuilder) {
               toReturn =
                  new ArtifactChange(branch, builder.getSourceGamma(), builder.getArtId(), builder.getTxDelta(),
                     builder.getModType(), isHistorical, changeArtifact, delta);
            } else if (builder instanceof AttributeChangeBuilder) {
               AttributeChangeBuilder attrBuilder = (AttributeChangeBuilder) builder;
               toReturn =
                  new AttributeChange(branch, attrBuilder.getSourceGamma(), attrBuilder.getArtId(),
                     attrBuilder.getTxDelta(), attrBuilder.getModType(), attrBuilder.getIsValue(),
                     attrBuilder.getWasValue(), attrBuilder.getAttrId(), attrBuilder.getAttributeType(),
                     attrBuilder.getArtModType(), isHistorical, changeArtifact, delta);
            } else if (builder instanceof RelationChangeBuilder) {
               RelationChangeBuilder relBuilder = (RelationChangeBuilder) builder;
               Artifact bArtifact = loadedMap.get(builder.getTxDelta().getEndTx(), relBuilder.getbArtId());
               toReturn =
                  new RelationChange(branch, builder.getSourceGamma(), builder.getArtId(), builder.getTxDelta(),
                     builder.getModType(), relBuilder.getbArtId(), relBuilder.getRelLinkId(),
                     relBuilder.getRationale(), relBuilder.getRelationType(), isHistorical, changeArtifact, delta,
                     bArtifact);
            }
         } else {
            toReturn =
               new ArtifactChange(branch, builder.getSourceGamma(), builder.getArtId(), builder.getTxDelta(),
                  builder.getModType(), isHistorical, null, null);
         }
         changes.add(toReturn);
      }
      return changes;
   }
}

Back to the top