1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package openr66.database.data;
22
23 import goldengate.common.database.DbPreparedStatement;
24 import goldengate.common.database.DbSession;
25 import goldengate.common.database.data.AbstractDbData;
26 import goldengate.common.database.data.DbValue;
27 import goldengate.common.database.exception.GoldenGateDatabaseException;
28 import goldengate.common.database.exception.GoldenGateDatabaseNoConnectionException;
29 import goldengate.common.database.exception.GoldenGateDatabaseNoDataException;
30 import goldengate.common.database.exception.GoldenGateDatabaseSqlException;
31
32 import java.sql.Types;
33 import java.util.concurrent.ConcurrentHashMap;
34
35 import openr66.commander.CommanderNoDb;
36 import openr66.protocol.configuration.Configuration;
37
38
39
40
41
42
43
44 public class DbConfiguration extends AbstractDbData {
45 public static enum Columns {
46 READGLOBALLIMIT,
47 WRITEGLOBALLIMIT,
48 READSESSIONLIMIT,
49 WRITESESSIONLIMIT,
50 DELAYLIMIT,
51 UPDATEDINFO,
52 HOSTID
53 }
54
55 public static final int[] dbTypes = {
56 Types.BIGINT, Types.BIGINT, Types.BIGINT, Types.BIGINT,
57 Types.BIGINT, Types.INTEGER, Types.VARCHAR };
58
59 public static final String table = " CONFIGURATION ";
60
61
62
63
64 private static final ConcurrentHashMap<String, DbConfiguration> dbR66ConfigurationHashMap =
65 new ConcurrentHashMap<String, DbConfiguration>();
66
67 private String hostid;
68
69 private long readgloballimit;
70
71 private long writegloballimit;
72
73 private long readsessionlimit;
74
75 private long writesessionlimit;
76
77 private long delayllimit;
78
79 private int updatedInfo = UpdatedInfo.UNKNOWN.ordinal();
80
81
82
83 public static final int NBPRKEY = 1;
84
85 protected static final String selectAllFields = Columns.READGLOBALLIMIT
86 .name() +
87 "," +
88 Columns.WRITEGLOBALLIMIT.name() +
89 "," +
90 Columns.READSESSIONLIMIT.name() +
91 "," +
92 Columns.WRITESESSIONLIMIT.name() +
93 "," +
94 Columns.DELAYLIMIT.name() +
95 "," + Columns.UPDATEDINFO.name() + "," + Columns.HOSTID.name();
96
97 protected static final String updateAllFields = Columns.READGLOBALLIMIT
98 .name() +
99 "=?," +
100 Columns.WRITEGLOBALLIMIT.name() +
101 "=?," +
102 Columns.READSESSIONLIMIT.name() +
103 "=?," +
104 Columns.WRITESESSIONLIMIT.name() +
105 "=?," +
106 Columns.DELAYLIMIT.name() +
107 "=?," +
108 Columns.UPDATEDINFO.name() +
109 "=?";
110
111 protected static final String insertAllValues = " (?,?,?,?,?,?,?) ";
112
113
114
115
116
117 @Override
118 protected void initObject() {
119 primaryKey = new DbValue[]{new DbValue(hostid, Columns.HOSTID
120 .name())};
121 otherFields = new DbValue[]{
122 new DbValue(readgloballimit, Columns.READGLOBALLIMIT.name()),
123 new DbValue(writegloballimit, Columns.WRITEGLOBALLIMIT.name()),
124 new DbValue(readsessionlimit, Columns.READSESSIONLIMIT.name()),
125 new DbValue(writesessionlimit, Columns.WRITESESSIONLIMIT.name()),
126 new DbValue(delayllimit, Columns.DELAYLIMIT.name()),
127 new DbValue(updatedInfo, Columns.UPDATEDINFO.name()) };
128 allFields = new DbValue[]{
129 otherFields[0], otherFields[1], otherFields[2], otherFields[3],
130 otherFields[4], otherFields[5], primaryKey[0] };
131 }
132
133
134
135
136 @Override
137 protected String getSelectAllFields() {
138 return selectAllFields;
139 }
140
141
142
143
144 @Override
145 protected String getTable() {
146 return table;
147 }
148
149
150
151
152 @Override
153 protected String getInsertAllValues() {
154 return insertAllValues;
155 }
156
157
158
159
160 @Override
161 protected String getUpdateAllFields() {
162 return updateAllFields;
163 }
164
165 @Override
166 protected void setToArray() {
167 allFields[Columns.HOSTID.ordinal()].setValue(hostid);
168 allFields[Columns.READGLOBALLIMIT.ordinal()].setValue((readgloballimit/10)*10);
169 allFields[Columns.WRITEGLOBALLIMIT.ordinal()]
170 .setValue((writegloballimit/10)*10);
171 allFields[Columns.READSESSIONLIMIT.ordinal()]
172 .setValue((readsessionlimit/10)*10);
173 allFields[Columns.WRITESESSIONLIMIT.ordinal()]
174 .setValue((writesessionlimit/10)*10);
175 allFields[Columns.DELAYLIMIT.ordinal()].setValue(delayllimit);
176 allFields[Columns.UPDATEDINFO.ordinal()].setValue(updatedInfo);
177 }
178
179 @Override
180 protected void setFromArray() throws GoldenGateDatabaseSqlException {
181 hostid = (String) allFields[Columns.HOSTID.ordinal()].getValue();
182 readgloballimit = (((Long) allFields[Columns.READGLOBALLIMIT.ordinal()]
183 .getValue())/10)*10;
184 writegloballimit = (((Long) allFields[Columns.WRITEGLOBALLIMIT.ordinal()]
185 .getValue())/10)*10;
186 readsessionlimit = (((Long) allFields[Columns.READSESSIONLIMIT.ordinal()]
187 .getValue())/10)*10;
188 writesessionlimit = (((Long) allFields[Columns.WRITESESSIONLIMIT
189 .ordinal()].getValue())/10)*10;
190 delayllimit = (Long) allFields[Columns.DELAYLIMIT.ordinal()].getValue();
191 updatedInfo = (Integer) allFields[Columns.UPDATEDINFO.ordinal()]
192 .getValue();
193 }
194
195
196
197
198 @Override
199 protected String getWherePrimaryKey() {
200 return primaryKey[0].column + " = ? ";
201 }
202
203
204
205
206 @Override
207 protected void setPrimaryKey() {
208 primaryKey[0].setValue(hostid);
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225 public DbConfiguration(DbSession dbSession, String hostid, long rg, long wg, long rs,
226 long ws, long del) {
227 super(dbSession);
228 this.hostid = hostid;
229 readgloballimit = (rg/10)*10;
230 writegloballimit = (wg/10)*10;
231 readsessionlimit = (rs/10)*10;
232 writesessionlimit = (ws/10)*10;
233 delayllimit = del;
234 setToArray();
235 isSaved = false;
236 }
237
238
239
240
241
242
243 public DbConfiguration(DbSession dbSession, String hostid) throws GoldenGateDatabaseException {
244 super(dbSession);
245 this.hostid = hostid;
246
247 select();
248 }
249
250
251
252
253
254
255 @Override
256 public void delete() throws GoldenGateDatabaseException {
257 if (dbSession == null) {
258 dbR66ConfigurationHashMap.remove(this.hostid);
259 isSaved = false;
260 return;
261 }
262 super.delete();
263 }
264
265
266
267
268
269
270 @Override
271 public void insert() throws GoldenGateDatabaseException {
272 if (isSaved) {
273 return;
274 }
275 if (dbSession == null) {
276 dbR66ConfigurationHashMap.put(this.hostid, this);
277 isSaved = true;
278 if (this.updatedInfo == UpdatedInfo.TOSUBMIT.ordinal()) {
279 CommanderNoDb.todoList.add(this);
280 }
281 return;
282 }
283 super.insert();
284 }
285
286
287
288
289 @Override
290 public boolean exist() throws GoldenGateDatabaseException {
291 if (dbSession == null) {
292 return dbR66ConfigurationHashMap.containsKey(hostid);
293 }
294 return super.exist();
295 }
296
297
298
299
300
301 @Override
302 public void select() throws GoldenGateDatabaseException {
303 if (dbSession == null) {
304 DbConfiguration conf = dbR66ConfigurationHashMap.get(this.hostid);
305 if (conf == null) {
306 throw new GoldenGateDatabaseNoDataException("No row found");
307 } else {
308
309 for (int i = 0; i < allFields.length; i++){
310 allFields[i].value = conf.allFields[i].value;
311 }
312 setFromArray();
313 isSaved = true;
314 return;
315 }
316 }
317 super.select();
318 }
319
320
321
322
323
324 @Override
325 public void update() throws GoldenGateDatabaseException {
326 if (isSaved) {
327 return;
328 }
329 if (dbSession == null) {
330 dbR66ConfigurationHashMap.put(this.hostid, this);
331 isSaved = true;
332 if (this.updatedInfo == UpdatedInfo.TOSUBMIT.ordinal()) {
333 CommanderNoDb.todoList.add(this);
334 }
335 return;
336 }
337 super.update();
338 }
339
340
341
342 private DbConfiguration(DbSession session) {
343 super(session);
344 }
345
346
347
348
349
350
351
352 public static DbConfiguration getFromStatement(DbPreparedStatement preparedStatement) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException {
353 DbConfiguration dbConfiguration = new DbConfiguration(preparedStatement.getDbSession());
354 dbConfiguration.getValues(preparedStatement, dbConfiguration.allFields);
355 dbConfiguration.setFromArray();
356 dbConfiguration.isSaved = true;
357 return dbConfiguration;
358 }
359
360
361
362
363
364
365 public static DbPreparedStatement getUpdatedPrepareStament(DbSession session) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException {
366 String request = "SELECT " +selectAllFields;
367 request += " FROM "+table+
368 " WHERE "+Columns.UPDATEDINFO.name()+" = "+
369 AbstractDbData.UpdatedInfo.TOSUBMIT.ordinal()+
370 " AND "+ Columns.HOSTID.name()+" = '"+Configuration.configuration.HOST_ID+"'";
371 DbPreparedStatement prep = new DbPreparedStatement(session, request);
372 return prep;
373 }
374
375
376
377
378
379 @Override
380 public void changeUpdatedInfo(UpdatedInfo info) {
381 if (updatedInfo != info.ordinal()) {
382 updatedInfo = info.ordinal();
383 allFields[Columns.UPDATEDINFO.ordinal()].setValue(updatedInfo);
384 isSaved = false;
385 }
386 }
387
388
389
390 public void updateConfiguration() {
391 Configuration.configuration.changeNetworkLimit(writegloballimit,
392 readgloballimit, writesessionlimit, readsessionlimit, delayllimit);
393 }
394
395
396
397
398 public boolean isOwnConfiguration() {
399 return this.hostid.equals(Configuration.configuration.HOST_ID);
400 }
401 }