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

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.osee.framework.branch.management.exchange.TranslationManager;
import org.eclipse.osee.framework.branch.management.internal.Activator;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.OseeConnection;
import org.eclipse.osee.framework.database.core.SupportedDatabase;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.resource.management.Options;

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

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

   private OseeConnection connection;
   private MetaData metadata;
   private TranslationManager translator;
   private Options options;
   private final IOseeDatabaseService service;

   protected BaseDbSaxHandler(IOseeDatabaseService 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.service = service;
      this.options = new Options();
      this.translator = null;
      this.metadata = null;
      this.connection = null;
      this.isCacheAll = isCacheAll;
      this.cacheLimit = cacheLimit;
      this.data = new ArrayList<Object[]>();
   }

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

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

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

   public void setConnection(OseeConnection connection) {
      this.connection = connection;
   }

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

   protected OseeConnection getConnection() {
      return this.connection;
   }

   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);
   }

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

   private boolean isTruncateSupported() throws OseeCoreException {
      boolean isTruncateSupported = false;
      DatabaseMetaData metaData = connection.getMetaData();
      if (!SupportedDatabase.isDatabaseType(metaData, SupportedDatabase.derby)) {
         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) {
            OseeLog.log(Activator.class, Level.INFO, ex1);
         } finally {
            if (resultSet != null) {
               try {
                  resultSet.close();
               } catch (SQLException ex) {
                  // Do Nothing
               }
            }
         }
      }
      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(connection, deleteSql);
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, Level.INFO, ex, "Error clearing: %s", deleteSql);
         throw ex;
      }
   }

   protected IOseeDatabaseService getDatabaseService() {
      return service;
   }

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

Back to the top