Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4e450afb78aa6a5caea452f4a9f07b77fb79b5f0 (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
/*******************************************************************************
 * 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 static org.eclipse.osee.framework.skynet.core.artifact.DeletionFlag.INCLUDE_DELETED;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.client.ClientSessionManager;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.database.core.ConnectionHandler;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.database.core.OseeSql;
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.Change;
import org.eclipse.osee.framework.skynet.core.change.ChangeBuilder;
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;

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

   protected RevisionChangeLoader() {
      super();
   }

   /**
    * @param artifact
    * @param monitor
    * @return Returns artifact, relation and attribute changes from a specific artifact
    * @throws OseeCoreException
    */
   public Collection<Change> getChangesPerArtifact(Artifact artifact, IProgressMonitor monitor) throws OseeCoreException {
      ArrayList<Change> changes = new ArrayList<Change>();
      Branch branch = artifact.getBranch();
      ArrayList<TransactionRecord> transactionIds = new ArrayList<TransactionRecord>();
      recurseBranches(branch, artifact, transactionIds, TransactionManager.getHeadTransaction(branch));

      for (TransactionRecord transactionId : transactionIds) {
         changes.addAll(getChanges(null, transactionId, monitor, artifact));
      }
      return changes;
   }

   private void recurseBranches(Branch branch, Artifact artifact, Collection<TransactionRecord> transactionIds, TransactionRecord transactionId) throws OseeCoreException {
      transactionIds.addAll(getTransactionsPerArtifact(branch, artifact, transactionId));

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

   private Collection<TransactionRecord> getTransactionsPerArtifact(Branch branch, Artifact artifact, TransactionRecord transactionId) throws OseeCoreException {
      Set<TransactionRecord> transactionIds = new HashSet<TransactionRecord>();

      IOseeStatement chStmt = ConnectionHandler.getStatement();
      try {
         chStmt.runPreparedQuery(ClientSessionManager.getSql(OseeSql.LOAD_REVISION_HISTORY_TRANSACTION_ATTR),
            artifact.getArtId(), branch.getId(), transactionId.getId());

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

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

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

      return transactionIds;
   }

   /**
    * Not Part of Change Report Acquires artifact, relation and attribute changes from a source branch since its
    * creation.
    * 
    * @param sourceBranch
    * @param baselineTransactionId
    * @return
    * @throws OseeCoreException
    */
   private Collection<Change> getChanges(Branch sourceBranch, TransactionRecord transactionId, IProgressMonitor monitor, Artifact specificArtifact) throws OseeCoreException {
      @SuppressWarnings("unused")
      //This is so weak references do not get collected from bulk loading
      Collection<Artifact> bulkLoadedArtifacts;
      ArrayList<Change> changes = new ArrayList<Change>();
      ArrayList<ChangeBuilder> changeBuilders = new ArrayList<ChangeBuilder>();

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

      monitor.beginTask("Find Changes", 100);

      ArtifactChangeAcquirer artifactChangeAcquirer =
         new ArtifactChangeAcquirer(sourceBranch, transactionId, monitor, specificArtifact, artIds, changeBuilders,
            newAndDeletedArtifactIds);
      changeBuilders = artifactChangeAcquirer.acquireChanges();

      AttributeChangeAcquirer attributeChangeAcquirer =
         new AttributeChangeAcquirer(sourceBranch, transactionId, monitor, specificArtifact, artIds, changeBuilders,
            newAndDeletedArtifactIds);
      changeBuilders = attributeChangeAcquirer.acquireChanges();

      RelationChangeAcquirer relationChangeAcquirer =
         new RelationChangeAcquirer(sourceBranch, transactionId, monitor, specificArtifact, artIds, changeBuilders,
            newAndDeletedArtifactIds);
      changeBuilders = relationChangeAcquirer.acquireChanges();

      monitor.subTask("Loading Artifacts from the Database");
      Branch branch = historical ? transactionId.getBranch() : sourceBranch;

      if (historical) {
         bulkLoadedArtifacts = ArtifactQuery.getHistoricalArtifactListFromIds(artIds, transactionId, INCLUDE_DELETED);
      } else {
         bulkLoadedArtifacts = ArtifactQuery.getArtifactListFromIds(artIds, branch, INCLUDE_DELETED);
      }

      //We build the changes after the artifact loader has been run so we can take advantage of bulk loading.
      for (ChangeBuilder builder : changeBuilders) {
         changes.add(builder.build(branch));
      }

      monitor.done();
      return changes;
   }
}

Back to the top