-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsoleDM.java
More file actions
346 lines (287 loc) · 9.01 KB
/
consoleDM.java
File metadata and controls
346 lines (287 loc) · 9.01 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package Jvakt;
/*
* 2025-06-09 V.57 Michael Ekdal Changed the order of the createStatement to accomodate changes in postgresql-42.7.6.jar
* 2025-04-01 V.56 Michael Ekdal Added variable recid
* 2023-01-09 V.55 Michael Ekdal Added isCheckStatusActive member.
* 2022-07-02 V.54 Michael Ekdal Added variable version.
*/
import javax.swing.table.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
class consoleDM extends AbstractTableModel {
static final long serialVersionUID = 50L;
String afn;
String columnNames[] = { "Id", "Prio", "Type", "CreDate", "ConDate", "Status", "Body", "Agent","RecId"};
static Vector<consoleROW> map = new Vector<consoleROW>(100,10);
consoleROW rad;
int i = 0;
static String DBUrl = "jdbc:postgresql://localhost:5433/Jvakt";
static Connection conn = null;
String version = "jVakt - consoleDM 2025-APR-01";
String database = "Jvakt";
String dbuser = "console";
String dbpassword = "";
String dbhost = "localhost";
String dbport = "5433";
String jvhost = "localhost";
String jvport = "1956";
Boolean swDBopen = false;
static Long lhou, lmin, Lsec, Lrptdat, Lchktim, Lchktimto;
static java.sql.Timestamp zD;
static java.sql.Timestamp zTs;
public consoleDM() throws IOException {
getProps();
if (openDB()) refreshData();
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return map.size()+1;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
// se till att objektet User hämtar värdet med rätt get metod ( if (col == 2) .........
if ( row >= map.size()) return null;
consoleROW rad;
rad = (consoleROW) map.get(row);
if (col == 0) {
return rad.getId();
} else if (col == 1) {
return rad.getPrio();
} else if (col == 2) {
return rad.getType();
} else if (col == 3) {
return rad.getCredat();
} else if (col == 4) {
return rad.getCondat();
} else if (col == 5) {
return rad.getStatus();
} else if (col == 6) {
return rad.getBody();
} else if (col == 7) {
return rad.getAgent();
} else if (col == 8) {
return rad.getRecid();
} else {
return null;
}
}
public Class<?> getColumnClass(int c) {
try {
// return getValueAt(0, c).getClass();
return getValueAt(0, c).getClass();
} catch (NullPointerException e) {
return String.class;
}
}
public boolean isCellEditable(int row, int col) {
return false;
}
public void setValueAt(Object value, int row, int col) {
// save the value in right field depending on the column (if (col == 2) .........
// System.out.println("wD setValueAt " + value + " " + row + " "+ col);
consoleROW rad;
if ( row >= map.size()) {
rad = new consoleROW();
map.add(rad);
}
else rad = map.get(row);
if (col == 0) {
rad.setCount((Integer)value);
} else if (col == 1) {
rad.setId((String)value);
} else if (col == 2) {
rad.setPrio((Integer)value);
} else if (col == 3) {
rad.setType((String)value);
} else if (col == 4) {
rad.setCredat((String)value);
} else if (col == 5) {
rad.setCondat((String)value);
} else if (col == 6) {
rad.setStatus((String)value);
} else if (col == 7) {
rad.setBody((String)value);
} else if (col == 8) {
rad.setAgent((String)value);
} else if (col == 9) {
rad.setRecid((String)value);
}
fireTableCellUpdated(row, col);
}
public boolean openDB() {
try {
Class.forName("org.postgresql.Driver").newInstance();
DBUrl = "jdbc:postgresql://"+dbhost+":"+dbport+"/"+database;
System.out.println(DBUrl);
System.out.println("dbuser= " + dbuser +" dbpassword "+ dbpassword);
conn = DriverManager.getConnection(DBUrl,dbuser,dbpassword);
conn.setAutoCommit(false);
conn.setNetworkTimeout(null, 5000);
swDBopen = true;
}
catch (SQLException e) {
// System.err.println(e);
System.err.println("#e1 "+e.getMessage());
swDBopen = false;
map.clear();
createEmptyRow();
}
catch (Exception e) {
// System.err.println(e);
System.err.println("#e2 "+e.getMessage());
swDBopen = false;
map.clear();
createEmptyRow();
}
return swDBopen;
}
public boolean getDBopen() {
return swDBopen;
}
public boolean refreshData() {
if (!swDBopen) {
if (!openDB()) return false;
}
try {
String s = new String("select * from console order by credat desc;");
// System.out.println(s);
// System.out.println(ResultSet.CONCUR_READ_ONLY+" "+ResultSet.TYPE_FORWARD_ONLY+" "+ResultSet.CLOSE_CURSORS_AT_COMMIT);
// Statement stmt = conn.createStatement(ResultSet.CONCUR_READ_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE);
// Statement stmt = conn.createStatement(ResultSet.CONCUR_UPDATABLE,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CLOSE_CURSORS_AT_COMMIT );
Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY,ResultSet.CLOSE_CURSORS_AT_COMMIT );
stmt.setFetchSize(25);
stmt.setQueryTimeout(5);
ResultSet rs = stmt.executeQuery(s);
map.clear();
while (rs.next()) {
// System.out.println("rs.next");
rad = new consoleROW();
rad.setCount(rs.getInt("count"));
rad.setId(rs.getString("id"));
rad.setPrio(rs.getInt("prio"));
rad.setType(rs.getString("type"));
rad.setCredat(rs.getString("credat"));
rad.setCondat(rs.getString("condat"));
rad.setStatus(rs.getString("status"));
rad.setBody(rs.getString("body"));
rad.setAgent(rs.getString("agent"));
rad.setRecid(rs.getString("recid"));
map.add(rad);
}
rs.close();
stmt.close();
// fireTableDataChanged();
}
catch (SQLException e) {
System.err.println(e);
System.err.println("#C1 "+e.getMessage());
swDBopen = false;
createEmptyRow();
return false;
}
catch (Exception e) {
System.err.println(e);
System.err.println("#C2 "+e.getMessage());
swDBopen = false;
createEmptyRow();
return false;
}
return true;
}
private boolean createEmptyRow() {
rad = new consoleROW();
rad.setCount(0);
rad.setId("**<>**");
rad.setPrio(0);
rad.setType(" ");
rad.setCredat(" ");
rad.setCondat(" ");
rad.setStatus(" ");
rad.setBody(" ");
rad.setAgent(" ");
rad.setRecid(null);
return true;
}
public boolean closeDB() {
try {
if (swDBopen) conn.close();
}
catch (SQLException e) {
System.err.println(e);
System.err.println(e.getMessage());
return true;
}
return true;
}
void getProps() {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("console.properties");
prop.load(input);
// get the property value and print it out
database = prop.getProperty("database");
dbuser = prop.getProperty("dbuser");
dbpassword = prop.getProperty("dbpassword");
if (dbpassword.startsWith("==y")) {
byte[] decodedBytes = Base64.getDecoder().decode(dbpassword.substring(3));
String decodedString = new String(decodedBytes);
dbpassword=decodedString;
}
dbhost = prop.getProperty("dbhost");
dbport = prop.getProperty("dbport");
jvport = prop.getProperty("jvport");
jvhost = prop.getProperty("jvhost");
input.close();
} catch (IOException ex) {
// ex.printStackTrace();
}
}
public boolean isCheckStatusActive() {
boolean statusRC = false;
if (!swDBopen) {
if (!openDB()) return false;
}
try {
String s = new String("select * from status WHERE (state='A' or state = 'D') and id='Jvakt-CheckStatus';");
Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY,ResultSet.CLOSE_CURSORS_AT_COMMIT );
stmt.setFetchSize(10);
stmt.setQueryTimeout(5);
ResultSet rs = stmt.executeQuery(s);
zTs = new java.sql.Timestamp((new Date(System.currentTimeMillis())).getTime()); // YYYY-MM-DD hh:mm.ss.nnn. now
// System.out.println("Last RS: "+statusRC) ;
// System.out.println("Getrow RS: "+rs.getRow()) ;
// System.out.println("zTs: "+zTs) ;
statusRC=false;
Lsec = new Long(0);
while (rs.next()) {
statusRC=true;
zD = rs.getTimestamp("rptdat");
Lsec = (zTs.getTime() / 1000 - zD.getTime() / 1000); // Actual time minus the time rptdat in seconds
// System.out.println("Lsec: "+Lsec) ;
}
if ( Lsec > 1200 ) statusRC = false; // false when mire than 20 minutes has passed since the Jvakt-CheckStatus was updated
rs.close();
stmt.close();
}
catch (SQLException e) {
System.err.println(e);
System.err.println(e.getMessage());
swDBopen = false;
}
catch (Exception e) {
System.err.println(e);
System.err.println(e.getMessage());
}
return statusRC;
}
}