Project

General

Profile

« Previous | Next » 

Revision 36962

Added by Nikon Gasparis over 9 years ago

[maven-release-plugin] copy for tag uoa-validator-commons-2.0.2

View differences:

modules/uoa-validator-commons/tags/uoa-validator-commons-2.0.2/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/tags/uoa-validator-commons-2.0.2/src/main/java/eu/dnetlib/validator/commons/dao/users/UsersDAO.java
1
package eu.dnetlib.validator.commons.dao.users;
2

  
3
import eu.dnetlib.domain.functionality.UserProfile;
4
import eu.dnetlib.validator.commons.dao.DAO;
5
import eu.dnetlib.validator.commons.dao.DaoException;
6

  
7

  
8
public interface UsersDAO extends DAO<UserProfile> {
9

  
10
	public boolean checkCorrectCreds(String email, String password) throws DaoException;
11

  
12
	public boolean isAdmin(String email) throws DaoException;
13

  
14
	public boolean isRepoAdmin(String email) throws DaoException;
15
	
16
	public boolean isActivated(String email) throws DaoException;
17
	
18
	public boolean activateUser(String activation_id) throws DaoException;
19

  
20
	public boolean userExists(String email) throws DaoException;
21
	
22
	public boolean usernameExists(String username) throws DaoException;
23

  
24
	public void prepareResetPassword(String uuid, String email) throws DaoException;
25

  
26
	public void ResetPassword(String activation_id, String password) throws DaoException;
27
	
28
	public UserProfile get(String email) throws DaoException;
29

  
30
}
modules/uoa-validator-commons/tags/uoa-validator-commons-2.0.2/src/main/java/eu/dnetlib/validator/commons/dao/users/UsersDAOimpl.java
1
package eu.dnetlib.validator.commons.dao.users;
2

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

  
8
import eu.dnetlib.domain.functionality.UserProfile;
9
import eu.dnetlib.validator.commons.dao.AbstractDAO;
10
import eu.dnetlib.validator.commons.dao.DaoException;
11

  
12
public class UsersDAOimpl extends AbstractDAO<UserProfile> implements UsersDAO {
13

  
14

  
15
	@Override
16
	protected PreparedStatement getUpdateStatement(UserProfile t, Connection con)
17
			throws SQLException {
18
		String query="UPDATE users SET email=?, institution=?, firstname=?, lastname=? WHERE username=?";
19
		PreparedStatement stmt = con.prepareStatement(query);
20
		stmt.setString(1, t.getEmail());
21
		stmt.setString(2, t.getInstitution());
22
		stmt.setString(3, t.getFirstname());
23
		stmt.setString(4, t.getLastname());
24
		stmt.setString(5, t.getUsername());
25
		return stmt;
26
	}
27

  
28
	
29
	@Override
30
	protected PreparedStatement getInsertStatement(UserProfile t, Connection con)
31
			throws SQLException {
32
		String query="INSERT INTO users(email,password,institution,username,firstname,lastname,activation_id) VALUES(?,?,?,?,?,?,?)";
33
		PreparedStatement stmt = con.prepareStatement(query);
34
		stmt.setString(1, t.getEmail());
35
		stmt.setString(2, t.getPassword());
36
		stmt.setString(3, t.getInstitution());
37
		stmt.setString(4, t.getUsername());
38
		stmt.setString(5, t.getFirstname());
39
		stmt.setString(6, t.getLastname());
40
		stmt.setString(7, t.getActivationId());		
41
		return stmt;
42
	}
43

  
44
	@Override
45
	protected PreparedStatement getDeleteStatement(int id, Connection con)
46
			throws SQLException {
47
		// TODO Auto-generated method stub
48
		return null;
49
	}
50

  
51
	@Override
52
	protected int getLastId() throws SQLException {
53
		// TODO Auto-generated method stub
54
		return 0;
55
	}
56

  
57
	@Override
58
	public UserProfile get(String email) throws DaoException {
59
		ResultSet rs = null;
60
		Connection con = null;
61
		PreparedStatement stmt = null;
62
		UserProfile retUser = null;
63
		logger.debug("Accessing DB to get User with email: " + email);
64
		try {
65
			con = getConnection();
66
			String query="SELECT * FROM users WHERE email=? OR username=?";
67
			stmt = con.prepareStatement(query);
68
			stmt.setString(1, email);
69
			stmt.setString(2, email);
70
			rs = stmt.executeQuery();
71
			if (rs!=null){
72
				if (rs.next()) {
73
					retUser = new UserProfile();
74
					retUser.setEmail(rs.getString("email"));
75
					retUser.setUsername(rs.getString("username"));
76
					retUser.setFirstname(rs.getString("firstname"));
77
					retUser.setLastname(rs.getString("lastname"));
78
					retUser.setInstitution(rs.getString("institution"));
79
					retUser.setActivationId(rs.getString("activation_id"));
80
				}				
81
			}
82
		} catch (Exception e) {
83
			logger.error("Error Accessing DB to get User.", e);
84
			throw new DaoException(e);
85
		} finally {
86
			if (stmt != null) {
87
				try {
88
					stmt.close();
89
				} catch (SQLException e) {
90
					logger.error("Error Accessing DB to get User.", e);
91
					throw new DaoException(e);
92
				}
93
			}
94
		}
95
		return retUser;
96
	
97
	}
98
	
99
	@Override
100
	public boolean activateUser(String activation_id) throws DaoException {
101
		Connection con = null;
102
		PreparedStatement stmt = null;
103
		logger.debug("Accessing DB to activate user");
104
		try {
105
			con = getConnection();
106
			String query="UPDATE users SET activation_id=? WHERE activation_id=?";
107
			stmt = con.prepareStatement(query);
108
			stmt.setString(1, null);
109
			stmt.setString(2, activation_id);
110
			if (stmt.executeUpdate() > 0){
111
				return true;
112
			}
113
		} catch (Exception e) {
114
			logger.error("Error while Accessing DB to activate user.", e);
115
			throw new DaoException(e);
116
		} finally {
117
			if (stmt != null) {
118
				try {
119
					stmt.close();
120
				} catch (SQLException e) {
121
					logger.error("Error while Accessing DB to activate user.", e);
122
					throw new DaoException(e);
123
				}
124
			}
125
		}
126
		return false;
127
	}
128
	
129
	@Override
130
	public UserProfile get(int id) {
131
		// TODO Auto-generated method stub
132
		return null;
133
	}
134

  
135

  
136
	@Override
137
	public boolean checkCorrectCreds(String email, String password) throws DaoException {
138
		ResultSet rs = null;
139
		Connection con = null;
140
		PreparedStatement stmt = null;
141
		logger.debug("Accessing DB to check correct credentials");
142
		try {
143
			con = getConnection();
144
			String query="SELECT * FROM users WHERE email=? AND password=?";
145
			stmt = con.prepareStatement(query);
146
			stmt.setString(1,email);
147
			stmt.setString(2,password);
148
			rs = stmt.executeQuery();
149
			if (rs.next()){
150
				return true;
151
			}
152
		} catch (Exception e) {
153
			logger.error("Error while accessing DB to check correct credentials.", e);
154
			throw new DaoException(e);
155
		} finally {
156
			if (stmt != null) {
157
				try {
158
					stmt.close();
159
				} catch (SQLException e) {
160
					logger.error("Error while accessing DB to check correct credentials.", e);
161
					throw new DaoException(e);
162
				}
163
			}
164
		}
165
		return false;
166
	}
167

  
168

  
169
	@Override
170
	public boolean isAdmin(String email) throws DaoException {
171
		ResultSet rs = null;
172
		Connection con = null;
173
		PreparedStatement stmt = null;
174
		logger.debug("Accessing DB to check if user is admin");
175
		try {
176
			con = getConnection();
177
			String query="SELECT * FROM admins WHERE username=? AND level=?";
178
			stmt = con.prepareStatement(query);
179
			stmt.setString(1,email);
180
			stmt.setString(2,"master");
181
			rs = stmt.executeQuery();
182
			if (rs.next()){
183
				return true;
184
			}
185
		} catch (Exception e) {
186
			logger.error("Error while accessing DB to check if user is admin.", e);
187
			throw new DaoException(e);
188
		} finally {
189
			if (stmt != null) {
190
				try {
191
					stmt.close();
192
				} catch (SQLException e) {
193
					logger.error("Error while accessing DB to check if user is admin.", e);
194
					throw new DaoException(e);
195
				}
196
			}
197
		}
198
		return false;
199
	}
200

  
201
	@Override
202
	public boolean isRepoAdmin(String email) throws DaoException {
203
		ResultSet rs = null;
204
		Connection con = null;
205
		PreparedStatement stmt = null;
206
		logger.debug("Accessing DB to check if user is repoAdmin");
207
		try {
208
			con = getConnection();
209
			String query="SELECT * FROM admins WHERE username=? AND level=?";
210
			stmt = con.prepareStatement(query);
211
			stmt.setString(1,email);
212
			stmt.setString(2,"secondary");
213
			rs = stmt.executeQuery();
214
			if (rs.next()){
215
				return true;
216
			}
217
		} catch (Exception e) {
218
			logger.error("Error while accessing DB to check if user is repoAdmin.", e);
219
			throw new DaoException(e);
220
		} finally {
221
			if (stmt != null) {
222
				try {
223
					stmt.close();
224
				} catch (SQLException e) {
225
					logger.error("Error while accessing DB to check if user is repoAdmin.", e);
226
					throw new DaoException(e);
227
				}
228
			}
229
		}
230
		return false;
231
	}
232

  
233
	@Override
234
	public boolean isActivated(String email) throws DaoException {
235
		ResultSet rs = null;
236
		Connection con = null;
237
		PreparedStatement stmt = null;
238
		logger.debug("Accessing DB to check if user is activated");
239
		try {
240
			con = getConnection();
241
			String query="SELECT * FROM users WHERE email=? AND activation_id is null";
242
			stmt = con.prepareStatement(query);
243
			stmt.setString(1,email);
244
//			stmt.setString(2,"NULL");
245
			rs = stmt.executeQuery();
246
			if (rs.next()){
247
				return true;
248
			}
249
		} catch (Exception e) {
250
			logger.error("Error while accessing DB to check if user is activated .", e);
251
			throw new DaoException(e);
252
		} finally {
253
			if (stmt != null) {
254
				try {
255
					stmt.close();
256
				} catch (SQLException e) {
257
					logger.error("Error while accessing DB to check if user is activated .", e);
258
					throw new DaoException(e);
259
				}
260
			}
261
		}
262
		return false;
263
	}
264

  
265

  
266
	@Override
267
	public boolean userExists(String email) throws DaoException {
268
		ResultSet rs = null;
269
		Connection con = null;
270
		PreparedStatement stmt = null;
271
		logger.debug("Accessing DB to check if user "+email+" exists");
272
		try {
273
			con = getConnection();
274
			String query="SELECT * FROM users WHERE email=?";
275
			stmt = con.prepareStatement(query);
276
			stmt.setString(1,email);
277
			rs = stmt.executeQuery();
278
			if (rs.next()){
279
				logger.debug("user exists");
280
				return true;
281
			}
282
		} catch (Exception e) {
283
			logger.error("Error while accessing DB to check if user exists.", e);
284
			throw new DaoException(e);
285
		} finally {
286
			if (stmt != null) {
287
				try {
288
					stmt.close();
289
				} catch (SQLException e) {
290
					logger.error("Error while accessing DB to check if user exists.", e);
291
					throw new DaoException(e);
292
				}
293
			}
294
		}
295
		return false;
296
	}
297
	
298
	@Override
299
	public boolean usernameExists(String username) throws DaoException {
300
		ResultSet rs = null;
301
		Connection con = null;
302
		PreparedStatement stmt = null;
303
		logger.debug("Accessing DB to check if user "+ username +" exists");
304
		try {
305
			con = getConnection();
306
			String query="SELECT * FROM users WHERE username=?";
307
			stmt = con.prepareStatement(query);
308
			stmt.setString(1,username);
309
			rs = stmt.executeQuery();
310
			if (rs.next()){
311
				logger.debug("user exists");
312
				return true;
313
			}
314
		} catch (Exception e) {
315
			logger.error("Error while accessing DB to check if user exists.", e);
316
			throw new DaoException(e);
317
		} finally {
318
			if (stmt != null) {
319
				try {
320
					stmt.close();
321
				} catch (SQLException e) {
322
					logger.error("Error while accessing DB to check if user exists.", e);
323
					throw new DaoException(e);
324
				}
325
			}
326
		}
327
		return false;
328
	}
329

  
330

  
331
	@Override
332
	public void prepareResetPassword(String uuid, String email) throws DaoException {
333
		Connection con = null;
334
		PreparedStatement stmt = null;
335
		logger.debug("Accessing DB to prepare reset password");
336
		try {
337
			con = getConnection();
338
			String query="UPDATE users SET activation_id=? WHERE email=?";;
339
			stmt = con.prepareStatement(query);
340
			stmt.setString(1,uuid);
341
			stmt.setString(2,email);
342
			stmt.executeUpdate();
343
		} catch (Exception e) {
344
			logger.error("Error while accessing DB to prepare reset password.", e);
345
			throw new DaoException(e);
346
		} finally {
347
			if (stmt != null) {
348
				try {
349
					stmt.close();
350
				} catch (SQLException e) {
351
					logger.error("Error while accessing DB to prepare reset password.", e);
352
					throw new DaoException(e);
353
				}
354
			}
355
		}
356
	}
357

  
358

  
359
	@Override
360
	public void ResetPassword(String uuid, String password) throws DaoException {
361
		Connection con = null;
362
		PreparedStatement stmt = null;
363
		logger.debug("Accessing DB to reset password");
364
		try {
365
			con = getConnection();
366
			String query="UPDATE users SET password=?, activation_id=? WHERE activation_id=?";;
367
			stmt = con.prepareStatement(query);
368
			stmt.setString(1,password);
369
			stmt.setString(2,null);
370
			stmt.setString(3,uuid);
371
			stmt.executeUpdate();
372
		} catch (Exception e) {
373
			logger.error("Error while accessing DB to reset password.", e);
374
			throw new DaoException(e);
375
		} finally {
376
			if (stmt != null) {
377
				try {
378
					stmt.close();
379
				} catch (SQLException e) {
380
					logger.error("Error while accessing DB to reset password.", e);
381
					throw new DaoException(e);
382
				}
383
			}
384
		}
385
	}
386

  
387
}
modules/uoa-validator-commons/tags/uoa-validator-commons-2.0.2/src/main/java/eu/dnetlib/validator/commons/dao/DaoException.java
1
package eu.dnetlib.validator.commons.dao;
2

  
3
public class DaoException extends Exception {
4

  
5
	/**
6
	 * 
7
	 * @author Nikon Gasparis
8
	 *
9
	 */
10
	
11
	private static final long serialVersionUID = 1030647214820156752L;
12

  
13
	public DaoException() {
14
		super();
15
	}
16
	
17
	public  DaoException(String message) {
18
		super(message);
19
	}
20

  
21
	public  DaoException(String message, Throwable cause) {
22
		super(message, cause);
23
	}
24

  
25
	public  DaoException(Throwable cause) {
26
		super(cause);
27
	}
28
}
29

  
modules/uoa-validator-commons/tags/uoa-validator-commons-2.0.2/src/main/java/eu/dnetlib/validator/commons/dao/DAO.java
1
package eu.dnetlib.validator.commons.dao;
2

  
3
public interface DAO<T> {
4

  
5
	public Integer save (T t) throws DaoException;
6
//	public String delete (T t);
7
	public String delete (int id) throws DaoException;
8
	public T get (int id) throws DaoException;
9
}
modules/uoa-validator-commons/tags/uoa-validator-commons-2.0.2/src/main/java/eu/dnetlib/validator/commons/dao/tasks/TaskStored.java
1
package eu.dnetlib.validator.commons.dao.tasks;
2

  
3

  
4
public class TaskStored {
5

  
6
	private String status, started, ended, valObjId, recordIdentifier, recordUrl;
7
	private int jobId, ruleId;
8
	private boolean success;
9
//	private RuleD rule;
10
	
11
	public String getRecordUrl() {
12
		return recordUrl;
13
	}
14
	public void setRecordUrl(String recordUrl) {
15
		this.recordUrl = recordUrl;
16
	}
17
	public String getStatus() { 
18
		return status;
19
	}
20
	public void setStatus(String status) {
21
		this.status = status;
22
	}
23
	public boolean getSuccess() {
24
		return success;
25
	}
26
	public void setSuccess(boolean success) {
27
		this.success = success;
28
	}
29
	public String getStarted() {
30
		return started;
31
	}
32
	public void setStarted(String started) {
33
		this.started = started;
34
	}
35
	public String getEnded() {
36
		return ended;
37
	}
38
	public void setEnded(String ended) {
39
		this.ended = ended;
40
	}
41
	
42
	public String getValObjId() {
43
		return valObjId;
44
	}
45
	public void setValObjId(String valObjId) {
46
		this.valObjId = valObjId;
47
	}
48
	public String getRecordIdentifier() {
49
		return recordIdentifier;
50
	}
51
	public void setRecordIdentifier(String recordIdentifier) {
52
		this.recordIdentifier = recordIdentifier;
53
	}
54
	public int getJobId() {
55
		return jobId;
56
	}
57
	public void setJobId(int jobId) {
58
		this.jobId = jobId;
59
	}
60
	public int getRuleId() {
61
		return ruleId;
62
	}
63
	public void setRuleId(int ruleId) {
64
		this.ruleId = ruleId;
65
	}
66
	
67
}
modules/uoa-validator-commons/tags/uoa-validator-commons-2.0.2/src/main/java/eu/dnetlib/validator/commons/dao/tasks/TasksDAO.java
1
package eu.dnetlib.validator.commons.dao.tasks;
2

  
3
import eu.dnetlib.validator.commons.dao.DAO;
4
import eu.dnetlib.validator.commons.dao.DaoException;
5
import eu.dnetlib.validator.commons.dao.rules.RuleStatus;
6

  
7
import java.util.List;
8
import java.util.Map;
9

  
10
public interface TasksDAO extends DAO<TaskStored> {
11

  
12
	public List<TaskStored> getTasksOfJob(int id) throws DaoException;
13

  
14
	public List<String> getValidationErrors(int jobId, int ruleId) throws DaoException;
15

  
16
	public List<String> getDistinctTasksOfJob(int jobId) throws DaoException;
17

  
18
	public List<TaskStored> getFinishedTasks(int jobId, int ruleId) throws DaoException;
19

  
20
	public void saveTasks(List<TaskStored> tasksStored, List<String> sets) throws DaoException;
21
	
22
	public void cleanTasks(int jobId) throws DaoException;
23

  
24
	public void saveTasksBatch(List<TaskStored> tasks, Map<String, List<String>> groupByMap) throws DaoException;
25

  
26
	public void saveTasks(Map<Integer, RuleStatus> scoreMapPerRule) throws DaoException;
27
}
modules/uoa-validator-commons/tags/uoa-validator-commons-2.0.2/src/main/java/eu/dnetlib/validator/commons/dao/tasks/TasksDAOimpl.java
1
package eu.dnetlib.validator.commons.dao.tasks;
2

  
3
import eu.dnetlib.validator.commons.dao.AbstractDAO;
4
import eu.dnetlib.validator.commons.dao.DaoException;
5
import eu.dnetlib.validator.commons.dao.rules.RuleStatus;
6

  
7
import java.sql.Connection;
8
import java.sql.PreparedStatement;
9
import java.sql.ResultSet;
10
import java.sql.SQLException;
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.Map.Entry;
15

  
16
public class TasksDAOimpl extends AbstractDAO<TaskStored> implements TasksDAO{
17

  
18
	@Override
19
	protected PreparedStatement getUpdateStatement(TaskStored t, Connection con)
20
			throws SQLException {
21
		String query="UPDATE tasks set status=?, success=?, started=?, ended=?, record_url=? WHERE job_id=? AND rule_id=? AND record_identifier=?";
22
		PreparedStatement stmt = con.prepareStatement(query);
23
//		logger.debug("getting taskStored updateStatement");
24
		stmt.setString(1, t.getStatus());
25
		stmt.setBoolean(2, t.getSuccess());
26
		stmt.setString(3, t.getStarted());
27
		stmt.setString(4, t.getEnded());
28
		stmt.setString(5, t.getRecordUrl());	
29
		stmt.setInt(6, t.getJobId());
30
		stmt.setInt(7, t.getRuleId());
31
		stmt.setString(8, t.getRecordIdentifier());
32
		return stmt;
33
	}
34

  
35
	@Override
36
	protected PreparedStatement getInsertStatement(TaskStored t, Connection con) throws SQLException {
37
		String query="INSERT INTO tasks(status, success, started, ended, job_id, rule_id, record_identifier, record_url) VALUES(?,?,?,?,?,?,?,?)";
38
		PreparedStatement stmt = con.prepareStatement(query);
39
//		logger.debug("getting taskStored insertStatement");
40
		stmt.setString(1, t.getStatus());
41
		stmt.setBoolean(2, t.getSuccess());
42
		stmt.setString(3, t.getStarted());
43
		stmt.setString(4, t.getEnded());
44
		stmt.setInt(5, t.getJobId());
45
		stmt.setInt(6, t.getRuleId());
46
		stmt.setString(7, t.getRecordIdentifier());
47
		stmt.setString(8, t.getRecordUrl());		
48
	
49
		return stmt;
50
	}
51

  
52
	@Override
53
	protected PreparedStatement getDeleteStatement(int id, Connection con)
54
			throws SQLException {
55
		String query="DELETE FROM tasks WHERE job_id=?";
56
		PreparedStatement stmt = con.prepareStatement(query);
57
		stmt.setInt(1, id);
58
		return stmt;
59
	}
60

  
61

  
62
	@Override
63
	public List<TaskStored> getTasksOfJob(int id) throws DaoException {
64
		ResultSet rs = null;
65
		Connection con = null;
66
		PreparedStatement stmt = null;
67
		TaskStored retTask = null;
68
		List<TaskStored> retList = null; 
69
		logger.debug("Accessing DB to get all Tasks of Job");
70
		try {
71
			con = getConnection();
72
			String query="SELECT status, success, started, ended, rule_id, record_identifier, record_url FROM tasks WHERE job_id=?";
73
			stmt = con.prepareStatement(query);
74
			stmt.setInt(1, id);
75
			rs = stmt.executeQuery();
76
			if (rs!=null){
77
				retList = new ArrayList<TaskStored>();
78
				
79
				while (rs.next()) {
80
					retTask = new TaskStored();
81
					retTask.setStatus(rs.getString(1));
82
					retTask.setSuccess(rs.getBoolean(2));
83
					retTask.setStarted(rs.getString(3));
84
					retTask.setEnded(rs.getString(4));
85
					retTask.setRuleId(rs.getInt(5));
86
					retTask.setRecordIdentifier(rs.getString(6));
87
					retTask.setRecordUrl(rs.getString(7));
88
					retTask.setJobId(id);
89
					retList.add(retTask);
90
				}				
91
			}
92
		} catch (Exception e) {
93
			logger.error("Accessing DB to get all Tasks of Job.", e);
94
			throw new DaoException(e);
95
		} finally {
96
			if (stmt != null) {
97
				try {
98
					stmt.close();
99
				} catch (SQLException e) {
100
					logger.error("Accessing DB to get all Tasks of Job.", e);
101
					throw new DaoException(e);
102
				}
103
			}
104
		}
105
		return retList;
106

  
107
	}
108
	
109
	@Override
110
	public TaskStored get(int id) {
111
		// TODO Auto-generated method stub
112
		return null;
113
	}
114

  
115
	@Override
116
	public List<String> getValidationErrors(int jobId, int ruleId) throws DaoException {
117
		ResultSet rs = null;
118
		Connection con = null;
119
		PreparedStatement stmt = null;
120
		List<String> retList = null; 
121
		logger.debug("Accessing DB to get Validation Errors of JobId " + jobId + " and RuleId " + ruleId);
122
		try {
123
			con = getConnection();
124
			String query="SELECT record_identifier FROM tasks WHERE job_id=? AND rule_id=? AND success=? LIMIT 30";
125
			stmt = con.prepareStatement(query);
126
			stmt.setInt(1, jobId);
127
			stmt.setInt(2, ruleId);
128
			stmt.setBoolean(3, false);
129
			rs = stmt.executeQuery();
130
			if (rs!=null){
131
				retList = new ArrayList<String>();
132
				
133
				while (rs.next()) {
134
//					if (!rs.getBoolean(1))
135
					retList.add(rs.getString(1));
136
				}				
137
			}
138
		} catch (Exception e) {
139
			logger.error("Accessing DB to get Validation Errors of a JobId and RuleId.", e);
140
			throw new DaoException(e);
141
		} finally {
142
			if (stmt != null) {
143
				try {
144
					stmt.close();
145
				} catch (SQLException e) {
146
					logger.error("Accessing DB to get Validation Errors of a JobId and RuleId.", e);
147
					throw new DaoException(e);
148
				}
149
			}
150
		}
151
		return retList;
152

  
153

  
154
	}
155

  
156
	@Override
157
	public List<String> getDistinctTasksOfJob(int jobId) throws DaoException {
158
		List<String> retList = null;
159
		ResultSet rs = null;
160
		Connection con = null;
161
		PreparedStatement stmt = null;
162
		logger.debug("Accessing DB to get Distinct Rule ids of Tasks");
163
		try {
164
			con = getConnection();
165
			String query="SELECT distinct rule_id from tasks where job_id=?";
166
			stmt = con.prepareStatement(query);
167
			stmt.setInt(1, jobId);
168
			rs = stmt.executeQuery();
169
			if (rs!=null){
170
				retList = new ArrayList<String>();
171
				
172
				while (rs.next()) {
173
					retList.add(Integer.toString(rs.getInt(1)));
174
				}				
175
			}
176
		} catch (Exception e) {
177
			logger.error("Accessing DB to get Distinct Rule ids of Tasks.", e);
178
			throw new DaoException(e);
179
		} finally {
180
			if (stmt != null) {
181
				try {
182
					stmt.close();
183
				} catch (SQLException e) {
184
					logger.error("Accessing DB to get Distinct Rule ids of Tasks.", e);
185
					throw new DaoException(e);
186
				}
187
			}
188
		}
189
		return retList;
190

  
191
		
192
	}
193

  
194
	@Override
195
	public List<TaskStored> getFinishedTasks(int jobId, int ruleId) throws DaoException {
196
		ResultSet rs = null;
197
		Connection con = null;
198
		PreparedStatement stmt = null;
199
		TaskStored retTask = null;
200
		List<TaskStored> retList = null; 
201
		logger.debug("Accessing DB to get Finished Tasks");
202
		try {
203
			con = getConnection();
204
			String query="SELECT success, record_identifier FROM tasks WHERE job_id=? AND rule_id=? AND status=?";
205
			stmt = con.prepareStatement(query);
206
			stmt.setInt(1, jobId);
207
			stmt.setInt(2, ruleId);
208
			stmt.setString(3, "finished");
209
			rs = stmt.executeQuery();
210
			if (rs!=null){
211
				retList = new ArrayList<TaskStored>();
212
				
213
				while (rs.next()) {
214
					retTask = new TaskStored();
215
					retTask.setSuccess(rs.getBoolean(1));
216
					retTask.setRecordIdentifier(rs.getString(2));
217
					retList.add(retTask);
218
				}				
219
			}
220
		} catch (Exception e) {
221
			logger.error("Accessing DB to get Finished Tasks.", e);
222
			throw new DaoException(e);
223
		} finally {
224
			if (stmt != null) {
225
				try {
226
					stmt.close();
227
				} catch (SQLException e) {
228
					logger.error("Accessing DB to get Finished Tasks.", e);
229
					throw new DaoException(e);
230
				}
231
			}
232
		}
233
		return retList;
234

  
235

  
236
	}
237

  
238
	@Override
239
	public void saveTasksBatch(List<TaskStored> tasks, Map<String,List<String>> groupByMap) throws DaoException {
240
		Connection con = null;
241
		PreparedStatement stmt = null, stmt1 = null;
242
		logger.debug("Accessing DB to save batch of tasks");
243
		try {
244
			
245
			con = getConnection();
246
			String query="INSERT INTO tasks(status, success, started, ended, job_id, rule_id, record_identifier, record_url) VALUES(?,?,?,?,?,?,?,?)";stmt = con.prepareStatement(query);
247
			stmt = con.prepareStatement(query);
248
						
249
			for (TaskStored t : tasks ) {
250
				stmt.setString(1, t.getStatus());
251
				stmt.setBoolean(2, t.getSuccess());
252
				stmt.setString(3, t.getStarted());
253
				stmt.setString(4, t.getEnded());
254
				stmt.setInt(5, t.getJobId());
255
				stmt.setInt(6, t.getRuleId());
256
				stmt.setString(7, t.getRecordIdentifier());
257
				stmt.setString(8, t.getRecordUrl());	
258
				stmt.addBatch();
259
		    }
260
		    stmt.executeBatch();
261
		    logger.debug("Tasks inserted: "+tasks.size());
262

  
263
		    if (!groupByMap.isEmpty()) {
264
		    	logger.debug("Inserting record's groupBy values..");
265
		    	for (Map.Entry<String, List<String>> entry : groupByMap.entrySet()) {
266
			    	query="INSERT INTO record_groupby(record_id, groupby, job_id) VALUES(?,?,?)";
267
			    	stmt1 = con.prepareStatement(query);
268
			    	for (String value :entry.getValue()) {
269
			    		stmt1.setString(1, entry.getKey());
270
			    		stmt1.setString(2, value);
271
			    		stmt1.setInt(3, tasks.get(0).getJobId());		
272
			    		stmt1.addBatch();
273
			    	}
274
		    	}
275
		    	stmt1.executeBatch();
276
//		    	logger.debug("groupBy values inserted: "+groupBy_values.size());		    	
277
		    }
278
		    
279
		} catch (Exception e) {
280
			logger.error("Error Accessing DB to save batch of tasks.", e);
281
			throw new DaoException(e);
282
		} finally {
283
			if (stmt != null) {
284
				try {
285
					stmt.close();
286
				} catch (SQLException e) {
287
					logger.error("Accessing DB to save batch of tasks.", e);
288
					throw new DaoException(e);
289
				}
290
			}
291
			if (stmt1 != null) {
292
				try {
293
					stmt1.close();
294
				} catch (SQLException e) {
295
					logger.error("Accessing DB to save batch of tasks.", e);
296
					throw new DaoException(e);
297
				}
298
			}			
299
		}
300
		
301
	}
302

  
303
	@Override
304
	public void saveTasks(List<TaskStored> tasks, List<String> groupBy_values) throws DaoException {
305
		Connection con = null;
306
		PreparedStatement stmt = null, stmt1 = null;
307
		logger.debug("Accessing DB to save batch of tasks");
308
		try {
309
			
310
			con = getConnection();
311
			String query="INSERT INTO tasks(status, success, started, ended, job_id, rule_id, record_identifier, record_url) VALUES(?,?,?,?,?,?,?,?)";stmt = con.prepareStatement(query);
312
			stmt = con.prepareStatement(query);
313
						
314
			for (TaskStored t : tasks ) {
315
				stmt.setString(1, t.getStatus());
316
				stmt.setBoolean(2, t.getSuccess());
317
				stmt.setString(3, t.getStarted());
318
				stmt.setString(4, t.getEnded());
319
				stmt.setInt(5, t.getJobId());
320
				stmt.setInt(6, t.getRuleId());
321
				stmt.setString(7, t.getRecordIdentifier());
322
				stmt.setString(8, t.getRecordUrl());	
323
				stmt.addBatch();
324
		    }
325
		    stmt.executeBatch();
326
		    logger.debug("Tasks inserted: "+tasks.size());
327

  
328
		    if (groupBy_values != null) {
329
		    	logger.debug("Inserting record's groupBy values..");
330
		    	query="INSERT INTO record_groupby(record_id, groupby, job_id) VALUES(?,?,?)";
331
		    	stmt1 = con.prepareStatement(query);
332
		    	for (String value : groupBy_values) {
333
		    		stmt1.setString(1, tasks.get(0).getRecordIdentifier());
334
		    		stmt1.setString(2, value);
335
		    		stmt1.setInt(3, tasks.get(0).getJobId());		
336
		    		stmt1.addBatch();
337
		    	}
338
		    	stmt1.executeBatch();
339
		    	logger.debug("groupBy values inserted: "+groupBy_values.size());		    	
340
		    }
341
		    
342
		} catch (Exception e) {
343
			logger.error("Error Accessing DB to save batch of tasks.", e);
344
			throw new DaoException(e);
345
		} finally {
346
			if (stmt != null) {
347
				try {
348
					stmt.close();
349
				} catch (SQLException e) {
350
					logger.error("Accessing DB to save batch of tasks.", e);
351
					throw new DaoException(e);
352
				}
353
			}
354
			if (stmt1 != null) {
355
				try {
356
					stmt1.close();
357
				} catch (SQLException e) {
358
					logger.error("Accessing DB to save batch of tasks.", e);
359
				}
360
			}			
361
		}
362
		
363
	}
364
	
365
	@Override
366
	public void saveTasks(Map<Integer, RuleStatus> scoreMapPerRule) throws DaoException{
367
		Connection con = null;
368
		PreparedStatement stmt = null;
369
		logger.debug("Accessing DB to save batch of failed tasks");
370
		try {
371
			
372
			con = getConnection();
373
			String query="INSERT INTO tasks(status, success, started, ended, job_id, rule_id, record_identifier, record_url) VALUES(?,?,?,?,?,?,?,?)";
374
			stmt = con.prepareStatement(query);
375
			
376
			for (Entry<Integer, RuleStatus> entry : scoreMapPerRule.entrySet()) {
377
				for (TaskStored t : entry.getValue().getFailedTasks() ) {
378
					stmt.setString(1, t.getStatus());
379
					stmt.setBoolean(2, t.getSuccess());
380
					stmt.setString(3, t.getStarted());
381
					stmt.setString(4, t.getEnded());
382
					stmt.setInt(5, t.getJobId());
383
					stmt.setInt(6, t.getRuleId());
384
					stmt.setString(7, t.getRecordIdentifier());
385
					stmt.setString(8, t.getRecordUrl());	
386
					stmt.addBatch();
387
				}
388
			}
389
		    int result = stmt.executeBatch().length;
390
		    logger.debug("Tasks inserted: "+ result);
391

  
392
		} catch (Exception e) {
393
			logger.error("Error Accessing DB to save batch of failed tasks", e);
394
			throw new DaoException(e);
395
		} finally {
396
			if (stmt != null) {
397
				try {
398
					stmt.close();
399
				} catch (SQLException e) {
400
					logger.error("Accessing DB to save batch of failed tasks.", e);
401
					throw new DaoException(e);
402
				}
403
			}
404
		}
405
		
406
	}
407

  
408
	
409
	@Override
410
	protected int getLastId() throws DaoException {
411
		return 1;
412
//		ResultSet rs = null;
413
//		Connection con = null;
414
//		PreparedStatement stmt = null;
415
//		int retId = -1;
416
//		logger.debug("Accessing DB to get Task's next available id");
417
//		try {
418
//			con = getConnection();
419
//			String query="SELECT currval(pg_get_serial_sequence(?,?)) FROM tasks";
420
//			stmt = con.prepareStatement(query);
421
//			stmt.setString(1, "tasks");
422
//			stmt.setString(2, "rule_id");
423
//			
424
//			rs = stmt.executeQuery();
425
//			if (rs!=null){
426
//				rs.next();
427
//				retId=rs.getInt(1);
428
//			}
429
//
430
//
431
//		} catch (SQLException e) {
432
//			logger.error("Error while accessing DB to get Task's next available id.", e);
433
//		} finally {
434
//			if (stmt != null) {
435
//				try {
436
//					stmt.close();
437
//				} catch (SQLException e) {
438
//					logger.error("Error while accessing DB to get Task's next available id.", e);
439
//				}
440
//			}
441
//		}
442
//		return retId;
443
	}
444

  
445
	@Override
446
	public void cleanTasks(int jobId) throws DaoException{
447
		Connection con = null;
448
		PreparedStatement stmt = null;
449
		logger.debug("Accessing DB to delete unneeded tasks");
450
		try {
451
			con = getConnection();
452
			String query="DELETE FROM tasks WHERE job_id = ? AND success = ?";
453
			stmt = con.prepareStatement(query);
454
			stmt.setInt(1, jobId);
455
			stmt.setBoolean(2, true);
456
			stmt.executeUpdate();
457
			stmt.close();
458
		} catch (Exception e) {
459
			logger.error("Error while Accessing DB to delete unneeded tasks.", e);
460
			throw new DaoException(e);
461
		} finally {
462
			if (stmt != null) {
463
				try {
464
					stmt.close();
465
				} catch (SQLException e) {
466
					logger.error("Error while Accessing DB to delete unneeded tasks.", e);
467
					throw new DaoException(e);
468
				}
469
			}
470
		}
471

  
472
	}
473

  
474
}
modules/uoa-validator-commons/tags/uoa-validator-commons-2.0.2/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, DaoException;
21
	
22
	protected static Logger logger = Logger.getLogger(AbstractDAO.class);
23
	
24
	@Override
25
	public Integer save(T t) throws DaoException {
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 = getUpdateStatement(t,con);
33
			
34
			if (stmt.executeUpdate() == 0) {
35
				stmt.close();
36
				stmt = getInsertStatement(t,con);
37
				stmt.executeUpdate();
38
				retId=this.getLastId();
39
			}
40
			
41
		} catch (SQLException e) {
42
			logger.error("Error while accessing DB to save/update: ", e);
43
			throw new DaoException(e);
44
		} finally {
45
			if (stmt != null) {
46
				try {
47
					stmt.close();
48
				} catch (SQLException e) {
49
					logger.error("Error while accessing DB to save/update: ", e);
50
					throw new DaoException(e);
51
				}
52
			}
53
		}
54
		return retId;
55
	}
56

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

  
85
	public Connection getConnection() throws DaoException {
86
		try {
87
			Connection conn = DataSourceUtils.getConnection(datasource);
88
			return conn;
89
		} catch (Exception e){
90
			throw new DaoException(e);
91
		}
92
	}
93
	public DataSource getDatasource() {
94
		return datasource;
95
	}
96
	public void setDatasource(DataSource datasource) {
97
		this.datasource = datasource;
98
	}
99
}
modules/uoa-validator-commons/tags/uoa-validator-commons-2.0.2/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/tags/uoa-validator-commons-2.0.2/src/main/java/eu/dnetlib/validator/commons/dao/repositories/RepositoriesDAO.java
1
package eu.dnetlib.validator.commons.dao.repositories;
2

  
3
import java.util.List;
4

  
5
import eu.dnetlib.validator.commons.dao.DAO;
6
import eu.dnetlib.validator.commons.dao.DaoException;
7

  
8
public interface RepositoriesDAO extends DAO<RepositoryStored> {
9

  
10
	public List<String> getBaseUrls() throws DaoException;
11
	
12
	
13

  
14
}
modules/uoa-validator-commons/tags/uoa-validator-commons-2.0.2/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/tags/uoa-validator-commons-2.0.2/src/main/java/eu/dnetlib/validator/commons/dao/repositories/RepositoriesDAOImpl.java
1
package eu.dnetlib.validator.commons.dao.repositories;
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.List;
9

  
10
import eu.dnetlib.validator.commons.dao.AbstractDAO;
11
import eu.dnetlib.validator.commons.dao.DaoException;
12

  
13
public class RepositoriesDAOImpl extends AbstractDAO<RepositoryStored> implements RepositoriesDAO {
14

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

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

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

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

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

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

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

  
91

  
92
}
modules/uoa-validator-commons/tags/uoa-validator-commons-2.0.2/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
import eu.dnetlib.validator.commons.dao.DaoException;
15

  
16
public class RulesDAOimpl extends AbstractDAO<Rule> implements RulesDAO {
17

  
18
	@Override
19
	public Integer save(Rule t) throws DaoException {
20
		Connection con = null;
21
		PreparedStatement stmt = null;
22
		Integer retId = -1;
23
		logger.debug("Accessing DB to save/update Rule");
24
		try {
25
			logger.debug("Accessing DB to update Rule");
26
			con = getConnection();
27
			String query="UPDATE rules SET name=?, description=?, type=?, mandatory=?, weight=?, provider_information=?, entity_type=?, for_cris=? WHERE id=?";
28
			stmt = con.prepareStatement(query);
29

  
30
			stmt.setString(1, t.getName());
31
			stmt.setString(2, t.getDescription());
32
			stmt.setString(3, t.getType());
33
			stmt.setBoolean(4, t.isMandatory());
34
			stmt.setInt(5, t.getWeight());
35
			stmt.setString(6, t.getProvider_information());
36
			stmt.setString(7, t.getEntity_type());
37
			stmt.setBoolean(8, t.isFor_cris());
38
			stmt.setInt(9, t.getId());
39
			
40
			if (stmt.executeUpdate() == 0) {
41
				stmt.close();
42
				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());
43
				query="INSERT INTO rules(name, description, type, mandatory, weight, provider_information, job_type, entity_type, for_cris) VALUES(?,?,?,?,?,?,?,?,?)";
44
				stmt = con.prepareStatement(query);
45
				stmt.setString(1, t.getName());
46
				stmt.setString(2, t.getDescription());
47
				stmt.setString(3, t.getType());
48
				stmt.setBoolean(4, t.isMandatory());
49
				stmt.setInt(5, t.getWeight());
50
				stmt.setString(6, t.getProvider_information());
51
				stmt.setString(7, t.getJob_type());
52
				stmt.setString(8, t.getEntity_type());
53
				stmt.setBoolean(9, t.isFor_cris());
54
				stmt.executeUpdate();
55
				retId = this.getLastId();
56
			} else {
57
				logger.debug("Accessing DB to update Rule-done");
58
				retId=t.getId();
59
			}
60
			
61
			stmt.close();
62
			logger.debug("Accessing DB to delete Rule properties");
63
			query="DELETE FROM rule_properties " + " WHERE rule_id=?";
64
			stmt = con.prepareStatement(query);
65
			stmt.setInt(1, retId);		
66
			if (stmt.executeUpdate() == 0) {
67
				stmt.close();
68
			}			
69
			logger.debug("Accessing DB to insert Rule properties");
70
			query="INSERT INTO rule_properties(rule_id, property_name, property_value) VALUES(?,?,?)";
71
		    stmt = con.prepareStatement(query);
72
			Properties pros = t.getConfiguration();
73
		    Enumeration<?> e = pros.propertyNames();
74
		    while (e.hasMoreElements()) {
75
			    String key = (String) e.nextElement();
76
			    logger.debug("Accessing DB to add property:"+key+"-"+pros.getProperty(key));
77
				stmt.setInt(1, retId);
78
				stmt.setString(2, key);
79
				stmt.setString(3, pros.getProperty(key));
80
				stmt.addBatch();
81
		    }
82
		    stmt.executeBatch();
83
		    logger.debug("Rule + Properties inserted/updated");
84
		    
85
		} catch (Exception e) {
86
			logger.error("Error accessing DB to get save/update Rule.", e);
87
			throw new DaoException(e);
88
		} finally {
89
			if (stmt != null) {
90
				try {
91
					stmt.close();
92
				} catch (SQLException e) {
93
					logger.error("Error accessing DB to get save/update Rule.", e);
94
					throw new DaoException(e);
95
				}
96
			}
97
		}
98
		return retId;
99
	}
