-
Notifications
You must be signed in to change notification settings - Fork 12
/
BaseDAO.java
133 lines (104 loc) · 5.36 KB
/
BaseDAO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package org.mifosx.admin.domain;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
@Repository
public class BaseDAO implements IBaseDAO {
@Autowired
JdbcTemplate jdbcTemplate;
private static final class TenantMapper implements RowMapper<Tenant> {
@Override
public Tenant mapRow(ResultSet rs, @SuppressWarnings("unused") int rowNum) throws SQLException {
Integer id = rs.getInt("id");
String name = rs.getString("name");
String timeZone = rs.getString("timezone_id");
String identifier = rs.getString("identifier");
String schemaName = rs.getString("schema_name");
return new Tenant(id, identifier, name, timeZone, schemaName);
}
}
@Override
public List<Tenant> retrieveTenants() {
String fetchTenantsSQL = "select t.id, t.name, t.identifier, t.timezone_id, tsc.schema_name from tenants t,tenant_server_connections tsc where t.oltp_id=tsc.id";
List<Tenant> tenants = jdbcTemplate.query(fetchTenantsSQL, new TenantMapper());
return tenants;
}
@Override
public Tenant retrieveTenant(Integer id) {
String fetchTenantsSQL = "select t.id, t.name, t.identifier, t.timezone_id, tsc.schema_name from tenants t,tenant_server_connections tsc where t.oltp_id=tsc.id"
+ " and t.id = ?";
Tenant tenant = jdbcTemplate.queryForObject(fetchTenantsSQL, new Object[] { id }, new TenantMapper());
return tenant;
}
@Override
public void appendTenantStatistics(List<Tenant> tenants) {
StringBuilder lastLoginDateQuery = new StringBuilder();
StringBuilder activeCentersQuery = new StringBuilder();
StringBuilder activeGroupsQuery = new StringBuilder();
StringBuilder activeClientsQuery = new StringBuilder();
StringBuilder activeLoanAccountsQuery = new StringBuilder();
StringBuilder activeSavingsAccountsQuery = new StringBuilder();
// generate queries for statistics across tenants
for (Tenant tenant : tenants) {
lastLoginDateQuery.append(" union all select max(made_on_date) from `" + tenant.getSchemaName()
+ "`.m_portfolio_command_source");
activeCentersQuery.append(" union all select count(*) from `" + tenant.getSchemaName()
+ "`.m_group where status_enum >=300 and status_enum < 600 and level_id=1 ");
activeGroupsQuery.append(" union all select count(*) from `" + tenant.getSchemaName()
+ "`.m_group where status_enum >=300 and status_enum < 600 and level_id=2 ");
activeClientsQuery.append(" union all select count(*) from `" + tenant.getSchemaName()
+ "`.m_client where status_enum >=300 and status_enum < 600 ");
activeLoanAccountsQuery.append(" union all select count(*) from `" + tenant.getSchemaName()
+ "`.m_loan where (loan_status_id > 200 and loan_status_id < 400) or loan_status_id = 700 ");
activeSavingsAccountsQuery.append(" union all select count(*) from `" + tenant.getSchemaName()
+ "`.m_savings_account where (status_enum > 200 and status_enum < 400) or status_enum = 800 ");
}
// Run SQL queries for generating statistics
String lastLoginDateQueryAsString = convertToValidSQLQuery(lastLoginDateQuery);
String activeCentersQueryAsString = convertToValidSQLQuery(activeCentersQuery);
String activeGroupsQueryAsString = convertToValidSQLQuery(activeGroupsQuery);
String activeClientsQueryAsString = convertToValidSQLQuery(activeClientsQuery);
String activeLoanAccountsQueryAsString = convertToValidSQLQuery(activeLoanAccountsQuery);
String activeSavingsAccountsQueryAsString = convertToValidSQLQuery(activeSavingsAccountsQuery);
List<Date> lastLoginDates = jdbcTemplate.queryForList(lastLoginDateQueryAsString, Date.class);
List<Integer> activeCenters = jdbcTemplate.queryForList(activeCentersQueryAsString, Integer.class);
List<Integer> activeGroups = jdbcTemplate.queryForList(activeGroupsQueryAsString, Integer.class);
List<Integer> activeClients = jdbcTemplate.queryForList(activeClientsQueryAsString, Integer.class);
List<Integer> activeLoanAccounts = jdbcTemplate.queryForList(activeLoanAccountsQueryAsString, Integer.class);
List<Integer> activeSavingsAccounts = jdbcTemplate.queryForList(activeSavingsAccountsQueryAsString,
Integer.class);
// update statistics back in Tenants
for (int i = 0; i < tenants.size(); i++) {
tenants.get(i).setStatistics(
new TenantStatistics(lastLoginDates.get(i), activeCenters.get(i), activeGroups.get(i),
activeClients.get(i), activeLoanAccounts.get(i), activeSavingsAccounts.get(i)));
}
}
/**
* @param lastLoginDateQuery
* @return
*/
private String convertToValidSQLQuery(StringBuilder lastLoginDateQuery) {
return lastLoginDateQuery.toString().replaceFirst("union all", "");
}
@Override
public void deleteTenant(Integer id) {
this.jdbcTemplate.update("delete from tenants,tenant_server_connections t where t.id=tenants.id and id = ?",
id);
}
@Override
public Tenant createTenant(Tenant tenant) {
// create an entry in tenants Table
this.jdbcTemplate.update(
"INSERT INTO `tenants` (`identifier`, `name`, `schema_name`, `timezone_id`) VALUES (?, ?, ?, ?)",
tenant.getIdentifier(), tenant.getName(), tenant.getSchemaName());
// create a database
this.jdbcTemplate.execute("create database " + tenant.getSchemaName());
return tenant;
}
}