Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3981ca66fceed63beb1cb2a490a13a952ae0c431 (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
/*******************************************************************************
 * 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.handler;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.jdbc.JdbcClient;
import org.eclipse.osee.jdbc.JdbcConnection;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.db.internal.exchange.TranslationManager;

/**
 * @author Roberto E. Escobar
 */
public abstract class BaseDbSaxHandler extends BaseExportImportSaxHandler {

   private final List<Object[]> data;
   private final int cacheLimit;
   private final boolean isCacheAll;

   private MetaData metadata;
   private TranslationManager translator;
   private PropertyStore options;
   private final JdbcClient service;
   private final Log logger;

   protected BaseDbSaxHandler(Log logger, JdbcClient service, boolean isCacheAll, int cacheLimit) {
      super();
      if (cacheLimit < 0) {
         throw new IllegalArgumentException(String.format("Cache limit cannot be less than zero - cacheLimit=[%d]",
            cacheLimit));
      }
      this.logger = logger;
      this.service = service;
      this.options = new PropertyStore();
      this.translator = null;
      this.metadata = null;
      this.isCacheAll = isCacheAll;
      this.cacheLimit = cacheLimit;
      this.data = new ArrayList<Object[]>();
   }

   protected Log getLogger() {
      return logger;
   }

   public void setOptions(PropertyStore options) {
      if (options != null) {
         this.options = options;
      }
   }

   protected PropertyStore getOptions() {
      return this.options;
   }

   public void setMetaData(MetaData metadata) {
      this.metadata = metadata;
   }

   public void setTranslator(TranslationManager translator) {
      this.translator = translator;
   }

   protected MetaData getMetaData() {
      return this.metadata;
   }

   protected TranslationManager getTranslator() {
      return this.translator;
   }

   public boolean isStorageNeeded() {
      return !isCacheAll && data.size() > cacheLimit;
   }

   protected void addData(Object[] objects) {
      this.data.add(objects);
   }

   public void store() throws OseeCoreException {
      store(null);
   }

   public void store(JdbcConnection connection) throws OseeCoreException {
      if (!data.isEmpty()) {
         getDatabaseService().runBatchUpdate(connection, getMetaData().getQuery(), data);
         data.clear();
      }
   }

   private boolean isTruncateSupported() throws OseeCoreException {
      boolean isTruncateSupported = false;
      JdbcConnection connection = service.getConnection();
      try {
         DatabaseMetaData metaData = connection.getMetaData();
         ResultSet resultSet = null;
         try {
            resultSet = metaData.getTablePrivileges(null, null, getMetaData().getTableName().toUpperCase());
            while (resultSet.next()) {
               String value = resultSet.getString("PRIVILEGE");
               if ("TRUNCATE".equalsIgnoreCase(value)) {
                  isTruncateSupported = true;
                  break;
               }
            }
         } catch (SQLException ex1) {
            logger.info(ex1, "Error determining truncate support");
         } finally {
            if (resultSet != null) {
               try {
                  resultSet.close();
               } catch (SQLException ex) {
                  // Do Nothing
               }
            }
         }
      } finally {
         connection.close();
      }
      return isTruncateSupported;
   }

   public void clearDataTable() throws OseeCoreException {
      String cmd = isTruncateSupported() ? "TRUNCATE TABLE" : "DELETE FROM";
      String deleteSql = String.format("%s %s", cmd, getMetaData().getTableName());
      try {
         getDatabaseService().runPreparedUpdate(deleteSql);
      } catch (OseeCoreException ex) {
         logger.info(ex, "Error clearing: %s", deleteSql);
         throw ex;
      }
   }

   protected JdbcClient getDatabaseService() {
      return service;
   }

   public void reset() {
      this.translator = null;
      this.options = null;
      this.metadata = null;
      this.data.clear();
   }
}

Back to the top