Format APPM source

feature/appm-store/pbac
lasanthaDLPDS 5 years ago
parent 345125c042
commit f48f939e7a

@ -28,24 +28,14 @@ import java.util.List;
*/ */
public interface LifecycleStateDAO { public interface LifecycleStateDAO {
/**
* To get the latest lifecycle state for the given application release id.
* @param applicationReleaseId id of the application release.
*
* @return Latest Lifecycle State for the given application release
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/
LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException;
/** /**
* To get the latest lifecycle state for the given application id and the application release UUID. * To get the latest lifecycle state for the given application id and the application release UUID.
* @param appId id of the application.
* @param uuid UUID of the application release * @param uuid UUID of the application release
* *
* @return Latest Lifecycle State for the given application release * @return Latest Lifecycle State for the given application release
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/ */
LifecycleState getLatestLifeCycleState(int appId, String uuid) throws LifeCycleManagementDAOException; LifecycleState getLatestLifeCycleState(String uuid) throws LifeCycleManagementDAOException;
/** /**
* To get all changed lifecycle states for the given application release id. * To get all changed lifecycle states for the given application release id.

@ -23,26 +23,42 @@ import org.wso2.carbon.device.application.mgt.core.exception.VisibilityManagemen
import java.util.List; import java.util.List;
/** /**
* This interface provides the list of operations that are performed in the database * This interface provides the list of operations that are performed in the database layer with respect to the
* layer with respect to the visibility. * visibility.
* *
*/ */
public interface VisibilityDAO { public interface VisibilityDAO {
/** /***
* To add unrestricted roles for a particular application. * This method is used to add unrestricted roles for a particular application.
* *
* @param unrestrictedRoles unrestrictedRoles that could available the application. * @param unrestrictedRoles List of roles. User should have assigned at least one role from unrestricted role list
* @throws VisibilityManagementDAOException Visiblity Management DAO Exception. * to view the application.
* @param applicationId Id of the application.
* @param tenantId Tenant Id.
* @throws VisibilityManagementDAOException if an error occured while executing the query.
*/ */
void addUnrestrictedRoles(List<String> unrestrictedRoles, int applicationId, int tenantId) throws void addUnrestrictedRoles(List<String> unrestrictedRoles, int applicationId, int tenantId)
VisibilityManagementDAOException; throws VisibilityManagementDAOException;
/***
* This method is used to get unrestricted roles of an particular application.
*
* @param applicationId Id of the application.
* @param tenantId Tenant Id.
* @return List of unrestricted roles of the application.
* @throws VisibilityManagementDAOException if an error occured while executing the query.
*/
List<String> getUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException; List<String> getUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException;
List<String> getUnrestrictedRolesByUUID(String uuid, int tenantId) throws VisibilityManagementDAOException; /***
* This method is used to delete unrestricted roles of an particular application.
void deleteUnrestrictedRoles(List<String> unrestrictedRoles, int applicationId, int tenantId) throws *
VisibilityManagementDAOException; * @param unrestrictedRoles List of unrestricted roles which are going to remove from the application.
* @param applicationId Id of the application.
* @param tenantId Tenant Id.
* @throws VisibilityManagementDAOException if an error occured while executing the query.
*/
void deleteUnrestrictedRoles(List<String> unrestrictedRoles, int applicationId, int tenantId)
throws VisibilityManagementDAOException;
} }

@ -21,7 +21,6 @@ package org.wso2.carbon.device.application.mgt.core.dao.impl.application;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.AppLifecycleState;
import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO;
import org.wso2.carbon.device.application.mgt.common.dto.CategoryDTO; import org.wso2.carbon.device.application.mgt.common.dto.CategoryDTO;
@ -994,8 +993,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
log.error(msg); log.error(msg);
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
String msg = "SQL Error occurred when deleting category which has ID: " + categoryId + ". Executed query: " String msg = "SQL Error occurred when deleting category which has ID: " + categoryId + ". Query: " + sql;
+ sql;
log.error(msg); log.error(msg);
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} }
@ -1111,7 +1109,10 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get distinct tag ids in tag mapping."); log.debug("Request received in DAO Layer to get distinct tag ids in tag mapping.");
} }
String sql = "SELECT DISTINCT tm.AP_APP_TAG_ID AS ID FROM AP_APP_TAG_MAPPING tm"; String sql = "SELECT "
+ "DISTINCT "
+ "tm.AP_APP_TAG_ID AS ID "
+ "FROM AP_APP_TAG_MAPPING tm";
try { try {
Connection conn = this.getDBConnection(); Connection conn = this.getDBConnection();
List<Integer> distinctTagIds = new ArrayList<>(); List<Integer> distinctTagIds = new ArrayList<>();
@ -1142,16 +1143,14 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
log.debug("Request received in DAO Layer to add application tags which has application ID: " log.debug("Request received in DAO Layer to add application tags which has application ID: "
+ applicationId); + applicationId);
} }
Connection conn;
PreparedStatement stmt = null;
String sql = "INSERT INTO AP_APP_TAG_MAPPING " String sql = "INSERT INTO AP_APP_TAG_MAPPING "
+ "(AP_APP_TAG_ID, " + "(AP_APP_TAG_ID, "
+ "AP_APP_ID, " + "AP_APP_ID, "
+ " TENANT_ID) " + " TENANT_ID) "
+ "VALUES (?, ?, ?)"; + "VALUES (?, ?, ?)";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
stmt = conn.prepareStatement(sql); try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (Integer tagId : tagIds) { for (Integer tagId : tagIds) {
stmt.setInt(1, tagId); stmt.setInt(1, tagId);
stmt.setInt(2, applicationId); stmt.setInt(2, applicationId);
@ -1159,23 +1158,24 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
}
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection to add tags for application which has ID: "
"Error occurred while obtaining the DB connection when adding tags", e); + applicationId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while adding tags", e); String msg = "SQL Error occurred when adding tags for application which has the ID: " + applicationId;
} finally { log.error(msg);
DAOUtil.cleanupResources(stmt, null); throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public List<String> getAppTags(int appId, int tenantId) throws ApplicationManagementDAOException { public List<String> getAppTags(int appId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get tags for given application."); log.debug("Request received in DAO Layer to get tags of application which has application Id " + appId);
} }
Connection conn;
List<String> tags = new ArrayList<>(); List<String> tags = new ArrayList<>();
String sql = "SELECT tag.TAG AS TAG " String sql = "SELECT tag.TAG AS TAG "
+ "FROM " + "FROM "
@ -1183,7 +1183,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
+ "INNER JOIN AP_APP app ON tag_map.AP_APP_ID = app.ID " + "INNER JOIN AP_APP app ON tag_map.AP_APP_ID = app.ID "
+ "WHERE app.ID = ? AND app.TENANT_ID = ?"; + "WHERE app.ID = ? AND app.TENANT_ID = ?";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)){ try (PreparedStatement stmt = conn.prepareStatement(sql)){
stmt.setInt(1, appId); stmt.setInt(1, appId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -1195,10 +1195,15 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
return tags; return tags;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection to get application tags. Application Id: "
"Error occurred while obtaining the DB connection when adding tags", e); + appId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while adding tags", e); String msg = "SQL error occured while getting application tags. ApplicationId: " + appId + " Executed "
+ "query " + sql;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@ -1207,7 +1212,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to verify whether tag is associated with an application."); log.debug("Request received in DAO Layer to verify whether tag is associated with an application.");
} }
Connection conn;
String sql = "SELECT tm.AP_APP_ID AS ID " String sql = "SELECT tm.AP_APP_ID AS ID "
+ "FROM AP_APP_TAG_MAPPING tm " + "FROM AP_APP_TAG_MAPPING tm "
+ "WHERE " + "WHERE "
@ -1215,7 +1219,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
+ "tm.AP_APP_ID = ? AND " + "tm.AP_APP_ID = ? AND "
+ "tm.TENANT_ID = ?"; + "tm.TENANT_ID = ?";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tagId); stmt.setInt(1, tagId);
stmt.setInt(2, applicationId); stmt.setInt(2, applicationId);
@ -1225,28 +1229,31 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection when verifying the existence of a tag "
"Error occurred while obtaining the DB connection when verifying the existence of a tag mapping", + "mapping. Application ID " + applicationId + " tag ID: " + tagId;
e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred when verifying the existence of a tag mapping.", String msg = "SQL Error occurred when verifying the existence of a tag mapping. Application ID "
e); + applicationId + " tag ID " + tagId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public boolean hasTagMapping (int tagId, int tenantId) throws ApplicationManagementDAOException{ public boolean hasTagMapping (int tagId, int tenantId) throws ApplicationManagementDAOException{
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to verify whether tag is associated with at least one application."); log.debug("Request received in DAO Layer to verify whether tag is associated with at least one "
+ "application.");
} }
Connection conn;
String sql = "SELECT tm.AP_APP_ID AS ID " String sql = "SELECT tm.AP_APP_ID AS ID "
+ "FROM AP_APP_TAG_MAPPING tm " + "FROM AP_APP_TAG_MAPPING tm "
+ "WHERE " + "WHERE "
+ "tm.AP_APP_TAG_ID = ? AND " + "tm.AP_APP_TAG_ID = ? AND "
+ "tm.TENANT_ID = ?"; + "tm.TENANT_ID = ?";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tagId); stmt.setInt(1, tagId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -1255,28 +1262,31 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection to verify whether tag is associated with at "
"Error occurred while obtaining the DB connection when verifying the existence of a tag mapping", + "least one application. Tag ID " + tagId;
e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred when verifying the existence of a tag mapping.", String msg = "Error occurred while executing the query to verify whether tag is associated with at least "
e); + "one application. Tag ID " + tagId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public void deleteApplicationTags(List<Integer> tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException{ public void deleteApplicationTags(List<Integer> tagIds, int applicationId, int tenantId)
throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to delete Tag mappings."); log.debug("Request received in DAO Layer to delete Tag mappings.");
} }
Connection conn;
String sql = "DELETE FROM " String sql = "DELETE FROM "
+ "AP_APP_TAG_MAPPING WHERE " + "AP_APP_TAG_MAPPING WHERE "
+ "AP_APP_TAG_ID = ? AND " + "AP_APP_TAG_ID = ? AND "
+ "AP_APP_ID = ? AND " + "AP_APP_ID = ? AND "
+ "TENANT_ID = ?"; + "TENANT_ID = ?";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)){ try (PreparedStatement stmt = conn.prepareStatement(sql)){
for (Integer tagId : tagIds){ for (Integer tagId : tagIds){
stmt.setInt(1, tagId); stmt.setInt(1, tagId);
@ -1287,10 +1297,15 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
stmt.executeBatch(); stmt.executeBatch();
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection to delete tag mapping. Application ID: "
"Error occurred while obtaining the DB connection when deleting tag mapping", e); + applicationId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred when deleting tag mapping", e); String msg = "SQL Error occurred while executing the query to delete tag mapping. Application ID: "
+ applicationId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@ -1299,7 +1314,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to delete Tag mapping."); log.debug("Request received in DAO Layer to delete Tag mapping.");
} }
Connection conn;
String sql = "DELETE FROM " String sql = "DELETE FROM "
+ "AP_APP_TAG_MAPPING " + "AP_APP_TAG_MAPPING "
+ "WHERE " + "WHERE "
@ -1307,7 +1321,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
+ "AP_APP_ID = ? AND " + "AP_APP_ID = ? AND "
+ "TENANT_ID = ?"; + "TENANT_ID = ?";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tagId); stmt.setInt(1, tagId);
stmt.setInt(2, applicationId); stmt.setInt(2, applicationId);
@ -1315,103 +1329,111 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
stmt.executeUpdate(); stmt.executeUpdate();
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection to delete a tag mapping. Application ID "
"Error occurred while obtaining the DB connection when deleting a tag mapping", e); + applicationId + " tag ID " + tagId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("SQL Error occurred when deleting a tag mapping", e); String msg = "SQL Error occurred while executing the query to delete a tag mapping. Application ID "
+ applicationId + " tag ID " + tagId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public void deleteApplicationTags(int applicationId, int tenantId) throws ApplicationManagementDAOException{ public void deleteApplicationTags(int applicationId, int tenantId) throws ApplicationManagementDAOException{
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to delete application tags."); log.debug("Request received in DAO Layer to delete application tags for application ID " + applicationId);
} }
Connection conn;
String sql = "DELETE FROM " String sql = "DELETE FROM "
+ "AP_APP_TAG_MAPPING " + "AP_APP_TAG_MAPPING "
+ "WHERE " + "WHERE "
+ "AP_APP_ID = ? AND " + "AP_APP_ID = ? AND "
+ "TENANT_ID = ?"; + "TENANT_ID = ?";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, applicationId); stmt.setInt(1, applicationId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection when deleting application tags for "
"Error occurred while obtaining the DB connection when deleting application tags for application ID: " + "application ID: " + applicationId;
+ applicationId, e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException( String msg = "SQL Error occurred when deleting application tags for application ID: " + applicationId + "."
"Error occurred when deleting application tags for application ID: " + applicationId, e); + " Executed query " + sql;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public void deleteTagMapping(int tagId, int tenantId) throws ApplicationManagementDAOException { public void deleteTagMapping(int tagId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to delete Tag mappings."); log.debug("Request received in DAO Layer to delete application tag. Tag Id " + tagId);
} }
Connection conn;
String sql = "DELETE FROM " + String sql = "DELETE FROM " +
"AP_APP_TAG_MAPPING " + "AP_APP_TAG_MAPPING " +
"WHERE " + "WHERE " +
"AP_APP_ID = ? AND " + "AP_APP_ID = ? AND " +
"TENANT_ID = ?"; "TENANT_ID = ?";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tagId); stmt.setInt(1, tagId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection when deleting application tag which has tag"
"Error occurred while obtaining the DB connection when deleting tag mapping of tag ID: " + tagId, + " ID: " + tagId;
e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred when deleting tag mapping of tag ID: " + tagId, String msg = "SQL error occurred when deleting application tag which has tag ID: " + tagId + ". executed "
e); + "query: " + sql;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public void deleteTag(int tagId, int tenantId) throws ApplicationManagementDAOException { public void deleteTag(int tagId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to delete Tag mappings."); log.debug("Request received in DAO Layer to delete Tag which has tag ID " + tagId);
} }
Connection conn;
String sql = "DELETE FROM " + String sql = "DELETE FROM " +
"AP_APP_TAG " + "AP_APP_TAG " +
"WHERE " + "WHERE " +
"ID = ? AND " + "ID = ? AND " +
"TENANT_ID = ?"; "TENANT_ID = ?";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tagId); stmt.setInt(1, tagId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection when deleting tag which has ID: " + tagId;
"Error occurred while obtaining the DB connection when deleting tag which has ID: " + tagId, log.error(msg);
e); throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred when deleting tag which has ID: " + tagId, String msg = "SQL Error occurred when deleting tag which has ID: " + tagId + ". Executed query " + sql;
e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public void updateTag(TagDTO tagDTO, int tenantId) throws ApplicationManagementDAOException { public void updateTag(TagDTO tagDTO, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to update the Tag."); log.debug("Request received in DAO Layer to update a Tag.");
} }
Connection conn;
String sql = "UPDATE " + String sql = "UPDATE " +
"AP_APP_TAG tag " + "AP_APP_TAG tag " +
"SET tag.TAG = ? " + "SET tag.TAG = ? " +
@ -1419,7 +1441,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
"tag.ID = ? AND " + "tag.ID = ? AND " +
"tag.TENANT_ID = ?"; "tag.TENANT_ID = ?";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, tagDTO.getTagName()); stmt.setString(1, tagDTO.getTagName());
stmt.setInt(2, tagDTO.getId()); stmt.setInt(2, tagDTO.getId());
@ -1427,21 +1449,23 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
stmt.executeUpdate(); stmt.executeUpdate();
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection to update tag which has ID: "
"Error occurred while obtaining the DB connection when updating tag which has ID: " + tagDTO + tagDTO.getId();
.getId(), e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException( String msg = "SQL Error occurred when updating tag which has ID: " + tagDTO.getId() + ". Executed query: "
"Error occurred when updating tag which has ID: " + tagDTO.getId(), e); + sql;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public List<String> getAppCategories(int appId, int tenantId) throws ApplicationManagementDAOException { public List<String> getAppCategories(int appId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get categories for given application."); log.debug("Request received in DAO Layer to get application categories for given application.");
} }
Connection conn;
List<String> categories = new ArrayList<>(); List<String> categories = new ArrayList<>();
String sql = "SELECT CATEGORY " String sql = "SELECT CATEGORY "
+ "FROM " + "FROM "
@ -1449,7 +1473,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
+ "INNER JOIN AP_APP app ON cat_map.AP_APP_ID = app.ID " + "INNER JOIN AP_APP app ON cat_map.AP_APP_ID = app.ID "
+ "WHERE app.ID = ? AND app.TENANT_ID = ?"; + "WHERE app.ID = ? AND app.TENANT_ID = ?";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)){ try (PreparedStatement stmt = conn.prepareStatement(sql)){
stmt.setInt(1, appId); stmt.setInt(1, appId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -1461,10 +1485,15 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
return categories; return categories;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection to get application categories for "
"Error occurred while obtaining the DB connection when adding tags", e); + "application which has ID " + appId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while adding tags", e); String msg = "SQL error occurred while executing query to get application categories for "
+ "application which has ID " + appId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@ -1473,14 +1502,13 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to verify whether tag is associated with at least one application."); log.debug("Request received in DAO Layer to verify whether tag is associated with at least one application.");
} }
Connection conn;
String sql = "SELECT cm.AP_APP_ID AS ID " String sql = "SELECT cm.AP_APP_ID AS ID "
+ "FROM AP_APP_CATEGORY_MAPPING cm " + "FROM AP_APP_CATEGORY_MAPPING cm "
+ "WHERE " + "WHERE "
+ "cm.AP_APP_CATEGORY_ID = ? AND " + "cm.AP_APP_CATEGORY_ID = ? AND "
+ "cm.TENANT_ID = ?"; + "cm.TENANT_ID = ?";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, categoryId); stmt.setInt(1, categoryId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -1489,53 +1517,56 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection when verifying the existence of a category "
"Error occurred while obtaining the DB connection when verifying the existence of a category mapping", + "mapping for category ID " + categoryId;
e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException( String msg = "SQL Error occurred when verifying the existence of a category mapping for category ID "
"Error occurred when verifying the existence of a category mapping.", e); + categoryId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public boolean isExistingAppName(String appName, int deviceTypeId, int tenantId) throws ApplicationManagementDAOException { public boolean isExistingAppName(String appName, int deviceTypeId, int tenantId) throws ApplicationManagementDAOException {
Connection conn; String sql = "SELECT AP_APP.ID AS ID "
PreparedStatement stmt = null;
ResultSet rs = null;
String sql;
try {
conn = this.getDBConnection();
sql = "SELECT AP_APP.ID AS ID "
+ "FROM AP_APP " + "FROM AP_APP "
+ "WHERE " + "WHERE "
+ "AP_APP.NAME = ? AND " + "AP_APP.NAME = ? AND "
+ "AP_APP.DEVICE_TYPE_ID = ? AND " + "AP_APP.DEVICE_TYPE_ID = ? AND "
+ "AP_APP.TENANT_ID = ?"; + "AP_APP.TENANT_ID = ?";
stmt = conn.prepareStatement(sql); try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)){
stmt.setString(1, appName); stmt.setString(1, appName);
stmt.setInt(2, deviceTypeId); stmt.setInt(2, deviceTypeId);
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
rs = stmt.executeQuery(); try (ResultSet rs = stmt.executeQuery()){
return rs.next(); return rs.next();
}
}
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); String msg = "Error occurred while obtaining the DB connection to check whether the existence of "
+ "application name for device type which has device type ID " + deviceTypeId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting application List", e); String msg = "Error occurred while executing query to check whether the existence of application name for "
} finally { + "device type which has device type ID " + deviceTypeId + ". executed query " + sql;
DAOUtil.cleanupResources(stmt, rs); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public void deleteApplication(int appId, int tenantId) throws ApplicationManagementDAOException { public void deleteApplication(int appId, int tenantId) throws ApplicationManagementDAOException {
Connection conn; String sql = "DELETE FROM AP_APP "
String sql;
try {
conn = this.getDBConnection();
sql = "DELETE FROM AP_APP "
+ "WHERE ID = ? AND " + "WHERE ID = ? AND "
+ "TENANT_ID = ?"; + "TENANT_ID = ?";
try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)){ try (PreparedStatement stmt = conn.prepareStatement(sql)){
stmt.setInt(1, appId); stmt.setInt(1, appId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -1548,7 +1579,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
log.error(msg); log.error(msg);
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while deleting application for application ID: " + appId; String msg = "SQL Error occurred while deleting application for application ID: " + appId + " Executed "
+ "query " + sql;
log.error(msg); log.error(msg);
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} }

@ -43,65 +43,53 @@ import java.util.List;
public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements LifecycleStateDAO { public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements LifecycleStateDAO {
private static final Log log = LogFactory.getLog(GenericLifecycleStateDAOImpl.class); private static final Log log = LogFactory.getLog(GenericLifecycleStateDAOImpl.class);
@Override
public LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException {
Connection conn = null; @Override
PreparedStatement stmt = null; public LifecycleState getLatestLifeCycleState(String uuid) throws LifeCycleManagementDAOException{
ResultSet rs = null; String sql = "SELECT "
+ "CURRENT_STATE, "
+ "PREVIOUS_STATE, "
+ "UPDATED_AT, "
+ "UPDATED_BY "
+ "FROM "
+ "AP_APP_LIFECYCLE_STATE "
+ "WHERE "
+ "AP_APP_RELEASE_ID = (SELECT ID FROM AP_APP_RELEASE WHERE UUID = ?) ORDER BY UPDATED_AT DESC";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
String sql = "SELECT ID, CURRENT_STATE, PREVIOUS_STATE, TENANT_ID, UPDATED_AT, UPDATED_BY FROM " try (PreparedStatement stmt = conn.prepareStatement(sql)) {
+ "AP_APP_LIFECYCLE_STATE WHERE AP_APP_RELEASE_ID=? ORDER BY UPDATED_AT DESC;"; stmt.setString(1, uuid);
try (ResultSet rs = stmt.executeQuery()){
stmt = conn.prepareStatement(sql);
stmt.setInt(1, applicationReleaseId);
rs = stmt.executeQuery();
return constructLifecycle(rs); return constructLifecycle(rs);
} catch (SQLException e) {
throw new LifeCycleManagementDAOException("Error occurred while getting application List", e);
} catch (DBConnectionException e) {
throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection to get latest"
+ " lifecycle state for a specific application", e);
} finally {
DAOUtil.cleanupResources(stmt, rs);
} }
} }
public LifecycleState getLatestLifeCycleState(int appId, String uuid) throws LifeCycleManagementDAOException{
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getDBConnection();
String sql = "SELECT ID, CURRENT_STATE, PREVIOUS_STATE, TENANT_ID, UPDATED_AT, UPDATED_BY FROM "
+ "AP_APP_LIFECYCLE_STATE WHERE AP_APP_ID=? AND AP_APP_RELEASE_ID=(SELECT ID FROM AP_APP_RELEASE "
+ "WHERE UUID=?) ORDER BY UPDATED_AT DESC;";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, appId);
stmt.setString(2, uuid);
rs = stmt.executeQuery();
return constructLifecycle(rs);
} catch (SQLException e) {
throw new LifeCycleManagementDAOException("Error occurred while getting application List", e);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection to get latest" String msg = "Error occurred while obtaining the DB connection to get latest lifecycle state for a specific"
+ " lifecycle state for a specific application", e); + " application. Application release UUID: " + uuid;
} finally { log.error(msg);
DAOUtil.cleanupResources(stmt, rs); throw new LifeCycleManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while executing query to get latest lifecycle state for a specific "
+ "application. Application release UUID: " + uuid + ". Executed Query: " + sql;
log.error(msg);
throw new LifeCycleManagementDAOException(msg, e);
} }
} }
@Override
public String getAppReleaseCreatedUsername(int appId, String uuid, int tenantId) throws LifeCycleManagementDAOException{ public String getAppReleaseCreatedUsername(int appId, String uuid, int tenantId) throws LifeCycleManagementDAOException{
Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
String sql = "SELECT UPDATED_BY FROM AP_APP_LIFECYCLE_STATE WHERE AP_APP_ID=? AND " String sql = "SELECT "
+ "AP_APP_RELEASE_ID=(SELECT ID FROM AP_APP_RELEASE WHERE UUID=?) AND CURRENT_STATE = ? AND TENANT_ID = ?;"; + "UPDATED_BY "
+ "FROM AP_APP_LIFECYCLE_STATE "
+ "WHERE "
+ "AP_APP_ID = ? AND "
+ "AP_APP_RELEASE_ID = (SELECT ID FROM AP_APP_RELEASE WHERE UUID=?) AND "
+ "CURRENT_STATE = ? AND "
+ "TENANT_ID = ? ORDER BY UPDATED_AT DESC LIMIT 1";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, appId); stmt.setInt(1, appId);
@ -127,9 +115,8 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
@Override @Override
public List<LifecycleState> getLifecycleStates(int appReleaseId, int tenantId) throws LifeCycleManagementDAOException { public List<LifecycleState> getLifecycleStates(int appReleaseId, int tenantId) throws LifeCycleManagementDAOException {
List<LifecycleState> lifecycleStates = new ArrayList<>(); List<LifecycleState> lifecycleStates = new ArrayList<>();
Connection conn ;
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
String sql = "SELECT " String sql = "SELECT "
+ "CURRENT_STATE, " + "CURRENT_STATE, "
+ "PREVIOUS_STATE, " + "PREVIOUS_STATE, "
@ -138,36 +125,32 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
+ "FROM AP_APP_LIFECYCLE_STATE " + "FROM AP_APP_LIFECYCLE_STATE "
+ "WHERE AP_APP_RELEASE_ID = ? AND " + "WHERE AP_APP_RELEASE_ID = ? AND "
+ "TENANT_ID = ? " + "TENANT_ID = ? "
+ "ORDER BY UPDATED_AT ASC;"; + "ORDER BY UPDATED_AT ASC";
try (PreparedStatement stmt = conn.prepareStatement(sql)){ try (PreparedStatement stmt = conn.prepareStatement(sql)){
stmt.setInt(1,appReleaseId); stmt.setInt(1,appReleaseId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
try (ResultSet rs = stmt.executeQuery()){ try (ResultSet rs = stmt.executeQuery()){
while (rs.next()) { while (rs.next()) {
LifecycleState lifecycleState = new LifecycleState(); lifecycleStates.add(constructLifecycle(rs));
lifecycleState.setCurrentState(rs.getString("CURRENT_STATE"));
lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE"));
lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT"));
lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY"));
lifecycleStates.add(lifecycleState);
} }
} }
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection when getting " String msg = "Error occurred while obtaining the DB connection when getting lifecycle states for an "
+ "lifecycle states for an application", e); + "application which has application ID: " + appReleaseId;
log.error(msg);
throw new LifeCycleManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new LifeCycleManagementDAOException("Error occurred while retrieving lifecycle states.", e); String msg = "SQL Error occurred while retrieving lifecycle states for application which has application "
+ "ID: " + appReleaseId;
log.error(msg);
throw new LifeCycleManagementDAOException(msg, e);
} }
return lifecycleStates; return lifecycleStates;
} }
@Override @Override
public void addLifecycleState(LifecycleState state, int appReleaseId, int tenantId) throws LifeCycleManagementDAOException { public void addLifecycleState(LifecycleState state, int appReleaseId, int tenantId) throws LifeCycleManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = this.getDBConnection();
String sql = "INSERT INTO AP_APP_LIFECYCLE_STATE " String sql = "INSERT INTO AP_APP_LIFECYCLE_STATE "
+ "(CURRENT_STATE, " + "(CURRENT_STATE, "
+ "PREVIOUS_STATE, " + "PREVIOUS_STATE, "
@ -175,14 +158,13 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
+ "UPDATED_BY, " + "UPDATED_BY, "
+ "UPDATED_AT, " + "UPDATED_AT, "
+ "REASON, " + "REASON, "
+ "AP_APP_RELEASE_ID, " + "AP_APP_RELEASE_ID) "
+ "AP_APP_ID) " + "VALUES (?, ?, ?, ?, ?, ?, ?)";
+ "VALUES (?, ?, ?, ?, ?, ?, ?, (SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE ID = ?));"; try {
Connection conn = this.getDBConnection();
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt = conn.prepareStatement(sql);
stmt.setString(1, state.getCurrentState().toUpperCase()); stmt.setString(1, state.getCurrentState().toUpperCase());
stmt.setString(2, state.getPreviousState().toUpperCase()); stmt.setString(2, state.getPreviousState().toUpperCase());
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
@ -190,47 +172,53 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
stmt.setTimestamp(5, timestamp); stmt.setTimestamp(5, timestamp);
stmt.setString(6, state.getReasonForChange()); stmt.setString(6, state.getReasonForChange());
stmt.setInt(7, appReleaseId); stmt.setInt(7, appReleaseId);
stmt.setInt(8, appReleaseId);
stmt.executeUpdate(); stmt.executeUpdate();
}
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
log.error("Error occurred while obtaining the DB connection.", e); String msg = "Error occurred while obtaining the DB connection to add lifecycle state for application "
throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection.", e); + "release which has ID " + appReleaseId + ". Lifecycle state " + state.getCurrentState();
log.error(msg);
throw new LifeCycleManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
log.error("Error occurred while adding lifecycle: " + state.getCurrentState(), e); String msg = "Error occurred while executing the query to add lifecycle state for application release which"
throw new LifeCycleManagementDAOException("Error occurred while adding lifecycle: " + state.getCurrentState(), e); + " has ID " + appReleaseId + ". Lifecycle state " + state.getCurrentState() + ". Executed query: "
} finally { + sql;
DAOUtil.cleanupResources(stmt, null); log.error(msg);
throw new LifeCycleManagementDAOException(msg, e);
} }
} }
@Override @Override
public void deleteLifecycleStateByReleaseId(int releaseId) throws LifeCycleManagementDAOException { public void deleteLifecycleStateByReleaseId(int releaseId) throws LifeCycleManagementDAOException {
Connection conn; String sql = "DELETE "
try { + "FROM AP_APP_LIFECYCLE_STATE " +
conn = this.getDBConnection();
String sql = "DELETE FROM " +
"AP_APP_LIFECYCLE_STATE " +
"WHERE AP_APP_RELEASE_ID = ?"; "WHERE AP_APP_RELEASE_ID = ?";
try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, releaseId); stmt.setInt(1, releaseId);
stmt.executeUpdate(); stmt.executeUpdate();
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection.", e); String msg = "Error occurred while obtaining the DB connection to delete lifecycle states for application "
+ "release ID: " + releaseId;
log.error(msg);
throw new LifeCycleManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new LifeCycleManagementDAOException("Error occurred while deleting lifecycle for application " String msg = "Error occurred while executing the query to delete lifecycle states for application release"
+ "release ID: " + releaseId, e); + " ID: " + releaseId + ". Executed query " + sql;
log.error(msg);
throw new LifeCycleManagementDAOException(msg, e);
} }
} }
@Override @Override
public void deleteLifecycleStates(List<Integer> appReleaseIds) throws LifeCycleManagementDAOException{ public void deleteLifecycleStates(List<Integer> appReleaseIds) throws LifeCycleManagementDAOException{
Connection conn; String sql = "DELETE "
try { + "FROM AP_APP_LIFECYCLE_STATE " +
conn = this.getDBConnection();
String sql = "DELETE FROM " +
"AP_APP_LIFECYCLE_STATE " +
"WHERE AP_APP_RELEASE_ID = ?"; "WHERE AP_APP_RELEASE_ID = ?";
try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (Integer releaseId : appReleaseIds) { for (Integer releaseId : appReleaseIds) {
stmt.setInt(1, releaseId); stmt.setInt(1, releaseId);
@ -239,29 +227,32 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
stmt.executeBatch(); stmt.executeBatch();
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection for deleting " String msg = "Error occurred while obtaining the DB connection for deleting application life-cycle states "
+ "application life-cycle states for given application Ids.", e); + "for given application Ids.";
log.error(msg);
throw new LifeCycleManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new LifeCycleManagementDAOException("Error occurred while deleting life-cycle states for given " String msg = "Error occurred while executing query to delete application life-cycle states for given "
+ "application releases.", e); + "application Ids.";
log.error(msg);
throw new LifeCycleManagementDAOException(msg, e);
} }
} }
private LifecycleState constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException { private LifecycleState constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException {
LifecycleState lifecycleState = null;
try { try {
if (rs !=null && rs.next()) { if (rs !=null && rs.next()) {
lifecycleState = new LifecycleState(); LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(rs.getString("CURRENT_STATE")); lifecycleState.setCurrentState(rs.getString("CURRENT_STATE"));
lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE")); lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE"));
lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT")); lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT"));
lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY")); lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY"));
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new LifeCycleManagementDAOException( String msg = "Error occurred while construct lifecycle state by data which is retrieved from SQL query";
"Error occurred while construct lifecycle state by retrieving data from SQL query", e); log.error(msg);
throw new LifeCycleManagementDAOException(msg, e);
} }
return lifecycleState; return null;
} }
} }

