1
|
package eu.dnetlib.validator.commons.dao.rules;
|
2
|
|
3
|
import java.sql.Connection;
|
4
|
import java.sql.PreparedStatement;
|
5
|
import java.sql.ResultSet;
|
6
|
import java.sql.SQLException;
|
7
|
import java.util.ArrayList;
|
8
|
import java.util.Enumeration;
|
9
|
import java.util.List;
|
10
|
import java.util.Properties;
|
11
|
|
12
|
import eu.dnetlib.domain.functionality.validator.Rule;
|
13
|
import eu.dnetlib.validator.commons.dao.AbstractDAO;
|
14
|
|
15
|
public class RulesDAOimpl extends AbstractDAO<Rule> implements RulesDAO{
|
16
|
|
17
|
@Override
|
18
|
public Integer save(Rule t) {
|
19
|
Connection con = null;
|
20
|
PreparedStatement stmt = null;
|
21
|
Integer retId = -1;
|
22
|
logger.debug("Accessing DB to save/update Rule");
|
23
|
try {
|
24
|
logger.debug("Accessing DB to update Rule");
|
25
|
con = getConnection();
|
26
|
String query="UPDATE rules SET name=?, description=?, type=?, mandatory=?, weight=?, provider_information=?, entity_type=?, for_cris=? WHERE id=?";
|
27
|
stmt = con.prepareStatement(query);
|
28
|
|
29
|
stmt.setString(1, t.getName());
|
30
|
stmt.setString(2, t.getDescription());
|
31
|
stmt.setString(3, t.getType());
|
32
|
stmt.setBoolean(4, t.isMandatory());
|
33
|
stmt.setInt(5, t.getWeight());
|
34
|
stmt.setString(6, t.getProvider_information());
|
35
|
stmt.setString(7, t.getEntity_type());
|
36
|
stmt.setBoolean(8, t.isFor_cris());
|
37
|
stmt.setInt(9, t.getId());
|
38
|
|
39
|
if (stmt.executeUpdate() == 0) {
|
40
|
stmt.close();
|
41
|
logger.debug("Accessing DB to save Rule with name:"+t.getName()+",desc:"+t.getDescription()+",type:"+t.getType()+",mand:"+t.isMandatory()+",weight:"+t.getWeight()+",pr_inf:"+t.getProvider_information()+",jb_tp:"+t.getJob_type());
|
42
|
query="INSERT INTO rules(name, description, type, mandatory, weight, provider_information, job_type, entity_type, for_cris) VALUES(?,?,?,?,?,?,?,?,?)";
|
43
|
stmt = con.prepareStatement(query);
|
44
|
stmt.setString(1, t.getName());
|
45
|
stmt.setString(2, t.getDescription());
|
46
|
stmt.setString(3, t.getType());
|
47
|
stmt.setBoolean(4, t.isMandatory());
|
48
|
stmt.setInt(5, t.getWeight());
|
49
|
stmt.setString(6, t.getProvider_information());
|
50
|
stmt.setString(7, t.getJob_type());
|
51
|
stmt.setString(8, t.getEntity_type());
|
52
|
stmt.setBoolean(9, t.isFor_cris());
|
53
|
stmt.executeUpdate();
|
54
|
retId = this.getLastId();
|
55
|
} else {
|
56
|
logger.debug("Accessing DB to update Rule-done");
|
57
|
retId=t.getId();
|
58
|
}
|
59
|
|
60
|
stmt.close();
|
61
|
logger.debug("Accessing DB to delete Rule properties");
|
62
|
query="DELETE FROM rule_properties " + " WHERE rule_id=?";
|
63
|
stmt = con.prepareStatement(query);
|
64
|
stmt.setInt(1, retId);
|
65
|
if (stmt.executeUpdate() == 0) {
|
66
|
stmt.close();
|
67
|
}
|
68
|
logger.debug("Accessing DB to insert Rule properties");
|
69
|
query="INSERT INTO rule_properties(rule_id, property_name, property_value) VALUES(?,?,?)";
|
70
|
stmt = con.prepareStatement(query);
|
71
|
Properties pros = t.getConfiguration();
|
72
|
Enumeration<?> e = pros.propertyNames();
|
73
|
while (e.hasMoreElements()) {
|
74
|
String key = (String) e.nextElement();
|
75
|
logger.debug("Accessing DB to add property:"+key+"-"+pros.getProperty(key));
|
76
|
stmt.setInt(1, retId);
|
77
|
stmt.setString(2, key);
|
78
|
stmt.setString(3, pros.getProperty(key));
|
79
|
stmt.addBatch();
|
80
|
}
|
81
|
stmt.executeBatch();
|
82
|
logger.debug("Rule + Properties inserted/updated");
|
83
|
|
84
|
} catch (SQLException e) {
|
85
|
logger.error("Error accessing DB to get save/update Rule"+e);
|
86
|
} finally {
|
87
|
if (stmt != null) {
|
88
|
try {
|
89
|
stmt.close();
|
90
|
} catch (SQLException e) {
|
91
|
logger.error("Error accessing DB to get save/update Rule"+e);
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
return retId;
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
public String delete(int id) {
|
100
|
Connection con = null;
|
101
|
PreparedStatement stmt = null;
|
102
|
logger.debug("Accessing DB to delete Rule");
|
103
|
try {
|
104
|
con = getConnection();
|
105
|
String query="DELETE FROM rules " + " WHERE id=?";
|
106
|
stmt = con.prepareStatement(query);
|
107
|
stmt.setInt(1, id);
|
108
|
if (stmt.executeUpdate() == 0) {
|
109
|
stmt.close();
|
110
|
}
|
111
|
query="DELETE FROM rule_properties " + " WHERE rule_id=?";
|
112
|
stmt = con.prepareStatement(query);
|
113
|
stmt.setInt(1, id);
|
114
|
if (stmt.executeUpdate() == 0) {
|
115
|
stmt.close();
|
116
|
}
|
117
|
|
118
|
} catch (SQLException e) {
|
119
|
logger.error("Error accessing DB to delete Rule"+e);
|
120
|
} finally {
|
121
|
if (stmt != null) {
|
122
|
try {
|
123
|
stmt.close();
|
124
|
} catch (SQLException e) {
|
125
|
logger.error("Error accessing DB to delete Rule"+e);
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
return null;
|
130
|
}
|
131
|
|
132
|
|
133
|
@Override
|
134
|
protected PreparedStatement getDeleteStatement(int id, Connection con)
|
135
|
throws SQLException {
|
136
|
String query="DELETE FROM rules " + " WHERE id=?";
|
137
|
PreparedStatement stmt = con.prepareStatement(query);
|
138
|
stmt.setInt(1, id);
|
139
|
return stmt;
|
140
|
}
|
141
|
|
142
|
@Override
|
143
|
public List<Rule> getAllRulesByJobType(String jobType) {
|
144
|
ResultSet rs = null;
|
145
|
Connection con = null;
|
146
|
PreparedStatement stmt = null;
|
147
|
Rule retRule = null;
|
148
|
List<Rule> retList = null;
|
149
|
logger.debug("Accessing DB to get All Rules by jobType");
|
150
|
try {
|
151
|
con = getConnection();
|
152
|
String query="SELECT * FROM rules WHERE job_type=? ORDER BY name";
|
153
|
stmt = con.prepareStatement(query);
|
154
|
stmt.setString(1, jobType);
|
155
|
rs = stmt.executeQuery();
|
156
|
if (rs!=null){
|
157
|
retList = new ArrayList<Rule>();
|
158
|
while (rs.next()) {
|
159
|
retRule = new Rule();
|
160
|
retRule.setName(rs.getString("name"));
|
161
|
retRule.setDescription(rs.getString("description"));
|
162
|
retRule.setType(rs.getString("type"));
|
163
|
retRule.setMandatory(rs.getBoolean("mandatory"));
|
164
|
retRule.setWeight(rs.getInt("weight"));
|
165
|
retRule.setProvider_information(rs.getString("provider_information"));
|
166
|
retRule.setId(rs.getInt("id"));
|
167
|
retRule.setEntity_type(rs.getString("entity_type"));
|
168
|
retRule.setFor_cris(rs.getBoolean("for_cris"));
|
169
|
retRule.setJob_type(rs.getString("job_type"));
|
170
|
retRule.setConfiguration(this.getProperties(rs.getInt("id")));
|
171
|
retList.add(retRule);
|
172
|
}
|
173
|
}
|
174
|
|
175
|
|
176
|
} catch (SQLException e) {
|
177
|
logger.error("Error accessing DB to get All Rules by jobType"+e);
|
178
|
} finally {
|
179
|
if (stmt != null) {
|
180
|
try {
|
181
|
stmt.close();
|
182
|
} catch (SQLException e) {
|
183
|
logger.error("Error accessing DB to get All Rules by jobType"+e);
|
184
|
}
|
185
|
}
|
186
|
}
|
187
|
return retList;
|
188
|
|
189
|
}
|
190
|
|
191
|
@Override
|
192
|
public List<Rule> getAllRulesByJobTypeEntityType(String jobType, String entityType) {
|
193
|
ResultSet rs = null;
|
194
|
Connection con = null;
|
195
|
PreparedStatement stmt = null;
|
196
|
Rule retRule = null;
|
197
|
List<Rule> retList = null;
|
198
|
logger.debug("Accessing DB to get All Rules by jobType");
|
199
|
try {
|
200
|
con = getConnection();
|
201
|
String query="SELECT * FROM rules WHERE job_type=? AND entity_type=? ORDER BY name";
|
202
|
stmt = con.prepareStatement(query);
|
203
|
stmt.setString(1, jobType);
|
204
|
stmt.setString(2, entityType);
|
205
|
rs = stmt.executeQuery();
|
206
|
if (rs!=null){
|
207
|
retList = new ArrayList<Rule>();
|
208
|
while (rs.next()) {
|
209
|
retRule = new Rule();
|
210
|
retRule.setName(rs.getString("name"));
|
211
|
retRule.setDescription(rs.getString("description"));
|
212
|
retRule.setType(rs.getString("type"));
|
213
|
retRule.setMandatory(rs.getBoolean("mandatory"));
|
214
|
retRule.setWeight(rs.getInt("weight"));
|
215
|
retRule.setProvider_information(rs.getString("provider_information"));
|
216
|
retRule.setId(rs.getInt("id"));
|
217
|
retRule.setEntity_type(rs.getString("entity_type"));
|
218
|
retRule.setFor_cris(rs.getBoolean("for_cris"));
|
219
|
retRule.setJob_type(rs.getString("job_type"));
|
220
|
retRule.setConfiguration(this.getProperties(rs.getInt("id")));
|
221
|
retList.add(retRule);
|
222
|
}
|
223
|
}
|
224
|
|
225
|
|
226
|
} catch (SQLException e) {
|
227
|
logger.error("Error accessing DB to get All Rules by jobType"+e);
|
228
|
} finally {
|
229
|
if (stmt != null) {
|
230
|
try {
|
231
|
stmt.close();
|
232
|
} catch (SQLException e) {
|
233
|
logger.error("Error accessing DB to get All Rules by jobType"+e);
|
234
|
}
|
235
|
}
|
236
|
}
|
237
|
return retList;
|
238
|
|
239
|
}
|
240
|
|
241
|
@Override
|
242
|
public List<Rule> getAllRules() {
|
243
|
ResultSet rs = null;
|
244
|
Connection con = null;
|
245
|
PreparedStatement stmt = null;
|
246
|
List<Rule> retList = null;
|
247
|
logger.debug("Accessing DB to get All Rules");
|
248
|
try {
|
249
|
con = getConnection();
|
250
|
String query="SELECT * FROM rules ORDER BY name";
|
251
|
stmt = con.prepareStatement(query);
|
252
|
rs = stmt.executeQuery();
|
253
|
if (rs!=null){
|
254
|
retList = new ArrayList<Rule>();
|
255
|
while (rs.next()) {
|
256
|
Rule retRule = new Rule();
|
257
|
retRule.setName(rs.getString("name"));
|
258
|
retRule.setDescription(rs.getString("description"));
|
259
|
retRule.setType(rs.getString("type"));
|
260
|
retRule.setMandatory(rs.getBoolean("mandatory"));
|
261
|
retRule.setWeight(rs.getInt("weight"));
|
262
|
retRule.setProvider_information(rs.getString("provider_information"));
|
263
|
retRule.setId(rs.getInt("id"));
|
264
|
retRule.setEntity_type(rs.getString("entity_type"));
|
265
|
retRule.setFor_cris(rs.getBoolean("for_cris"));
|
266
|
retRule.setJob_type(rs.getString("job_type"));
|
267
|
retRule.setConfiguration(this.getProperties(rs.getInt("id")));
|
268
|
retList.add(retRule);
|
269
|
}
|
270
|
}
|
271
|
|
272
|
logger.debug("rules: " + retList.size());
|
273
|
} catch (SQLException e) {
|
274
|
logger.error("Error accessing DB to get All Rule-pairs"+e);
|
275
|
} finally {
|
276
|
if (stmt != null) {
|
277
|
try {
|
278
|
stmt.close();
|
279
|
} catch (SQLException e) {
|
280
|
logger.error("Error accessing DB to get All Rule-pairs"+e);
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
return retList;
|
285
|
|
286
|
}
|
287
|
|
288
|
public Properties getProperties(int ruleId) {
|
289
|
ResultSet rs = null;
|
290
|
Connection con = null;
|
291
|
PreparedStatement stmt = null;
|
292
|
Properties pros = null;
|
293
|
// logger.debug("Accessing DB to get Rule Properties");
|
294
|
try {
|
295
|
con = getConnection();
|
296
|
String query="SELECT property_name, property_value FROM rule_properties WHERE rule_id=?";
|
297
|
stmt = con.prepareStatement(query);
|
298
|
stmt.setInt(1, ruleId);
|
299
|
rs = stmt.executeQuery();
|
300
|
if (rs!=null){
|
301
|
pros = new Properties();
|
302
|
while (rs.next()) {
|
303
|
pros.setProperty(rs.getString(1), rs.getString(2));
|
304
|
}
|
305
|
}
|
306
|
|
307
|
} catch (SQLException e) {
|
308
|
logger.error("Accessing DB to get Rule Properties: "+e);
|
309
|
} finally {
|
310
|
if (stmt != null) {
|
311
|
try {
|
312
|
stmt.close();
|
313
|
} catch (SQLException e) {
|
314
|
logger.error("Accessing DB to get Rule Properties: "+e);
|
315
|
}
|
316
|
}
|
317
|
}
|
318
|
return pros;
|
319
|
}
|
320
|
|
321
|
@Override
|
322
|
public Rule get(int id) {
|
323
|
ResultSet rs = null;
|
324
|
Connection con = null;
|
325
|
PreparedStatement stmt = null;
|
326
|
Rule retRule = null;
|
327
|
// logger.debug("Accessing DB to get Rule with id: "+id);
|
328
|
try {
|
329
|
con = getConnection();
|
330
|
String query="SELECT name, description, type, mandatory, weight, provider_information, job_type, entity_type, for_cris FROM rules WHERE id=?";
|
331
|
stmt = con.prepareStatement(query);
|
332
|
stmt.setInt(1, id);
|
333
|
rs = stmt.executeQuery();
|
334
|
if (rs!=null){
|
335
|
if (rs.next()) {
|
336
|
retRule = new Rule();
|
337
|
retRule.setName(rs.getString(1));
|
338
|
retRule.setDescription(rs.getString(2));
|
339
|
retRule.setType(rs.getString(3));
|
340
|
retRule.setMandatory(rs.getBoolean(4));
|
341
|
retRule.setWeight(rs.getInt(5));
|
342
|
retRule.setProvider_information(rs.getString(6));
|
343
|
retRule.setJob_type(rs.getString(7));
|
344
|
retRule.setEntity_type(rs.getString(8));
|
345
|
retRule.setFor_cris(rs.getBoolean(9));
|
346
|
retRule.setId(id);
|
347
|
retRule.setConfiguration(this.getProperties(id));
|
348
|
|
349
|
}
|
350
|
}
|
351
|
|
352
|
|
353
|
} catch (SQLException e) {
|
354
|
logger.error("Accessing DB to get Rule: "+e);
|
355
|
} finally {
|
356
|
if (stmt != null) {
|
357
|
try {
|
358
|
stmt.close();
|
359
|
} catch (SQLException e) {
|
360
|
logger.error("Accessing DB to get Rule: "+e);
|
361
|
}
|
362
|
}
|
363
|
}
|
364
|
// logger.debug("Accessing DB to get Rule with name: "+retRule.getName());
|
365
|
return retRule;
|
366
|
}
|
367
|
|
368
|
@Override
|
369
|
protected int getLastId() throws SQLException {
|
370
|
ResultSet rs = null;
|
371
|
Connection con = null;
|
372
|
PreparedStatement stmt = null;
|
373
|
int retId = -1;
|
374
|
logger.debug("Accessing DB to get Rule's next available id");
|
375
|
try {
|
376
|
con = getConnection();
|
377
|
String query="SELECT currval(pg_get_serial_sequence(?,?)) FROM rules";
|
378
|
stmt = con.prepareStatement(query);
|
379
|
stmt.setString(1, "rules");
|
380
|
stmt.setString(2, "id");
|
381
|
|
382
|
rs = stmt.executeQuery();
|
383
|
if (rs!=null){
|
384
|
rs.next();
|
385
|
retId=rs.getInt(1);
|
386
|
}
|
387
|
|
388
|
|
389
|
} catch (SQLException e) {
|
390
|
logger.error("Error while accessing DB to get Rule's next available id: "+e);
|
391
|
} finally {
|
392
|
if (stmt != null) {
|
393
|
try {
|
394
|
stmt.close();
|
395
|
} catch (SQLException e) {
|
396
|
logger.error("Error while accessing DB to get Rule's next available id: "+e);
|
397
|
}
|
398
|
}
|
399
|
}
|
400
|
return retId;
|
401
|
}
|
402
|
|
403
|
@Override
|
404
|
protected PreparedStatement getUpdateStatement(Rule t, Connection con)
|
405
|
throws SQLException {
|
406
|
// TODO Auto-generated method stub
|
407
|
return null;
|
408
|
}
|
409
|
|
410
|
@Override
|
411
|
protected PreparedStatement getInsertStatement(Rule t, Connection con)
|
412
|
throws SQLException {
|
413
|
// TODO Auto-generated method stub
|
414
|
return null;
|
415
|
}
|
416
|
|
417
|
}
|