Project

General

Profile

« Previous | Next » 

Revision 36960

Added by Nikon Gasparis about 9 years ago

merged from trunk. preparing to release 2.0.2

View differences:

modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/deploy.info
1
{
2
  "type_source": "SVN", 
3
  "goal": "package -U -T 4C source:jar", 
4
  "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-validator-commons/trunk", 
5
  "deploy_repository": "dnet4-snapshots", 
6
  "version": "4", 
7
  "mail": "nikonas@di.uoa.gr, antleb@di.uoa.gr, kiatrop@di.uoa.gr", 
8
  "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet4-snapshots", 
9
  "name": "uoa-validator-commons"
10
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/AbstractDAO.java
1
package eu.dnetlib.validator.commons.dao;
2

  
3
import java.sql.Connection;
4
import java.sql.PreparedStatement;
5
import java.sql.SQLException;
6

  
7
import javax.sql.DataSource;
8

  
9
import org.apache.log4j.Logger;
10
import org.springframework.jdbc.datasource.DataSourceUtils;
11

  
12

  
13
public abstract class AbstractDAO<T> implements DAO<T> {
14

  
15
	protected DataSource datasource = null;
16
	
17
	protected abstract PreparedStatement getUpdateStatement(T t,Connection con) throws SQLException;
18
	protected abstract PreparedStatement getInsertStatement(T t,Connection con) throws SQLException;
19
	protected abstract PreparedStatement getDeleteStatement(int id,Connection con) throws SQLException;
20
	protected abstract int getLastId() throws SQLException;
21
	
22
	protected static Logger logger = Logger.getLogger(AbstractDAO.class);
23
	
24
	@Override
25
	public Integer save(T t) {
26
		Connection con = null;
27
		PreparedStatement stmt = null;
28
		Integer retId = -1;
29
		logger.debug("Accessing DB to save/update ");
30
		try {
31
			con = getConnection();
32
//			stmt = con.prepareStatement(getUpdateStatement(t));
33
			stmt = getUpdateStatement(t,con);
34
			
35
			if (stmt.executeUpdate() == 0) {
36
				stmt.close();
37
				stmt = getInsertStatement(t,con);
38
				stmt.executeUpdate();
39
//				con.commit();
40
				retId=this.getLastId();
41
			}
42
//			retId=this.getLastId();
43
			
44
		} catch (SQLException e) {
45
			logger.error("Error while accessing DB to save/update: "+e);
46
		} finally {
47
			if (stmt != null) {
48
				try {
49
					stmt.close();
50
				} catch (SQLException e) {
51
					logger.error("Error while accessing DB to save/update: "+e);
52
				}
53
			}
54
		}
55
		return retId;
56
	}
57

  
58
	@Override
59
	public String delete(int id) {
60
		Connection con = null;
61
		PreparedStatement stmt = null;
62
		logger.debug("Accessing DB to delete ");
63
		try {
64
			con = getConnection();
65
			stmt = getDeleteStatement(id,con);
66
			
67
			if (stmt.executeUpdate() == 0) {
68
				stmt.close();
69
			}
70
			
71
		} catch (SQLException e) {
72
			
73
			logger.error("Error while accessing DB to delete: "+e);
74
		} finally {
75
			if (stmt != null) {
76
				try {
77
					stmt.close();
78
				} catch (SQLException e) {
79
					logger.error("Error while accessing DB to delete: "+e);
80
				}
81
			}
82
		}
83
		return null;
84
	}
85

  
86
	public Connection getConnection() throws SQLException {
87
		Connection conn = DataSourceUtils.getConnection(datasource);
88
		return conn;
89
	}
90
	public DataSource getDatasource() {
91
		return datasource;
92
	}
93
	public void setDatasource(DataSource datasource) {
94
		this.datasource = datasource;
95
	}
96
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/repositories/RepositoriesDAOImpl.java
1
package eu.dnetlib.validator.commons.dao.repositories;
2

  
3
import eu.dnetlib.validator.commons.dao.AbstractDAO;
4

  
5
import java.sql.Connection;
6
import java.sql.PreparedStatement;
7
import java.sql.ResultSet;
8
import java.sql.SQLException;
9
import java.util.ArrayList;
10
import java.util.List;
11

  
12
public class RepositoriesDAOImpl extends AbstractDAO<RepositoryStored> implements RepositoriesStoredDAO {
13

  
14
	@Override
15
	public List<String> getBaseUrls() {
16
		ResultSet rs = null;
17
		Connection con = null;
18
		PreparedStatement stmt = null;
19
		List<String> retList = null;
20
		logger.debug("Accessing DB to get all activated Repositories Stored");
21
		try {
22
			con = getConnection();
23
			String query="SELECT base_url FROM repositories WHERE activation_id is null";
24
			stmt = con.prepareStatement(query);
25
			rs = stmt.executeQuery();
26
			if (rs!=null){
27
				retList = new ArrayList<String>();				
28
				while (rs.next()) {
29
					retList.add(rs.getString(1));
30
				}				
31
			}
32

  
33
		} catch (SQLException e) {
34
			logger.error("Error while accessing DB to get all activated Repositories Stored: .", e);
35
		} finally {
36
			if (stmt != null) {
37
				try {
38
					stmt.close();
39
				} catch (SQLException e) {
40
					logger.error("Error while accessing DB to get all activated Repositories Stored: .", e);
41
				}
42
			}
43
		}
44
		return retList;
45

  
46
		
47
	}
48
	
49
	@Override
50
	protected PreparedStatement getUpdateStatement(RepositoryStored t,
51
			Connection con) throws SQLException {
52
		String query="UPDATE repositories SET activation_id=? WHERE activation_id=?";
53
		PreparedStatement stmt = con.prepareStatement(query);
54
		stmt.setString(1, null);
55
		stmt.setString(2, t.getActivationId());
56
		return stmt;
57
	}
58

  
59
	@Override
60
	protected PreparedStatement getInsertStatement(RepositoryStored t,
61
			Connection con) throws SQLException {
62
		String query="INSERT INTO repositories(base_url,activation_id) VALUES(?,?)";
63
		PreparedStatement stmt = con.prepareStatement(query);
64
		stmt.setString(1, t.getBaseUrl());
65
		stmt.setString(2, t.getBaseUrl());
66
		return stmt;
67
	}
68

  
69
	@Override
70
	protected PreparedStatement getDeleteStatement(int id, Connection con)
71
			throws SQLException {
72
		// TODO Auto-generated method stub
73
		return null;
74
	}
75

  
76
	@Override
77
	protected int getLastId() throws SQLException {
78
		// TODO Auto-generated method stub
79
		return 0;
80
	}
81

  
82
	@Override
83
	public RepositoryStored get(int id) {
84
		// TODO Auto-generated method stub
85
		return null;
86
	}
87

  
88

  
89
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/repositories/RepositoryStored.java
1
package eu.dnetlib.validator.commons.dao.repositories;
2

  
3
/**
4
 * 
5
 * @author Nikon Gasparis
6
 *
7
 */
8
public class RepositoryStored {
9
	
10
	private String baseUrl;
11
	private String activationId;
12
	
13
	
14
	public String getBaseUrl() {
15
		return baseUrl;
16
	}
17
	public void setBaseUrl(String baseUrl) {
18
		this.baseUrl = baseUrl;
19
	}
20
	public String getActivationId() {
21
		return activationId;
22
	}
23
	public void setActivationId(String activationId) {
24
		this.activationId = activationId;
25
	}
26
	
27
	
28
	
29

  
30
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/repositories/RepositoriesStoredDAO.java
1
package eu.dnetlib.validator.commons.dao.repositories;
2

  
3
import eu.dnetlib.validator.commons.dao.DAO;
4

  
5
import java.util.List;
6

  
7
public interface RepositoriesStoredDAO extends DAO<RepositoryStored> {
8

  
9
	public List<String> getBaseUrls();
10
	
11
	
12

  
13
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/rules/RulesDAO.java
1
package eu.dnetlib.validator.commons.dao.rules;
2

  
3
import java.util.List;
4

  
5
import eu.dnetlib.domain.functionality.validator.Rule;
6
import eu.dnetlib.validator.commons.dao.DAO;
7

  
8
public interface RulesDAO extends DAO<Rule> {
9

  
10
	public List<Rule> getAllRulesByJobTypeEntityType(String jobType, String entityType);
11
	public List<Rule> getAllRulesByJobType(String jobType);
12
	public List<Rule> getAllRules();
13

  
14
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/rules/RulesDAOimpl.java
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
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/rules/RulesetsDAO.java
1
package eu.dnetlib.validator.commons.dao.rules;
2

  
3
import eu.dnetlib.domain.functionality.validator.RuleSet;
4
import eu.dnetlib.validator.commons.dao.DAO;
5

  
6
import java.util.List;
7

  
8
/**
9
 * 
10
 * @author nickonas
11
 *
12
 */
13
public interface RulesetsDAO extends DAO<RuleSet> {
14

  
15
	/**
16
	 * 
17
	 * @return
18
	 */
19
	public List<RuleSet> getRuleSets();
20
	
21
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/rules/RuleStatus.java
1
package eu.dnetlib.validator.commons.dao.rules;
2

  
3
import eu.dnetlib.validator.commons.dao.tasks.TaskStored;
4

  
5
import java.util.ArrayList;
6
import java.util.List;
7

  
8
public class RuleStatus {
9

  
10
	public int total = 0;
11
	public int success = 0;
12
	private int weight = 0;
13
	private List<TaskStored> failedTasks = new ArrayList<TaskStored>();
14
	boolean mandatory;
15

  
16
	public RuleStatus() {
17
		super();
18
	}
19

  
20
	public boolean isMandatory() {
21
		return mandatory;
22
	}
23

  
24
	public void setMandatory(boolean mandatory) {
25
		this.mandatory = mandatory;
26
	}
27

  
28
	public int getTotal() {
29
		return total;
30
	}
31

  
32
	public void setTotal(int total) {
33
		this.total = total;
34
	}
35

  
36
	public int getSuccess() {
37
		return success;
38
	}
39

  
40
	public void setSuccess(int success) {
41
		this.success = success;
42
	}
43

  
44
	public List<TaskStored> getFailedTasks() {
45
		return failedTasks;
46
	}
47

  
48
	public void setFailedTasks(List<TaskStored> failedTasks) {
49
		this.failedTasks = failedTasks;
50
	}
51

  
52
	public int getWeight() {
53
		return weight;
54
	}
55

  
56
	public void setWeight(int weight) {
57
		this.weight = weight;
58
	}
59

  
60
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/rules/RulesetsDAOimpl.java
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.Utilities;
18

  
19
public class RulesetsDAOimpl extends AbstractDAO<RuleSet> implements RulesetsDAO{
20

  
21
	@Override
22
	public Integer save(RuleSet t) {
23
		Connection con = null;
24
		PreparedStatement stmt = null;
25
		Integer retId = -1;
26
		logger.debug("Accessing DB to save/update Rule");
27
		try {
28
			logger.debug("Accessing DB to update Rule");
29
			con = getConnection();
30
			String query="UPDATE rulesets SET name=?, description=?, guidelines_acronym = ?, visibility = ?, short_name=? WHERE id=?";
31
			stmt = con.prepareStatement(query);
32
			stmt.setString(1, t.getName());
33
			stmt.setString(2, t.getDescription());
34
			stmt.setString(3, t.getGuidelinesAcronym());
35
			String[] data = t.getVisibility().toArray(new String[t.getVisibility().size()]);
36
			stmt.setArray(4, con.createArrayOf("text", data));
37
			stmt.setString(5, t.getShortName());
38
			stmt.setInt(6, t.getId());
39
			
40
			if (stmt.executeUpdate() == 0) {
41
				stmt.close();
42
				logger.debug("Accessing DB to save RuleSet with name: "+t.getName());
43
				query="INSERT INTO rulesets(name,description,guidelines_acronym,visibility,short_name) VALUES(?,?,?,?,?)";
44
				stmt = con.prepareStatement(query);
45
				stmt.setString(1, t.getName());
46
				stmt.setString(2, t.getDescription());
47
				stmt.setString(3, t.getGuidelinesAcronym());
48
				data = t.getVisibility().toArray(new String[t.getVisibility().size()]);
49
				stmt.setArray(4, con.createArrayOf("text", data));
50
				stmt.setString(5, t.getShortName());
51
				stmt.executeUpdate();
52
				retId = this.getLastId();
53
			} else {
54
				logger.debug("Accessing DB to update RuleSet-done");
55
				retId=t.getId();
56
			}
57
			
58
			stmt.close();
59
			logger.debug("Accessing DB to delete ruleSet_has_rules values");
60
			query="DELETE FROM ruleset_has_rules " + " WHERE ruleset_id=?";
61
			stmt = con.prepareStatement(query);
62
			stmt.setInt(1, retId);		
63
			if (stmt.executeUpdate() == 0) {
64
				stmt.close();
65
			}			
66
			logger.debug("Accessing DB to insert ruleSet_has_rules properties");
67
		    query="INSERT INTO ruleset_has_rules(ruleset_id, rule_id) VALUES (?,?)";
68
			stmt= con.prepareStatement(query);
69
			for (int id : t.getContentRulesIds()) {
70
				stmt.setInt(1,retId);
71
				stmt.setInt(2,id);
72
				stmt.addBatch();
73
			}
74
			for (int id : t.getUsageRulesIds()) {
75
				stmt.setInt(1,retId);
76
				stmt.setInt(2,id);
77
				stmt.addBatch();
78
			}
79
			stmt.executeBatch();
80
		    logger.debug("Ruleset_has_rules values inserted/updated");
81
		    
82
		} catch (SQLException e) {
83
			logger.error("Error accessing DB to save ruleset.", e);
84
		} finally {
85
			if (stmt != null) {
86
				try {
87
					stmt.close();
88
				} catch (SQLException e) {
89
					logger.error("Error accessing DB to save ruleset.", e);
90
				}
91
			}
92
		}
93
		return retId;
94
	}
95
	
96
	@Override
97
	public RuleSet get(int id) {
98
		ResultSet rs = null;
99
		Connection con = null;
100
		PreparedStatement stmt = null;
101
		RuleSet retSet = null;
102
		logger.debug("Accessing DB to get RuleSet with id: " + id);
103
		try {
104
			con = getConnection();
105
			String query="SELECT name, description, guidelines_acronym, visibility, short_name FROM rulesets WHERE id=?";
106
			stmt = con.prepareStatement(query);
107
			stmt.setInt(1, id);
108
			rs = stmt.executeQuery();
109
			if (rs!=null){
110
				retSet = new RuleSet();
111
				while (rs.next()) {
112
					retSet.setName(rs.getString(1));
113
					retSet.setDescription(rs.getString(2));
114
					retSet.setGuidelinesAcronym(rs.getString(3));
115
					List<String> visibility = new ArrayList<String>();
116
					Array tt = rs.getArray("visibility");
117
					if (tt != null) {
118
						String[] ent = (String[])tt.getArray();
119
						visibility.addAll(Arrays.asList(ent));
120
					}
121
					retSet.setVisibility(visibility);
122
					retSet.setShortName(rs.getString(5));
123
					retSet.setId(id);
124
					List <Rule> rules = this.getRulesOfRuleset(id);
125
					List <Rule> contentRules = new ArrayList<Rule>();
126
					List <Rule> usageRules = new ArrayList<Rule>();
127
					Set <Integer> contentRulesIds = new HashSet<Integer>();
128
					Set <Integer> usageRulesIds = new HashSet<Integer>();
129
					for (Rule rule : rules) {
130
						if (rule.getJob_type().equals("content")) {
131
							contentRules.add(rule);
132
							contentRulesIds.add(rule.getId());
133
						} else if (rule.getJob_type().equals("usage")) {
134
							usageRules.add(rule);
135
							usageRulesIds.add(rule.getId());
136
						}
137
					}
138
					retSet.setContentRules(contentRules);
139
					retSet.setUsageRules(usageRules);
140
					retSet.setContentRulesIds(contentRulesIds);
141
					retSet.setUsageRulesIds(usageRulesIds);
142
				}				
143
			}
144
		} catch (SQLException e) {
145
			logger.error("Accessing DB to get RuleSet.", e);
146
		} finally {
147
			if (stmt != null) {
148
				try {
149
					stmt.close();
150
				} catch (SQLException e) {
151
					logger.error("Accessing DB to get RuleSet.", e);
152
				}
153
			}
154
		}
155
		return retSet;
156
	}
157
	
158
	public List<RuleSet> updateRuleSetHasRules() {
159
		ResultSet rs = null;
160
		Connection con = null;
161
		PreparedStatement stmt = null, stmt1 = null;;
162
		List<RuleSet> retList = new ArrayList<RuleSet>();
163
		RuleSet retSet = null;
164
		logger.debug("Accessing DB to get all RuleSets");
165
		try {
166
			con = getConnection();
167
			String query="SELECT name, description, guidelines_acronym, visibility, content_rules, usage_rules, id, short_name FROM rulesets";
168
			stmt = con.prepareStatement(query);
169
			rs = stmt.executeQuery();
170
			if (rs!=null){
171
				
172
				while (rs.next()) {
173
					retSet = new RuleSet();
174
					retSet.setName(rs.getString(1));
175
					retSet.setDescription(rs.getString(2));
176
					retSet.setGuidelinesAcronym(rs.getString(3));
177
					retSet.setVisibility(Utilities.convertStringToList(rs.getString(4)));
178
					retSet.setId(rs.getInt(7));
179
					retSet.setShortName(rs.getString(8));
180
					retList.add(retSet);
181
					String query1="INSERT INTO ruleset_has_rules(ruleset_id, rule_id) values (?,?)";
182
					stmt1 = con.prepareStatement(query1);
183
					for (String id : Utilities.convertStringToList(rs.getString(5))) {
184
						stmt1.setInt(1,retSet.getId());
185
						stmt1.setInt(2,Integer.parseInt(id));
186
						stmt1.addBatch();
187
					}
188
					for (String id : Utilities.convertStringToList(rs.getString(6))) {
189
						stmt1.setInt(1,retSet.getId());
190
						stmt1.setInt(2,Integer.parseInt(id));
191
						stmt1.addBatch();
192
					}
193
					stmt1.executeBatch();
194
				}				
195
			}
196

  
197
		} catch (SQLException e) {
198
			logger.error("Accessing DB to get all RuleSets.", e);
199
		} finally {
200
			if (stmt != null) {
201
				try {
202
					stmt.close();
203
				} catch (SQLException e) {
204
					logger.error("Accessing DB to get all RuleSets.", e);
205
				}
206
			}
207
		}
208
		return retList;
209
	}
210
	@Override
211
	public List<RuleSet> getRuleSets() {
212
		ResultSet rs = null;
213
		Connection con = null;
214
		PreparedStatement stmt = null;
215
		List<RuleSet> retList = new ArrayList<RuleSet>();
216
		RuleSet retSet = null;
217
		logger.debug("Accessing DB to get all RuleSets");
218
		try {
219
			con = getConnection();
220
			String query="SELECT name, description, guidelines_acronym, visibility, id, short_name FROM rulesets ORDER BY id";
221
			stmt = con.prepareStatement(query);
222
			rs = stmt.executeQuery();
223
			if (rs!=null){
224
				
225
				while (rs.next()) {
226
					retSet = new RuleSet();
227
					retSet.setName(rs.getString(1));
228
					retSet.setDescription(rs.getString(2));
229
					retSet.setGuidelinesAcronym(rs.getString(3));
230
					List<String> visibility = new ArrayList<String>();
231
					Array tt = rs.getArray("visibility");
232
					if (tt != null) {
233
						String[] ent = (String[])tt.getArray();
234
						visibility.addAll(Arrays.asList(ent));
235
					}
236
					retSet.setVisibility(visibility);
237
					retSet.setId(rs.getInt(5));
238
					retSet.setShortName(rs.getString(6));
239
					List <Rule> rules = this.getRulesOfRuleset(retSet.getId());
240
					List <Rule> contentRules = new ArrayList<Rule>();
241
					List <Rule> usageRules = new ArrayList<Rule>();
242
					Set <Integer> contentRulesIds = new HashSet<Integer>();
243
					Set <Integer> usageRulesIds = new HashSet<Integer>();
244
					for (Rule rule : rules) {
245
						if (rule.getJob_type().equals("content")) {
246
							contentRules.add(rule);
247
							contentRulesIds.add(rule.getId());
248
						} else if (rule.getJob_type().equals("usage")) {
249
							usageRules.add(rule);
250
							usageRulesIds.add(rule.getId());
251
						}
252
					}
253
					retSet.setContentRules(contentRules);
254
					retSet.setUsageRules(usageRules);
255
					retSet.setContentRulesIds(contentRulesIds);
256
					retSet.setUsageRulesIds(usageRulesIds);
257
					retList.add(retSet);
258
				}				
259
			}
260

  
261
		} catch (SQLException e) {
262
			logger.error("Accessing DB to get all RuleSets.", e);
263
		} finally {
264
			if (stmt != null) {
265
				try {
266
					stmt.close();
267
				} catch (SQLException e) {
268
					logger.error("Accessing DB to get all RuleSets.", e);
269
				}
270
			}
271
		}
272
		return retList;
273
	}
274

  
275
	public List<Rule> getRulesOfRuleset(int ruleSetId) {
276
		ResultSet rs = null;
277
		Connection con = null;
278
		PreparedStatement stmt = null;
279
		Rule retRule = null;
280
		List<Rule> retList = null;
281
		logger.debug("Accessing DB to get All Rules of ruleset");
282
		try {
283
			con = getConnection();
284
			String query="SELECT * FROM rules r, ruleset_has_rules rhs WHERE rhs.rule_id = r.id AND rhs.ruleset_id=?";
285
			stmt = con.prepareStatement(query);
286
			stmt.setInt(1, ruleSetId);
287
			rs = stmt.executeQuery();
288
			if (rs!=null){
289
				retList = new ArrayList<Rule>();				
290
				while (rs.next()) {
291
					retRule = new Rule();
292
					retRule.setName(rs.getString("name"));
293
					retRule.setDescription(rs.getString("description"));
294
					retRule.setType(rs.getString("type"));
295
					retRule.setMandatory(rs.getBoolean("mandatory"));
296
					retRule.setWeight(rs.getInt("weight"));
297
					retRule.setProvider_information(rs.getString("provider_information"));
298
					retRule.setId(rs.getInt("id"));
299
					retRule.setEntity_type(rs.getString("entity_type"));
300
					retRule.setFor_cris(rs.getBoolean("for_cris"));
301
					retRule.setJob_type(rs.getString("job_type"));
302
//					retRule.setConfiguration(this.getProperties(rs.getInt("id")));
303
					retList.add(retRule);
304
				}				
305
			}
306
			logger.debug("rules: " + retList.size());
307

  
308
		} catch (SQLException e) {
309
			logger.error("Error accessing DB to get All Rules by jobType.", e);
310
		} finally {
311
			if (stmt != null) {
312
				try {
313
					stmt.close();
314
				} catch (SQLException e) {
315
					logger.error("Error accessing DB to get All Rules by jobType.", e);
316
				}
317
			}
318
		}
319
		return retList;
320

  
321
	}
322

  
323
	@Override
324
	protected PreparedStatement getDeleteStatement(int id, Connection con)
325
			throws SQLException {
326
		String query="DELETE FROM rulesets WHERE id=?";
327
		PreparedStatement stmt = con.prepareStatement(query);
328
		stmt.setInt(1,id);
329
		return stmt;
330
	}
331

  
332
	@Override
333
	protected int getLastId() throws SQLException {
334
		ResultSet rs = null;
335
		Connection con = null;
336
		PreparedStatement stmt = null;
337
		int retId = -1;
338
		logger.debug("Accessing DB to get RuleSet's next available id");
339
		try {
340
			con = getConnection();
341
			String query="SELECT currval(pg_get_serial_sequence(?,?)) FROM rulesets";
342
			stmt = con.prepareStatement(query);
343
			stmt.setString(1, "rulesets");
344
			stmt.setString(2, "id");
345
			
346
			rs = stmt.executeQuery();
347
			if (rs!=null){
348
				rs.next();
349
				retId=rs.getInt(1);
350
			}
351

  
352

  
353
		} catch (SQLException e) {
354
			logger.error("Error while accessing DB to get RuleSet's next available id.", e);
355
		} finally {
356
			if (stmt != null) {
357
				try {
358
					stmt.close();
359
				} catch (SQLException e) {
360
					logger.error("Error while accessing DB to get RuleSet's next available id.", e);
361
				}
362
			}
363
		}
364
		return retId;
365
	}
366

  
367
	@Override
368
	protected PreparedStatement getUpdateStatement(RuleSet t, Connection con)
369
			throws SQLException {
370
		// TODO Auto-generated method stub
371
		return null;
372
	}
373

  
374
	@Override
375
	protected PreparedStatement getInsertStatement(RuleSet t, Connection con)
376
			throws SQLException {
377
		// TODO Auto-generated method stub
378
		return null;
379
	}
380
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/Utilities.java
1
package eu.dnetlib.validator.commons.dao;
2

  
3
import java.util.ArrayList;
4
import java.util.HashSet;
5
import java.util.List;
6
import java.util.Set;
7

  
8
public class Utilities {
9

  
10
	public static Set<Integer> convertArrayToSet (String[] array){
11
		Set<Integer> ret= new HashSet<Integer>();
12
		for (String id : array) {
13
			ret.add(Integer.parseInt(id));
14
		}
15
		return ret;
16
	}
17
	
18
	public static List<String> convertArrayToList (String[] array){
19
		List<String> ret= new ArrayList<String>();
20
		for (String id : array) {
21
			ret.add(id);
22
		}
23
		return ret;
24
	}
25

  
26
	public static Set<Integer> convertListToSet (List<String> list){
27
		Set<Integer> ret= new HashSet<Integer>();
28
		for (String id : list) {
29
			ret.add(Integer.parseInt(id));
30
		}
31
		return ret;
32
	}	
33
	
34
	public static String convertListToString(List<String> ruleIds) {
35
		String rules = "";
36
		for (int i = 0; i < ruleIds.size() - 1; i++)
37
			rules += ruleIds.get(i) + ",";
38

  
39
		rules += ruleIds.get(ruleIds.size() - 1);
40
		return rules;
41
	}
42

  
43
	public static List<String> convertStringToList(String rules) {
44
		List<String> ruleIds = new ArrayList<String>();
45
		String[] arrRules = rules.split(",");
46
		for (String sRule : arrRules) {
47
			ruleIds.add(sRule.trim());
48
		}
49
		return ruleIds;
50
	}
51

  
52
	public static String convertSetToString(Set<Integer> ruleIds) {
53
		String rules = "";
54
		for (int i  : ruleIds)
55
			rules += i + ",";
56
		rules = rules.substring(0,rules.length()-1);
57
		return rules;
58
	}
59
	
60
	public static Set<Integer> convertStringToSet(String rules) {
61
		Set<Integer> ruleIds = new HashSet<Integer>();
62
		String[] arrRules = rules.split(",");
63
		for (String sRule : arrRules) {
64
			ruleIds.add(Integer.parseInt(sRule.trim()));
65
		}
66
		return ruleIds;
67
	}
68
	
69
	public static String formatTime(long mills) {
70
		long secs = mills / 1000;
71
		long mins = secs / 60;
72
		if (mins == 0)
73
			return secs + " secs";
74
		secs = secs % 60;
75
		long hours = mins / 60;
76
		if (hours == 0)
77
			return mins + " mins " + secs + " secs";
78
		mins = mins % 60;
79
		return hours + " hours " + mins + " mins " + secs + " secs";
80
	}	
81
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/jobs/Entry.java
1
package eu.dnetlib.validator.commons.dao.jobs;
2

  
3
import java.util.List;
4

  
5
public class Entry {
6

  
7
	private String name, description;
8
	private String successes;
9
	private int weight;
10
	private List<String> errors;
11
	private int ruleId;
12
	private boolean hasErrors, mandatory;
13
	
14
	public String getName() {
15
		return name;
16
	}
17
	public void setName(String name) {
18
		this.name = name;
19
	}
20
	public String getDescription() {
21
		return description;
22
	}
23
	public void setDescription(String description) {
24
		this.description = description;
25
	}
26
	public String getSuccesses() {
27
		return successes;
28
	}
29
	public void setSuccesses(String successes) {
30
		this.successes = successes;
31
	}
32
	public List<String> getErrors() {
33
		return errors;
34
	}
35
	public void setErrors(List<String> errors) {
36
		this.errors = errors;
37
	}
38
	public void setRuleId(int ruleId) {
39
		this.ruleId = ruleId;
40
	}
41
	public int getRuleId() {
42
		return ruleId;
43
	}
44
	public void setHasErrors(boolean hasErrors) {
45
		this.hasErrors = hasErrors;
46
	}
47
	public boolean isHasErrors() {
48
		return hasErrors;
49
	}
50
	public void setWeight(int weight) {
51
		this.weight = weight;
52
	}
53
	public int getWeight() {
54
		return weight;
55
	}
56
	public boolean isMandatory() {
57
		return mandatory;
58
	}
59
	public void setMandatory(boolean mandatory) {
60
		this.mandatory = mandatory;
61
	}
62

  
63
	
64
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/jobs/JobsDAO.java
1
package eu.dnetlib.validator.commons.dao.jobs;
2

  
3
import java.util.List;
4
import java.util.Map;
5

  
6
import eu.dnetlib.domain.functionality.validator.StoredJob;
7
import eu.dnetlib.validator.commons.dao.DAO;
8
import eu.dnetlib.validator.commons.dao.rules.RuleStatus;
9

  
10
public interface JobsDAO extends DAO<StoredJob> {
11

  
12
	public int deleteUncompletedJobs();
13
	
14
	public int deleteOld(String date, String period, String jobType);
15

  
16
	public int setJobFinished(int jobId,
17
			Map<String, Map<Integer, RuleStatus>> scoreMapPerGroupBy,
18
			String error, Boolean failed, int objsValidated, String validationType);
19
	public void setTotalJobFinished(int jobId, String error, Boolean failed);
20

  
21
	public void setStatus(int jobId, String status, int recordsTested, String validationType);
22
	
23
	public List<StoredJob> getUncompletedJobs();
24
	
25
	public List<StoredJob> getJobs(String userName, String jobType, Integer offset, Integer limit, String dateFrom, String dateTo);
26
	
27
	public int getJobsTotalNumber(String userName, String jobType);
28

  
29
	public StoredJob getJobSummary(int jobId, String groupby);
30
	
31
	public void importOldJobs();
32
	
33
	/*
34
	public Map<String, List<StoredJob>> getJobsOfUserSplitted(String userName);
35

  
36
	public boolean getJobError(int jobId);
37

  
38
	public void deleteOldCompatibilityTestsOnly(String date);
39
	
40
	public void deleteJobForRegistration(String activationId);
41

  
42
	public void deleteSemiCompletedRegistrationJobs(String activationId);
43
	
44
	public void deleteJobsForRegistration();
45

  
46

  
47
	*/
48

  
49
}
modules/uoa-validator-commons/branches/uoa-validator-commons-2.0/uoa-validator-commons-2.0.0/src/main/java/eu/dnetlib/validator/commons/dao/jobs/JobsDAOImpl.java
1
package eu.dnetlib.validator.commons.dao.jobs;
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.text.ParseException;
9
import java.text.SimpleDateFormat;
10
import java.util.ArrayList;
11
import java.util.Arrays;
12
import java.util.Calendar;
13
import java.util.Date;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Map.Entry;
19
import java.util.Set;
20

  
21
import eu.dnetlib.domain.functionality.validator.JobForValidation;
22
import eu.dnetlib.domain.functionality.validator.JobResultEntry;
23
import eu.dnetlib.domain.functionality.validator.StoredJob;
24
import eu.dnetlib.validator.commons.dao.AbstractDAO;
25
import eu.dnetlib.validator.commons.dao.Utilities;
26
import eu.dnetlib.validator.commons.dao.rules.RuleStatus;
27

  
28
public class JobsDAOImpl extends AbstractDAO<StoredJob> implements JobsDAO{
29

  
30
	@Override
31
	public Integer save(StoredJob job) {
32
		Connection con = null;
33
		PreparedStatement stmt = null;
34
		Integer retId = -1;
35
		logger.debug("Accessing DB to save/update Job");
36
		try {
37
			logger.debug("Accessing DB to update Job");
38
			Calendar cal = Calendar.getInstance();
39
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
40

  
41
			con = getConnection();
42
			logger.debug("getting submittedjob updateStatement");
43
			String query="UPDATE jobs SET validation_type=?, started=to_timestamp(?, 'YYYY-MM-DD HH24:MI:SS'), guidelines=?, content_job_status=?, repo=?, duration=? WHERE id=?";
44

  
45
			stmt = con.prepareStatement(query);
46
			stmt.setString(1, job.getValidationType());
47
			stmt.setString(2, sdf.format(cal.getTime()));
48
			stmt.setString(3, job.getDesiredCompatibilityLevel());
49
			stmt.setString(4, job.getContentJobStatus());
50
			stmt.setString(5, job.getBaseUrl());
51
			stmt.setString(6, job.getDuration());
52
			stmt.setInt(7, job.getId());
53
			
54
			if (stmt.executeUpdate() == 0) {
55
				stmt.close();
56
				logger.debug("Accessing DB to save Job");
57
				query="INSERT INTO jobs(validation_type,started,guidelines,user_email,content_job_status, usage_job_status, repo, duration, rules, records, set, groupby_xpath, metadata_prefix, job_type) VALUES(?,to_timestamp(?, 'YYYY-MM-DD HH24:MI:SS'),?,?,?,?,?,?,?,?,?,?,?,?)";
58
				stmt = con.prepareStatement(query);
59
				stmt.setString(1, job.getValidationType());
60
				stmt.setString(2, sdf.format(cal.getTime()));
61
//				stmt.setTimestamp(2, getCurrentTimeStamp());
62
				stmt.setString(3, job.getDesiredCompatibilityLevel());
63
				stmt.setString(4, job.getUserEmail());
64
				stmt.setString(5, job.getContentJobStatus());
65
				stmt.setString(6, job.getUsageJobStatus());
66
				stmt.setString(7, job.getBaseUrl());
67
				stmt.setString(8, job.getDuration());
68
				stmt.setString(9, Utilities.convertSetToString(job.getRules()));
69
				stmt.setString(10, Integer.toString(job.getRecords()));
70
				stmt.setString(11, job.getValidationSet());
71
				stmt.setString(12, job.getGroupByXpath());
72
				stmt.setString(13, job.getMetadataPrefix());
73
				stmt.setString(14, job.getJobType());
74
				stmt.executeUpdate();
75
				retId = this.getLastId();
76
			} else {
77
				logger.debug("Accessing DB to update job-done");
78
				retId=job.getId();
79
			}
80
			
81
			if (job.isRegistration()) {
82
				this.storeJobForRegistration(job, retId);
83
			}
84
			if (job.isCris()) {
85
				this.storeJobForCris(job, retId);
86
			}
87
			stmt.close();
88
		    
89
		} catch (SQLException e) {
90
			logger.error("Error accessing DB to get save/update Rule.", e);
91
		} finally {
92
			if (stmt != null) {
93
				try {
94
					stmt.close();
95
				} catch (SQLException e) {
96
					logger.error("Error accessing DB to get save/update Rule.", e);
97
				}
98
			}
99
		}
100
		return retId;
101
	}
102

  
103
	@Override
104
	protected PreparedStatement getDeleteStatement(int id, Connection con) throws SQLException {
105
		String query="DELETE FROM jobs " + " WHERE id=?";
106
		PreparedStatement stmt = con.prepareStatement(query);
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff