refactoring into GenericDataService
This commit is contained in:
@@ -9,12 +9,16 @@ import org.mifosng.platform.api.data.GenericResultsetData;
|
|||||||
|
|
||||||
public interface GenericDataService {
|
public interface GenericDataService {
|
||||||
|
|
||||||
public CachedRowSet getCachedResultSet(String sql, String errorMsg);
|
CachedRowSet getCachedResultSet(String sql, String errorMsg);
|
||||||
|
|
||||||
|
void updateSQL(String sql, String sqlErrorMsg);
|
||||||
|
|
||||||
GenericResultsetData fillGenericResultSet(final String sql);
|
GenericResultsetData fillGenericResultSet(final String sql);
|
||||||
|
|
||||||
String replace(String str, String pattern, String replace);
|
String replace(String str, String pattern, String replace);
|
||||||
|
|
||||||
|
String wrapSQL(String sql);
|
||||||
|
|
||||||
void dbClose(Statement db_statement, Connection db_connection);
|
void dbClose(Statement db_statement, Connection db_connection);
|
||||||
|
|
||||||
}
|
}
|
||||||
+40
-3
@@ -40,17 +40,19 @@ public class GenericDataServiceImpl implements GenericDataService {
|
|||||||
@Override
|
@Override
|
||||||
public CachedRowSet getCachedResultSet(String sql, String sqlErrorMsg) {
|
public CachedRowSet getCachedResultSet(String sql, String sqlErrorMsg) {
|
||||||
|
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
Connection db_connection = null;
|
Connection db_connection = null;
|
||||||
Statement db_statement = null;
|
Statement db_statement = null;
|
||||||
|
CachedRowSet crs = null;
|
||||||
try {
|
try {
|
||||||
db_connection = dataSource.getConnection();
|
db_connection = dataSource.getConnection();
|
||||||
db_statement = db_connection.createStatement();
|
db_statement = db_connection.createStatement();
|
||||||
ResultSet rs = db_statement.executeQuery(sql);
|
ResultSet rs = db_statement.executeQuery(sql);
|
||||||
|
crs = new CachedRowSetImpl();
|
||||||
|
|
||||||
CachedRowSet crs = new CachedRowSetImpl();
|
|
||||||
crs.populate(rs);
|
crs.populate(rs);
|
||||||
//logger.info("RS Size: " + crs.size() + " getCachedResultSet sql: " + sql);
|
// logger.info("RS Size: " + crs.size() +
|
||||||
return crs;
|
// " getCachedResultSet sql: " + sql);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new PlatformDataIntegrityException("error.msg.sql.error",
|
throw new PlatformDataIntegrityException("error.msg.sql.error",
|
||||||
e.getMessage(), sqlErrorMsg);
|
e.getMessage(), sqlErrorMsg);
|
||||||
@@ -59,6 +61,30 @@ public class GenericDataServiceImpl implements GenericDataService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long elapsed = System.currentTimeMillis() - startTime;
|
||||||
|
logger.info("Elapsed Time: " + elapsed + " SQL: " + sql);
|
||||||
|
return crs;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateSQL(String sql, String sqlErrorMsg) {
|
||||||
|
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
Connection db_connection = null;
|
||||||
|
Statement db_statement = null;
|
||||||
|
try {
|
||||||
|
db_connection = dataSource.getConnection();
|
||||||
|
db_statement = db_connection.createStatement();
|
||||||
|
db_statement.executeUpdate(sql);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new PlatformDataIntegrityException("error.msg.sql.error",
|
||||||
|
e.getMessage(), sqlErrorMsg);
|
||||||
|
} finally {
|
||||||
|
dbClose(db_statement, db_connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
long elapsed = System.currentTimeMillis() - startTime;
|
||||||
|
logger.info("Elapsed Time FOR UPDATE: " + elapsed + " SQL: " + sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -120,6 +146,17 @@ public class GenericDataServiceImpl implements GenericDataService {
|
|||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String wrapSQL(String sql) {
|
||||||
|
// wrap sql to prevent JDBC sql errors, prevent malicious sql and a
|
||||||
|
// CachedRowSetImpl bug
|
||||||
|
|
||||||
|
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7046875 - prevent
|
||||||
|
// Invalid Column Name bug in sun's CachedRowSetImpl where it doesn't
|
||||||
|
// pick up on label names, only column names
|
||||||
|
return "select x.* from (" + sql + ") x";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dbClose(Statement db_statement, Connection db_connection) {
|
public void dbClose(Statement db_statement, Connection db_connection) {
|
||||||
logger.debug("dbClose");
|
logger.debug("dbClose");
|
||||||
|
|||||||
+3
-13
@@ -209,27 +209,17 @@ public class ReadReportingServiceImpl implements ReadReportingService {
|
|||||||
sql = genericDataService.replace(sql, "${currentUserId}", currentUser
|
sql = genericDataService.replace(sql, "${currentUserId}", currentUser
|
||||||
.getId().toString());
|
.getId().toString());
|
||||||
|
|
||||||
sql = wrapSQL(sql);
|
sql = genericDataService.wrapSQL(sql);
|
||||||
|
|
||||||
return sql;
|
return sql;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String wrapSQL(String sql) {
|
|
||||||
// wrap sql to prevent JDBC sql errors, prevent malicious sql and a
|
|
||||||
// CachedRowSetImpl bug
|
|
||||||
|
|
||||||
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7046875 - prevent
|
|
||||||
// Invalid Column Name bug in sun's CachedRowSetImpl where it doesn't
|
|
||||||
// pick up on label names, only column names
|
|
||||||
return "select x.* from (" + sql + ") x";
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getSql(String name, String type) {
|
private String getSql(String name, String type) {
|
||||||
|
|
||||||
String inputSql = "select " + type + "_sql as the_sql from stretchy_"
|
String inputSql = "select " + type + "_sql as the_sql from stretchy_"
|
||||||
+ type + " where " + type + "_name = '" + name + "'";
|
+ type + " where " + type + "_name = '" + name + "'";
|
||||||
inputSql = wrapSQL(inputSql);
|
inputSql = genericDataService.wrapSQL(inputSql);
|
||||||
|
|
||||||
String sqlErrorMsg = "Sql: " + inputSql;
|
String sqlErrorMsg = "Sql: " + inputSql;
|
||||||
CachedRowSet rs = genericDataService.getCachedResultSet(inputSql,
|
CachedRowSet rs = genericDataService.getCachedResultSet(inputSql,
|
||||||
@@ -254,7 +244,7 @@ public class ReadReportingServiceImpl implements ReadReportingService {
|
|||||||
String sql = "SELECT ifnull(report_type,'') as report_type FROM `stretchy_report` where report_name = '"
|
String sql = "SELECT ifnull(report_type,'') as report_type FROM `stretchy_report` where report_name = '"
|
||||||
+ reportName + "'";
|
+ reportName + "'";
|
||||||
|
|
||||||
sql = wrapSQL(sql);
|
sql = genericDataService.wrapSQL(sql);
|
||||||
|
|
||||||
String sqlErrorMsg = "Report Name: " + reportName + " Sql: " + sql;
|
String sqlErrorMsg = "Report Name: " + reportName + " Sql: " + sql;
|
||||||
CachedRowSet rs = genericDataService.getCachedResultSet(sql,
|
CachedRowSet rs = genericDataService.getCachedResultSet(sql,
|
||||||
|
|||||||
+60
-88
@@ -1,15 +1,11 @@
|
|||||||
package org.mifosng.platform.noncore;
|
package org.mifosng.platform.noncore;
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import javax.sql.rowset.CachedRowSet;
|
import javax.sql.rowset.CachedRowSet;
|
||||||
|
|
||||||
import org.mifosng.platform.api.data.AdditionalFieldsSetData;
|
import org.mifosng.platform.api.data.AdditionalFieldsSetData;
|
||||||
@@ -18,7 +14,6 @@ import org.mifosng.platform.api.data.ResultsetColumnHeader;
|
|||||||
import org.mifosng.platform.api.data.ResultsetDataRow;
|
import org.mifosng.platform.api.data.ResultsetDataRow;
|
||||||
import org.mifosng.platform.exceptions.AdditionalFieldsNotFoundException;
|
import org.mifosng.platform.exceptions.AdditionalFieldsNotFoundException;
|
||||||
import org.mifosng.platform.exceptions.PlatformDataIntegrityException;
|
import org.mifosng.platform.exceptions.PlatformDataIntegrityException;
|
||||||
import org.mifosng.platform.infrastructure.TenantAwareRoutingDataSource;
|
|
||||||
import org.mifosng.platform.security.PlatformSecurityContext;
|
import org.mifosng.platform.security.PlatformSecurityContext;
|
||||||
import org.mifosng.platform.user.domain.AppUser;
|
import org.mifosng.platform.user.domain.AppUser;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -35,15 +30,9 @@ public class ReadWriteNonCoreDataServiceImpl implements
|
|||||||
private final static Logger logger = LoggerFactory
|
private final static Logger logger = LoggerFactory
|
||||||
.getLogger(ReadWriteNonCoreDataServiceImpl.class);
|
.getLogger(ReadWriteNonCoreDataServiceImpl.class);
|
||||||
|
|
||||||
private final DataSource dataSource;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ReadWriteNonCoreDataServiceImpl(
|
public ReadWriteNonCoreDataServiceImpl(final PlatformSecurityContext context) {
|
||||||
final PlatformSecurityContext context,
|
|
||||||
final TenantAwareRoutingDataSource dataSource) {
|
|
||||||
|
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.dataSource = dataSource;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -52,36 +41,36 @@ public class ReadWriteNonCoreDataServiceImpl implements
|
|||||||
@Override
|
@Override
|
||||||
public List<AdditionalFieldsSetData> retrieveExtraDatasetNames(String type) {
|
public List<AdditionalFieldsSetData> retrieveExtraDatasetNames(String type) {
|
||||||
|
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
List<AdditionalFieldsSetData> additionalFieldsSets = new ArrayList<AdditionalFieldsSetData>();
|
List<AdditionalFieldsSetData> additionalFieldsSets = new ArrayList<AdditionalFieldsSetData>();
|
||||||
|
|
||||||
Connection db_connection = null;
|
String andClause;
|
||||||
Statement db_statement = null;
|
if (type == null) {
|
||||||
|
andClause = "";
|
||||||
|
} else {
|
||||||
|
andClause = " and t.`name` = '" + type + "'";
|
||||||
|
}
|
||||||
|
// PERMITTED ADDITIONAL FIELDS datasets
|
||||||
|
String sql = "select d.id, d.`name` as 'set', t.`name` as 'type' "
|
||||||
|
+ " from stretchydata_dataset d join stretchydata_datasettype t on t.id = d.datasettype_id "
|
||||||
|
+ " where exists"
|
||||||
|
+ " (select 'f'"
|
||||||
|
+ " from m_appuser_role ur "
|
||||||
|
+ " join m_role r on r.id = ur.role_id"
|
||||||
|
+ " left join m_role_permission rp on rp.role_id = r.id"
|
||||||
|
+ " left join m_permission p on p.id = rp.permission_id"
|
||||||
|
+ " where ur.appuser_id = "
|
||||||
|
+ context.authenticatedUser().getId()
|
||||||
|
+ " and (r.name = 'Super User' or r.name = 'Read Only') or p.code = concat('CAN_READ_', t.`name`, '_x', d.`name`)) "
|
||||||
|
+ andClause + " order by d.`name`";
|
||||||
|
|
||||||
|
sql = genericDataService.wrapSQL(sql);
|
||||||
|
String sqlErrorMsg = "Additional Fields Type: " + type + " sql: "
|
||||||
|
+ sql;
|
||||||
|
CachedRowSet rs = genericDataService.getCachedResultSet(sql,
|
||||||
|
sqlErrorMsg);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
db_connection = dataSource.getConnection();
|
|
||||||
db_statement = db_connection.createStatement();
|
|
||||||
|
|
||||||
String andClause;
|
|
||||||
if (type == null) {
|
|
||||||
andClause = "";
|
|
||||||
} else {
|
|
||||||
andClause = " and t.`name` = '" + type + "'";
|
|
||||||
}
|
|
||||||
// PERMITTED ADDITIONAL FIELDS datasets
|
|
||||||
String sql = "select d.id, d.`name` as 'set', t.`name` as 'type' "
|
|
||||||
+ " from stretchydata_dataset d join stretchydata_datasettype t on t.id = d.datasettype_id "
|
|
||||||
+ " where exists"
|
|
||||||
+ " (select 'f'"
|
|
||||||
+ " from m_appuser_role ur "
|
|
||||||
+ " join m_role r on r.id = ur.role_id"
|
|
||||||
+ " left join m_role_permission rp on rp.role_id = r.id"
|
|
||||||
+ " left join m_permission p on p.id = rp.permission_id"
|
|
||||||
+ " where ur.appuser_id = "
|
|
||||||
+ context.authenticatedUser().getId()
|
|
||||||
+ " and (r.name = 'Super User' or r.name = 'Read Only') or p.code = concat('CAN_READ_', t.`name`, '_x', d.`name`)) "
|
|
||||||
+ andClause + " order by d.`name`";
|
|
||||||
|
|
||||||
ResultSet rs = db_statement.executeQuery(sql);
|
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
additionalFieldsSets.add(new AdditionalFieldsSetData(rs
|
additionalFieldsSets.add(new AdditionalFieldsSetData(rs
|
||||||
.getInt("id"), rs.getString("set"), rs
|
.getInt("id"), rs.getString("set"), rs
|
||||||
@@ -90,11 +79,13 @@ public class ReadWriteNonCoreDataServiceImpl implements
|
|||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new PlatformDataIntegrityException("error.msg.sql.error",
|
throw new PlatformDataIntegrityException("error.msg.sql.error",
|
||||||
e.getMessage(), "Additional Fields Type: " + type);
|
e.getMessage(), sqlErrorMsg);
|
||||||
} finally {
|
|
||||||
genericDataService.dbClose(db_statement, db_connection);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long elapsed = System.currentTimeMillis() - startTime;
|
||||||
|
logger.info("FINISHING retrieveExtraDatasetNames: Elapsed Time: "
|
||||||
|
+ elapsed);
|
||||||
|
|
||||||
return additionalFieldsSets;
|
return additionalFieldsSets;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,8 +235,8 @@ public class ReadWriteNonCoreDataServiceImpl implements
|
|||||||
@Override
|
@Override
|
||||||
public void updateExtraData(String type, String set, Long id,
|
public void updateExtraData(String type, String set, Long id,
|
||||||
Map<String, String> queryParams) {
|
Map<String, String> queryParams) {
|
||||||
logger.info("updateExtraData - type: " + type + " set: " + set
|
|
||||||
+ " id: " + id);
|
long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
checkResourceTypeThere(type, set);
|
checkResourceTypeThere(type, set);
|
||||||
String fullDatasetName = getFullDatasetName(type, set);
|
String fullDatasetName = getFullDatasetName(type, set);
|
||||||
@@ -253,35 +244,26 @@ public class ReadWriteNonCoreDataServiceImpl implements
|
|||||||
|
|
||||||
String saveSql = getSaveSql(fullDatasetName, id, transType, queryParams);
|
String saveSql = getSaveSql(fullDatasetName, id, transType, queryParams);
|
||||||
|
|
||||||
logger.info("saveSQL: " + saveSql);
|
String sqlErrorMsg = "Additional Fields Type: " + type + " Set: "
|
||||||
Connection db_connection = null;
|
+ set + " Id: " + id;
|
||||||
Statement db_statement = null;
|
genericDataService.updateSQL(saveSql, sqlErrorMsg);
|
||||||
try {
|
|
||||||
db_connection = dataSource.getConnection();
|
|
||||||
db_statement = db_connection.createStatement();
|
|
||||||
db_statement.executeUpdate(saveSql);
|
|
||||||
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new PlatformDataIntegrityException("error.msg.sql.error",
|
|
||||||
e.getMessage(), "Additional Fields Type: " + type
|
|
||||||
+ " Set: " + set + " Id: " + id);
|
|
||||||
} finally {
|
|
||||||
genericDataService.dbClose(db_statement, db_connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
long elapsed = System.currentTimeMillis() - startTime;
|
||||||
|
logger.info("FINISHING updateExtraData: Elapsed Time: " + elapsed
|
||||||
|
+ " - type: " + type + " set: " + set + " id: " + id);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkResourceTypeThere(String type, String set) {
|
private void checkResourceTypeThere(String type, String set) {
|
||||||
|
|
||||||
String sql = "select 'f' from stretchydata_datasettype t join stretchydata_dataset d on d.datasettype_id = t.id where d.`name` = '"
|
String sql = "select t.id from stretchydata_datasettype t join stretchydata_dataset d on d.datasettype_id = t.id where d.`name` = '"
|
||||||
+ set + "' and t.`name` = '" + type + "'";
|
+ set + "' and t.`name` = '" + type + "'";
|
||||||
|
|
||||||
Connection db_connection = null;
|
String sqlErrorMsg = "Additional Fields Type: " + type + " Set: "
|
||||||
Statement db_statement = null;
|
+ set + " Sql: " + sql;
|
||||||
|
CachedRowSet rs = genericDataService.getCachedResultSet(sql,
|
||||||
|
sqlErrorMsg);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
db_connection = dataSource.getConnection();
|
|
||||||
db_statement = db_connection.createStatement();
|
|
||||||
ResultSet rs = db_statement.executeQuery(sql);
|
|
||||||
|
|
||||||
if (!(rs.next())) {
|
if (!(rs.next())) {
|
||||||
throw new AdditionalFieldsNotFoundException(type, set);
|
throw new AdditionalFieldsNotFoundException(type, set);
|
||||||
@@ -289,47 +271,37 @@ public class ReadWriteNonCoreDataServiceImpl implements
|
|||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new PlatformDataIntegrityException("error.msg.sql.error",
|
throw new PlatformDataIntegrityException("error.msg.sql.error",
|
||||||
e.getMessage(), "Additional Fields Type: " + type
|
e.getMessage(), sqlErrorMsg);
|
||||||
+ " Set: " + set + " Sql: " + sql);
|
|
||||||
} finally {
|
|
||||||
genericDataService.dbClose(db_statement, db_connection);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getTransType(String type, String fullDatasetName, Long id) {
|
private String getTransType(String type, String fullDatasetName, Long id) {
|
||||||
String transType = null;
|
|
||||||
|
|
||||||
String sql = "select s.id from `" + type + "` t left join `"
|
String sql = "select s.id from `" + type + "` t left join `"
|
||||||
+ fullDatasetName + "` s on s.id = t.id where t.id = " + id;
|
+ fullDatasetName + "` s on s.id = t.id where t.id = " + id;
|
||||||
|
|
||||||
Connection db_connection = null;
|
String sqlErrorMsg = "Additional Fields Type: " + type
|
||||||
Statement db_statement = null;
|
+ " Full Set Name: " + fullDatasetName + " Sql: " + sql;
|
||||||
|
CachedRowSet rs = genericDataService.getCachedResultSet(sql,
|
||||||
|
sqlErrorMsg);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
db_connection = dataSource.getConnection();
|
|
||||||
db_statement = db_connection.createStatement();
|
|
||||||
ResultSet rs = db_statement.executeQuery(sql);
|
|
||||||
|
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
String idValue = rs.getString("id");
|
String idValue = rs.getString("id");
|
||||||
if (idValue != null) {
|
if (idValue != null)
|
||||||
transType = "E";
|
return "E"; // update (edit)
|
||||||
} else {
|
|
||||||
transType = "A";
|
return "A"; // create (add)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new AdditionalFieldsNotFoundException(type, id);
|
|
||||||
}
|
}
|
||||||
|
throw new AdditionalFieldsNotFoundException(type, id);
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new PlatformDataIntegrityException("error.msg.sql.error",
|
throw new PlatformDataIntegrityException("error.msg.sql.error",
|
||||||
e.getMessage(), "Additional Fields Type: " + type
|
e.getMessage(), sqlErrorMsg);
|
||||||
+ " Full Set Name: " + fullDatasetName
|
|
||||||
+ " Sql: " + sql);
|
|
||||||
} finally {
|
|
||||||
genericDataService.dbClose(db_statement, db_connection);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return transType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getSaveSql(String fullSetName, Long id, String transType,
|
private String getSaveSql(String fullSetName, Long id, String transType,
|
||||||
|
|||||||
Reference in New Issue
Block a user