@ -47,7 +47,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
public List<Integer> addDeviceSubscription(String subscribedBy, List<Integer> deviceIds, public List<Integer> addDeviceSubscription(String subscribedBy, List<Integer> deviceIds,
String subscribedFrom, String installStatus, int releaseId, int tenantId) String subscribedFrom, String installStatus, int releaseId, int tenantId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
Connection conn;
String sql = "INSERT INTO " String sql = "INSERT INTO "
+ "AP_DEVICE_SUBSCRIPTION(" + "AP_DEVICE_SUBSCRIPTION("
+ "SUBSCRIBED_BY, " + "SUBSCRIBED_BY, "
@ -59,7 +58,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
+ "TENANT_ID) " + "TENANT_ID) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?)"; + "VALUES (?, ?, ?, ?, ?, ?, ?)";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
@ -103,10 +102,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
public List<Integer> updateDeviceSubscription(String updateBy, List<Integer> deviceIds, public List<Integer> updateDeviceSubscription(String updateBy, List<Integer> deviceIds,
boolean isUnsubscribed, String actionTriggeredFrom, String installStatus, int releaseId, int tenantId) boolean isUnsubscribed, String actionTriggeredFrom, String installStatus, int releaseId, int tenantId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
Connection conn;
try { try {
conn = this.getDBConnection();
String sql = "UPDATE AP_DEVICE_SUBSCRIPTION SET "; String sql = "UPDATE AP_DEVICE_SUBSCRIPTION SET ";
if (isUnsubscribed) { if (isUnsubscribed) {
@ -121,6 +117,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
"AP_APP_RELEASE_ID = ? AND " + "AP_APP_RELEASE_ID = ? AND " +
"TENANT_ID = ?"; "TENANT_ID = ?";
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
@ -144,27 +141,31 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
} }
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection to update the device subscriptions of application."; String msg = "Error occurred while obtaining the DB connection to update device subscriptions of "
+ "application. Updated by: " + updateBy + " and updating action triggered from "
+ actionTriggeredFrom;
log.error(msg); log.error(msg);
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred when obtaining database connection for updating the device subscriptions of application."; String msg = "Error occurred when obtaining database connection for updating the device subscriptions of "
+ "application. Updated by: " + updateBy + " and updating action triggered from "
+ actionTriggeredFrom;
log.error(msg); log.error(msg);
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override public void addOperationMapping(int operationId, List<Integer> deviceSubscriptionIds, int tenantId) @Override
public void addOperationMapping(int operationId, List<Integer> deviceSubscriptionIds, int tenantId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
Connection conn;
try {
String sql = "INSERT INTO " String sql = "INSERT INTO "
+ "AP_APP_SUB_OP_MAPPING(" + "AP_APP_SUB_OP_MAPPING("
+ "OPERATION_ID, " + "OPERATION_ID, "
+ "AP_DEVICE_SUBSCRIPTION_ID, " + "AP_DEVICE_SUBSCRIPTION_ID, "
+ "TENANT_ID) " + "TENANT_ID) "
+ "VALUES (?, ?, ?)"; + "VALUES (?, ?, ?)";
conn = this.getDBConnection(); try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (Integer subId : deviceSubscriptionIds) { for (Integer subId : deviceSubscriptionIds) {
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
@ -177,18 +178,21 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
} }
stmt.executeBatch(); stmt.executeBatch();
} }
} catch (SQLException | DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while adding operation subscription mapping to DB", String msg = "Error occurred while getting database connection to add operation subscription mapping to DB";
e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while executing query to add operation subscription mapping to DB. Executed "
+ "query: " + sql;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public void addUserSubscriptions(int tenantId, String subscribedBy, List<String> users, int releaseId) public void addUserSubscriptions(int tenantId, String subscribedBy, List<String> users, int releaseId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
Connection conn;
try {
String sql = "INSERT INTO " String sql = "INSERT INTO "
+ "AP_USER_SUBSCRIPTION(" + "AP_USER_SUBSCRIPTION("
+ "TENANT_ID, " + "TENANT_ID, "
@ -197,11 +201,12 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
+ "USER_NAME, " + "USER_NAME, "
+ "AP_APP_RELEASE_ID) " + "AP_APP_RELEASE_ID) "
+ "VALUES (?, ?, ?, ?, ?)"; + "VALUES (?, ?, ?, ?, ?)";
conn = this.getDBConnection(); try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (String user : users) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
for (String user : users) {
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setString(2, subscribedBy); stmt.setString(2, subscribedBy);
stmt.setTimestamp(3, timestamp); stmt.setTimestamp(3, timestamp);
@ -209,22 +214,28 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
stmt.setInt(5, releaseId); stmt.setInt(5, releaseId);
stmt.addBatch(); stmt.addBatch();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Adding a mapping to user[" + user + "] to the application release[" + releaseId + "]"); log.debug("Adding an user subscription for user " + user + " and application release which "
+ "has Id " + releaseId + " to the database.");
} }
} }
stmt.executeBatch(); stmt.executeBatch();
} }
} catch (SQLException | DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while adding device application mapping to DB", String msg = "Error occurred while getting database connection to add user subscription. Subscribing user "
e); + "is " + subscribedBy;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while executing query to add user subscription. Subscribing user is "
+ subscribedBy + " and executed query: " + sql;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public void addRoleSubscriptions(int tenantId, String subscribedBy, List<String> roles, int releaseId) public void addRoleSubscriptions(int tenantId, String subscribedBy, List<String> roles, int releaseId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
Connection conn;
try {
String sql = "INSERT INTO " String sql = "INSERT INTO "
+ "AP_ROLE_SUBSCRIPTION(" + "AP_ROLE_SUBSCRIPTION("
+ "TENANT_ID, " + "TENANT_ID, "
@ -233,11 +244,12 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
+ "ROLE_NAME, " + "ROLE_NAME, "
+ "AP_APP_RELEASE_ID) " + "AP_APP_RELEASE_ID) "
+ "VALUES (?, ?, ?, ?, ?)"; + "VALUES (?, ?, ?, ?, ?)";
conn = this.getDBConnection(); try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (String role : roles) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
for (String role : roles) {
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setString(2, subscribedBy); stmt.setString(2, subscribedBy);
stmt.setTimestamp(3, timestamp); stmt.setTimestamp(3, timestamp);
@ -245,22 +257,28 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
stmt.setInt(5, releaseId); stmt.setInt(5, releaseId);
stmt.addBatch(); stmt.addBatch();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Adding a mapping to role[" + role + "] to the application release[" + releaseId + "]"); log.debug("Adding a role subscription for role " + role + " and application release which "
+ "has Id " + releaseId + " to the database.");
} }
} }
stmt.executeBatch(); stmt.executeBatch();
} }
} catch (SQLException | DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while adding role subscription to APPM DB", String msg = "Error occurred while getting database connection to add role subscription. Subscribing role "
e); + "is " + subscribedBy;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while executing query to add role subscription. Subscribing role is "
+ subscribedBy + " and executed query: " + sql;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@Override @Override
public void addGroupSubscriptions(int tenantId, String subscribedBy, List<String> groups, int releaseId) public void addGroupSubscriptions(int tenantId, String subscribedBy, List<String> groups, int releaseId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
Connection conn;
try {
String sql = "INSERT INTO " String sql = "INSERT INTO "
+ "AP_GROUP_SUBSCRIPTION(" + "AP_GROUP_SUBSCRIPTION("
+ "TENANT_ID, " + "TENANT_ID, "
@ -269,11 +287,12 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
+ "GROUP_NAME, " + "GROUP_NAME, "
+ "AP_APP_RELEASE_ID) " + "AP_APP_RELEASE_ID) "
+ "VALUES (?, ?, ?, ?, ?)"; + "VALUES (?, ?, ?, ?, ?)";
conn = this.getDBConnection(); try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (String group : groups) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
for (String group : groups) {
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setString(2, subscribedBy); stmt.setString(2, subscribedBy);
stmt.setTimestamp(3, timestamp); stmt.setTimestamp(3, timestamp);
@ -281,14 +300,22 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
stmt.setInt(5, releaseId); stmt.setInt(5, releaseId);
stmt.addBatch(); stmt.addBatch();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Adding a mapping to group[" + group + "] to the application release[" + releaseId + "]"); log.debug("Adding a group subscription for role " + group + " and application release which "
+ "has Id " + releaseId + " to the database.");
} }
} }
stmt.executeBatch(); stmt.executeBatch();
} }
} catch (SQLException | DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while adding group subscription to APPM DB", String msg = "Error occurred while getting database connection to add group subscription. Subscribing "
e); + "group is " + subscribedBy;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while executing query to add group subscription. Subscribing group is "
+ subscribedBy + " and executed query: " + sql;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@ -299,9 +326,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
log.debug("Getting device subscriptions for the application release id " + appReleaseId log.debug("Getting device subscriptions for the application release id " + appReleaseId
+ " from the database"); + " from the database");
} }
Connection conn;
try {
conn = this.getDBConnection();
String sql = "SELECT " String sql = "SELECT "
+ "DS.ID AS ID, " + "DS.ID AS ID, "
+ "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, "
@ -313,6 +337,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
+ "DS.DM_DEVICE_ID AS DEVICE_ID " + "DS.DM_DEVICE_ID AS DEVICE_ID "
+ "FROM AP_DEVICE_SUBSCRIPTION DS " + "FROM AP_DEVICE_SUBSCRIPTION DS "
+ "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.TENANT_ID=?"; + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.TENANT_ID=?";
try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, appReleaseId); stmt.setInt(1, appReleaseId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -324,15 +350,13 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
return DAOUtil.loadDeviceSubscriptions(rs); return DAOUtil.loadDeviceSubscriptions(rs);
} }
} }
} catch (SQLException e) { } catch (DBConnectionException e) {
String msg = String msg = "Error occurred while obtaining the DB connection for getting device subscription for "
"Error occurred while getting device subscription data for application ID: " + appReleaseId + "."; + "application Id: " + appReleaseId + ".";
log.error(msg); log.error(msg);
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} catch (DBConnectionException e) { } catch (SQLException e) {
String msg = String msg = "Error occurred while getting device subscription data for application ID: " + appReleaseId;
"Error occurred while obtaining the DB connection for getting device subscription for applicationID: "
+ appReleaseId + ".";
log.error(msg); log.error(msg);
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} }
@ -377,12 +401,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
} }
return deviceSubscriptionDTOHashMap; return deviceSubscriptionDTOHashMap;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection to get device subscriptions for given device"
"Error occurred while obtaining the DB connection when getting device subscriptions for given " + " Ids.";
+ "device Ids.", e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("SWL Error occurred while getting device subscriptions for given" String msg = "SQL Error occurred while getting device subscriptions for given device Ids.";
+ " device Ids.", e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@ -415,12 +441,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
} }
return subscribedUsers; return subscribedUsers;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection to get already subscribed users for given "
"Error occurred while obtaining the DB connection when getting subscribed users for given " + "user names.";
+ "user names.", e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("SWL Error occurred while getting suscribed users for given" String msg = "SQL Error occurred while getting subscribed users for given user names.";
+ " user names.", e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@ -428,8 +456,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
public List<String> getSubscribedRoleNames(List<String> roles, int tenantId) public List<String> getSubscribedRoleNames(List<String> roles, int tenantId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug( log.debug("Request received in DAO Layer to get already subscribed role names for given list of roles.");
"Request received in DAO Layer to get already subscribed role names for given list of role names.");
} }
try { try {
Connection conn = this.getDBConnection(); Connection conn = this.getDBConnection();
@ -454,12 +481,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
} }
return subscribedUsers; return subscribedUsers;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection to getg subscribed roles for given role "
"Error occurred while obtaining the DB connection when getting subscribed roles for given " + "names.";
+ "role names.", e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("SWL Error occurred while getting subscribes roles for given" String msg = "SWL Error occurred while getting subscribes roles for given role names.";
+ " role names.", e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@ -467,7 +496,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
public List<String> getSubscribedGroupNames(List<String> groups, int tenantId) public List<String> getSubscribedGroupNames(List<String> groups, int tenantId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get already subscribed groups for given list of group names."); log.debug("Request received in DAO Layer to get already subscribed groups for given list of groups.");
} }
try { try {
Connection conn = this.getDBConnection(); Connection conn = this.getDBConnection();
@ -492,12 +521,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
} }
return subscribedUsers; return subscribedUsers;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection to get already subscribed groups for given "
"Error occurred while obtaining the DB connection when getting subscribed groups for given " + "group names.";
+ "group names.", e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("SWL Error occurred while getting subscribed groups for given" String msg = "SQL Error occurred while getting already subscribed groups for given group names.";
+ " group names.", e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} }
} }
@ -530,20 +561,22 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
} }
return subscribedDevices; return subscribedDevices;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( String msg = "Error occurred while obtaining the DB connection to get subscribed device Ids for given "
"Error occurred while obtaining the DB connection when getting subscribed device Ids for given " + "device Id list.";
+ "device Id list.", e); log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("SWL Error occurred while getting subscribed device ids for " String msg = "SQL Error occurred while getting already subscribed device ids for given device Id list.";
+ "given devie Id list.", e); log.error(msg);
} } throw new ApplicationManagementDAOException(msg, e);
}
}
@Override @Override
public void updateSubscriptions(int tenantId, String updateBy, List<String> paramList, int releaseId, public void updateSubscriptions(int tenantId, String updateBy, List<String> paramList, int releaseId,
String subType, String action) throws ApplicationManagementDAOException { String subType, String action) throws ApplicationManagementDAOException {
Connection conn;
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
String sql = "UPDATE "; String sql = "UPDATE ";
if (SubsciptionType.USER.toString().equalsIgnoreCase(subType)) { if (SubsciptionType.USER.toString().equalsIgnoreCase(subType)) {
sql += "AP_USER_SUBSCRIPTION SET "; sql += "AP_USER_SUBSCRIPTION SET ";
@ -583,11 +616,13 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
stmt.executeBatch(); stmt.executeBatch();
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection to update the user subscriptions of application."; String msg = "Error occurred while obtaining the DB connection to update the user subscriptions of "
+ "application.";
log.error(msg); log.error(msg);
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred when obtaining database connection for updating the user subscriptions of application."; String msg = "Error occurred when obtaining database connection for updating the user subscriptions of "
+ "application.";
log.error(msg); log.error(msg);
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} }
@ -596,9 +631,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
@Override @Override
public List<Integer> getDeviceSubIdsForOperation(int operationId, int tenantId) public List<Integer> getDeviceSubIdsForOperation(int operationId, int tenantId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
Connection conn;
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
List<Integer> deviceSubIds = new ArrayList<>(); List<Integer> deviceSubIds = new ArrayList<>();
String sql = "SELECT " String sql = "SELECT "
+ "ID " + "ID "
@ -631,9 +665,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
@Override @Override
public boolean updateDeviceSubStatus(int deviceId, List<Integer> deviceSubIds, String status, int tenantId) public boolean updateDeviceSubStatus(int deviceId, List<Integer> deviceSubIds, String status, int tenantId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
Connection conn;
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
int index = 1; int index = 1;
StringJoiner joiner = new StringJoiner(",", StringJoiner joiner = new StringJoiner(",",
"UPDATE AP_DEVICE_SUBSCRIPTION SET STATUS = ? " "UPDATE AP_DEVICE_SUBSCRIPTION SET STATUS = ? "

@ -21,7 +21,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO; import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO;
import org.wso2.carbon.device.application.mgt.core.util.DAOUtil;
import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl;
import org.wso2.carbon.device.application.mgt.core.exception.VisibilityManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.VisibilityManagementDAOException;
@ -43,16 +42,18 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
public void addUnrestrictedRoles(List<String> unrestrictedRoles, int applicationId, int tenantId) throws public void addUnrestrictedRoles(List<String> unrestrictedRoles, int applicationId, int tenantId) throws
VisibilityManagementDAOException { VisibilityManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to add unrestricted roles"); log.debug("Request received in DAO Layer to add unrestricted roles for application which has application"
} + " ID " + applicationId);
Connection conn; }
PreparedStatement stmt = null; String sql = "INSERT INTO "
ResultSet rs = null; + "AP_UNRESTRICTED_ROLE "
String sql = "INSERT INTO AP_UNRESTRICTED_ROLE (ROLE, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)"; + "(ROLE, "
try{ + "TENANT_ID, "
conn = this.getDBConnection(); + "AP_APP_ID) "
conn.setAutoCommit(false); + "VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql); try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (String role : unrestrictedRoles) { for (String role : unrestrictedRoles) {
stmt.setString(1, role); stmt.setString(1, role);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -60,70 +61,37 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
}
}catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new VisibilityManagementDAOException("Error occurred while obtaining the DB connection when adding roles", e); String msg = "Error occurred while obtaining the DB connection when adding unrestricted roles for "
}catch (SQLException e) { + "application which has Id " + applicationId;
throw new VisibilityManagementDAOException("Error occurred while adding unrestricted roles", e); log.error(msg);
} finally { throw new VisibilityManagementDAOException(msg, e);
DAOUtil.cleanupResources(stmt, rs); } catch (SQLException e) {
String msg = "Error occurred while executing query to add unrestricted roles for application which has Id "
+ applicationId + ". Executed query " + sql;
log.error(msg);
throw new VisibilityManagementDAOException(msg, e);
} }
} }
@Override @Override
public List<String> getUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException { public List<String> getUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get unrestricted roles"); log.debug("Request received in DAO Layer to get unrestricted roles for application which has application "
+ "ID " + applicationId);
} }
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<String> unrestrictedRoles = new ArrayList<>(); List<String> unrestrictedRoles = new ArrayList<>();
String sql = "SELECT ROLE " String sql = "SELECT ROLE "
+ "FROM AP_UNRESTRICTED_ROLE " + "FROM AP_UNRESTRICTED_ROLE "
+ "WHERE AP_APP_ID = ? AND TENANT_ID = ?"; + "WHERE AP_APP_ID = ? AND "
try{ + "TENANT_ID = ?";
conn = this.getDBConnection();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql);
stmt.setInt(1, applicationId);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery();
while (rs.next()){
unrestrictedRoles.add(rs.getString("ROLE"));
}
return unrestrictedRoles;
}catch (DBConnectionException e) {
throw new VisibilityManagementDAOException("Error occurred while obtaining the DB connection when adding roles", e);
}catch (SQLException e) {
throw new VisibilityManagementDAOException("Error occurred while adding unrestricted roles", e);
} finally {
DAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public List<String> getUnrestrictedRolesByUUID(String uuid, int tenantId) throws VisibilityManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get unrestricted roles for UUID: " + uuid);
}
Connection conn;
List<String> unrestrictedRoles = new ArrayList<>();
String sql = "SELECT ROLE FROM AP_UNRESTRICTED_ROLE "
+ "WHERE "
+ "AP_APP_ID = (SELECT AR.AP_APP_ID FROM AP_APP_RELEASE AR WHERE AR.UUID = ? AND AR.TENANT_ID = ? ) "
+ "AND TENANT_ID = ?";
try { try {
conn = this.getDBConnection(); Connection conn = this.getDBConnection();
conn.setAutoCommit(false);
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, uuid); stmt.setInt(1, applicationId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId); try (ResultSet rs = stmt.executeQuery()) {
try (ResultSet rs = stmt.executeQuery();) {
while (rs.next()) { while (rs.next()) {
unrestrictedRoles.add(rs.getString("ROLE")); unrestrictedRoles.add(rs.getString("ROLE"));
} }
@ -131,29 +99,33 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
} }
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new VisibilityManagementDAOException( String msg = "Error occurred while obtaining the DB connection to get unrestricted roles for application "
"Error occurred while obtaining the DB connection when getting unrestricted roles for UUID: " + "which has application Id " + applicationId;
+ uuid, e); log.error(msg);
throw new VisibilityManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new VisibilityManagementDAOException( String msg = "Error occurred while executing query to get unrestricted roles for application which has "
"Error occurred while getting unrestricted roles for UUID: " + uuid, e); + "application Id " + applicationId + ". Executed query: " + sql;
log.error(msg);
throw new VisibilityManagementDAOException(msg, e);
} }
} }
@Override @Override
public void deleteUnrestrictedRoles(List<String> unrestrictedRoles, int applicationId, int tenantId) throws VisibilityManagementDAOException { public void deleteUnrestrictedRoles(List<String> unrestrictedRoles, int applicationId, int tenantId)
throws VisibilityManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to delete unrestricted roles"); log.debug("Request received in DAO Layer to delete unrestricted roles of application which has "
} + "application Id " + applicationId);
Connection conn; }
PreparedStatement stmt = null; String sql = "DELETE "
ResultSet rs = null; + "FROM AP_UNRESTRICTED_ROLE "
String sql = "DELETE FROM AP_UNRESTRICTED_ROLE WHERE AP_APP_ID = ? AND ROLE = ? AND TENANT_ID = ?;"; + "WHERE AP_APP_ID = ? AND "
try{ + "ROLE = ? AND "
conn = this.getDBConnection(); + "TENANT_ID = ?";
conn.setAutoCommit(false); try {
stmt = conn.prepareStatement(sql); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (String role : unrestrictedRoles) { for (String role : unrestrictedRoles) {
stmt.setInt(1, applicationId); stmt.setInt(1, applicationId);
stmt.setString(2, role); stmt.setString(2, role);
@ -161,13 +133,17 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
}
}catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new VisibilityManagementDAOException("Error occurred while obtaining the DB connection when adding roles", e); String msg = "Error occurred while obtaining the DB connection to delete unrestricted roles of an "
}catch (SQLException e) { + "application which has application Id " + applicationId;
throw new VisibilityManagementDAOException("Error occurred while adding unrestricted roles", e); log.error(msg);
} finally { throw new VisibilityManagementDAOException(msg, e);
DAOUtil.cleanupResources(stmt, rs); } catch (SQLException e) {
String msg = "Error occurred while executing query to delete unrestricted roles of an application which has"
+ " application Id " + applicationId + ". executed query: " + sql;
log.error(msg);
throw new VisibilityManagementDAOException(msg, e);
} }
} }
} }

