use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method replaceResourceType.
/**
* {@inheritDoc}
*/
@Override
public void replaceResourceType(ResourceType resourceType) throws ConfigurationManagementException {
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
try {
String query = SQLConstants.INSERT_OR_UPDATE_RESOURCE_TYPE_MYSQL;
if (isH2DB()) {
query = INSERT_OR_UPDATE_RESOURCE_TYPE_H2;
} else if (isPostgreSQLDB()) {
query = INSERT_OR_UPDATE_RESOURCE_TYPE_POSTGRESQL;
} else if (isMSSqlDB() || isDB2DB()) {
query = INSERT_OR_UPDATE_RESOURCE_TYPE_MSSQL_OR_DB2;
} else if (isOracleDB()) {
query = INSERT_OR_UPDATE_RESOURCE_TYPE_ORACLE;
}
jdbcTemplate.executeInsert(query, preparedStatement -> {
int initialParameterIndex = 1;
preparedStatement.setString(initialParameterIndex, resourceType.getId());
preparedStatement.setString(++initialParameterIndex, resourceType.getName());
preparedStatement.setString(++initialParameterIndex, resourceType.getDescription());
}, resourceType, false);
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_UPDATE_RESOURCE_TYPE, resourceType.getName(), e);
}
}
use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method deleteFileById.
@Override
public void deleteFileById(String resourceType, String resourceName, String fileId) throws ConfigurationManagementException {
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
try {
boolean isOracleOrMssql = isOracleDB() || isMSSqlDB();
jdbcTemplate.withTransaction(template -> {
// Get resource id for the deleting file.
String sqlStmt = isH2DB() ? SQLConstants.GET_FILE_BY_ID_SQL_H2 : SQLConstants.GET_FILE_BY_ID_SQL;
String resourceId = template.fetchSingleRecord(sqlStmt, (resultSet, rowNumber) -> resultSet.getString(DB_SCHEMA_COLUMN_NAME_RESOURCE_ID), preparedStatement -> {
preparedStatement.setString(1, fileId);
preparedStatement.setString(2, resourceName);
preparedStatement.setString(3, resourceType);
});
template.executeUpdate(DELETE_FILE_SQL, (preparedStatement -> preparedStatement.setString(1, fileId)));
List<String> availableFilesForTheResource = template.executeQuery(GET_FILES_BY_RESOURCE_ID_SQL, ((resultSet, rowNumber) -> resultSet.getString(DB_SCHEMA_COLUMN_NAME_ID)), preparedStatement -> preparedStatement.setString(1, resourceId));
if (availableFilesForTheResource.isEmpty()) {
template.executeUpdate(UPDATE_HAS_FILE_SQL, preparedStatement -> {
if (isOracleOrMssql) {
preparedStatement.setInt(1, 0);
} else {
preparedStatement.setBoolean(1, false);
}
preparedStatement.setString(2, resourceId);
});
}
updateResourceLastModified(template, resourceId);
return null;
});
} catch (TransactionException e) {
throw handleServerException(ERROR_CODE_DELETE_FILE, fileId, e);
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_CHECK_DB_METADATA, e.getMessage(), e);
}
}
use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method addFile.
@Override
public void addFile(String fileId, String resourceId, String fileName, InputStream fileStream) throws ConfigurationManagementException {
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
try {
boolean isOracleOrMssql = isOracleDB() || isMSSqlDB();
boolean isPostgreSQL = isPostgreSQLDB();
String sqlStmt = isH2DB() ? SQLConstants.INSERT_FILE_SQL_H2 : SQLConstants.INSERT_FILE_SQL;
jdbcTemplate.withTransaction(template -> {
template.executeUpdate(sqlStmt, preparedStatement -> {
preparedStatement.setString(1, fileId);
if (isPostgreSQL) {
preparedStatement.setBinaryStream(2, fileStream);
} else {
preparedStatement.setBlob(2, fileStream);
}
preparedStatement.setString(3, resourceId);
preparedStatement.setString(4, fileName);
});
template.executeUpdate(SQLConstants.UPDATE_HAS_FILE_SQL, preparedStatement -> {
if (isOracleOrMssql) {
preparedStatement.setInt(1, 1);
} else {
preparedStatement.setBoolean(1, true);
}
preparedStatement.setString(2, resourceId);
});
updateResourceLastModified(template, resourceId);
return null;
});
} catch (TransactionException e) {
throw handleServerException(ERROR_CODE_INSERT_FILE, fileId, e);
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_CHECK_DB_METADATA, e.getMessage(), e);
}
}
use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method getResourceByName.
/**
* {@inheritDoc}
*/
@Override
public Resource getResourceByName(int tenantId, String resourceTypeId, String resourceName) throws ConfigurationManagementException {
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
List<ConfigurationRawDataCollector> configurationRawDataCollectors;
try {
String queryWithCreatedTime = GET_RESOURCE_BY_NAME_MYSQL;
String queryWithOutCreatedTime = GET_RESOURCE_BY_NAME_MYSQL_WITHOUT_CREATED_TIME;
if (isOracleDB() || isMSSqlDB()) {
queryWithCreatedTime = GET_RESOURCE_BY_NAME_MSSQL_OR_ORACLE;
}
configurationRawDataCollectors = jdbcTemplate.executeQuery(useCreatedTimeField() ? queryWithCreatedTime : queryWithOutCreatedTime, (resultSet, rowNumber) -> {
ConfigurationRawDataCollector.ConfigurationRawDataCollectorBuilder configurationRawDataCollectorBuilder = new ConfigurationRawDataCollector.ConfigurationRawDataCollectorBuilder().setResourceId(resultSet.getString(DB_SCHEMA_COLUMN_NAME_ID)).setTenantId(resultSet.getInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID)).setResourceName(resultSet.getString(DB_SCHEMA_COLUMN_NAME_NAME)).setLastModified(resultSet.getTimestamp(DB_SCHEMA_COLUMN_NAME_LAST_MODIFIED, calendar)).setResourceTypeName(resultSet.getString(DB_SCHEMA_COLUMN_NAME_RESOURCE_TYPE)).setResourceTypeDescription(resultSet.getString(DB_SCHEMA_COLUMN_NAME_DESCRIPTTION)).setAttributeKey(resultSet.getString(DB_SCHEMA_COLUMN_NAME_ATTRIBUTE_KEY)).setAttributeValue(resultSet.getString(DB_SCHEMA_COLUMN_NAME_ATTRIBUTE_VALUE)).setAttributeId(resultSet.getString(DB_SCHEMA_COLUMN_NAME_ATTRIBUTE_ID)).setFileId(resultSet.getString(DB_SCHEMA_COLUMN_NAME_FILE_ID)).setFileName(resultSet.getString(DB_SCHEMA_COLUMN_NAME_FILE_NAME)).setHasFile(resultSet.getBoolean(DB_SCHEMA_COLUMN_NAME_HAS_FILE)).setHasAttribute(resultSet.getBoolean(DB_SCHEMA_COLUMN_NAME_HAS_ATTRIBUTE));
if (useCreatedTimeField()) {
configurationRawDataCollectorBuilder.setCreatedTime(resultSet.getTimestamp(DB_SCHEMA_COLUMN_NAME_CREATED_TIME, calendar));
}
return configurationRawDataCollectorBuilder.build();
}, preparedStatement -> {
int initialParameterIndex = 1;
preparedStatement.setString(initialParameterIndex, resourceName);
preparedStatement.setInt(++initialParameterIndex, tenantId);
preparedStatement.setString(++initialParameterIndex, resourceTypeId);
});
/*
Database call can contain duplicate data for some columns. Need to filter them in order to build the
resource.
*/
return configurationRawDataCollectors == null || configurationRawDataCollectors.size() == 0 ? null : buildResourceFromRawData(configurationRawDataCollectors);
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_GET_RESOURCE, resourceName, e);
}
}
use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method deleteResourceById.
/**
* {@inheritDoc}
*/
@Override
public void deleteResourceById(int tenantId, String resourceId) throws ConfigurationManagementException {
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
try {
if (isMySQLDB()) {
deleteFiles(resourceId);
}
jdbcTemplate.executeUpdate(SQLConstants.DELETE_RESOURCE_BY_ID_SQL, preparedStatement -> {
int initialParameterIndex = 1;
preparedStatement.setString(initialParameterIndex, resourceId);
preparedStatement.setInt(++initialParameterIndex, tenantId);
});
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_DELETE_RESOURCE, resourceId, e);
}
}
Aggregations