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
|
}
|