diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/UnrestrictedRole.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/UnrestrictedRole.java index d8ef0e3e24a..5171648eceb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/UnrestrictedRole.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/UnrestrictedRole.java @@ -30,7 +30,7 @@ public class UnrestrictedRole { @Exclude private int id; - private String tenantId; + private int tenantId; private String role; @@ -42,11 +42,11 @@ public class UnrestrictedRole { this.id = id; } - public String getTenantId() { + public int getTenantId() { return tenantId; } - public void setTenantId(String tenantId) { + public void setTenantId(int tenantId) { this.tenantId = tenantId; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index 48834e0d03c..207f92199f1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -45,14 +45,6 @@ public interface ApplicationDAO { */ void addTags(List tags, int applicationId, int tenantId) throws ApplicationManagementDAOException; - /** - * To add unrestricted roles for a particular application. - * - * @param unrestrictedRoles unrestrictedRoles that could available the application. - * @throws ApplicationManagementDAOException Application Management DAO Exception. - */ - void addUnrestrictedRoles(List unrestrictedRoles, int applicationId, int tenantId) throws ApplicationManagementDAOException; - /** * To check application existence. * diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java index c1c6a1fd307..5ad5ceecec5 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/VisibilityDAO.java @@ -18,7 +18,7 @@ */ package org.wso2.carbon.device.application.mgt.core.dao; -import org.wso2.carbon.device.application.mgt.common.Visibility; +import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole; import org.wso2.carbon.device.application.mgt.core.exception.VisibilityManagementDAOException; import java.util.List; @@ -30,13 +30,18 @@ import java.util.List; */ public interface VisibilityDAO { - int getVisibilityID(Visibility.Type visibilityType) throws VisibilityManagementDAOException; + /** + * To add unrestricted roles for a particular application. + * + * @param unrestrictedRoles unrestrictedRoles that could available the application. + * @throws VisibilityManagementDAOException Visiblity Management DAO Exception. + */ + void addUnrestrictedRoles(List unrestrictedRoles, int applicationId, int tenantId) throws + VisibilityManagementDAOException; - void add(int applicationID, int visibilityTypeID, List allowedList) - throws VisibilityManagementDAOException; + List getUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException; - void delete(int applicationId) throws VisibilityManagementDAOException; - - Visibility get(int applicationID) throws VisibilityManagementDAOException; + void deleteUnrestrictedRoles(List unrestrictedRoles, int applicationId, int tenantId) throws + VisibilityManagementDAOException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index e0eb692c2e7..38b74cc41a5 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -112,37 +112,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } - @Override - public void addUnrestrictedRoles(List unrestrictedRoles, int applicationId, int tenantId) throws ApplicationManagementDAOException { - if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to add unrestricted roles"); - } - 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.addBatch(); - } - stmt.executeBatch(); - - }catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection when adding roles", e); - }catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while adding unrestricted roles", e); - } finally { - Util.cleanupResources(stmt, rs); - } - } - @Override public int isExistApplication(String appName, String type, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java index 06e9902d8bc..d5f0dadf727 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java @@ -17,7 +17,9 @@ */ package org.wso2.carbon.device.application.mgt.core.dao.impl.visibility; -import org.wso2.carbon.device.application.mgt.common.Visibility; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole; 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.common.Util; @@ -36,121 +38,106 @@ import java.util.List; */ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements VisibilityDAO { + private static final Log log = LogFactory.getLog(GenericVisibilityDAOImpl.class); + @Override - public int getVisibilityID(Visibility.Type visibilityType) throws VisibilityManagementDAOException { + public void addUnrestrictedRoles(List unrestrictedRoles, int applicationId, int tenantId) throws + VisibilityManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to add unrestricted roles"); + } + Connection conn; PreparedStatement stmt = null; - ResultSet resultSet = null; - try { - Connection connection = getDBConnection(); - String sql = "SELECT ID FROM APPM_RESOURCE_TYPE WHERE NAME = ?"; - stmt = connection.prepareStatement(sql); - stmt.setString(1, visibilityType.toString().toUpperCase()); - resultSet = stmt.executeQuery(); - if (resultSet.next()) { - return resultSet.getInt("ID"); + 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.addBatch(); } - return -1; - } catch (DBConnectionException e) { - throw new VisibilityManagementDAOException("Error occurred while obtaining the connection " + - "for the visibility management of applications", e); - } catch (SQLException e) { - throw new VisibilityManagementDAOException("Error occurred when trying to get the ID of the" + - " visibility type - " + visibilityType.toString(), e); + stmt.executeBatch(); + + }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 { - Util.cleanupResources(stmt, resultSet); + Util.cleanupResources(stmt, rs); } } @Override - public void add(int applicationID, int visibilityTypeID, List allowedList) - throws VisibilityManagementDAOException { + public List getUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get unrestricted roles"); + } + Connection conn; PreparedStatement stmt = null; - try { - Connection connection = getDBConnection(); - String sql = "INSERT INTO APPM_VISIBILITY (VALUE, RESOURCE_TYPE_ID, APPLICATION_ID) VALUES (?, ?, ?)"; - stmt = connection.prepareStatement(sql); - if (allowedList == null) { - stmt.setString(1, null); - stmt.setInt(2, visibilityTypeID); - stmt.setInt(3, applicationID); - stmt.execute(); - } else { - for (String allowed : allowedList) { - stmt.setString(1, allowed); - stmt.setInt(2, visibilityTypeID); - stmt.setInt(3, applicationID); - stmt.addBatch(); - } - stmt.executeBatch(); + ResultSet rs = null; + List 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); + rs = stmt.executeQuery(); + + while (rs.next()){ + unrestrictedRole = new UnrestrictedRole(); + unrestrictedRole.setId(rs.getInt("ID")); + unrestrictedRole.setRole(rs.getString("ROLE")); + unrestrictedRoles.add(unrestrictedRole); } - } catch (DBConnectionException e) { - throw new VisibilityManagementDAOException("Error occurred while obtaining the connection " + - "for adding the visibility mapping for the application ID - " + applicationID, e); - } catch (SQLException e) { - throw new VisibilityManagementDAOException("Error occurred while adding the visibility mapping " + - "for the application ID - " + applicationID, e); + 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 { - Util.cleanupResources(stmt, null); + Util.cleanupResources(stmt, rs); } } @Override - public void delete(int applicationId) throws VisibilityManagementDAOException { - PreparedStatement stmt = null; - try { - Connection connection = getDBConnection(); - String sql = "DELETE FROM APPM_VISIBILITY WHERE APPLICATION_ID = ?"; - stmt = connection.prepareStatement(sql); - stmt.setInt(1, applicationId); - stmt.execute(); - } catch (DBConnectionException e) { - throw new VisibilityManagementDAOException("Error occurred while obtaining the connection " + - "for deleting the visibility mapping for the application ID - " + applicationId, e); - } catch (SQLException e) { - throw new VisibilityManagementDAOException("Error occurred while deleting the visibility mapping " + - "for the application ID - " + applicationId, e); - } finally { - Util.cleanupResources(stmt, null); + public void deleteUnrestrictedRoles(List unrestrictedRoles, int applicationId, int tenantId) throws VisibilityManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to delete unrestricted roles"); } - } - - public Visibility get(int applicationId) throws VisibilityManagementDAOException { + Connection conn; PreparedStatement stmt = null; - ResultSet resultSet = null; - final String visibilityTypeColumn = "VISIBILITY_TYPE"; - final String allowedValColumn = "ALLOWED_VAL"; - try { - Connection connection = getDBConnection(); - String sql = "SELECT APPM_VISIBILITY.VALUE as " + allowedValColumn + ", APPM_RESOURCE_TYPE.NAME AS " + - visibilityTypeColumn + " FROM APPM_VISIBILITY JOIN APPM_RESOURCE_TYPE " + - "ON APPM_VISIBILITY.RESOURCE_TYPE_ID = APPM_RESOURCE_TYPE.ID " + - "WHERE APPM_VISIBILITY.APPLICATION_ID = ?"; - stmt = connection.prepareStatement(sql); - stmt.setInt(1, applicationId); - resultSet = stmt.executeQuery(); - Visibility visibility = new Visibility(); - List allowedVal = new ArrayList<>(); - while (resultSet.next()) { - if (visibility.getType() == null) { - visibility.setType(Visibility.Type.valueOf(resultSet.getString(visibilityTypeColumn))); - } - String val = resultSet.getString(allowedValColumn); - if (val != null) { - allowedVal.add(val); - } - } - if (!allowedVal.isEmpty()) { - visibility.setAllowedList(allowedVal); + 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;"; + 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.addBatch(); } - return visibility; - } catch (DBConnectionException e) { - throw new VisibilityManagementDAOException("Error occurred while obtaining the connection " + - "for getting the visibility mapping for the application ID - " + applicationId, e); - } catch (SQLException e) { - throw new VisibilityManagementDAOException("Error occurred while getting the visibility mapping " + - "for the application ID - " + applicationId, e); + stmt.executeBatch(); + + }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 { - Util.cleanupResources(stmt, resultSet); + Util.cleanupResources(stmt, rs); } } }