use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method getTenantResources.
/**
* {@inheritDoc}
*/
@Override
public Resources getTenantResources(Condition condition) throws ConfigurationManagementException {
PlaceholderSQL placeholderSQL = buildPlaceholderSQL(condition, useCreatedTimeField());
if (placeholderSQL.getQuery().getBytes().length > getMaximumQueryLengthInBytes()) {
if (log.isDebugEnabled()) {
log.debug("Error building SQL query for the search. Search expression " + "query length: " + placeholderSQL.getQuery().length() + " exceeds the maximum limit: " + MAX_QUERY_LENGTH_IN_BYTES_SQL);
}
throw handleClientException(ERROR_CODE_QUERY_LENGTH_EXCEEDED, null);
}
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
List<ConfigurationRawDataCollector> configurationRawDataCollectors;
try {
configurationRawDataCollectors = jdbcTemplate.executeQuery(placeholderSQL.getQuery(), (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));
if (useCreatedTimeField()) {
configurationRawDataCollectorBuilder.setCreatedTime(resultSet.getTimestamp(DB_SCHEMA_COLUMN_NAME_CREATED_TIME, calendar));
}
return configurationRawDataCollectorBuilder.build();
}, preparedStatement -> {
for (int count = 0; count < placeholderSQL.getData().size(); count++) {
if (placeholderSQL.getData().get(count).getClass().equals(Integer.class)) {
preparedStatement.setInt(count + 1, (Integer) placeholderSQL.getData().get(count));
} else {
preparedStatement.setString(count + 1, (String) placeholderSQL.getData().get(count));
}
}
});
/*
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 : buildResourcesFromRawData(configurationRawDataCollectors);
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_SEARCH_TENANT_RESOURCES, null, e);
}
}
use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method addResource.
/**
* {@inheritDoc}
*/
@Override
public void addResource(Resource resource) throws ConfigurationManagementException {
String resourceTypeId = getResourceTypeByName(resource.getResourceType()).getId();
Timestamp currentTime = new java.sql.Timestamp(new Date().getTime());
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
try {
boolean isOracleOrMssql = isOracleDB() || isMSSqlDB();
jdbcTemplate.withTransaction(template -> {
boolean isAttributeExists = resource.getAttributes() != null;
boolean isFileExists = resource.getFiles() != null && !resource.getFiles().isEmpty();
// Insert resource metadata.
template.executeInsert(useCreatedTimeField() ? INSERT_RESOURCE_SQL : INSERT_RESOURCE_SQL_WITHOUT_CREATED_TIME, preparedStatement -> {
int initialParameterIndex = 1;
preparedStatement.setString(initialParameterIndex, resource.getResourceId());
preparedStatement.setInt(++initialParameterIndex, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
preparedStatement.setString(++initialParameterIndex, resource.getResourceName());
if (useCreatedTimeField()) {
preparedStatement.setTimestamp(++initialParameterIndex, currentTime, calendar);
}
preparedStatement.setTimestamp(++initialParameterIndex, currentTime, calendar);
if (isOracleOrMssql) {
preparedStatement.setInt(++initialParameterIndex, isFileExists ? 1 : 0);
preparedStatement.setInt(++initialParameterIndex, isAttributeExists ? 1 : 0);
} else {
preparedStatement.setBoolean(++initialParameterIndex, isFileExists);
preparedStatement.setBoolean(++initialParameterIndex, isAttributeExists);
}
preparedStatement.setString(++initialParameterIndex, resourceTypeId);
}, resource, false);
// Insert attributes.
if (isAttributeExists) {
insertResourceAttributes(template, resource);
}
// Insert files.
if (isFileExists) {
for (ResourceFile file : resource.getFiles()) {
insertResourceFile(template, resource, file.getId(), file.getName(), file.getInputStream());
}
}
return null;
});
resource.setLastModified(currentTime.toInstant().toString());
if (useCreatedTimeField()) {
resource.setCreatedTime(currentTime.toInstant().toString());
}
} catch (TransactionException e) {
throw handleServerException(ERROR_CODE_ADD_RESOURCE, resource.getResourceName(), 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 updateMetadataForMYSQL.
private void updateMetadataForMYSQL(Resource resource, String resourceTypeId, boolean isAttributeExists, Timestamp currentTime, boolean useCreatedTime) throws TransactionException, ConfigurationManagementServerException {
String query = INSERT_OR_UPDATE_RESOURCE_MYSQL;
try {
if (isPostgreSQLDB()) {
query = INSERT_OR_UPDATE_RESOURCE_POSTGRESQL;
} else if (isMSSqlDB() || isDB2DB()) {
query = INSERT_OR_UPDATE_RESOURCE_MSSQL_OR_DB2;
} else if (isOracleDB()) {
query = INSERT_OR_UPDATE_RESOURCE_ORACLE;
}
boolean isOracleOrMssql = isOracleDB() || isMSSqlDB();
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
final String finalQuery = query;
jdbcTemplate.withTransaction(template -> template.executeInsert(useCreatedTime ? finalQuery : INSERT_OR_UPDATE_RESOURCE_MYSQL_WITHOUT_CREATED_TIME, preparedStatement -> {
int initialParameterIndex = 1;
preparedStatement.setString(initialParameterIndex, resource.getResourceId());
preparedStatement.setInt(++initialParameterIndex, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
preparedStatement.setString(++initialParameterIndex, resource.getResourceName());
if (useCreatedTime) {
preparedStatement.setTimestamp(++initialParameterIndex, currentTime, calendar);
}
preparedStatement.setTimestamp(++initialParameterIndex, currentTime, calendar);
if (isOracleOrMssql) {
preparedStatement.setInt(++initialParameterIndex, 0);
preparedStatement.setInt(++initialParameterIndex, isAttributeExists ? 1 : 0);
} else {
preparedStatement.setBoolean(++initialParameterIndex, false);
preparedStatement.setBoolean(++initialParameterIndex, isAttributeExists);
}
preparedStatement.setString(++initialParameterIndex, resourceTypeId);
}, resource, false));
} 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 deleteResourceTypeByName.
/**
* {@inheritDoc}
*/
@Override
public void deleteResourceTypeByName(String resourceTypeName) throws ConfigurationManagementException {
try {
if (isMySQLDB()) {
JdbcTemplate jdbcTemplateGetResourceTypeId = JdbcUtils.getNewTemplate();
String resourceTypeId = jdbcTemplateGetResourceTypeId.withTransaction(template -> template.fetchSingleRecord(GET_RESOURCE_TYPE_ID_BY_NAME_SQL, (resultSet, rowNumber) -> resultSet.getString(DB_SCHEMA_COLUMN_NAME_ID), preparedStatement -> {
int initialParameterIndex = 1;
preparedStatement.setString(initialParameterIndex, resourceTypeName);
}));
JdbcTemplate jdbcTemplateGetIds = JdbcUtils.getNewTemplate();
jdbcTemplateGetIds.executeQuery(GET_RESOURCE_ID_TENANT_ID_BY_TYPE_ID_SQL, ((resultSet, rowNumber) -> {
String ResourceId = resultSet.getString(DB_SCHEMA_COLUMN_NAME_ID);
int TenantId = resultSet.getInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID);
try {
CachedBackedConfigurationDAO cachedBackedConfigurationDAO = new CachedBackedConfigurationDAO(this);
cachedBackedConfigurationDAO.deleteResourceById(TenantId, ResourceId);
} catch (ConfigurationManagementException e) {
log.error(ERROR_CODE_DELETE_RESOURCE + ResourceId, e);
}
return null;
}), preparedStatement -> preparedStatement.setString(1, resourceTypeId));
}
} catch (TransactionException | DataAccessException e) {
throw handleServerException(ERROR_CODE_DELETE_RESOURCE_TYPE, resourceTypeName, e);
}
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
try {
jdbcTemplate.executeUpdate(selectDeleteResourceTypeQuery(null), (preparedStatement -> preparedStatement.setString(1, resourceTypeName)));
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_DELETE_RESOURCE_TYPE, resourceTypeName, e);
}
}
use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method getResourceById.
/**
* {@inheritDoc}
*/
@Override
public Resource getResourceById(String resourceId) throws ConfigurationManagementException {
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
List<ConfigurationRawDataCollector> configurationRawDataCollectors;
try {
configurationRawDataCollectors = jdbcTemplate.executeQuery(useCreatedTimeField() ? GET_RESOURCE_BY_ID_MYSQL : GET_RESOURCE_BY_ID_MYSQL_WITHOUT_CREATED_TIME, (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)).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 -> preparedStatement.setString(1, resourceId));
/*
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, "id = " + resourceId, e);
}
}
Aggregations