Refactor APPM DAO Layer

Refactored the APPM DAO layer. Further verified and added required functionality to compatible with other DBMS, such as Oracle, PostgreSQL, MSSQL
feature/appm-store/pbac
lasanthaDLPDS 7 years ago
parent f9559a510b
commit 71aa6762ac

@ -97,5 +97,4 @@ public class ConfigurationManager {
}
throw new InvalidConfigurationException("Expecting an extension with name - " + extName + " , but not found!");
}
}

@ -62,7 +62,7 @@ public interface ApplicationReleaseDAO {
* @return list of the application releases
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
List<ApplicationRelease> getApplicationReleases(String applicationName, String applicationType, int tenantId) throws
List<ApplicationRelease> getReleases(String applicationName, String applicationType, int tenantId) throws
ApplicationManagementDAOException;
/**

@ -26,8 +26,8 @@ import org.wso2.carbon.device.application.mgt.core.dao.*;
import org.wso2.carbon.device.application.mgt.core.dao.impl.Comment.CommentDAOImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.application.GenericApplicationDAOImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.application.release.GenericApplicationReleaseDAOImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.application.release.OracleApplicationDAOImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.GenericLifecycleStateImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.application.OracleApplicationDAOImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.GenericLifecycleStateDAOImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.subscription.GenericSubscriptionDAOImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.visibility.GenericVisibilityDAOImpl;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
@ -79,7 +79,7 @@ public class ApplicationManagementDAOFactory {
case Constants.DataBaseTypes.DB_TYPE_MYSQL:
case Constants.DataBaseTypes.DB_TYPE_POSTGRESQL:
case Constants.DataBaseTypes.DB_TYPE_ORACLE:
return new GenericLifecycleStateImpl();
return new GenericLifecycleStateDAOImpl();
default:
throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine);
}
@ -116,6 +116,8 @@ public class ApplicationManagementDAOFactory {
switch (databaseEngine) {
case Constants.DataBaseTypes.DB_TYPE_H2:
case Constants.DataBaseTypes.DB_TYPE_MYSQL:
case Constants.DataBaseTypes.DB_TYPE_POSTGRESQL:
case Constants.DataBaseTypes.DB_TYPE_ORACLE:
return new GenericVisibilityDAOImpl();
default:
throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine);

@ -175,7 +175,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
"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) "
+ "LEFT JOIN AP_UNRESTRICTED_ROLES ON AP_APP.ID = AP_UNRESTRICTED_ROLES.AP_APP_ID) "
+ "WHERE AP_APP.TENANT_ID = ?";
+ "WHERE AP_APP.TENANT_ID = ? AND AP_APP.STATUS != ?";
if (filter == null) {
@ -204,20 +204,21 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
conn = this.getDBConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, AppLifecycleState.REMOVED.toString());
if (filter.getAppType() != null) {
stmt.setString(2, filter.getAppType());
stmt.setString(3, filter.getAppType());
}
if (filter.getAppName() != null) {
if (filter.isFullMatch()) {
stmt.setString(3, filter.getAppName().toLowerCase());
stmt.setString(4, filter.getAppName().toLowerCase());
} else {
stmt.setString(3, "%" + filter.getAppName().toLowerCase() + "%");
stmt.setString(4, "%" + filter.getAppName().toLowerCase() + "%");
}
}
stmt.setInt(4, filter.getLimit());
stmt.setInt(5, filter.getOffset());
stmt.setInt(5, filter.getLimit());
stmt.setInt(6, filter.getOffset());
rs = stmt.executeQuery();
applicationList.setApplications(Util.loadApplications(rs));
applicationList.setPagination(pagination);
@ -254,7 +255,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
conn = this.getDBConnection();
sql += "SELECT APP_RELEASE.UUID AS UUID FROM AP_APP_RELEASE AS APP_RELEASE, AP_APP_LIFECYCLE_STATE "
+ "AS LIFECYCLE WHERE APP_RELEASE.AP_APP_ID=? AND APP_RELEASE.ID = LIFECYCLE.AP_APP_RELEASE_ID "
+ "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.setInt(1, appId);
@ -333,12 +334,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
conn = this.getDBConnection();
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 AS IS_FREE, AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG" +
".TAG AS " +
"APP_TAG, AP_UNRESTRICTED_ROLES.ROLE AS AS ROLE FROM AP_APP, AP_APP_TAG, " +
"AP_UNRESTRICTED_ROLES " +
"WHERE AP_APP.NAME=? AND AP_APP.TYPE= ? AND AP_APP.TENANT_ID=?;";
+ "AS APP_CATEGORY, AP_APP.IS_FREE AS IS_FREE, AP_APP.RESTRICTED AS RESTRICTED, " +
"AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLES.ROLE AS ROLE FROM AP_APP, AP_APP_TAG, " +
"AP_UNRESTRICTED_ROLES WHERE AP_APP.NAME=? AND AP_APP.TYPE= ? AND AP_APP.TENANT_ID=?;";
stmt = conn.prepareStatement(sql);
stmt.setString(1, appName);
@ -378,13 +376,10 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
try {
conn = this.getDBConnection();
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 AS IS_FREE, AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG" +
".TAG AS APP_TAG, " +
"AP_UNRESTRICTED_ROLES.ROLE AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE " +
"AP_APP.ID=?" +
" AND AP_APP.TENANT_ID=?;";
"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 AS IS_FREE, AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG"
+ ".TAG AS APP_TAG, AP_UNRESTRICTED_ROLES.ROLE AS ROLE FROM AP_APP, AP_APP_TAG, " +
"AP_UNRESTRICTED_ROLES WHERE AP_APP.ID=? AND AP_APP.TENANT_ID=?;";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, applicationId);
@ -424,10 +419,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
conn = this.getDBConnection();
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_TAG.TAG, AP_UNRESTRICTED_ROLES.ROLE AS RELESE_ID" +
" FROM "
+ "AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.ID=?;";
+ "AS APP_CATEGORY, AP_APP.IS_FREE, AP_APP_TAG.TAG, AP_UNRESTRICTED_ROLES.ROLE AS RELESE_ID"
+ " FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.ID = ?;";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, appId);

@ -0,0 +1,127 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.application.mgt.core.dao.impl.application;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.wso2.carbon.device.application.mgt.common.AppLifecycleState;
import org.wso2.carbon.device.application.mgt.common.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.Filter;
import org.wso2.carbon.device.application.mgt.common.Pagination;
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
import org.wso2.carbon.device.application.mgt.core.dao.common.Util;
import org.wso2.carbon.device.application.mgt.core.dao.impl.application.GenericApplicationDAOImpl;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* This is a ApplicationDAO Implementation specific to Oracle.
*/
public class OracleApplicationDAOImpl extends GenericApplicationDAOImpl {
private static final Log log = LogFactory.getLog(OracleApplicationDAOImpl.class);
@Override
public ApplicationList getApplications(Filter filter, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting application data from the database");
log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset()));
}
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
ApplicationList applicationList = new ApplicationList();
Pagination pagination = new Pagination();
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_UNRESTRICTED_ROLES FROM ((AP_APP LEFT JOIN AP_APP_TAG ON " +
"AP_APP.ID = AP_APP_TAG.AP_APP_ID) LEFT JOIN AP_UNRESTRICTED_ROLES ON " +
"AP_APP.ID = AP_UNRESTRICTED_ROLES.AP_APP_ID) WHERE AP_APP.TENANT_ID = ? AND AP_APP.STATUS != ?";
if (filter == null) {
throw new ApplicationManagementDAOException("Filter need to be instantiated");
}
if (filter.getAppType() != null) {
sql += " AND AP_APP.TYPE ";
sql += "= ?";
}
if (filter.getAppName() != null) {
sql += " AND LOWER (AP_APP.NAME) ";
if (filter.isFullMatch()) {
sql += "= ?";
} else {
sql += "LIKE ?";
}
}
sql += " AND rownum <= ? OFFSET ? " + filter.getSortBy() + " APP_ID";
pagination.setLimit(filter.getLimit());
pagination.setOffset(filter.getOffset());
try {
conn = this.getDBConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, AppLifecycleState.REMOVED.toString());
if (filter.getAppType() != null) {
stmt.setString(3, filter.getAppType());
}
if (filter.getAppName() != null) {
if (filter.isFullMatch()) {
stmt.setString(4, filter.getAppName().toLowerCase());
} else {
stmt.setString(4, "%" + filter.getAppName().toLowerCase() + "%");
}
}
stmt.setInt(5, filter.getLimit());
stmt.setInt(6, filter.getOffset());
rs = stmt.executeQuery();
applicationList.setApplications(Util.loadApplications(rs));
applicationList.setPagination(pagination);
applicationList.getPagination().setSize(filter.getOffset());
applicationList.getPagination().setCount(applicationList.getApplications().size());
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting application list for the tenant"
+ " " + tenantId + ". While executing " + sql, e);
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection while "
+ "getting application list for the tenant " + tenantId,
e);
} catch (JSONException e) {
throw new ApplicationManagementDAOException("Error occurred while parsing JSON ", e);
} finally {
Util.cleanupResources(stmt, rs);
}
return applicationList;
}
}