100

  
101
	@Override
102
	public String delete(int id) throws DaoException {
103
		Connection con = null;
104
		PreparedStatement stmt = null;
105
		logger.debug("Accessing DB to delete Rule");
106
		try {
107
			con = getConnection();
108
			String query="DELETE FROM rules " + " WHERE id=?";
109
			stmt = con.prepareStatement(query);
110
			stmt.setInt(1, id);			
111
			if (stmt.executeUpdate() == 0) {
112
				stmt.close();
113
			}
114
			query="DELETE FROM rule_properties " + " WHERE rule_id=?";
115
			stmt = con.prepareStatement(query);
116
			stmt.setInt(1, id);		
117
			if (stmt.executeUpdate() == 0) {
118
				stmt.close();
119
			}			
120
			
121
		} catch (Exception e) {
122
			logger.error("Error accessing DB to delete Rule.", e);
123
			throw new DaoException(e);
124
		} finally {
125
			if (stmt != null) {
126
				try {
127
					stmt.close();
128
				} catch (SQLException e) {
129
					logger.error("Error accessing DB to delete Rule.", e);
130
					throw new DaoException(e);
131
				}
132
			}
133
		}
134
		return null;
135
	}
136

  
137
	
138
	@Override
139
	protected PreparedStatement getDeleteStatement(int id, Connection con)
140
			throws SQLException {
141
		String query="DELETE FROM rules " + " WHERE id=?";
142
		PreparedStatement stmt = con.prepareStatement(query);
143
		stmt.setInt(1, id);	
144
		return stmt;
145
	}
146

  
147
	@Override
148
	public List<Rule> getAllRulesByJobType(String jobType) throws DaoException {
149
		ResultSet rs = null;
150
		Connection con = null;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff