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.protocol.configuration.Configuration;
36
37
38
39
40
41
42
43 public class DbMultipleMonitor extends AbstractDbData {
44 public static enum Columns {
45 COUNTCONFIG,
46 COUNTHOST,
47 COUNTRULE,
48 HOSTID
49 }
50
51 public static final int[] dbTypes = {
52 Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR };
53
54 public static final String table = " MULTIPLEMONITOR ";
55
56
57
58
59 private static final ConcurrentHashMap<String, DbMultipleMonitor> dbR66MMHashMap =
60 new ConcurrentHashMap<String, DbMultipleMonitor>();
61
62 private String hostid;
63
64 public int countConfig;
65
66 public int countHost;
67
68 public int countRule;
69
70
71 public static final int NBPRKEY = 1;
72
73 protected static final String selectAllFields = Columns.COUNTCONFIG
74 .name() +
75 "," +
76 Columns.COUNTHOST.name() +
77 "," +
78 Columns.COUNTRULE.name() +
79 "," +
80 Columns.HOSTID.name();
81
82 protected static final String updateAllFields = Columns.COUNTCONFIG
83 .name() +
84 "=?," +
85 Columns.COUNTHOST.name() +
86 "=?," +
87 Columns.COUNTRULE.name() +
88 "=?";
89
90 protected static final String insertAllValues = " (?,?,?,?) ";
91
92
93
94
95
96 @Override
97 protected void initObject() {
98 primaryKey = new DbValue[]{new DbValue(hostid, Columns.HOSTID
99 .name())};
100 otherFields = new DbValue[]{
101 new DbValue(countConfig, Columns.COUNTCONFIG.name()),
102 new DbValue(countHost, Columns.COUNTHOST.name()),
103 new DbValue(countRule, Columns.COUNTRULE.name()) };
104 allFields = new DbValue[]{
105 otherFields[0], otherFields[1], otherFields[2], primaryKey[0] };
106 }
107
108
109
110
111 @Override
112 protected String getSelectAllFields() {
113 return selectAllFields;
114 }
115
116
117
118
119 @Override
120 protected String getTable() {
121 return table;
122 }
123
124
125
126
127 @Override
128 protected String getInsertAllValues() {
129 return insertAllValues;
130 }
131
132
133
134
135 @Override
136 protected String getUpdateAllFields() {
137 return updateAllFields;
138 }
139
140 @Override
141 protected void setToArray() {
142 allFields[Columns.HOSTID.ordinal()].setValue(hostid);
143 allFields[Columns.COUNTCONFIG.ordinal()].setValue(countConfig);
144 allFields[Columns.COUNTHOST.ordinal()]
145 .setValue(countHost);
146 allFields[Columns.COUNTRULE.ordinal()]
147 .setValue(countRule);
148 }
149
150 @Override
151 protected void setFromArray() throws GoldenGateDatabaseSqlException {
152 hostid = (String) allFields[Columns.HOSTID.ordinal()].getValue();
153 countConfig = (Integer) allFields[Columns.COUNTCONFIG.ordinal()]
154 .getValue();
155 countHost = (Integer) allFields[Columns.COUNTHOST.ordinal()]
156 .getValue();
157 countRule = (Integer) allFields[Columns.COUNTRULE.ordinal()]
158 .getValue();
159 }
160
161
162
163
164 @Override
165 protected String getWherePrimaryKey() {
166 return primaryKey[0].column + " = ? ";
167 }
168
169
170
171
172 @Override
173 protected void setPrimaryKey() {
174 primaryKey[0].setValue(hostid);
175 }
176
177
178
179
180
181
182
183
184
185
186
187 public DbMultipleMonitor(DbSession dbSession, String hostid, int cc, int ch, int cr) {
188 super(dbSession);
189 this.hostid = hostid;
190 countConfig = cc;
191 countHost = ch;
192 countRule = cr;
193 setToArray();
194 isSaved = false;
195 }
196
197
198
199
200
201
202 public DbMultipleMonitor(DbSession dbSession, String hostid) throws GoldenGateDatabaseException {
203 super(dbSession);
204 this.hostid = hostid;
205
206 select();
207 }
208
209
210
211
212
213
214 @Override
215 public void delete() throws GoldenGateDatabaseException {
216 if (dbSession == null) {
217 dbR66MMHashMap.remove(this.hostid);
218 isSaved = false;
219 return;
220 }
221 super.delete();
222 }
223
224
225
226
227
228
229 @Override
230 public void insert() throws GoldenGateDatabaseException {
231 if (isSaved) {
232 return;
233 }
234 if (dbSession == null) {
235 dbR66MMHashMap.put(this.hostid, this);
236 isSaved = true;
237 return;
238 }
239 super.insert();
240 }
241
242
243
244
245 @Override
246 public boolean exist() throws GoldenGateDatabaseException {
247 if (dbSession == null) {
248 return dbR66MMHashMap.containsKey(hostid);
249 }
250 return super.exist();
251 }
252
253
254
255
256
257 @Override
258 public void select() throws GoldenGateDatabaseException {
259 if (dbSession == null) {
260 DbMultipleMonitor conf = dbR66MMHashMap.get(this.hostid);
261 if (conf == null) {
262 throw new GoldenGateDatabaseNoDataException("No row found");
263 } else {
264
265 for (int i = 0; i < allFields.length; i++){
266 allFields[i].value = conf.allFields[i].value;
267 }
268 setFromArray();
269 isSaved = true;
270 return;
271 }
272 }
273 super.select();
274 }
275
276
277
278
279
280 @Override
281 public void update() throws GoldenGateDatabaseException {
282 if (isSaved) {
283 return;
284 }
285 if (dbSession == null) {
286 dbR66MMHashMap.put(this.hostid, this);
287 isSaved = true;
288 return;
289 }
290 super.update();
291 }
292
293
294
295 private DbMultipleMonitor(DbSession session) {
296 super(session);
297 }
298
299
300
301
302
303
304
305 public static DbMultipleMonitor getFromStatement(DbPreparedStatement preparedStatement) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException {
306 DbMultipleMonitor dbMm = new DbMultipleMonitor(preparedStatement.getDbSession());
307 dbMm.getValues(preparedStatement, dbMm.allFields);
308 dbMm.setFromArray();
309 dbMm.isSaved = true;
310 return dbMm;
311 }
312
313
314
315
316
317
318 public static DbPreparedStatement getUpdatedPrepareStament(DbSession session) throws GoldenGateDatabaseNoConnectionException, GoldenGateDatabaseSqlException {
319 String request = "SELECT " +selectAllFields;
320 request += " FROM "+table+ " WHERE "+Columns.HOSTID.name()+" = '"+Configuration.configuration.HOST_ID+"'"+
321 " FOR UPDATE ";
322 DbPreparedStatement prep = new DbPreparedStatement(session, request);
323 return prep;
324 }
325
326
327
328
329 public boolean checkUpdateConfig(){
330 if (countConfig <= 0) {
331 countConfig = Configuration.configuration.multipleMonitors;
332 countConfig --;
333 this.isSaved = false;
334 } else {
335 countConfig --;
336 this.isSaved = false;
337 }
338 return this.countConfig <= 0;
339 }
340
341
342
343
344 public boolean checkUpdateHost(){
345 if (countHost <= 0) {
346 countHost = Configuration.configuration.multipleMonitors;
347 countHost --;
348 this.isSaved = false;
349 } else {
350 countHost --;
351 this.isSaved = false;
352 }
353 return this.countHost <= 0;
354 }
355
356
357
358
359 public boolean checkUpdateRule(){
360 if (countRule <= 0) {
361 countRule = Configuration.configuration.multipleMonitors;
362 countRule --;
363 this.isSaved = false;
364 } else {
365 countRule --;
366 this.isSaved = false;
367 }
368 return this.countRule <= 0;
369 }
370
371
372
373
374
375 @Override
376 public void changeUpdatedInfo(UpdatedInfo info) {
377 }
378
379
380
381 public String toString() {
382 return "DbMM "+countConfig+":"+countHost+":"+countRule;
383 }
384 }