@ -294,7 +294,8 @@ public class ReviewManagerImpl implements ReviewManager {
if (reviewDTO.getRootParentId() == -1 && reviewDTO.getImmediateParentId() == -1) { if (reviewDTO.getRootParentId() == -1 && reviewDTO.getImmediateParentId() == -1) {
if (!reviewDTO.getReleaseUuid().equals(uuid)) { if (!reviewDTO.getReleaseUuid().equals(uuid)) {
isActiveReview = false; isActiveReview = false;
} else if (updatingReview.getRating() > 0 && updatingReview.getRating() != reviewDTO.getRating()) { } else {
if (updatingReview.getRating() > 0 && updatingReview.getRating() != reviewDTO.getRating()) {
Runnable task = () -> ReviewManagerImpl.this Runnable task = () -> ReviewManagerImpl.this
.calculateRating(updatingReview.getRating(), reviewDTO.getRating(), uuid, tenantId); .calculateRating(updatingReview.getRating(), reviewDTO.getRating(), uuid, tenantId);
new Thread(task).start(); new Thread(task).start();
@ -303,6 +304,7 @@ public class ReviewManagerImpl implements ReviewManager {
if (!reviewDTO.getContent().equals(updatingReview.getContent())) { if (!reviewDTO.getContent().equals(updatingReview.getContent())) {
reviewDTO.setContent(updatingReview.getContent()); reviewDTO.setContent(updatingReview.getContent());
} }
}
} else { } else {
if (!reviewDTO.getReleaseUuid().equals(uuid)) { if (!reviewDTO.getReleaseUuid().equals(uuid)) {
String msg = "You are trying to update reply comment, but associated application release UUID and " String msg = "You are trying to update reply comment, but associated application release UUID and "
@ -775,7 +777,7 @@ public class ReviewManagerImpl implements ReviewManager {
List<String> uuids = applicationDTO.getApplicationReleaseDTOs().stream().map(ApplicationReleaseDTO::getUuid) List<String> uuids = applicationDTO.getApplicationReleaseDTOs().stream().map(ApplicationReleaseDTO::getUuid)
.collect(Collectors.toList()); .collect(Collectors.toList());
List<Integer> appRatings = this.reviewDAO.getAllAppRatingValues(uuids, tenantId); List<Integer> appRatings = this.reviewDAO.getAllAppRatingValues(uuids, tenantId);
double appAverageRatingValue = appRatings.stream().mapToDouble(x -> x).average().orElse(Double.NaN); double appAverageRatingValue = appRatings.stream().mapToDouble(x -> x).average().orElse(0.0);
this.applicationDAO.updateApplicationRating(uuid, appAverageRatingValue, tenantId); this.applicationDAO.updateApplicationRating(uuid, appAverageRatingValue, tenantId);
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
String msg = "Error occurred when getting application data or updating application rating value."; String msg = "Error occurred when getting application data or updating application rating value.";

@ -29,11 +29,8 @@ import org.wso2.carbon.device.application.mgt.core.util.APIUtil;
import org.wso2.carbon.device.application.mgt.store.api.services.admin.ReviewManagementStoreAdminAPI; import org.wso2.carbon.device.application.mgt.store.api.services.admin.ReviewManagementStoreAdminAPI;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
/** /**

@ -81,7 +81,6 @@ CREATE TABLE IF NOT EXISTS AP_APP_LIFECYCLE_STATE(
UPDATED_BY VARCHAR(100) NOT NULL, UPDATED_BY VARCHAR(100) NOT NULL,
UPDATED_AT TIMESTAMP NOT NULL, UPDATED_AT TIMESTAMP NOT NULL,
AP_APP_RELEASE_ID INTEGER NOT NULL, AP_APP_RELEASE_ID INTEGER NOT NULL,
AP_APP_ID INTEGER NOT NULL,
REASON TEXT DEFAULT NULL, REASON TEXT DEFAULT NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT fk_AP_APP_LIFECYCLE_STATE_AP_APP_RELEASE1 CONSTRAINT fk_AP_APP_LIFECYCLE_STATE_AP_APP_RELEASE1

Loading…
Cancel
Save