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