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

import java.util.Collection;
import org.eclipse.osee.framework.core.enums.ModificationType;
import org.eclipse.osee.framework.core.enums.TxChange;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.database.core.OseeSql;
import org.eclipse.osee.framework.skynet.core.event.ArtifactTransactionModifiedEvent;
import org.eclipse.osee.framework.skynet.core.event.msgs.TransactionEvent;

/**
 * @author Jeff C. Phillips
 * @author Roberto E. Escobar
 */
public abstract class BaseTransactionData {
   private static final String INSERT_INTO_TRANSACTION_TABLE =
         "INSERT INTO osee_txs (transaction_id, gamma_id, mod_type, tx_current, branch_id) VALUES (?, ?, ?, ?, ?)";

   private static final int PRIME_NUMBER = 37;
   private final int itemId;
   private ModificationType modificationType;
   private Integer gammaId;

   public BaseTransactionData(int itemId, ModificationType modificationType) {
      this.modificationType = modificationType;
      this.itemId = itemId;
   }

   protected boolean useExistingBackingData() {
      switch (modificationType) {
         case ARTIFACT_DELETED:
         case DELETED:
         case INTRODUCED:
            return true;
      }
      return false;
   }

   @Override
   public boolean equals(Object obj) {
      if (obj instanceof BaseTransactionData) {
         BaseTransactionData data = (BaseTransactionData) obj;
         return data.itemId == this.itemId && data.getClass().equals(this.getClass());
      }
      return false;
   }

   @Override
   public int hashCode() {
      return itemId * PRIME_NUMBER * this.getClass().hashCode();
   }

   protected void addInsertToBatch(SkynetTransaction transaction) throws OseeCoreException {
      internalAddInsertToBatch(transaction, Integer.MAX_VALUE, INSERT_INTO_TRANSACTION_TABLE,
            transaction.getTransactionNumber(), getGammaId(), getModificationType().getValue(), TxChange.getCurrent(
                  getModificationType()).getValue(), transaction.getBranch().getId());
   }

   protected final int getItemId() {
      return itemId;
   }

   protected final ModificationType getModificationType() {
      return modificationType;
   }

   protected final int getGammaId() throws OseeCoreException {
      if (gammaId == null) {
         gammaId = createGammaId();
      }
      return gammaId;
   }

   final void setModificationType(ModificationType modificationType) {
      this.modificationType = modificationType;
   }

   protected abstract OseeSql getSelectTxNotCurrentSql();

   /**
    * Should be called by child classes during their implementation of addInsertToBatch.
    */
   protected final void internalAddInsertToBatch(SkynetTransaction transaction, int insertPriority, String insertSql, Object... data) {
      transaction.internalAddInsertToBatch(insertPriority, insertSql, data);
   }

   /**
    * Should not be called by application. This should only be called once after the transaction has been committed.
    */
   protected abstract void internalUpdate(TransactionRecord transactionId) throws OseeCoreException;

   /**
    * Should not be called by application. This should only be called once after the transaction has been committed.
    */
   protected abstract void internalClearDirtyState();

   /**
    * Should not be called by application. This should only be called once if there was an error committing the
    * transaction.
    */
   protected abstract void internalOnRollBack() throws OseeCoreException;

   /**
    * Should not be called by application. This method will be called by the base class when required;
    */
   protected abstract int createGammaId() throws OseeCoreException;

   /**
    * Should not be called by application. This should only be called once after the transaction has been committed.
    * 
    * @param transactionEvent TODO
    */
   protected abstract void internalAddToEvents(Collection<ArtifactTransactionModifiedEvent> events, TransactionEvent transactionEvent) throws OseeCoreException;
}

Back to the top