Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6f70f8d8f3cdfdb37bc8d77843cbdac7975a6b27 (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
/*******************************************************************************
 * 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.branch.management.exchange;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.osee.framework.branch.management.ExportOptions;
import org.eclipse.osee.framework.branch.management.exchange.export.AbstractExportItem;
import org.eclipse.osee.framework.branch.management.exchange.export.DbTableExportItem;
import org.eclipse.osee.framework.branch.management.exchange.export.ManifestExportItem;
import org.eclipse.osee.framework.branch.management.exchange.export.MetadataExportItem;
import org.eclipse.osee.framework.branch.management.exchange.handler.ExportItem;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.IOseeSequence;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.resource.management.Options;

/**
 * @author Roberto E. Escobar
 */
public final class ExchangeDb {

   public static final String GAMMA_ID = "gamma_id";
   public static final String TRANSACTION_ID = "transaction_id";
   public static final String ATTRIBUTE_ID = "attr_id";
   public static final String ARTIFACT_ID = "art_id";
   public static final String RELATION_ID = "rel_link_id";
   public static final String BRANCH_ID = "branch_id";
   public static final String CONFLICT_ID = "conflict_id";
   public static final String CONFLICT_TYPE = "conflict_type";
   public static final String TYPE_GUID = "type_guid";

   private static final String[] BRANCH_ID_NEG_ONE_ALIASES = new String[] {"parent_branch_id"};

   private static final String[] BRANCH_ID_REG_ALIASES = new String[] {
      "mapped_branch_id",
      "source_branch_id",
      "merge_branch_id",
      "dest_branch_id"};

   private static final String[] ARTIFACT_ID_NEG_ONE_ALIASES = new String[] {
      "commit_art_id",
      "associated_art_id",
      "author"};

   private static final String[] ARTIFACT_ID_REG_ALIASES = new String[] {"a_art_id", "b_art_id", "privilege_entity_id"};

   private static final String[] GAMMA_ID_REG_ALIASES = new String[] {"source_gamma_id", "dest_gamma_id"};

   private static final String[] TRANSACTION_ID_REG_ALIASES = new String[0];
   private static final String[] TRANSACTION_ID_NEG_ONE_ALIASES = new String[] {"commit_transaction_id"};

   private static final String[] ARTIFACT_ID_ALIASES;
   private static final String[] BRANCH_ID_ALIASES;
   private static final String[] GAMMA_ID_ALIASES;
   private static final String[] TRANSACTION_ID_ALIASES;
   static {
      Set<String> artIdAliases = new HashSet<String>();
      artIdAliases.add(ARTIFACT_ID);
      artIdAliases.addAll(Arrays.asList(ARTIFACT_ID_REG_ALIASES));
      artIdAliases.addAll(Arrays.asList(ARTIFACT_ID_NEG_ONE_ALIASES));
      ARTIFACT_ID_ALIASES = artIdAliases.toArray(new String[artIdAliases.size()]);

      Set<String> branchIdAliases = new HashSet<String>();
      branchIdAliases.add(BRANCH_ID);
      branchIdAliases.addAll(Arrays.asList(BRANCH_ID_REG_ALIASES));
      branchIdAliases.addAll(Arrays.asList(BRANCH_ID_NEG_ONE_ALIASES));
      BRANCH_ID_ALIASES = branchIdAliases.toArray(new String[branchIdAliases.size()]);

      Set<String> gammaIdAliases = new HashSet<String>();
      gammaIdAliases.add(GAMMA_ID);
      gammaIdAliases.addAll(Arrays.asList(GAMMA_ID_REG_ALIASES));
      GAMMA_ID_ALIASES = gammaIdAliases.toArray(new String[gammaIdAliases.size()]);

      Set<String> txIdAliases = new HashSet<String>();
      txIdAliases.add(TRANSACTION_ID);
      txIdAliases.addAll(Arrays.asList(TRANSACTION_ID_REG_ALIASES));
      txIdAliases.addAll(Arrays.asList(TRANSACTION_ID_NEG_ONE_ALIASES));
      TRANSACTION_ID_ALIASES = txIdAliases.toArray(new String[txIdAliases.size()]);
   }

   public static final String GET_MAX_TX =
      "SELECT last_sequence FROM osee_sequence WHERE sequence_name = '" + IOseeSequence.TRANSACTION_ID_SEQ + "'";

   private static final String BRANCH_TABLE_QUERY =
      "SELECT br1.* FROM osee_branch br1, osee_join_export_import jex1 WHERE br1.branch_id = jex1.id1 AND jex1.query_id=? ORDER BY br1.branch_id";

   private static final String TX_DETAILS_TABLE_QUERY =
      "SELECT txd1.TRANSACTION_ID, txd1.TIME, txd1.AUTHOR, txd1.OSEE_COMMENT, txd1.BRANCH_ID, txd1.COMMIT_ART_ID, txd1.TX_TYPE FROM osee_tx_details txd1, osee_join_export_import jex1 WHERE txd1.branch_id = jex1.id1 AND jex1.query_id=? %s ORDER BY txd1.transaction_id";

   private static final String TXS_ARCHIVE_TABLE_QUERY =
      "SELECT txs1.GAMMA_ID, txs1.TRANSACTION_ID, txs1.TX_CURRENT, txs1.MOD_TYPE, txs1.BRANCH_ID FROM osee_txs_archived txs1, osee_tx_details txd1, osee_join_export_import jex1 WHERE txs1.transaction_id = txd1.transaction_id AND txs1.branch_id = jex1.id1 AND jex1.query_id=? %s";

   private static final String TXS_TABLE_QUERY =
      "SELECT txs1.GAMMA_ID, txs1.TRANSACTION_ID, txs1.TX_CURRENT, txs1.MOD_TYPE, txs1.BRANCH_ID FROM osee_txs txs1, osee_tx_details txd1, osee_join_export_import jex1 WHERE txs1.transaction_id = txd1.transaction_id AND txs1.branch_id = jex1.id1 AND jex1.query_id=? %s";

   private static final String ARTIFACT_TABLE_QUERY =
      "SELECT DISTINCT (art1.GAMMA_ID), art1.art_id, art1.GUID, art1.HUMAN_READABLE_ID, art1.ART_TYPE_ID FROM osee_artifact art1, osee_txs txs1, osee_tx_details txd1, osee_join_export_import jex1 WHERE art1.gamma_id = txs1.gamma_id AND txs1.transaction_id = txd1.transaction_id AND txs1.branch_id = jex1.id1 AND jex1.query_id=? %s";

   private static final String ATTRIBUTE_TABLE_QUERY =
      "SELECT DISTINCT (attr1.GAMMA_ID), attr1.ATTR_ID, attr1.ART_ID, attr1.VALUE, attr1.ATTR_TYPE_ID, attr1.URI FROM osee_attribute attr1, osee_txs txs1, osee_tx_details txd1, osee_join_export_import jex1 WHERE attr1.gamma_id = txs1.gamma_id AND txs1.transaction_id = txd1.transaction_id AND txs1.branch_id = jex1.id1 AND jex1.query_id=? %s";

   private static final String RELATION_LINK_TABLE_QUERY =
      "SELECT DISTINCT (rel1.GAMMA_ID), rel1.REL_LINK_ID, rel1.B_ART_ID, rel1.A_ART_ID, rel1.RATIONALE, rel1.REL_LINK_TYPE_ID FROM osee_relation_link rel1, osee_txs txs1, osee_tx_details txd1, osee_join_export_import jex1 WHERE rel1.gamma_id = txs1.gamma_id AND txs1.transaction_id = txd1.transaction_id AND txs1.branch_id = jex1.id1 AND jex1.query_id=? %s";

   private static final String MERGE_TABLE_QUERY =
      "SELECT om1.* FROM osee_merge om1, osee_join_export_import jex1 WHERE om1.merge_branch_id = jex1.id1 AND jex1.query_id=? %s";

   private static final String CONFLICT_TABLE_QUERY =
      "SELECT oc1.* FROM osee_conflict oc1, osee_merge om1, osee_join_export_import jex1 WHERE oc1.merge_branch_id = om1.merge_branch_id AND om1.merge_branch_id = jex1.id1 AND jex1.query_id=? %s";

   private static final String ARTIFACT_ACL_QUERY =
      "SELECT oaa1.* FROM osee_artifact_acl oaa1, osee_join_export_import jex1 WHERE oaa1.branch_id = jex1.id1 AND jex1.query_id=? ORDER BY oaa1.branch_id";

   private static final String BRANCH_ACL_QUERY =
      "SELECT oba1.* FROM osee_branch_acl oba1, osee_join_export_import jex1 WHERE oba1.branch_id = jex1.id1 AND jex1.query_id=? ORDER BY oba1.branch_id";

   static List<AbstractExportItem> createTaskList(OseeServices services) throws OseeCoreException {
      List<AbstractExportItem> items = new ArrayList<AbstractExportItem>();
      items.add(new ManifestExportItem(items));
      items.add(new MetadataExportItem(items, services.getDatabaseService().getConnection().getMetaData()));
      items.add(new DbTableExportItem(services, ExportItem.OSEE_BRANCH_DATA, BRANCH_TABLE_QUERY));
      items.add(new DbTableExportItem(services, ExportItem.OSEE_TX_DETAILS_DATA, TX_DETAILS_TABLE_QUERY));
      items.add(new DbTableExportItem(services, ExportItem.OSEE_TXS_DATA, TXS_TABLE_QUERY));
      items.add(new DbTableExportItem(services, ExportItem.OSEE_TXS_ARCHIVED_DATA, TXS_ARCHIVE_TABLE_QUERY));
      items.add(new DbTableExportItem(services, ExportItem.OSEE_ARTIFACT_DATA, ARTIFACT_TABLE_QUERY));
      items.add(new DbTableExportItem(services, ExportItem.OSEE_ATTRIBUTE_DATA, ATTRIBUTE_TABLE_QUERY));
      items.add(new DbTableExportItem(services, ExportItem.OSEE_RELATION_LINK_DATA, RELATION_LINK_TABLE_QUERY));
      items.add(new DbTableExportItem(services, ExportItem.OSEE_MERGE_DATA, MERGE_TABLE_QUERY));
      items.add(new DbTableExportItem(services, ExportItem.OSEE_CONFLICT_DATA, CONFLICT_TABLE_QUERY));
      items.add(new DbTableExportItem(services, ExportItem.OSEE_BRANCH_ACL_DATA, BRANCH_ACL_QUERY));
      items.add(new DbTableExportItem(services, ExportItem.OSEE_ARTIFACT_ACL_DATA, ARTIFACT_ACL_QUERY));

      return items;
   }

   static List<IndexCollector> createCheckList() {
      List<IndexCollector> items = new ArrayList<IndexCollector>();
      items.add(new IndexCollector(ExportItem.OSEE_TXS_DATA, GAMMA_ID, GAMMA_ID_REG_ALIASES));
      items.add(new IndexCollector(ExportItem.OSEE_TXS_ARCHIVED_DATA, GAMMA_ID, GAMMA_ID_REG_ALIASES));
      items.add(new IndexCollector(ExportItem.OSEE_TX_DETAILS_DATA, TRANSACTION_ID, TRANSACTION_ID_REG_ALIASES,
         TRANSACTION_ID_NEG_ONE_ALIASES));
      items.add(new IndexCollector(ExportItem.OSEE_ARTIFACT_DATA, ARTIFACT_ID, ARTIFACT_ID_REG_ALIASES,
         ARTIFACT_ID_NEG_ONE_ALIASES));
      items.add(new IndexCollector(ExportItem.OSEE_ATTRIBUTE_DATA, ATTRIBUTE_ID));
      items.add(new IndexCollector(ExportItem.OSEE_RELATION_LINK_DATA, RELATION_ID));
      items.add(new IndexCollector(ExportItem.OSEE_BRANCH_DATA, BRANCH_ID, BRANCH_ID_REG_ALIASES,
         BRANCH_ID_NEG_ONE_ALIASES));
      return items;
   }

   static List<IdTranslator> createTranslators(IOseeDatabaseService service) {
      List<IdTranslator> translators = new ArrayList<IdTranslator>();
      translators.add(new IdTranslator(service, IOseeSequence.GAMMA_ID_SEQ, GAMMA_ID_ALIASES));
      translators.add(new IdTranslator(service, IOseeSequence.TRANSACTION_ID_SEQ, TRANSACTION_ID_ALIASES));
      translators.add(new IdTranslator(service, IOseeSequence.BRANCH_ID_SEQ, BRANCH_ID_ALIASES));
      translators.add(new IdTranslator(service, IOseeSequence.ART_ID_SEQ, ARTIFACT_ID_ALIASES));
      translators.add(new IdTranslator(service, IOseeSequence.ATTR_ID_SEQ, ATTRIBUTE_ID));
      translators.add(new IdTranslator(service, IOseeSequence.REL_LINK_ID_SEQ, RELATION_ID));
      return translators;
   }

   public static Pair<String, Object[]> getQueryWithOptions(String originalQuery, int queryId, Options options) throws Exception {
      if (originalQuery.contains("%s")) {
         if (originalQuery.contains("osee_tx_details") || originalQuery.contains("osee_merge")) {
            List<Object> dataArray = new ArrayList<Object>();
            dataArray.add(queryId);
            StringBuilder optionString = new StringBuilder();
            if (options.getBoolean(ExportOptions.EXCLUDE_BASELINE_TXS.name()) && originalQuery.contains("txd1")) {
               optionString.append(" AND txd1.TX_TYPE = 0");
            }

            long minTxs = getMinTransaction(options);
            long maxTxs = getMaxTransaction(options);

            if (minTxs != Long.MIN_VALUE) {
               if (originalQuery.contains("om1")) {
                  optionString.append(" AND om1.commit_transaction_id >= ?");
                  dataArray.add(minTxs);
               } else if (originalQuery.contains("txs1")) {
                  optionString.append(" AND txd1.transaction_id >= ?");
                  dataArray.add(minTxs);
               }
            }

            if (maxTxs != Long.MIN_VALUE) {
               if (originalQuery.contains("om1")) {
                  optionString.append(" AND om1.commit_transaction_id <= ?");
                  dataArray.add(maxTxs);
               } else if (originalQuery.contains("txs1")) {
                  optionString.append(" AND txd1.transaction_id <= ?");
                  dataArray.add(maxTxs);
               }
            }
            if (minTxs > maxTxs) {
               throw new OseeArgumentException("Invalid transaction range: min - %d >  max - %d", minTxs, maxTxs);
            }

            return new Pair<String, Object[]>(String.format(originalQuery, optionString),
               dataArray.toArray(new Object[dataArray.size()]));
         }
      }
      return new Pair<String, Object[]>(originalQuery, new Object[] {queryId});
   }

   static Long getMaxTransaction(Options options) {
      return getTransactionNumber(options, ExportOptions.MAX_TXS.name());
   }

   static Long getMinTransaction(Options options) {
      return getTransactionNumber(options, ExportOptions.MIN_TXS.name());
   }

   private static Long getTransactionNumber(Options options, String exportOption) {
      String transactionNumber = options.get(exportOption);
      long toReturn = Long.MIN_VALUE;
      if (Strings.isValid(transactionNumber)) {
         toReturn = Long.valueOf(transactionNumber);
      }
      return toReturn;
   }

   private ExchangeDb() {
      // utility class
   }
}

Back to the top