Skip to main content
summaryrefslogtreecommitdiffstats
blob: ce0159f5275da7418ea1e8178e9e3f9fdbdea1e9 (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
/*******************************************************************************
 * Copyright (c) 2009 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.internal.accessors;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.osee.framework.core.data.OseeServerContext;
import org.eclipse.osee.framework.core.enums.CacheOperation;
import org.eclipse.osee.framework.core.enums.CoreTranslatorId;
import org.eclipse.osee.framework.core.enums.TransactionVersion;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.message.CacheUpdateRequest;
import org.eclipse.osee.framework.core.message.TransactionCacheUpdateResponse;
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.core.ConnectionHandler;
import org.eclipse.osee.framework.skynet.core.artifact.HttpClientMessage;

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

   private static final String GET_PRIOR_TRANSACTION =
      "select transaction_id FROM osee_tx_details where branch_id = ? and transaction_id < ? order by transaction_id desc";

   private final TransactionRecordFactory txFactory;
   private final BranchCache branchCache;

   public ClientTransactionAccessor(TransactionRecordFactory txFactory, BranchCache branchCache) {
      super();
      this.txFactory = txFactory;
      this.branchCache = branchCache;
   }

   @Override
   public void loadTransactionRecord(TransactionCache cache, Collection<Integer> transactionIds) throws OseeCoreException {
      CacheUpdateRequest request = new CacheUpdateRequest(cache.getCacheId(), transactionIds);
      requestUpdateMessage(cache, request);

   }

   @Override
   public void loadTransactionRecord(TransactionCache cache, Branch branch) {
      // provided for subclass implementation
   }

   @Override
   public TransactionRecord loadTransactionRecord(TransactionCache cache, Branch branch, TransactionVersion transactionType) {
      return null;
   }

   @Override
   public void load(TransactionCache transactionCache) throws OseeCoreException {
      requestUpdateMessage(transactionCache, new CacheUpdateRequest(transactionCache.getCacheId()));
   }

   protected void requestUpdateMessage(TransactionCache cache, CacheUpdateRequest updateRequest) throws OseeCoreException {
      Map<String, String> parameters = new HashMap<String, String>();
      parameters.put("function", CacheOperation.UPDATE.name());

      TransactionCacheUpdateResponse response =
         HttpClientMessage.send(OseeServerContext.CACHE_CONTEXT, parameters,
            CoreTranslatorId.OSEE_CACHE_UPDATE_REQUEST, updateRequest, CoreTranslatorId.TX_CACHE_UPDATE_RESPONSE);
      for (TransactionRecord row : response.getTxRows()) {
         TransactionRecord record =
            txFactory.createOrUpdate(cache, row.getId(), row.getBranchId(), row.getComment(), row.getTimeStamp(),
               row.getAuthor(), row.getCommit(), row.getTxType());
         record.setBranchCache(branchCache);
         record.clearDirty();
      }
   }

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

Back to the top