Skip to main content
summaryrefslogtreecommitdiffstats
blob: feef2a63a7ac3f9ecf566391c49bfd5064ff0a7a (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
/*******************************************************************************
 * 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.core.datastore.cache;

import java.util.Collection;
import java.util.Date;
import org.eclipse.osee.framework.core.enums.TransactionDetailsType;
import org.eclipse.osee.framework.core.enums.TransactionVersion;
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.model.TransactionRecordFactory;
import org.eclipse.osee.framework.core.model.cache.BranchCache;
import org.eclipse.osee.framework.core.model.cache.ITransactionDataAccessor;
import org.eclipse.osee.framework.core.model.cache.TransactionCache;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.database.core.IdJoinQuery;
import org.eclipse.osee.framework.database.core.JoinUtility;
import org.eclipse.osee.framework.jdk.core.type.MutableInteger;

/**
 * @author Roberto E. Escobar
 */
public class DatabaseTransactionRecordAccessor implements ITransactionDataAccessor {

   private static final String SELECT_BASE_TRANSACTION =
      "select * from osee_tx_details where branch_id = ? and tx_type = ?";

   private static final String SELECT_BY_TRANSACTION = "select * from osee_tx_details WHERE transaction_id = ?";

   private static final String SELECT_HEAD_TRANSACTION =
      "select * from osee_tx_details where transaction_id = (select max(transaction_id) from osee_tx_details where branch_id = ?) and branch_id = ?";

   private static final String SELECT_TRANSACTIONS_BY_QUERY_ID =
      "select * from osee_join_id oji, osee_tx_details txd where oji.query_id = ? and txd.transaction_id = oji.id";

   private static final String SELECT_NON_EXISTING_TRANSACTIONS_BY_QUERY_ID =
      "select oji.id from osee_join_id oji where oji.query_id = ? and not exists (select 1 from osee_tx_details txd where txd.transaction_id = oji.id)";

   private static final String GET_PRIOR_TRANSACTION =
      "select max(transaction_id) FROM osee_tx_details where branch_id = ? and transaction_id < ?";

   private final IOseeDatabaseService oseeDatabaseService;
   private final BranchCache branchCache;
   private final TransactionRecordFactory factory;

   public DatabaseTransactionRecordAccessor(IOseeDatabaseService oseeDatabaseService, BranchCache branchCache, TransactionRecordFactory factory) {
      this.oseeDatabaseService = oseeDatabaseService;
      this.branchCache = branchCache;
      this.factory = factory;
   }

   private synchronized void ensureDependantCachePopulated() throws OseeCoreException {
      branchCache.ensurePopulated();
   }

   @Override
   public void loadTransactionRecord(TransactionCache cache, Collection<Integer> transactionIds) throws OseeCoreException {
      if (transactionIds.isEmpty()) {
         return;
      }
      ensureDependantCachePopulated();
      if (transactionIds.size() > 1) {
         IdJoinQuery joinQuery = JoinUtility.createIdJoinQuery();
         try {
            for (Integer txNumber : transactionIds) {
               joinQuery.add(txNumber);
            }
            joinQuery.store();

            loadTransactions(cache, transactionIds.size(), SELECT_TRANSACTIONS_BY_QUERY_ID, joinQuery.getQueryId());

         } finally {
            joinQuery.delete();
         }
      } else {
         loadTransaction(cache, SELECT_BY_TRANSACTION, transactionIds.iterator().next());
      }
   }

   @Override
   public TransactionRecord loadTransactionRecord(TransactionCache cache, Branch branch, TransactionVersion transactionType) throws OseeCoreException {
      ensureDependantCachePopulated();
      TransactionRecord toReturn = null;
      switch (transactionType) {
         case BASE:
            toReturn =
               loadTransaction(cache, SELECT_BASE_TRANSACTION, branch.getId(), TransactionDetailsType.Baselined);
            break;
         case HEAD:
            toReturn = loadTransaction(cache, SELECT_HEAD_TRANSACTION, branch.getId(), branch.getId());
            break;
         default:
            throw new OseeStateException("Transaction Type [%s] is not supported", transactionType);
      }
      return toReturn;
   }

   private void loadTransactions(TransactionCache cache, int expectedCount, String query, int queryId) throws OseeCoreException {
      MutableInteger numberLoaded = new MutableInteger(-1);
      loadFromTransaction(cache, expectedCount, numberLoaded, query, queryId);

      if (numberLoaded.getValue() != expectedCount) {
         IOseeStatement chStmt = oseeDatabaseService.getStatement();
         try {
            chStmt.runPreparedQuery(expectedCount, SELECT_NON_EXISTING_TRANSACTIONS_BY_QUERY_ID, queryId);
            while (chStmt.next()) {
               int transactionNumber = chStmt.getInt("id");
               factory.getOrCreate(cache, transactionNumber);
            }
         } finally {
            chStmt.close();
         }
      }
   }

   private TransactionRecord loadTransaction(TransactionCache cache, String query, Object... parameters) throws OseeCoreException {
      return loadFromTransaction(cache, 1, new MutableInteger(0), query, parameters);
   }

   private TransactionRecord loadFromTransaction(TransactionCache cache, int expectedCount, MutableInteger numberLoaded, String query, Object... parameters) throws OseeCoreException {
      IOseeStatement chStmt = oseeDatabaseService.getStatement();
      TransactionRecord record = null;
      int count = 0;
      try {
         chStmt.runPreparedQuery(expectedCount, query, parameters);
         while (chStmt.next()) {
            count++;
            int branchId = chStmt.getInt("branch_id");
            int transactionNumber = chStmt.getInt("transaction_id");
            String comment = chStmt.getString("osee_comment");
            Date timestamp = chStmt.getTimestamp("time");
            int authorArtId = chStmt.getInt("author");
            int commitArtId = chStmt.getInt("commit_art_id");
            TransactionDetailsType txType = TransactionDetailsType.toEnum(chStmt.getInt("tx_type"));

            record =
               prepareTransactionRecord(cache, transactionNumber, branchId, comment, timestamp, authorArtId,
                  commitArtId, txType);
         }
         numberLoaded.setValue(count);
      } finally {
         chStmt.close();
      }
      return record;
   }

   private TransactionRecord prepareTransactionRecord(TransactionCache cache, int transactionNumber, int branchId, String comment, Date timestamp, int authorArtId, int commitArtId, TransactionDetailsType txType) throws OseeCoreException {
      TransactionRecord record =
         factory.createOrUpdate(cache, transactionNumber, branchId, comment, timestamp, authorArtId, commitArtId,
            txType);
      record.setBranchCache(branchCache);
      record.clearDirty();
      return record;
   }

   @SuppressWarnings("unused")
   @Override
   public void load(TransactionCache transactionCache) throws OseeCoreException {
      // Not implemented
   }

   @Override
   public TransactionRecord getOrLoadPriorTransaction(TransactionCache cache, int transactionNumber, int branchId) throws OseeCoreException {
      int priorTransactionId =
         oseeDatabaseService.runPreparedQueryFetchObject(-1, GET_PRIOR_TRANSACTION, branchId, transactionNumber);
      return cache.getOrLoad(priorTransactionId);
   }
}

Back to the top