-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsoleHstDM.java
More file actions
317 lines (261 loc) · 7.8 KB
/
consoleHstDM.java
File metadata and controls
317 lines (261 loc) · 7.8 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
package Jvakt;
/*
* 2025-06-09 V.55 Michael Ekdal Changed the order of the createStatement to accomodate changes in postgresql-42.7.6.jar
* 2022-06-23 V.54 Michael Ekdal Added getVersion() to get at consistent version throughout all classes.
*/
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 consoleHstDM extends AbstractTableModel {
static final long serialVersionUID = 50L;
String afn;
String columnNames[] = {"CreDate", "DelDate","Count", "Id", "Prio", "Type", "Status", "Body", "Agent"};
// static Vector map = new Vector(1000,100);
static Vector<consoleHstROW> map = new Vector<consoleHstROW>(100,100);
consoleHstROW rad;
int i = 0;
static String DBUrl = "jdbc:postgresql://localhost:5433/Jvakt";
static Connection conn = null;
String version = "jVakt - consoleHstDM ( 2021-DEC-30)";
String database = "Jvakt";
String dbuser = "console";
String dbpassword = "";
String dbhost = "localhost";
String dbport = "5433";
String jvhost = "localhost";
String jvport = "1956";
Boolean swDBopen = false;
String where = "";
public consoleHstDM() 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;
consoleHstROW rad;
rad = (consoleHstROW) map.get(row);
if (col == 0) {
return rad.getCredat();
} else if (col == 1) {
return rad.getDeldat();
} else if (col == 2) {
return rad.getCount();
} else if (col == 3) {
return rad.getId();
} else if (col == 4) {
return rad.getPrio();
} else if (col == 5) {
return rad.getType();
} else if (col == 6) {
return rad.getStatus();
} else if (col == 7) {
// if (rad.getStatus().startsWith("T")) return "The Jvakt agent did not report in set time";
return rad.getBody();
} else if (col == 77) {
return rad.getBody();
} else if (col == 8) {
return rad.getAgent();
} 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 setWhere(String where) {
this.where = where;
// System.out.println("This where: " + this.where);
}
public void setValueAt(Object value, int row, int col) {
// se till att objektet User sparar v�rdet med r�tt set metod ( if (col == 2) .........
// System.out.println("wD setValueAt " + value + " " + row + " "+ col);
consoleHstROW rad;
if ( row >= map.size()) {
rad = new consoleHstROW();
map.add(rad);
}
else rad = (consoleHstROW) map.get(row);
if (col == 0) {
rad.setCredat((String)value);
} else if (col == 1) {
rad.setDeldat((String)value);
} else if (col == 2) {
rad.setCount((Integer)value);
// rad.setCount((int)value);
} else if (col == 3) {
rad.setId((String)value);
} else if (col == 4) {
rad.setPrio((Integer)value);
// rad.setPrio((int)value);
} else if (col == 5) {
rad.setType((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);
}
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(e.getMessage());
swDBopen = false;
map.clear();
createEmptyRow();
}
catch (Exception e) {
// System.err.println(e);
System.err.println(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;
if (where.length() < 5 || !where.endsWith(" ") )
s = new String("select * from consoleHst order by credat desc;");
else
s = new String("select * from consoleHst where "+ where +" order by credat desc;");
// System.out.println(s);
// Statement stmt = conn.createStatement(ResultSet.CONCUR_READ_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE);
Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY,ResultSet.CLOSE_CURSORS_AT_COMMIT );
stmt.setFetchSize(1000);
stmt.setQueryTimeout(5);
ResultSet rs = stmt.executeQuery(s);
// if (!rs.first()) {
// System.out.println("createemptyrow");
// createEmptyRow();
// }
map.clear();
while (rs.next()) {
//--
// System.out.println("rs.next");
rad = new consoleHstROW();
// for (int i = 1; i <= 7; i++) {
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.setDeldat(rs.getString("deldat"));
rad.setStatus(rs.getString("status"));
rad.setBody(rs.getString("body"));
rad.setAgent(rs.getString("agent"));
// }
map.add(rad);
}
rs.close();
stmt.close();
// fireTableDataChanged();
}
catch (SQLException e) {
System.err.println(e);
System.err.println(e.getMessage());
swDBopen = false;
createEmptyRow();
return false;
}
catch (Exception e) {
System.err.println(e);
System.err.println(e.getMessage());
swDBopen = false;
createEmptyRow();
return false;
}
return true;
}
private boolean createEmptyRow() {
rad = new consoleHstROW();
rad.setCount(0);
rad.setId("**<>**");
rad.setPrio(0);
rad.setType(" ");
rad.setCredat(" ");
rad.setDeldat(" ");
rad.setStatus(" ");
rad.setBody(" ");
rad.setAgent(" ");
// map.add(rad);
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();
}
}
}