1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.openr66.database.data;
21
22 import com.fasterxml.jackson.databind.JsonNode;
23 import org.waarp.common.database.DbConstant;
24 import org.waarp.common.database.DbPreparedStatement;
25 import org.waarp.common.database.DbSession;
26 import org.waarp.common.database.exception.WaarpDatabaseException;
27 import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
28 import org.waarp.common.database.exception.WaarpDatabaseNoDataException;
29 import org.waarp.common.database.exception.WaarpDatabaseSqlException;
30 import org.waarp.openr66.dao.AbstractDAO;
31 import org.waarp.openr66.dao.DAOFactory;
32 import org.waarp.openr66.dao.MultipleMonitorDAO;
33 import org.waarp.openr66.dao.database.DBDAOFactory;
34 import org.waarp.openr66.dao.database.StatementExecutor;
35 import org.waarp.openr66.dao.exception.DAOConnectionException;
36 import org.waarp.openr66.dao.exception.DAONoDataException;
37 import org.waarp.openr66.pojo.MultipleMonitor;
38 import org.waarp.openr66.protocol.configuration.Configuration;
39
40 import java.sql.SQLException;
41 import java.sql.Types;
42
43
44
45
46 public class DbMultipleMonitor extends AbstractDbDataDao<MultipleMonitor> {
47 public enum Columns {
48 COUNTCONFIG, COUNTHOST, COUNTRULE, HOSTID
49 }
50
51 public static final int[] dbTypes =
52 { Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.NVARCHAR };
53
54 public static final String table = " MULTIPLEMONITOR ";
55
56 public static final String selectAllFields =
57 Columns.COUNTCONFIG.name() + ',' + Columns.COUNTHOST.name() + ',' +
58 Columns.COUNTRULE.name() + ',' + Columns.HOSTID.name();
59
60 @Override
61 protected final void initObject() {
62
63 }
64
65 @Override
66 protected final String getTable() {
67 return table;
68 }
69
70 @Override
71 protected final AbstractDAO<MultipleMonitor> getDao(final boolean isCacheable)
72 throws DAOConnectionException {
73 return DAOFactory.getInstance().getMultipleMonitorDAO(isCacheable);
74 }
75
76 @Override
77 protected final String getPrimaryKey() {
78 if (pojo != null) {
79 return pojo.getHostid();
80 }
81 throw new IllegalArgumentException("pojo is null");
82 }
83
84 @Override
85 protected final String getPrimaryField() {
86 return Columns.HOSTID.name();
87 }
88
89
90
91
92
93
94
95 public DbMultipleMonitor(final String hostid, final int cc, final int ch,
96 final int cr) throws WaarpDatabaseSqlException {
97 pojo = new MultipleMonitor(hostid, cc, ch, cr);
98 }
99
100
101
102
103
104
105 public DbMultipleMonitor(final String hostid) throws WaarpDatabaseException {
106 MultipleMonitorDAO monitorAccess = null;
107 try {
108 monitorAccess = DAOFactory.getInstance().getMultipleMonitorDAO(true);
109 pojo = monitorAccess.select(hostid);
110 } catch (final DAOConnectionException e) {
111 throw new WaarpDatabaseException(e);
112 } catch (final DAONoDataException e) {
113 throw new WaarpDatabaseNoDataException("Cannot find " + "MultipleMonitor",
114 e);
115 } finally {
116 DBDAOFactory.closeDAO(monitorAccess);
117 }
118 }
119
120 @Override
121 protected final void checkValues() throws WaarpDatabaseSqlException {
122 pojo.checkValues();
123 }
124
125 @Override
126 protected final void setFromJson(final String field, final JsonNode value) {
127 if (value == null) {
128 return;
129 }
130 for (final Columns column : Columns.values()) {
131 if (column.name().equalsIgnoreCase(field)) {
132 switch (column) {
133 case COUNTCONFIG:
134 pojo.setCountConfig(value.asInt());
135 break;
136 case COUNTHOST:
137 pojo.setCountHost(value.asInt());
138 break;
139 case COUNTRULE:
140 pojo.setCountRule(value.asInt());
141 break;
142 case HOSTID:
143 pojo.setHostid(value.asText());
144 break;
145 }
146 }
147 }
148 }
149
150
151
152
153 private DbMultipleMonitor() {
154 pojo = new MultipleMonitor();
155 }
156
157
158
159
160
161
162
163
164
165
166
167 public static DbMultipleMonitor getFromStatement(
168 final DbPreparedStatement preparedStatement)
169 throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException {
170 final DbMultipleMonitor dbMm = new DbMultipleMonitor();
171 AbstractDAO<MultipleMonitor> multipleDAO = null;
172 try {
173 multipleDAO = dbMm.getDao(false);
174 dbMm.pojo =
175 ((StatementExecutor<MultipleMonitor>) multipleDAO).getFromResultSet(
176 preparedStatement.getResultSet());
177 return dbMm;
178 } catch (final SQLException e) {
179 DbConstant.error(e);
180 throw new WaarpDatabaseSqlException("Getting values in error", e);
181 } catch (final DAOConnectionException e) {
182 throw new WaarpDatabaseSqlException("Getting values in error", e);
183 } finally {
184 DAOFactory.closeDAO(multipleDAO);
185 }
186 }
187
188
189
190
191
192
193
194
195 public static DbPreparedStatement getUpdatedPrepareStament(
196 final DbSession session)
197 throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException {
198 final DbMultipleMonitor multipleMonitor =
199 new DbMultipleMonitor(Configuration.configuration.getHostId(), 0, 0, 0);
200 try {
201 if (!multipleMonitor.exist()) {
202 multipleMonitor.insert();
203 session.commit();
204 }
205 } catch (final WaarpDatabaseException ignored) {
206
207 }
208 String request = "SELECT " + selectAllFields;
209 request += " FROM " + table + " WHERE " + Columns.HOSTID.name() + " = '" +
210 Configuration.configuration.getHostId() + '\'' + " FOR UPDATE ";
211 return new DbPreparedStatement(session, request);
212 }
213
214
215
216
217
218
219 public final boolean checkUpdateConfig() {
220 if (getCountConfig() <= 0) {
221 setCountConfig(Configuration.configuration.getMultipleMonitors());
222 setCountConfig(getCountConfig() - 1);
223 isSaved = false;
224 } else {
225 setCountConfig(getCountConfig() - 1);
226 isSaved = false;
227 }
228 return getCountConfig() <= 0;
229 }
230
231
232
233
234
235
236 public final boolean checkUpdateHost() {
237 if (getCountHost() <= 0) {
238 setCountHost(Configuration.configuration.getMultipleMonitors());
239 setCountHost(getCountHost() - 1);
240 isSaved = false;
241 } else {
242 setCountHost(getCountHost() - 1);
243 isSaved = false;
244 }
245 return getCountHost() <= 0;
246 }
247
248
249
250
251
252
253 public final boolean checkUpdateRule() {
254 if (getCountRule() <= 0) {
255 setCountRule(Configuration.configuration.getMultipleMonitors());
256 setCountRule(getCountRule() - 1);
257 isSaved = false;
258 } else {
259 setCountRule(getCountRule() - 1);
260 isSaved = false;
261 }
262 return getCountRule() <= 0;
263 }
264
265 @Override
266 public final void changeUpdatedInfo(final UpdatedInfo info) {
267
268 }
269
270
271
272
273 @Override
274 public final String toString() {
275 return "DbMM " + getCountConfig() + ':' + getCountHost() + ':' +
276 getCountRule();
277 }
278
279
280
281
282 public final int getCountConfig() {
283 return pojo.getCountConfig();
284 }
285
286
287
288
289 private void setCountConfig(final int countConfig) {
290 isSaved = false;
291 pojo.setCountConfig(countConfig);
292 }
293
294
295
296
297 public final int getCountHost() {
298 return pojo.getCountHost();
299 }
300
301
302
303
304 private void setCountHost(final int countHost) {
305 isSaved = false;
306 pojo.setCountHost(countHost);
307 }
308
309
310
311
312 public final int getCountRule() {
313 return pojo.getCountRule();
314 }
315
316
317
318
319 private void setCountRule(final int countRule) {
320 isSaved = false;
321 pojo.setCountRule(countRule);
322 }
323 }