@ -182,7 +182,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
@Override
public List<ApplicationRelease> getApplicationReleases(String applicationName, String applicationType, int tenantId)
public List<ApplicationRelease> getReleases(String applicationName, String applicationType, int tenantId)
throws ApplicationManagementDAOException {
Connection connection;
PreparedStatement statement = null;

@ -1,76 +0,0 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.device.application.mgt.core.dao.impl.application.release;
import org.wso2.carbon.device.application.mgt.common.Filter;
import org.wso2.carbon.device.application.mgt.core.dao.impl.application.GenericApplicationDAOImpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* This is a ApplicationDAO Implementation specific to Oracle.
*/
public class OracleApplicationDAOImpl extends GenericApplicationDAOImpl {
// @Override
// protected PreparedStatement generateGetApplicationsStatement(Filter filter, Connection conn,
// int tenantId) throws SQLException {
// int index = 0;
// String sql = "SELECT APP.*, APL.NAME AS APL_NAME, APL.IDENTIFIER AS APL_IDENTIFIER, CAT.ID AS CAT_ID, "
// + "CAT.NAME AS CAT_NAME, LS.NAME AS LS_NAME, LS.IDENTIFIER AS LS_IDENTIFIER, "
// + "LS.DESCRIPTION AS LS_DESCRIPTION " + "FROM APPM_APPLICATION APP " + "INNER JOIN APPM_PLATFORM APL "
// + "ON APP.PLATFORM_ID = APL.ID " + "INNER JOIN APPM_APPLICATION_CATEGORY CAT "
// + "ON APP.APPLICATION_CATEGORY_ID = CAT.ID " + "INNER JOIN APPM_LIFECYCLE_STATE LS "
// + "ON APP.LIFECYCLE_STATE_ID = LS.ID WHERE APP.TENANT_ID = ? ";
//
// String userName = filter.getUserName();
// if (!userName.equals("ALL")) {
// sql += " AND APP.CREATED_BY = ? ";
// }
// if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
// sql += "AND LOWER (APP.NAME) ";
// if (filter.isFullMatch()) {
// sql += "= ?";
// } else {
// sql += "LIKE ?";
// }
// }
// sql += " ORDER BY APP.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
// PreparedStatement stmt = conn.prepareStatement(sql);
// stmt.setInt(++index, tenantId);
//
// if (!userName.equals("ALL")) {
// stmt.setString(++index, userName);
// }
// if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
// if (filter.isFullMatch()) {
// stmt.setString(++index, filter.getSearchQuery().toLowerCase());
// } else {
// stmt.setString(++index, "%" + filter.getSearchQuery().toLowerCase() + "%");
// }
// }
// stmt.setInt(++index, filter.getOffset());
// stmt.setInt(++index, filter.getLimit());
// return stmt;
// }
}

@ -36,7 +36,7 @@ import java.util.List;
/**
* Concrete implementation for Lifecycle related DB operations.
*/
public class GenericLifecycleStateImpl extends AbstractDAOImpl implements LifecycleStateDAO {
public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements LifecycleStateDAO {
@Override
public LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws ApplicationManagementDAOException {
@ -80,13 +80,12 @@ public class GenericLifecycleStateImpl extends AbstractDAOImpl implements Lifecy
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
int index = 0;
try {
conn = this.getDBConnection();
String sql = "SELECT ID, CURRENT_STATE, PREVIOUSE_STATE, TENANT_ID, UPDATED_AT, UPDATED_BY FROM "
+ "AP_APP_LIFECYCLE_STATE WHERE AP_APP_RELEASE_ID = ? ORDER BY UPDATED_AT ASC;";
stmt = conn.prepareStatement(sql);
stmt.setInt(++index,appReleaseId);
stmt.setInt(1,appReleaseId);
rs = stmt.executeQuery();
while (rs.next()) {
LifecycleState lifecycleState = new LifecycleState();

@ -49,16 +49,15 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
int index = 0;
String sql = "INSERT INTO AP_UNRESTRICTED_ROLES (ROLE, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)";
try{
conn = this.getDBConnection();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql);
for (UnrestrictedRole role : unrestrictedRoles) {
stmt.setString(++index, role.getRole());
stmt.setInt(++index, tenantId);
stmt.setInt(++index, applicationId);
stmt.setString(1, role.getRole());
stmt.setInt(2, tenantId);
stmt.setInt(3, applicationId);
stmt.addBatch();
}
stmt.executeBatch();
@ -82,14 +81,13 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
ResultSet rs = null;
List<UnrestrictedRole> unrestrictedRoles = new ArrayList<>();
UnrestrictedRole unrestrictedRole = null;
int index = 0;
String sql = "SELECT ID, ROLE FROM AP_UNRESTRICTED_ROLES WHERE AP_APP_ID = ? AND TENANT_ID = ?;";
try{
conn = this.getDBConnection();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql);
stmt.setInt(++index, applicationId);
stmt.setInt(++index, tenantId);
stmt.setInt(1, applicationId);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery();
while (rs.next()){
@ -117,17 +115,16 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
int index = 0;
String sql = "DELETE FROM AP_UNRESTRICTED_ROLES WHERE AP_APP_ID = 1 AND ROLE = 'role1' AND TENANT_ID = -1234;";
String sql = "DELETE FROM AP_UNRESTRICTED_ROLES WHERE AP_APP_ID = ? AND ROLE = ? AND TENANT_ID = ?;";
try{
conn = this.getDBConnection();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql);
for (UnrestrictedRole role : unrestrictedRoles) {
stmt.setInt(++index, applicationId);
stmt.setString(++index, role.getRole());
stmt.setInt(++index, role.getTenantId());
stmt.setInt(1, applicationId);
stmt.setString(2, role.getRole());
stmt.setInt(3, role.getTenantId());
stmt.addBatch();
}
stmt.executeBatch();

@ -387,7 +387,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
try {
ConnectionManagerUtil.openDBConnection();
applicationReleases = ApplicationManagementDAOFactory.getApplicationReleaseDAO()
.getApplicationReleases(application.getName(), application.getType(), tenantId);
.getReleases(application.getName(), application.getType(), tenantId);
for (ApplicationRelease applicationRelease : applicationReleases) {
if (!AppLifecycleState.REMOVED.toString().equals(ApplicationManagementDAOFactory.getLifecycleStateDAO().
getLatestLifeCycleStateByReleaseID(applicationRelease.getId()).getCurrentState())) {

Loading…
Cancel
Save