Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9ee1c365a6724faa7b03f7d67d73a406bd11a1ad (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
/*******************************************************************************
 * 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.artifact;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.exception.TransactionDoesNotExist;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.core.model.event.DefaultBasicGuidArtifact;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.database.IOseeDatabaseServiceProvider;
import org.eclipse.osee.framework.database.core.AbstractDbTxOperation;
import org.eclipse.osee.framework.database.core.ConnectionHandler;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.database.core.JoinUtility;
import org.eclipse.osee.framework.database.core.JoinUtility.TransactionJoinQuery;
import org.eclipse.osee.framework.database.core.OseeConnection;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.logging.OseeLog;
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.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.model.TransactionChange;
import org.eclipse.osee.framework.skynet.core.event.model.TransactionEvent;
import org.eclipse.osee.framework.skynet.core.event.model.TransactionEventType;
import org.eclipse.osee.framework.skynet.core.internal.Activator;
import org.eclipse.osee.framework.skynet.core.revision.ChangeManager;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;

/**
 * @author Ryan D. Brooks
 */
public class PurgeTransactionOperation extends AbstractDbTxOperation {

   private static final String UPDATE_TXS_DETAILS_COMMENT =
      "UPDATE osee_tx_details SET osee_comment = replace(osee_comment, ?, ?) WHERE osee_comment like ?";

   private static final String SELECT_GAMMAS_FROM_TRANSACTION =
      "SELECT txs1.gamma_id, txs1.transaction_id FROM osee_txs txs1, osee_join_transaction txj1 WHERE " + "txs1.transaction_id = txj1.transaction_id AND txj1.query_id = ? AND " + "NOT EXISTS (SELECT 'x' FROM osee_txs txs2 WHERE txs1.gamma_id = txs2.gamma_id AND txs1.transaction_id <> txs2.transaction_id)";

   private static final String DELETE_TRANSACTION_FROM_TRANSACTION_DETAILS =
      "DELETE FROM osee_tx_details WHERE transaction_id IN (SELECT txj1.transaction_id FROM osee_join_transaction txj1 WHERE txj1.query_id = ?)";

   private static final String DELETE_POSTFIX =
      "outerTb where outerTb.gamma_id = (SELECT txj1.gamma_id from osee_join_transaction txj1 WHERE outerTb.gamma_id = txj1.gamma_id AND txj1.query_id = ?)";

   private final static String DELETE_ARTIFACT_VERSIONS = "DELETE FROM osee_artifact " + DELETE_POSTFIX;
   private final static String DELETE_ATTRIBUTES = "DELETE FROM osee_attribute " + DELETE_POSTFIX;
   private final static String DELETE_RELATIONS = "DELETE FROM osee_relation_link " + DELETE_POSTFIX;

   private final static String DELETE_TXS = "delete from osee_txs where transaction_id = ?";

   private final static String TRANSACATION_GAMMA_IN_USE =
      "SELECT txs2.transaction_id FROM osee_txs txs1, osee_txs txs2, osee_join_transaction jn where txs1.transaction_id = jn.transaction_id AND txs1.gamma_id = txs2.gamma_id and txs2.transaction_id != txs1.transaction_id AND jn.query_id = ?";

   private final static String LOAD_ARTIFACTS =
      "INSERT INTO osee_join_artifact (query_id, art_id, branch_id, insert_time) (SELECT ?, art_id, txs.branch_id, CURRENT_TIMESTAMP from osee_txs txs, osee_attribute att, osee_join_transaction tran where tran.query_id = ? AND tran.transaction_id = txs.transaction_id AND txs.gamma_id = att.gamma_id) UNION (SELECT ?, art_id, txs.branch_id, CURRENT_TIMESTAMP FROM osee_txs txs, osee_artifact art, osee_join_transaction tran WHERE tran.query_id = ? AND tran.transaction_id = txs.transaction_id AND txs.gamma_id = art.gamma_id) UNION (SELECT ?, a_art_id as art_id, txs.branch_id, CURRENT_TIMESTAMP FROM osee_txs txs, osee_relation_link rel, osee_join_transaction tran WHERE tran.query_id = ? AND tran.transaction_id = txs.transaction_id AND txs.gamma_id = rel.gamma_id) UNION (SELECT ?, b_art_id as art_id, txs.branch_id, CURRENT_TIMESTAMP FROM osee_txs txs, osee_relation_link rel, osee_join_transaction tran WHERE tran.query_id = ? AND tran.transaction_id = txs.transaction_id AND txs.gamma_id = rel.gamma_id)";

   private static final String UPDATE_TXS =
      "UPDATE osee_txs SET tx_current = (CASE WHEN mod_type = 3 THEN 2 ELSE 1 END) WHERE (transaction_id, gamma_id) IN ((SELECT maxt, txs2.gamma_id FROM osee_txs txs2, osee_attribute att2, (SELECT MAX(txs.transaction_id) AS maxt, att.attr_id AS atid FROM osee_txs txs, osee_attribute att, osee_join_artifact jar WHERE txs.branch_id = jar.branch_id AND txs.gamma_id = att.gamma_id and att.art_id = jar.art_id AND jar.query_id = ? GROUP BY att.attr_id, txs.branch_id) new_stuff WHERE txs2.gamma_id = att2.gamma_id AND att2.attr_id = atid AND maxt = txs2.transaction_id) UNION (SELECT maxt, txs2.gamma_id FROM osee_txs txs2, osee_artifact art2, (SELECT MAX(txs.transaction_id) AS maxt, ver.art_id AS atid FROM osee_txs txs, osee_artifact ver, osee_join_artifact jar WHERE txs.branch_id = jar.branch_id AND txs.gamma_id = ver.gamma_id and ver.art_id = jar.art_id AND jar.query_id = ? GROUP BY ver.art_id, txs.branch_id) new_stuff WHERE txs2.gamma_id = art2.gamma_id AND art2.art_id = atid AND maxt = txs2.transaction_id) UNION (SELECT maxt, txs2.gamma_id FROM osee_txs txs2, osee_relation_link rel2, (SELECT MAX(txs.transaction_id) AS maxt, rel.rel_link_id AS linkid FROM osee_txs txs, osee_relation_link rel, osee_join_artifact jar WHERE txs.branch_id = jar.branch_id AND txs.gamma_id = rel.gamma_id and (rel.a_art_id = jar.art_id or rel.b_art_id = jar.art_id) AND jar.query_id = ? GROUP BY rel.rel_link_id, txs.branch_id) new_stuff WHERE txs2.gamma_id = rel2.gamma_id AND rel2.rel_link_id = linkid AND maxt = txs2.transaction_id))";

   private final int[] txIdsToDelete;
   private final boolean force;
   private int artifactJoinId;

   public PurgeTransactionOperation(IOseeDatabaseServiceProvider serviceProvider, boolean force, int... txIdsToDelete) {
      super(serviceProvider, String.format("Delete transactions: %s", Arrays.toString(txIdsToDelete)),
         Activator.PLUGIN_ID);
      this.txIdsToDelete = txIdsToDelete;
      this.force = force;
   }

   public PurgeTransactionOperation(IOseeDatabaseServiceProvider serviceProvider, int... txIdsToDelete) {
      this(serviceProvider, true, txIdsToDelete);
   }

   @Override
   protected void doTxWork(IProgressMonitor monitor, OseeConnection connection) throws OseeCoreException {
      TransactionJoinQuery txsToDeleteQuery = JoinUtility.createTransactionJoinQuery();
      try {
         Arrays.sort(txIdsToDelete);

         // Need to compute the events prior to purge
         TransactionEvent transactionEvent = getPurgeTransactionEvent(txIdsToDelete);

         HashCollection<Branch, TxDeleteInfo> fromToTxData =
            getTransactionPairs(monitor, txIdsToDelete, txsToDeleteQuery, 0.20);
         txsToDeleteQuery.store(connection);
         checkForModifiedBaselines(connection, force, txsToDeleteQuery.getQueryId());
         getAffectedArtifacts(connection, txsToDeleteQuery.getQueryId());

         setChildBranchBaselineTxs(connection, monitor, fromToTxData, 0.20);
         deleteItemEntriesForTransactions(connection, monitor, txsToDeleteQuery.getQueryId(), 0.20);
         deleteTransactionsFromTxDetails(connection, monitor, txsToDeleteQuery.getQueryId(), 0.20);

         deleteTxCurrent(connection);
         updateTxCurrent(connection, monitor, 0.20);

         OseeEventManager.kickTransactionEvent(this, transactionEvent);
      } catch (OseeCoreException ex) {
         if (connection != null && connection.isClosed() != true) {
            txsToDeleteQuery.delete(connection);
            ArtifactLoader.clearQuery(connection, artifactJoinId);
         }
         OseeExceptions.wrapAndThrow(ex);
      } finally {
         txsToDeleteQuery.delete();
         ArtifactLoader.clearQuery(artifactJoinId);
      }
   }

   public static void handleRemotePurgeTransactionEvent(TransactionEvent transEvent) {
      if (transEvent.getEventType() != TransactionEventType.Purged) {
         return;
      }

      Set<Artifact> artifactsInCache = new HashSet<Artifact>();
      for (TransactionChange transChange : transEvent.getTransactions()) {
         try {
            TransactionManager.deCache(transChange.getTransactionId());
         } catch (OseeCoreException ex1) {
            OseeLog.log(Activator.class, Level.SEVERE, ex1);
         }

         for (DefaultBasicGuidArtifact guidArt : transChange.getArtifacts()) {
            try {
               Artifact artifact = ArtifactCache.getActive(guidArt);
               if (artifact != null) {
                  artifactsInCache.add(artifact);
               }
            } catch (OseeCoreException ex) {
               OseeLog.log(Activator.class, Level.SEVERE, ex);
            }
         }

      } // This will kick the artifacts reloaded event which should be handled by Applications/UIs
      if (artifactsInCache.size() > 0) {
         try {
            ArtifactQuery.reloadArtifacts(artifactsInCache);
         } catch (OseeCoreException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }

   }

   private TransactionEvent getPurgeTransactionEvent(int[] txIdsToDelete) {
      try {
         TransactionEvent transactionEvent = new TransactionEvent();
         transactionEvent.setEventType(TransactionEventType.Purged);
         for (int transId : txIdsToDelete) {
            try {
               TransactionChange transChange = new TransactionChange();
               transChange.setTransactionId(transId);
               TransactionRecord transRecord = TransactionManager.getTransactionId(transId);
               Branch branch = BranchManager.getBranch(transRecord.getBranchId());
               transChange.setBranchGuid(branch.getGuid());

               Collection<Change> changes = new ArrayList<Change>();
               IOperation operation = ChangeManager.comparedToPreviousTx(transRecord, changes);
               Operations.executeWorkAndCheckStatus(operation);
               Set<Artifact> processedArts = new HashSet<Artifact>();
               for (Change change : changes) {
                  Artifact art = change.getChangeArtifact();
                  if (!processedArts.contains(art)) {
                     transChange.getArtifacts().add(art.getBasicGuidArtifact());
                     processedArts.add(art);
                  }
               }
               transactionEvent.getTransactions().add(transChange);
            } catch (Exception ex) {
               OseeLog.log(Activator.class, Level.SEVERE, ex);
            }
         }
         return transactionEvent;
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
      return null;
   }

   private void getAffectedArtifacts(OseeConnection connection, int transactionQueryId) throws OseeDataStoreException {
      artifactJoinId = ArtifactLoader.getNewQueryId();
      ConnectionHandler.runPreparedUpdate(connection, LOAD_ARTIFACTS, artifactJoinId, transactionQueryId,
         artifactJoinId, transactionQueryId, artifactJoinId, transactionQueryId, artifactJoinId, transactionQueryId);
   }

   private HashCollection<Branch, TxDeleteInfo> getTransactionPairs(IProgressMonitor monitor, int[] txsToDelete, TransactionJoinQuery txsToDeleteQuery, double workPercentage) throws OseeCoreException {
      HashCollection<Branch, TxDeleteInfo> fromToTxData = new HashCollection<Branch, TxDeleteInfo>();
      if (txsToDelete.length > 0) {
         double workStep = workPercentage / txsToDelete.length;
         for (int index = 0; index < txsToDelete.length; index++) {
            monitor.subTask(String.format("Fetching Previous Tx Info: [%d of %d]", index + 1, txsToDelete.length));
            int fromTx = txsToDelete[index];
            TransactionRecord fromTransaction = TransactionManager.getTransactionId(fromTx);
            TransactionRecord previousTransaction;
            try {
               previousTransaction = TransactionManager.getPriorTransaction(fromTransaction);
            } catch (TransactionDoesNotExist ex) {
               throw new OseeCoreException(
                  "You are trying to delete Transaction: " + fromTx + " which is a baseline transaction.  If your intent is to delete the Branch use the delete Branch Operation.  \n\nNO TRANSACTIONS WERE DELETED.");

            }

            fromToTxData.put(fromTransaction.getBranch(), new TxDeleteInfo(fromTransaction, previousTransaction));

            // Store transaction id(s) to delete - no need for gammas
            txsToDeleteQuery.add(-1L, fromTx);
            monitor.worked(calculateWork(workStep));
         }
      } else {
         monitor.worked(calculateWork(workPercentage));
      }
      return fromToTxData;
   }

   private void deleteTransactionsFromTxDetails(OseeConnection connection, IProgressMonitor monitor, int queryId, double workPercentage) throws OseeDataStoreException {
      monitor.subTask("Deleting Tx");
      ConnectionHandler.runPreparedUpdate(connection, DELETE_TRANSACTION_FROM_TRANSACTION_DETAILS, queryId);
      monitor.worked(calculateWork(workPercentage));
   }

   private void deleteItemEntriesForTransactions(OseeConnection connection, IProgressMonitor monitor, int txsToDeleteQueryId, double workPercentage) throws OseeDataStoreException {
      monitor.subTask("Deleting Tx Items");
      TransactionJoinQuery txGammasToDelete = JoinUtility.createTransactionJoinQuery();
      try {
         populateJoinQueryFromSql(connection, txGammasToDelete, SELECT_GAMMAS_FROM_TRANSACTION, "transaction_id",
            txsToDeleteQueryId);
         txGammasToDelete.store();
         int deleteQueryId = txGammasToDelete.getQueryId();
         ConnectionHandler.runPreparedUpdate(connection, DELETE_ARTIFACT_VERSIONS, deleteQueryId);
         ConnectionHandler.runPreparedUpdate(connection, DELETE_ATTRIBUTES, deleteQueryId);
         ConnectionHandler.runPreparedUpdate(connection, DELETE_RELATIONS, deleteQueryId);

      } finally {
         if (txGammasToDelete != null && connection != null && connection.isClosed() != true) {
            txGammasToDelete.delete(connection);
         }
      }
      monitor.worked(calculateWork(workPercentage));
   }

   private void populateJoinQueryFromSql(OseeConnection connection, TransactionJoinQuery joinQuery, String sql, String txFieldName, Object... data) throws OseeDataStoreException {
      IOseeStatement chStmt = ConnectionHandler.getStatement(connection);
      try {
         chStmt.runPreparedQuery(sql, data);
         while (chStmt.next()) {
            joinQuery.add(chStmt.getLong("gamma_id"), chStmt.getInt(txFieldName));
         }
      } finally {
         chStmt.close();
      }
   }

   private void updateTxCurrent(OseeConnection conn, IProgressMonitor monitor, double workPercentage) throws OseeDataStoreException {
      monitor.setTaskName("Updating Previous Tx to Current");
      ConnectionHandler.runPreparedUpdate(conn, UPDATE_TXS, artifactJoinId, artifactJoinId, artifactJoinId);
      monitor.worked(calculateWork(workPercentage));
   }

   private void deleteTxCurrent(OseeConnection connection) throws OseeCoreException {
      for (int txId : txIdsToDelete) {
         ConnectionHandler.runPreparedUpdate(connection, DELETE_TXS, txId);
      }
   }

   private void setChildBranchBaselineTxs(OseeConnection connection, IProgressMonitor monitor, HashCollection<Branch, TxDeleteInfo> transactions, double workPercentage) throws OseeDataStoreException {
      List<Object[]> data = new ArrayList<Object[]>();
      monitor.setTaskName("Update Baseline Txs for Child Branches");
      for (TxDeleteInfo entry : transactions.getValues()) {
         TransactionRecord previousTransaction = entry.getPreviousTx();
         if (previousTransaction != null) {
            int toDeleteTransaction = entry.getTxToDelete().getId();

            data.add(new Object[] {
               String.valueOf(toDeleteTransaction),
               String.valueOf(previousTransaction.getId()),
               "%" + toDeleteTransaction});
         }
      }
      if (data.size() > 0) {
         ConnectionHandler.runBatchUpdate(connection, UPDATE_TXS_DETAILS_COMMENT, data);
      }
      monitor.worked(calculateWork(workPercentage));
   }

   private void checkForModifiedBaselines(OseeConnection connection, boolean force, int queryId) throws OseeCoreException {
      int transaction_id =
         ConnectionHandler.runPreparedQueryFetchInt(connection, 0, TRANSACATION_GAMMA_IN_USE, queryId);
      if (transaction_id > 0 && !force) {
         throw new OseeCoreException(
            "The Transaction " + transaction_id + " holds a Gamma that is in use in other transactions.  In order to delete this Transaction you will need to select the force check box.\n\nNO TRANSACTIONS WERE DELETED.");
      }
   }

   private final static class TxDeleteInfo {
      private final TransactionRecord txToDelete;
      private final TransactionRecord previousTxFromTxToDelete;

      public TxDeleteInfo(TransactionRecord txToDelete, TransactionRecord previousTxFromTxToDelete) {
         super();
         this.txToDelete = txToDelete;
         this.previousTxFromTxToDelete = previousTxFromTxToDelete;
      }

      public TransactionRecord getTxToDelete() {
         return txToDelete;
      }

      public TransactionRecord getPreviousTx() {
         return previousTxFromTxToDelete;
      }
   }

}

Back to the top