refactored the Application management DAO layer

feature/appm-store/pbac
lasantha 7 years ago
parent 6e3cc085ff
commit c3f04eeed0

@ -137,10 +137,11 @@ public interface ApplicationDAO {
* To get the application count that satisfies gives search query. * To get the application count that satisfies gives search query.
* *
* @param filter Application Filter. * @param filter Application Filter.
* @param tenantId Id of the tenant
* @return count of the applications * @return count of the applications
* @throws ApplicationManagementDAOException Application Management DAO Exception. * @throws ApplicationManagementDAOException Application Management DAO Exception.
*/ */
int getApplicationCount(Filter filter) throws ApplicationManagementDAOException; int getApplicationCount(Filter filter, int tenantId) throws ApplicationManagementDAOException;
/** /**

@ -61,20 +61,19 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
int index = 0;
int applicationId = -1; int applicationId = -1;
try { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
stmt = conn.prepareStatement("INSERT INTO AP_APP (NAME, TYPE, APP_CATEGORY, " stmt = conn.prepareStatement("INSERT INTO AP_APP (NAME, TYPE, APP_CATEGORY, "
+ "IS_FREE, PAYMENT_CURRENCY, RESTRICTED, TENANT_ID) VALUES " + "IS_FREE, PAYMENT_CURRENCY, RESTRICTED, TENANT_ID) VALUES "
+ "(?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); + "(?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
stmt.setString(++index, application.getName()); stmt.setString(1, application.getName());
stmt.setString(++index, application.getType()); stmt.setString(2, application.getType());
stmt.setString(++index, application.getAppCategory()); stmt.setString(3, application.getAppCategory());
stmt.setInt(++index, application.getIsFree()); stmt.setInt(4, application.getIsFree());
stmt.setString(++index, application.getPaymentCurrency()); stmt.setString(5, application.getPaymentCurrency());
stmt.setInt(++index, application.getIsRestricted()); stmt.setInt(6, application.getIsRestricted());
stmt.setInt(++index, application.getUser().getTenantId()); stmt.setInt(7, application.getUser().getTenantId());
stmt.executeUpdate(); stmt.executeUpdate();
rs = stmt.getGeneratedKeys(); rs = stmt.getGeneratedKeys();
@ -99,17 +98,15 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null;
int index = 0;
String sql = "INSERT INTO AP_APP_TAG (TAG, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)"; String sql = "INSERT INTO AP_APP_TAG (TAG, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)";
try { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
conn.setAutoCommit(false); conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
for (Tag tag : tags) { for (Tag tag : tags) {
stmt.setString(++index, tag.getTagName()); stmt.setString(1, tag.getTagName());
stmt.setInt(++index, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(++index, applicationId); stmt.setInt(3, applicationId);
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
@ -119,7 +116,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while adding tags", e); throw new ApplicationManagementDAOException("Error occurred while adding tags", e);
} finally { } finally {
Util.cleanupResources(stmt, rs); Util.cleanupResources(stmt, null);
} }
} }
@ -132,15 +129,14 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
int isExist = 0; int isExist = 0;
int index = 0;
String sql = "SELECT * FROM AP_APP WHERE NAME = ? AND TYPE = ? AND TENANT_ID = ?"; String sql = "SELECT * FROM AP_APP WHERE NAME = ? AND TYPE = ? AND TENANT_ID = ?";
try{ try{
conn = this.getDBConnection(); conn = this.getDBConnection();
conn.setAutoCommit(false); conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(++index, appName); stmt.setString(1, appName);
stmt.setString(++index, type); stmt.setString(2, type);
stmt.setInt(++index, tenantId); stmt.setInt(3, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
isExist = 1; isExist = 1;
@ -169,7 +165,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
ResultSet rs = null; ResultSet rs = null;
ApplicationList applicationList = new ApplicationList(); ApplicationList applicationList = new ApplicationList();
Pagination pagination = new Pagination(); Pagination pagination = new Pagination();
int index = 0;
String sql = "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY" String sql = "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY"
+ " AS APP_CATEGORY, AP_APP.IS_FREE, AP_APP.RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLES.ROLE " + " AS APP_CATEGORY, AP_APP.IS_FREE, AP_APP.RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLES.ROLE "
+ "AS APP_UNRESTRICTED_ROLES FROM ((AP_APP LEFT JOIN AP_APP_TAG ON AP_APP.ID = AP_APP_TAG.AP_APP_ID) " + "AS APP_UNRESTRICTED_ROLES FROM ((AP_APP LEFT JOIN AP_APP_TAG ON AP_APP.ID = AP_APP_TAG.AP_APP_ID) "
@ -194,7 +189,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
} }
sql += " LIMIT ? OFFSET ? ORDER BY DESC APP_ID"; sql += " LIMIT ? OFFSET ? ORDER BY " + filter.getSortBy() + " APP_ID";
pagination.setLimit(filter.getLimit()); pagination.setLimit(filter.getLimit());
pagination.setOffset(filter.getOffset()); pagination.setOffset(filter.getOffset());
@ -202,21 +197,21 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
try { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(++index, tenantId); stmt.setInt(1, tenantId);
if (filter.getAppType() != null) { if (filter.getAppType() != null) {
stmt.setString(++index, filter.getAppType()); stmt.setString(2, filter.getAppType());
} }
if (filter.getAppName() != null) { if (filter.getAppName() != null) {
if (filter.isFullMatch()) { if (filter.isFullMatch()) {
stmt.setString(++index, filter.getAppName().toLowerCase()); stmt.setString(3, filter.getAppName().toLowerCase());
} else { } else {
stmt.setString(++index, "%" + filter.getAppName().toLowerCase() + "%"); stmt.setString(3, "%" + filter.getAppName().toLowerCase() + "%");
} }
} }
stmt.setInt(++index, filter.getLimit()); stmt.setInt(4, filter.getLimit());
stmt.setInt(++index, filter.getOffset()); stmt.setInt(5, filter.getOffset());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
applicationList.setApplications(Util.loadApplications(rs)); applicationList.setApplications(Util.loadApplications(rs));
applicationList.setPagination(pagination); applicationList.setPagination(pagination);
@ -247,7 +242,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
String sql = ""; String sql = "";
int index = 0;
String uuId = null; String uuId = null;
try { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
@ -256,8 +250,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
+ "AND LIFECYCLE.CURRENT_STATE = ? order by APP_RELEASE.ID DESC;"; + "AND LIFECYCLE.CURRENT_STATE = ? order by APP_RELEASE.ID DESC;";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(++index, appId); stmt.setInt(1, appId);
stmt.setString(++index, "PUBLISHED"); stmt.setString(2, AppLifecycleState.PUBLISHED.toString());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
uuId = rs.getString("UUID"); uuId = rs.getString("UUID");
@ -274,7 +268,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
@Override @Override
public int getApplicationCount(Filter filter) throws ApplicationManagementDAOException { public int getApplicationCount(Filter filter, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Getting application count from the database"); log.debug("Getting application count from the database");
log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset())); log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset()));
@ -300,9 +294,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
sql += ";"; sql += ";";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
int index = 0; stmt.setInt(1, tenantId);
if (filter.getAppName() != null) { if (filter.getAppName() != null) {
stmt.setString(++index, "%" + filter.getAppName().toLowerCase() + "%"); stmt.setString(2, "%" + filter.getAppName().toLowerCase() + "%");
} }
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
@ -450,7 +444,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
try { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
int index = 0;
String sql = "UPDATE AP_APP SET "; String sql = "UPDATE AP_APP SET ";
@ -474,22 +467,22 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
if (application.getName() != null && !application.getName().equals(existingApplication.getName())) { if (application.getName() != null && !application.getName().equals(existingApplication.getName())) {
stmt.setString(++index, application.getName()); stmt.setString(1, application.getName());
} }
if (application.getType() != null && !application.getType().equals(existingApplication.getType())) { if (application.getType() != null && !application.getType().equals(existingApplication.getType())) {
stmt.setString(++index, application.getType()); stmt.setString(2, application.getType());
} }
if (application.getAppCategory() != null && !application.getAppCategory().equals(existingApplication.getAppCategory())) { if (application.getAppCategory() != null && !application.getAppCategory().equals(existingApplication.getAppCategory())) {
stmt.setString(++index, application.getAppCategory()); stmt.setString(3, application.getAppCategory());
} }
if (application.getIsRestricted() != existingApplication.getIsRestricted()) { if (application.getIsRestricted() != existingApplication.getIsRestricted()) {
stmt.setInt(++index, application.getIsRestricted()); stmt.setInt(4, application.getIsRestricted());
} }
if (application.getIsFree() != existingApplication.getIsFree()) { if (application.getIsFree() != existingApplication.getIsFree()) {
stmt.setInt(++index, application.getIsFree()); stmt.setInt(5, application.getIsFree());
} }
stmt.setInt(++index, application.getId()); stmt.setInt(6, application.getId());
stmt.executeUpdate(); stmt.executeUpdate();
return application; return application;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {

Loading…
Cancel
Save