|
|
@ -17,7 +17,9 @@
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
package org.wso2.carbon.device.application.mgt.core.dao.impl.visibility;
|
|
|
|
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.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.dao.common.Util;
|
|
|
|
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 {
|
|
|
|
public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements VisibilityDAO {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(GenericVisibilityDAOImpl.class);
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int getVisibilityID(Visibility.Type visibilityType) throws VisibilityManagementDAOException {
|
|
|
|
public void addUnrestrictedRoles(List<UnrestrictedRole> 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;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
ResultSet resultSet = null;
|
|
|
|
ResultSet rs = null;
|
|
|
|
try {
|
|
|
|
int index = 0;
|
|
|
|
Connection connection = getDBConnection();
|
|
|
|
String sql = "INSERT INTO AP_UNRESTRICTED_ROLES (ROLE, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)";
|
|
|
|
String sql = "SELECT ID FROM APPM_RESOURCE_TYPE WHERE NAME = ?";
|
|
|
|
try{
|
|
|
|
stmt = connection.prepareStatement(sql);
|
|
|
|
conn = this.getDBConnection();
|
|
|
|
stmt.setString(1, visibilityType.toString().toUpperCase());
|
|
|
|
conn.setAutoCommit(false);
|
|
|
|
resultSet = stmt.executeQuery();
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
if (resultSet.next()) {
|
|
|
|
for (UnrestrictedRole role : unrestrictedRoles) {
|
|
|
|
return resultSet.getInt("ID");
|
|
|
|
stmt.setString(++index, role.getRole());
|
|
|
|
|
|
|
|
stmt.setInt(++index, tenantId);
|
|
|
|
|
|
|
|
stmt.setInt(++index, applicationId);
|
|
|
|
|
|
|
|
stmt.addBatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
stmt.executeBatch();
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the connection " +
|
|
|
|
}catch (DBConnectionException e) {
|
|
|
|
"for the visibility management of applications", e);
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the DB connection when adding roles", e);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
}catch (SQLException e) {
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred when trying to get the ID of the" +
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while adding unrestricted roles", e);
|
|
|
|
" visibility type - " + visibilityType.toString(), e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
Util.cleanupResources(stmt, resultSet);
|
|
|
|
Util.cleanupResources(stmt, rs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void add(int applicationID, int visibilityTypeID, List<String> allowedList)
|
|
|
|
public List<UnrestrictedRole> getUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException {
|
|
|
|
throws VisibilityManagementDAOException {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Request received in DAO Layer to get unrestricted roles");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Connection conn;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
try {
|
|
|
|
ResultSet rs = null;
|
|
|
|
Connection connection = getDBConnection();
|
|
|
|
List<UnrestrictedRole> unrestrictedRoles = new ArrayList<>();
|
|
|
|
String sql = "INSERT INTO APPM_VISIBILITY (VALUE, RESOURCE_TYPE_ID, APPLICATION_ID) VALUES (?, ?, ?)";
|
|
|
|
UnrestrictedRole unrestrictedRole = null;
|
|
|
|
stmt = connection.prepareStatement(sql);
|
|
|
|
int index = 0;
|
|
|
|
if (allowedList == null) {
|
|
|
|
String sql = "SELECT ID, ROLE FROM AP_UNRESTRICTED_ROLES WHERE AP_APP_ID = ? AND TENANT_ID = ?;";
|
|
|
|
stmt.setString(1, null);
|
|
|
|
try{
|
|
|
|
stmt.setInt(2, visibilityTypeID);
|
|
|
|
conn = this.getDBConnection();
|
|
|
|
stmt.setInt(3, applicationID);
|
|
|
|
conn.setAutoCommit(false);
|
|
|
|
stmt.execute();
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
} else {
|
|
|
|
stmt.setInt(++index, applicationId);
|
|
|
|
for (String allowed : allowedList) {
|
|
|
|
stmt.setInt(++index, tenantId);
|
|
|
|
stmt.setString(1, allowed);
|
|
|
|
rs = stmt.executeQuery();
|
|
|
|
stmt.setInt(2, visibilityTypeID);
|
|
|
|
|
|
|
|
stmt.setInt(3, applicationID);
|
|
|
|
while (rs.next()){
|
|
|
|
stmt.addBatch();
|
|
|
|
unrestrictedRole = new UnrestrictedRole();
|
|
|
|
}
|
|
|
|
unrestrictedRole.setId(rs.getInt("ID"));
|
|
|
|
stmt.executeBatch();
|
|
|
|
unrestrictedRole.setRole(rs.getString("ROLE"));
|
|
|
|
|
|
|
|
unrestrictedRoles.add(unrestrictedRole);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
return unrestrictedRoles;
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the connection " +
|
|
|
|
|
|
|
|
"for adding the visibility mapping for the application ID - " + applicationID, e);
|
|
|
|
}catch (DBConnectionException e) {
|
|
|
|
} catch (SQLException e) {
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the DB connection when adding roles", e);
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while adding the visibility mapping " +
|
|
|
|
}catch (SQLException e) {
|
|
|
|
"for the application ID - " + applicationID, e);
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while adding unrestricted roles", e);
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
Util.cleanupResources(stmt, null);
|
|
|
|
Util.cleanupResources(stmt, rs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void delete(int applicationId) throws VisibilityManagementDAOException {
|
|
|
|
public void deleteUnrestrictedRoles(List<UnrestrictedRole> unrestrictedRoles, int applicationId, int tenantId) throws VisibilityManagementDAOException {
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
try {
|
|
|
|
log.debug("Request received in DAO Layer to delete unrestricted roles");
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Connection conn;
|
|
|
|
|
|
|
|
|
|
|
|
public Visibility get(int applicationId) throws VisibilityManagementDAOException {
|
|
|
|
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
ResultSet resultSet = null;
|
|
|
|
ResultSet rs = null;
|
|
|
|
final String visibilityTypeColumn = "VISIBILITY_TYPE";
|
|
|
|
int index = 0;
|
|
|
|
final String allowedValColumn = "ALLOWED_VAL";
|
|
|
|
String sql = "DELETE FROM AP_UNRESTRICTED_ROLES WHERE AP_APP_ID = 1 AND ROLE = 'role1' AND TENANT_ID = -1234;";
|
|
|
|
try {
|
|
|
|
try{
|
|
|
|
Connection connection = getDBConnection();
|
|
|
|
conn = this.getDBConnection();
|
|
|
|
String sql = "SELECT APPM_VISIBILITY.VALUE as " + allowedValColumn + ", APPM_RESOURCE_TYPE.NAME AS " +
|
|
|
|
conn.setAutoCommit(false);
|
|
|
|
visibilityTypeColumn + " FROM APPM_VISIBILITY JOIN APPM_RESOURCE_TYPE " +
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
"ON APPM_VISIBILITY.RESOURCE_TYPE_ID = APPM_RESOURCE_TYPE.ID " +
|
|
|
|
|
|
|
|
"WHERE APPM_VISIBILITY.APPLICATION_ID = ?";
|
|
|
|
for (UnrestrictedRole role : unrestrictedRoles) {
|
|
|
|
stmt = connection.prepareStatement(sql);
|
|
|
|
stmt.setInt(++index, applicationId);
|
|
|
|
stmt.setInt(1, applicationId);
|
|
|
|
stmt.setString(++index, role.getRole());
|
|
|
|
resultSet = stmt.executeQuery();
|
|
|
|
stmt.setInt(++index, role.getTenantId());
|
|
|
|
Visibility visibility = new Visibility();
|
|
|
|
stmt.addBatch();
|
|
|
|
List<String> 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);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return visibility;
|
|
|
|
stmt.executeBatch();
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the connection " +
|
|
|
|
}catch (DBConnectionException e) {
|
|
|
|
"for getting the visibility mapping for the application ID - " + applicationId, e);
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the DB connection when adding roles", e);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
}catch (SQLException e) {
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while getting the visibility mapping " +
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while adding unrestricted roles", e);
|
|
|
|
"for the application ID - " + applicationId, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
Util.cleanupResources(stmt, resultSet);
|
|
|
|
Util.cleanupResources(stmt, rs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|