Skip to main content
summaryrefslogtreecommitdiffstats
blob: a63102c55001239c260f092bf80da53ecfa99c98 (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
/*******************************************************************************
 * 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.message.internal.translation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.eclipse.osee.framework.core.enums.TransactionDetailsType;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.message.TransactionCacheUpdateResponse;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.core.model.TransactionRecordFactory;
import org.eclipse.osee.framework.core.translation.ITranslator;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;

/**
 * @author Roberto E. Escobar
 * @author Jeff C. Phillips
 */
public class TransactionCacheUpdateResponseTranslator implements ITranslator<TransactionCacheUpdateResponse> {

   private static enum Fields {
      TX_COUNT,
      TX_ROW,
      TX_TO_BRANCH;
   }

   private final TransactionRecordFactory txRecordFactory;

   public TransactionCacheUpdateResponseTranslator(TransactionRecordFactory txRecordFactory) {
      this.txRecordFactory = txRecordFactory;
   }

   private TransactionRecordFactory getFactory() {
      return txRecordFactory;
   }

   @Override
   public TransactionCacheUpdateResponse convert(PropertyStore store) throws OseeCoreException {
      TransactionRecordFactory factory = getFactory();
      List<TransactionRecord> rows = new ArrayList<TransactionRecord>();
      int rowCount = store.getInt(Fields.TX_COUNT.name());
      for (int index = 0; index < rowCount; index++) {
         String[] rowData = store.getArray(createKey(Fields.TX_ROW, index));
         rows.add(fromArray(factory, rowData));
      }
      return new TransactionCacheUpdateResponse(rows);
   }

   @Override
   public PropertyStore convert(TransactionCacheUpdateResponse object) {
      PropertyStore store = new PropertyStore();
      List<TransactionRecord> rows = object.getTxRows();
      for (int index = 0; index < rows.size(); index++) {
         TransactionRecord row = rows.get(index);
         store.put(createKey(Fields.TX_ROW, index), toArray(row));
      }
      store.put(Fields.TX_COUNT.name(), rows.size());
      return store;
   }

   private String createKey(Fields key, int index) {
      return String.format("%s_%s", key.name(), index);
   }

   private String[] toArray(TransactionRecord row) {
      return new String[] {
         String.valueOf(row.getId()),
         row.getTxType().name(),
         row.getComment(),
         String.valueOf(row.getTimeStamp().getTime()),
         String.valueOf(row.getAuthor()),
         String.valueOf(row.getCommit()),
         String.valueOf(row.getBranchId())};
   }

   private static TransactionRecord fromArray(TransactionRecordFactory factory, String[] data) throws OseeCoreException {
      int index = 0;
      int txId = Integer.valueOf(data[index++]);
      TransactionDetailsType txType = TransactionDetailsType.valueOf(data[index++]);
      String comment = data[index++];
      Date timeStamp = new Date(Long.valueOf(data[index++]));
      int authorArtId = Integer.valueOf(data[index++]);
      int commitArtId = Integer.valueOf(data[index++]);
      int branchId = Integer.valueOf(data[index++]);
      return factory.create(txId, branchId, comment, timeStamp, authorArtId, commitArtId, txType);
   }
}

Back to the top