Skip to main content
summaryrefslogtreecommitdiffstats
blob: bb597d7a839a1efb91307ffae2df500d7b6118aa (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
/*******************************************************************************
 * 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.orcs.db.internal.exchange;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.resource.management.IResourceManager;
import org.eclipse.osee.jdbc.JdbcClient;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.ExportOptions;
import org.eclipse.osee.orcs.OrcsTypes;
import org.eclipse.osee.orcs.core.SystemPreferences;
import org.eclipse.osee.orcs.db.internal.IdentityManager;
import org.eclipse.osee.orcs.db.internal.exchange.export.AbstractExportItem;
import org.eclipse.osee.orcs.db.internal.exchange.export.DbTableExportItem;
import org.eclipse.osee.orcs.db.internal.exchange.export.ManifestExportItem;
import org.eclipse.osee.orcs.db.internal.exchange.export.MetadataExportItem;
import org.eclipse.osee.orcs.db.internal.exchange.export.OseeTypeModelExportItem;
import org.eclipse.osee.orcs.db.internal.exchange.handler.ExportItem;

public class ExportItemFactory {
   private static final String GET_MAX_TX =
      "SELECT last_sequence FROM osee_sequence WHERE sequence_name = '" + IdentityManager.TRANSACTION_ID_SEQ + "'";

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

   private static final String TX_DETAILS_TABLE_QUERY =
      "SELECT txd.* FROM osee_join_export_import jex, osee_tx_details txd WHERE jex.query_id=? AND jex.id1=txd.branch_id";

   private static final String TXS_TABLE_QUERY =
      "SELECT txs.* FROM osee_join_export_import jex, osee_txs txs WHERE jex.query_id=? AND jex.id1=txs.branch_id";

   private static final String TXS_ARCHIVE_TABLE_QUERY = TXS_TABLE_QUERY.replace("osee_txs", "osee_txs_archived");

   private static final String ARTIFACT_TABLE_QUERY =
      "SELECT item.* FROM osee_join_id oji, osee_artifact item WHERE oji.query_id = ? AND oji.id = item.gamma_id";

   private static final String ATTRIBUTE_TABLE_QUERY = ARTIFACT_TABLE_QUERY.replace("osee_artifact", "osee_attribute");

   private static final String RELATION_LINK_TABLE_QUERY =
      ARTIFACT_TABLE_QUERY.replace("osee_artifact", "osee_relation_link");

   private static final String MERGE_TABLE_QUERY =
      "SELECT om.* FROM osee_join_export_import jex, osee_merge om WHERE jex.query_id=? AND jex.id1=om.merge_branch_id ORDER BY om.merge_branch_id";

   private static final String CONFLICT_TABLE_QUERY =
      "SELECT oc.* FROM osee_join_export_import jex, osee_merge om, osee_conflict oc WHERE jex.query_id=? AND jex.id1=om.merge_branch_id AND om.merge_branch_id=oc.merge_branch_id";

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

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

   private final Log logger;
   private final SystemPreferences preferences;
   private final JdbcClient jdbcClient;
   private final IResourceManager resourceManager;
   private final OrcsTypes orcsTypes;

   public ExportItemFactory(Log logger, SystemPreferences preferences, JdbcClient jdbcClient, IResourceManager resourceManager, OrcsTypes orcsTypes) {
      this.logger = logger;
      this.preferences = preferences;
      this.jdbcClient = jdbcClient;
      this.resourceManager = resourceManager;
      this.orcsTypes = orcsTypes;
   }

   public Log getLogger() {
      return logger;
   }

   public JdbcClient getDbService() {
      return jdbcClient;
   }

   public IResourceManager getResourceManager() {
      return resourceManager;
   }

   private OrcsTypes getOrcsTypes() {
      return orcsTypes;
   }

   public List<AbstractExportItem> createTaskList(int joinId, PropertyStore options) throws OseeCoreException {
      List<AbstractExportItem> items = new ArrayList<>();

      processTxOptions(options);

      int gammaJoinId = createGammaJoin(getDbService(), joinId, options);

      items.add(new ManifestExportItem(logger, preferences, items, options));
      items.add(new MetadataExportItem(logger, items, getDbService()));
      items.add(new OseeTypeModelExportItem(logger, getOrcsTypes()));

      addItem(items, joinId, options, gammaJoinId, ExportItem.OSEE_BRANCH_DATA, BRANCH_TABLE_QUERY);
      addItem(items, joinId, options, gammaJoinId, ExportItem.OSEE_TX_DETAILS_DATA, TX_DETAILS_TABLE_QUERY);
      addItem(items, joinId, options, gammaJoinId, ExportItem.OSEE_TXS_DATA, TXS_TABLE_QUERY);
      addItem(items, joinId, options, gammaJoinId, ExportItem.OSEE_TXS_ARCHIVED_DATA, TXS_ARCHIVE_TABLE_QUERY);
      addItem(items, joinId, options, gammaJoinId, ExportItem.OSEE_ARTIFACT_DATA, ARTIFACT_TABLE_QUERY);
      addItem(items, joinId, options, gammaJoinId, ExportItem.OSEE_ATTRIBUTE_DATA, ATTRIBUTE_TABLE_QUERY);
      addItem(items, joinId, options, gammaJoinId, ExportItem.OSEE_RELATION_LINK_DATA, RELATION_LINK_TABLE_QUERY);
      addItem(items, joinId, options, gammaJoinId, ExportItem.OSEE_MERGE_DATA, MERGE_TABLE_QUERY);
      addItem(items, joinId, options, gammaJoinId, ExportItem.OSEE_CONFLICT_DATA, CONFLICT_TABLE_QUERY);
      addItem(items, joinId, options, gammaJoinId, ExportItem.OSEE_BRANCH_ACL_DATA, BRANCH_ACL_QUERY);
      addItem(items, joinId, options, gammaJoinId, ExportItem.OSEE_ARTIFACT_ACL_DATA, ARTIFACT_ACL_QUERY);
      return items;
   }

   private void addItem(List<AbstractExportItem> items, int exportJoinId, PropertyStore options, int gammaJoinId, ExportItem exportItem, String query) throws OseeCoreException {
      StringBuilder modifiedQuery = new StringBuilder(query);
      Object[] bindData = prepareQuery(exportItem, modifiedQuery, options, exportJoinId, gammaJoinId);
      items.add(new DbTableExportItem(getLogger(), getDbService(), getResourceManager(), exportItem,
         modifiedQuery.toString(), bindData));
   }

   private void processTxOptions(PropertyStore options) throws OseeCoreException {
      long maxTx = getDbService().runPreparedQueryFetchObject(-1L, GET_MAX_TX);
      long userMaxTx = getMaxTransaction(options);
      if (userMaxTx == Long.MIN_VALUE || userMaxTx > maxTx) {
         options.put(ExportOptions.MAX_TXS.name(), Long.toString(maxTx));
      }
   }

   private int createGammaJoin(JdbcClient jdbcClient, int exportJoinId, PropertyStore options) throws OseeCoreException {
      List<Object> bindList = new ArrayList<>();
      int gammaJoinId = new Random().nextInt();
      StringBuilder sql = new StringBuilder(
         "INSERT INTO osee_join_id (id, query_id) SELECT DISTINCT(gamma_id), %s FROM osee_join_export_import jex, osee_txs txs WHERE jex.query_id=? AND jex.id1 = txs.branch_id");
      bindList.add(exportJoinId);
      addMaxMinFilter(sql, bindList, options);

      sql.append(
         " UNION SELECT DISTINCT(gamma_id), %s FROM osee_join_export_import jex, osee_txs_archived txs WHERE jex.query_id=? AND jex.id1 = txs.branch_id");
      bindList.add(exportJoinId);
      addMaxMinFilter(sql, bindList, options);

      Object[] bindData = bindList.toArray(new Object[bindList.size()]);
      String insert = String.format(sql.toString(), gammaJoinId, gammaJoinId);
      int itemsInserted = jdbcClient.runPreparedUpdate(insert, bindData);

      getLogger().info("Export join rows: [%s]", itemsInserted);

      return gammaJoinId;
   }

   private static Object[] prepareQuery(ExportItem exportItem, StringBuilder query, PropertyStore options, int exportJoinId, int gammaJionId) throws OseeCoreException {
      List<Object> bindData = new ArrayList<>();

      if (exportItem.matches(ExportItem.OSEE_ARTIFACT_DATA, ExportItem.OSEE_ATTRIBUTE_DATA,
         ExportItem.OSEE_RELATION_LINK_DATA)) {
         bindData.add(gammaJionId);
      } else {
         bindData.add(exportJoinId);
      }

      if (exportItem.matches(ExportItem.OSEE_TX_DETAILS_DATA, ExportItem.OSEE_TXS_DATA,
         ExportItem.OSEE_TXS_ARCHIVED_DATA)) {
         // this can not be accurately applied to osee_merge and osee_conflict because the best you can do is filter a the merge_branch level
         addMaxMinFilter(query, bindData, options);
      }

      if (exportItem.matches(ExportItem.OSEE_TX_DETAILS_DATA)) {
         // tx_details needs to be ordered so transactions are sequenced properly
         query.append(" ORDER BY transaction_id ASC");
      }
      return bindData.toArray(new Object[bindData.size()]);
   }

   private static void addMaxMinFilter(StringBuilder query, List<Object> bindData, PropertyStore options) throws OseeCoreException {
      long minTxs = getMinTransaction(options);
      long maxTxs = getMaxTransaction(options);

      if (minTxs > maxTxs) {
         throw new OseeArgumentException("Invalid transaction range: min - %d >  max - %d", minTxs, maxTxs);
      }

      if (minTxs != Long.MIN_VALUE) {
         query.append(" AND transaction_id >= ?");
         bindData.add(minTxs);
      }
      if (maxTxs != Long.MIN_VALUE) {
         query.append(" AND transaction_id <= ?");
         bindData.add(maxTxs);
      }
   }

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

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

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

Back to the top